Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rm unused helpers from older markdown code #2998

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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