Skip to content

Commit

Permalink
Fix bad import into core (facebook#3861)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Feb 9, 2023
1 parent 85a6bfc commit 1dc6efa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 49 deletions.
51 changes: 3 additions & 48 deletions packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import {$cloneWithProperties} from '@lexical/selection';
import {
$copyNode,
$createParagraphNode,
$getRoot,
$getSelection,
Expand All @@ -19,6 +18,7 @@ import {
$isRootOrShadowRoot,
$isTextNode,
$setSelection,
$splitNode,
DEPRECATED_$isGridSelection,
EditorState,
ElementNode,
Expand All @@ -28,6 +28,8 @@ import {
} from 'lexical';
import invariant from 'shared/invariant';

export {$splitNode};

export type DFSNode = Readonly<{
depth: number;
node: LexicalNode;
Expand Down Expand Up @@ -387,50 +389,3 @@ export function $wrapNodeInElement(
elementNode.append(node);
return elementNode;
}

export function $splitNode(
node: ElementNode,
offset: number,
): [ElementNode | null, ElementNode] {
let startNode = node.getChildAtIndex(offset);
if (startNode == null) {
startNode = node;
}

invariant(
!$isRootOrShadowRoot(node),
'Can not call $splitNode() on root element',
);

const recurse = (
currentNode: LexicalNode,
): [ElementNode, ElementNode, LexicalNode] => {
const parent = currentNode.getParentOrThrow();
const isParentRoot = $isRootOrShadowRoot(parent);
// The node we start split from (leaf) is moved, but its recursive
// parents are copied to create separate tree
const nodeToMove =
currentNode === startNode && !isParentRoot
? currentNode
: $copyNode(currentNode);

if (isParentRoot) {
currentNode.insertAfter(nodeToMove);
return [
currentNode as ElementNode,
nodeToMove as ElementNode,
nodeToMove,
];
} else {
const [leftTree, rightTree, newParent] = recurse(parent);
const nextSiblings = currentNode.getNextSiblings();

newParent.append(nodeToMove, ...nextSiblings);
return [leftTree, rightTree, nodeToMove];
}
};

const [leftTree, rightTree] = recurse(startNode);

return [leftTree, rightTree];
}
2 changes: 1 addition & 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 {LexicalNode, NodeKey} from './LexicalNode';
import type {ElementNode} from './nodes/LexicalElementNode';
import type {TextFormatType} from './nodes/LexicalTextNode';

import {$splitNode} from '@lexical/utils';
import {IS_CHROME} from 'shared/environment';
import invariant from 'shared/invariant';

Expand Down Expand Up @@ -57,6 +56,7 @@ import {
$isRootOrShadowRoot,
$isTokenOrSegmented,
$setCompositionKey,
$splitNode,
doesContainGrapheme,
getDOMSelection,
getDOMTextNode,
Expand Down
47 changes: 47 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1500,3 +1500,50 @@ export function updateDOMBlockCursorElement(
export function getDOMSelection(targetWindow: null | Window): null | Selection {
return !CAN_USE_DOM ? null : (targetWindow || window).getSelection();
}

export function $splitNode(
node: ElementNode,
offset: number,
): [ElementNode | null, ElementNode] {
let startNode = node.getChildAtIndex(offset);
if (startNode == null) {
startNode = node;
}

invariant(
!$isRootOrShadowRoot(node),
'Can not call $splitNode() on root element',
);

const recurse = (
currentNode: LexicalNode,
): [ElementNode, ElementNode, LexicalNode] => {
const parent = currentNode.getParentOrThrow();
const isParentRoot = $isRootOrShadowRoot(parent);
// The node we start split from (leaf) is moved, but its recursive
// parents are copied to create separate tree
const nodeToMove =
currentNode === startNode && !isParentRoot
? currentNode
: $copyNode(currentNode);

if (isParentRoot) {
currentNode.insertAfter(nodeToMove);
return [
currentNode as ElementNode,
nodeToMove as ElementNode,
nodeToMove,
];
} else {
const [leftTree, rightTree, newParent] = recurse(parent);
const nextSiblings = currentNode.getNextSiblings();

newParent.append(nodeToMove, ...nextSiblings);
return [leftTree, rightTree, nodeToMove];
}
};

const [leftTree, rightTree] = recurse(startNode);

return [leftTree, rightTree];
}
1 change: 1 addition & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export {
$nodesOfType,
$setCompositionKey,
$setSelection,
$splitNode,
isSelectionWithinEditor,
} from './LexicalUtils';
export {$isDecoratorNode, DecoratorNode} from './nodes/LexicalDecoratorNode';
Expand Down

0 comments on commit 1dc6efa

Please sign in to comment.