From 710a0ce01f102af8c54c75d670c43b2e80b70721 Mon Sep 17 00:00:00 2001 From: Gerard Rovira Date: Wed, 27 Nov 2024 13:02:41 -0500 Subject: [PATCH] address comments --- packages/lexical-utils/src/markSelection.ts | 21 ++++++------------- .../lexical-utils/src/positionNodeOnRange.ts | 11 +++------- packages/lexical/src/LexicalUtils.ts | 4 ++-- packages/lexical/src/index.ts | 2 ++ 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/packages/lexical-utils/src/markSelection.ts b/packages/lexical-utils/src/markSelection.ts index 1f834935f80..382b57b4bb7 100644 --- a/packages/lexical-utils/src/markSelection.ts +++ b/packages/lexical-utils/src/markSelection.ts @@ -9,8 +9,10 @@ import { $getSelection, $isRangeSelection, + $isTextNode, type EditorState, ElementNode, + getDOMTextNode, type LexicalEditor, TextNode, } from 'lexical'; @@ -64,7 +66,7 @@ export default function markSelection( currentAnchorOffset !== previousAnchorOffset || currentAnchorNodeKey !== previousAnchorNode.getKey() || (currentAnchorNode !== previousAnchorNode && - (!(previousAnchorNode instanceof TextNode) || + (!$isTextNode(previousAnchorNode) || currentAnchorNode.updateDOM( previousAnchorNode, currentAnchorNodeDOM, @@ -76,7 +78,7 @@ export default function markSelection( currentFocusOffset !== previousFocusOffset || currentFocusNodeKey !== previousFocusNode.getKey() || (currentFocusNode !== previousFocusNode && - (!(previousFocusNode instanceof TextNode) || + (!$isTextNode(previousFocusNode) || currentFocusNode.updateDOM( previousFocusNode, currentFocusNodeDOM, @@ -106,8 +108,8 @@ export default function markSelection( lastHTMLElement = focusHTMLElement; lastOffset = focus.offset; } - const firstHTMLElementTextChild = firstTextChild(firstHTMLElement); - const lastHTMLElementtextChild = firstTextChild(lastHTMLElement); + const firstHTMLElementTextChild = getDOMTextNode(firstHTMLElement); + const lastHTMLElementtextChild = getDOMTextNode(lastHTMLElement); range.setStart( firstHTMLElementTextChild || firstHTMLElement, firstOffset, @@ -160,14 +162,3 @@ export default function markSelection( }, ); } - -function firstTextChild(node: Node): null | Text { - let current: Node | null = node; - while (current !== null) { - if (current instanceof Text) { - return current; - } - current = current.firstChild; - } - return null; -} diff --git a/packages/lexical-utils/src/positionNodeOnRange.ts b/packages/lexical-utils/src/positionNodeOnRange.ts index 20bcd5292f9..2ae7df5e35e 100644 --- a/packages/lexical-utils/src/positionNodeOnRange.ts +++ b/packages/lexical-utils/src/positionNodeOnRange.ts @@ -6,9 +6,8 @@ * */ -import type {LexicalEditor} from 'lexical'; - import {createRectsFromDOMRange} from '@lexical/selection'; +import {isHTMLElement, type LexicalEditor} from 'lexical'; import invariant from 'shared/invariant'; import px from './px'; @@ -21,11 +20,7 @@ const mutationObserverConfig = { }; function prependDOMNode(parent: HTMLElement, node: HTMLElement) { - if (parent.hasChildNodes()) { - parent.insertBefore(node, parent.firstChild); - } else { - parent.append(node); - } + parent.insertBefore(node, parent.firstChild); } /** @@ -126,7 +121,7 @@ export default function mlcPositionNodeOnRange( return stop(); } const currentParentDOMNode = currentRootDOMNode.parentElement; - if (!(currentParentDOMNode instanceof HTMLElement)) { + if (currentParentDOMNode === null || !isHTMLElement(currentParentDOMNode)) { return stop(); } stop(); diff --git a/packages/lexical/src/LexicalUtils.ts b/packages/lexical/src/LexicalUtils.ts index 28b4f57e943..f4cc88ac86a 100644 --- a/packages/lexical/src/LexicalUtils.ts +++ b/packages/lexical/src/LexicalUtils.ts @@ -194,14 +194,14 @@ export function $isTokenOrSegmented(node: TextNode): boolean { return node.isToken() || node.isSegmented(); } -function isDOMNodeLexicalTextNode(node: Node): node is Text { +export function isDOMTextNode(node: Node): node is Text { return node.nodeType === DOM_TEXT_TYPE; } export function getDOMTextNode(element: Node | null): Text | null { let node = element; while (node != null) { - if (isDOMNodeLexicalTextNode(node)) { + if (isDOMTextNode(node)) { return node; } node = node.firstChild; diff --git a/packages/lexical/src/index.ts b/packages/lexical/src/index.ts index 5f1eae58210..562c7c4e387 100644 --- a/packages/lexical/src/index.ts +++ b/packages/lexical/src/index.ts @@ -180,10 +180,12 @@ export { $setSelection, $splitNode, getDOMSelection, + getDOMTextNode, getEditorPropertyFromDOMNode, getNearestEditorFromDOMNode, isBlockDomNode, isDocumentFragment, + isDOMTextNode, isDOMUnmanaged, isHTMLAnchorElement, isHTMLElement,