Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed Nov 27, 2024
1 parent a9c12e4 commit 710a0ce
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
21 changes: 6 additions & 15 deletions packages/lexical-utils/src/markSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import {
$getSelection,
$isRangeSelection,
$isTextNode,
type EditorState,
ElementNode,
getDOMTextNode,
type LexicalEditor,
TextNode,
} from 'lexical';
Expand Down Expand Up @@ -64,7 +66,7 @@ export default function markSelection(
currentAnchorOffset !== previousAnchorOffset ||
currentAnchorNodeKey !== previousAnchorNode.getKey() ||
(currentAnchorNode !== previousAnchorNode &&
(!(previousAnchorNode instanceof TextNode) ||
(!$isTextNode(previousAnchorNode) ||
currentAnchorNode.updateDOM(
previousAnchorNode,
currentAnchorNodeDOM,
Expand All @@ -76,7 +78,7 @@ export default function markSelection(
currentFocusOffset !== previousFocusOffset ||
currentFocusNodeKey !== previousFocusNode.getKey() ||
(currentFocusNode !== previousFocusNode &&
(!(previousFocusNode instanceof TextNode) ||
(!$isTextNode(previousFocusNode) ||
currentFocusNode.updateDOM(
previousFocusNode,
currentFocusNodeDOM,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
11 changes: 3 additions & 8 deletions packages/lexical-utils/src/positionNodeOnRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ export {
$setSelection,
$splitNode,
getDOMSelection,
getDOMTextNode,
getEditorPropertyFromDOMNode,
getNearestEditorFromDOMNode,
isBlockDomNode,
isDocumentFragment,
isDOMTextNode,
isDOMUnmanaged,
isHTMLAnchorElement,
isHTMLElement,
Expand Down

0 comments on commit 710a0ce

Please sign in to comment.