Skip to content

Commit

Permalink
Fix @lexical/utils import (#5245)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx authored Nov 17, 2023
1 parent cdf9553 commit d1bde33
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 45 deletions.
17 changes: 12 additions & 5 deletions packages/lexical-link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -529,3 +525,14 @@ export function toggleLink(
});
}
}

function $getAncestor<NodeType extends LexicalNode = LexicalNode>(
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;
}
38 changes: 37 additions & 1 deletion packages/lexical-selection/src/range-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import type {
DecoratorNode,
ElementNode,
GridSelection,
LexicalNode,
Expand All @@ -16,7 +17,6 @@ import type {
TextNode,
} from 'lexical';

import {$getAncestor, INTERNAL_$isBlock} from '@lexical/utils';
import {
$getAdjacentNode,
$getPreviousSelection,
Expand All @@ -25,6 +25,7 @@ import {
$isDecoratorNode,
$isElementNode,
$isLeafNode,
$isLineBreakNode,
$isRangeSelection,
$isRootNode,
$isRootOrShadowRoot,
Expand Down Expand Up @@ -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<unknown> {
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<NodeType extends LexicalNode = LexicalNode>(
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;
}
38 changes: 0 additions & 38 deletions packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ import {
$getPreviousSelection,
$getRoot,
$getSelection,
$isDecoratorNode,
$isElementNode,
$isLineBreakNode,
$isNodeSelection,
$isRangeSelection,
$isRootOrShadowRoot,
$isTextNode,
$setSelection,
$splitNode,
DecoratorNode,
DEPRECATED_$isGridSelection,
EditorState,
ElementNode,
Expand Down Expand Up @@ -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<unknown> {
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<NodeType extends LexicalNode = LexicalNode>(
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;
}
3 changes: 2 additions & 1 deletion packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -50,6 +49,7 @@ import {
import {
$findMatchingParent,
$getAdjacentNode,
$getAncestor,
$getChildrenRecursively,
$getCompositionKey,
$getNearestRootOrShadowRoot,
Expand All @@ -65,6 +65,7 @@ import {
getElementByKeyOrThrow,
getNodeFromDOM,
getTextNodeOffset,
INTERNAL_$isBlock,
isSelectionCapturedInDecoratorInput,
isSelectionWithinEditor,
removeDOMBlockCursorElement,
Expand Down
35 changes: 35 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> {
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<NodeType extends LexicalNode = LexicalNode>(
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;
}

2 comments on commit d1bde33

@vercel
Copy link

@vercel vercel bot commented on d1bde33 Nov 17, 2023

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-fbopensource.vercel.app
lexicaljs.com
lexicaljs.org
www.lexical.dev
lexical.dev

@vercel
Copy link

@vercel vercel bot commented on d1bde33 Nov 17, 2023

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.vercel.app
lexical-playground-git-main-fbopensource.vercel.app
lexical-playground-fbopensource.vercel.app
playground.lexical.dev

Please sign in to comment.