Skip to content

Commit

Permalink
Rm unused helpers from older markdown code (#2998)
Browse files Browse the repository at this point in the history
  • Loading branch information
fantactuka committed Sep 12, 2022
1 parent 5608b71 commit 4e935a1
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 124 deletions.
11 changes: 0 additions & 11 deletions packages/lexical-text/flow/LexicalText.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ declare export function $findTextIntersectionFromCharacters(
node: TextNode,
offset: number,
};
declare export function $joinTextNodesInElementNode(
elementNode: ElementNode,
separator: string,
stopAt: TextNodeWithOffset,
): string;
declare export function $findNodeWithOffsetFromJoinedText(
offsetInJoinedText: number,
joinedTextLength: number,
separatorLength: number,
elementNode: ElementNode,
): ?TextNodeWithOffset;
declare export function $isRootTextContentEmpty(
isEditorComposing: boolean,
trim?: boolean,
Expand Down
114 changes: 1 addition & 113 deletions packages/lexical-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
*
*/

import type {
ElementNode,
Klass,
LexicalEditor,
LexicalNode,
RootNode,
} from 'lexical';
import type {Klass, LexicalEditor, LexicalNode, RootNode} from 'lexical';

import {
$createTextNode,
Expand All @@ -22,7 +16,6 @@ import {
$isTextNode,
TextNode,
} from 'lexical';
import invariant from 'shared/invariant';

export type TextNodeWithOffset = {
node: TextNode;
Expand Down Expand Up @@ -74,111 +67,6 @@ export function $findTextIntersectionFromCharacters(
return null;
}

// Return text content for child text nodes. Each non-text node is separated by input string.
// Caution, this function creates a string and should not be used within a tight loop.
// Use $getNodeWithOffsetsFromJoinedTextNodesFromElementNode below to convert
// indexes in the return string back into their corresponding node and offsets.
export function $joinTextNodesInElementNode(
elementNode: ElementNode,
separator: string,
stopAt: TextNodeWithOffset,
): string {
let textContent = '';
const children = elementNode.getChildren();
const length = children.length;

for (let i = 0; i < length; ++i) {
const child = children[i];

if ($isTextNode(child)) {
const childTextContent = child.getTextContent();

if (child.is(stopAt.node)) {
if (stopAt.offset > childTextContent.length) {
invariant(
false,
'Node %s and selection point do not match.',
child.__key,
);
}
textContent += child.getTextContent().substr(0, stopAt.offset);
break;
} else {
textContent += childTextContent;
}
} else {
textContent += separator;
}
}

return textContent;
}

// This function converts the offsetInJoinedText to
// a node and offset result or null if not found.
// This function is to be used in conjunction with joinTextNodesInElementNode above.
// The joinedTextContent should be return value from joinTextNodesInElementNode.
//
// The offsetInJoinedText is relative to the entire string which
// itself is relevant to the parent ElementNode.
//
// Example:
// Given a Paragraph with 2 TextNodes. The first is Hello, the second is World.
// The joinedTextContent would be "HelloWorld"
// The offsetInJoinedText might be for the letter "e" = 1 or "r" = 7.
// The return values would be {TextNode1, 1} or {TextNode2,2}, respectively.

export function $findNodeWithOffsetFromJoinedText(
offsetInJoinedText: number,
joinedTextLength: number,
separatorLength: number,
elementNode: ElementNode,
): TextNodeWithOffset | null {
const children = elementNode.getChildren();
const childrenLength = children.length;
let runningLength = 0;
let isPriorNodeTextNode = false;

for (let i = 0; i < childrenLength; ++i) {
// We must examine the offsetInJoinedText that is located
// at the length of the string.
// For example, given "hello", the length is 5, yet
// the caller still wants the node + offset at the
// right edge of the "o".

if (runningLength > joinedTextLength) {
break;
}

const child = children[i];
const isChildNodeTestNode = $isTextNode(child);
const childContentLength = isChildNodeTestNode
? child.getTextContent().length
: separatorLength;

const newRunningLength = runningLength + childContentLength;

const isJoinedOffsetWithinNode =
(isPriorNodeTextNode === false && runningLength === offsetInJoinedText) ||
(runningLength === 0 && runningLength === offsetInJoinedText) ||
(runningLength < offsetInJoinedText &&
offsetInJoinedText <= newRunningLength);

if (isJoinedOffsetWithinNode && $isTextNode(child)) {
// Check isTextNode again for flow.

return {
node: child,
offset: offsetInJoinedText - runningLength,
};
}
runningLength = newRunningLength;
isPriorNodeTextNode = isChildNodeTestNode;
}

return null;
}

export function $isRootTextContentEmpty(
isEditorComposing: boolean,
trim = true,
Expand Down

2 comments on commit 4e935a1

@vercel
Copy link

@vercel vercel bot commented on 4e935a1 Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-git-main-fbopensource.vercel.app
lexical.dev
lexical-fbopensource.vercel.app
lexicaljs.com
www.lexical.dev
lexicaljs.org

@vercel
Copy link

@vercel vercel bot commented on 4e935a1 Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-git-main-fbopensource.vercel.app
lexical-playground.vercel.app
playground.lexical.dev
lexical-playground-fbopensource.vercel.app

Please sign in to comment.