From d1bde33201d83fdf144ebd9fa580a0dfde011d71 Mon Sep 17 00:00:00 2001 From: Gerard Rovira Date: Fri, 17 Nov 2023 18:25:00 -0500 Subject: [PATCH] Fix @lexical/utils import (#5245) --- packages/lexical-link/src/index.ts | 17 ++++++--- .../lexical-selection/src/range-selection.ts | 38 ++++++++++++++++++- packages/lexical-utils/src/index.ts | 38 ------------------- packages/lexical/src/LexicalSelection.ts | 3 +- packages/lexical/src/LexicalUtils.ts | 35 +++++++++++++++++ 5 files changed, 86 insertions(+), 45 deletions(-) diff --git a/packages/lexical-link/src/index.ts b/packages/lexical-link/src/index.ts index 0bf5161d957..deb162ae5c4 100644 --- a/packages/lexical-link/src/index.ts +++ b/packages/lexical-link/src/index.ts @@ -20,11 +20,7 @@ import type { SerializedElementNode, } from 'lexical'; -import { - $getAncestor, - addClassNamesToElement, - isHTMLAnchorElement, -} from '@lexical/utils'; +import {addClassNamesToElement, isHTMLAnchorElement} from '@lexical/utils'; import { $applyNodeReplacement, $getSelection, @@ -529,3 +525,14 @@ export function toggleLink( }); } } + +function $getAncestor( + node: LexicalNode, + predicate: (ancestor: LexicalNode) => ancestor is NodeType, +) { + let parent = node; + while (parent !== null && parent.getParent() !== null && !predicate(parent)) { + parent = parent.getParentOrThrow(); + } + return predicate(parent) ? parent : null; +} diff --git a/packages/lexical-selection/src/range-selection.ts b/packages/lexical-selection/src/range-selection.ts index f6e5c63c7d6..79fbe913ee0 100644 --- a/packages/lexical-selection/src/range-selection.ts +++ b/packages/lexical-selection/src/range-selection.ts @@ -7,6 +7,7 @@ */ import type { + DecoratorNode, ElementNode, GridSelection, LexicalNode, @@ -16,7 +17,6 @@ import type { TextNode, } from 'lexical'; -import {$getAncestor, INTERNAL_$isBlock} from '@lexical/utils'; import { $getAdjacentNode, $getPreviousSelection, @@ -25,6 +25,7 @@ import { $isDecoratorNode, $isElementNode, $isLeafNode, + $isLineBreakNode, $isRangeSelection, $isRootNode, $isRootOrShadowRoot, @@ -556,3 +557,38 @@ export function $getSelectionStyleValueForProperty( return styleValue === null ? defaultValue : styleValue; } + +/** + * This function is for internal use of the library. + * Please do not use it as it may change in the future. + */ +export function INTERNAL_$isBlock( + node: LexicalNode, +): node is ElementNode | DecoratorNode { + if ($isDecoratorNode(node) && !node.isInline()) { + return true; + } + if (!$isElementNode(node) || $isRootOrShadowRoot(node)) { + return false; + } + + const firstChild = node.getFirstChild(); + const isLeafElement = + firstChild === null || + $isLineBreakNode(firstChild) || + $isTextNode(firstChild) || + firstChild.isInline(); + + return !node.isInline() && node.canBeEmpty() !== false && isLeafElement; +} + +export function $getAncestor( + node: LexicalNode, + predicate: (ancestor: LexicalNode) => ancestor is NodeType, +) { + let parent = node; + while (parent !== null && parent.getParent() !== null && !predicate(parent)) { + parent = parent.getParentOrThrow(); + } + return predicate(parent) ? parent : null; +} diff --git a/packages/lexical-utils/src/index.ts b/packages/lexical-utils/src/index.ts index c44ea0bec13..6cedbce1c19 100644 --- a/packages/lexical-utils/src/index.ts +++ b/packages/lexical-utils/src/index.ts @@ -13,16 +13,13 @@ import { $getPreviousSelection, $getRoot, $getSelection, - $isDecoratorNode, $isElementNode, - $isLineBreakNode, $isNodeSelection, $isRangeSelection, $isRootOrShadowRoot, $isTextNode, $setSelection, $splitNode, - DecoratorNode, DEPRECATED_$isGridSelection, EditorState, ElementNode, @@ -521,38 +518,3 @@ export function $insertFirst(parent: ElementNode, node: LexicalNode): void { parent.append(node); } } - -/** - * This function is for internal use of the library. - * Please do not use it as it may change in the future. - */ -export function INTERNAL_$isBlock( - node: LexicalNode, -): node is ElementNode | DecoratorNode { - if ($isDecoratorNode(node) && !node.isInline()) { - return true; - } - if (!$isElementNode(node) || $isRootOrShadowRoot(node)) { - return false; - } - - const firstChild = node.getFirstChild(); - const isLeafElement = - firstChild === null || - $isLineBreakNode(firstChild) || - $isTextNode(firstChild) || - firstChild.isInline(); - - return !node.isInline() && node.canBeEmpty() !== false && isLeafElement; -} - -export function $getAncestor( - node: LexicalNode, - predicate: (ancestor: LexicalNode) => ancestor is NodeType, -) { - let parent = node; - while (parent !== null && parent.getParent() !== null && !predicate(parent)) { - parent = parent.getParentOrThrow(); - } - return predicate(parent) ? parent : null; -} diff --git a/packages/lexical/src/LexicalSelection.ts b/packages/lexical/src/LexicalSelection.ts index fe625006970..2adfa9a4fb4 100644 --- a/packages/lexical/src/LexicalSelection.ts +++ b/packages/lexical/src/LexicalSelection.ts @@ -12,7 +12,6 @@ import type {NodeKey} from './LexicalNode'; import type {ElementNode} from './nodes/LexicalElementNode'; import type {TextFormatType} from './nodes/LexicalTextNode'; -import {$getAncestor, INTERNAL_$isBlock} from '@lexical/utils'; import invariant from 'shared/invariant'; import { @@ -50,6 +49,7 @@ import { import { $findMatchingParent, $getAdjacentNode, + $getAncestor, $getChildrenRecursively, $getCompositionKey, $getNearestRootOrShadowRoot, @@ -65,6 +65,7 @@ import { getElementByKeyOrThrow, getNodeFromDOM, getTextNodeOffset, + INTERNAL_$isBlock, isSelectionCapturedInDecoratorInput, isSelectionWithinEditor, removeDOMBlockCursorElement, diff --git a/packages/lexical/src/LexicalUtils.ts b/packages/lexical/src/LexicalUtils.ts index 10e24a67461..5ccf1eefb01 100644 --- a/packages/lexical/src/LexicalUtils.ts +++ b/packages/lexical/src/LexicalUtils.ts @@ -1590,3 +1590,38 @@ export function isHTMLElement(x: Node | EventTarget): x is HTMLElement { // @ts-ignore-next-line - strict check on nodeType here should filter out non-Element EventTarget implementors return x.nodeType === 1; } + +/** + * This function is for internal use of the library. + * Please do not use it as it may change in the future. + */ +export function INTERNAL_$isBlock( + node: LexicalNode, +): node is ElementNode | DecoratorNode { + if ($isDecoratorNode(node) && !node.isInline()) { + return true; + } + if (!$isElementNode(node) || $isRootOrShadowRoot(node)) { + return false; + } + + const firstChild = node.getFirstChild(); + const isLeafElement = + firstChild === null || + $isLineBreakNode(firstChild) || + $isTextNode(firstChild) || + firstChild.isInline(); + + return !node.isInline() && node.canBeEmpty() !== false && isLeafElement; +} + +export function $getAncestor( + node: LexicalNode, + predicate: (ancestor: LexicalNode) => ancestor is NodeType, +) { + let parent = node; + while (parent !== null && parent.getParent() !== null && !predicate(parent)) { + parent = parent.getParentOrThrow(); + } + return predicate(parent) ? parent : null; +}