Skip to content

Commit

Permalink
Replace .append implementation with .splice (facebook#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
fantactuka authored Apr 11, 2022
1 parent 26cb2da commit 3ee7e12
Showing 1 changed file with 15 additions and 38 deletions.
53 changes: 15 additions & 38 deletions packages/lexical/src/nodes/LexicalElementNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,39 +265,8 @@ export class ElementNode extends LexicalNode {
}
append(...nodesToAppend: LexicalNode[]): ElementNode {
errorOnReadOnly();
const writableSelf = this.getWritable();
const writableSelfKey = writableSelf.__key;
const writableSelfChildren = writableSelf.__children;
const nodesToAppendLength = nodesToAppend.length;
const lastChild = this.getLastChild();
if (lastChild !== null) {
internalMarkNodeAsDirty(lastChild);
}
for (let i = 0; i < nodesToAppendLength; i++) {
const nodeToAppend = nodesToAppend[i];
if (nodeToAppend.__key === writableSelfKey) {
invariant(false, 'append: attemtping to append self');
}
const writableNodeToAppend = nodeToAppend.getWritable();
// Remove node from previous parent
const oldParent = writableNodeToAppend.getParent();
if (oldParent !== null) {
const writableParent = oldParent.getWritable();
const children = writableParent.__children;
const index = children.indexOf(writableNodeToAppend.__key);
if (index === -1) {
invariant(false, 'Node is not a child of its parent');
}
children.splice(index, 1);
}
// Set child parent to self
writableNodeToAppend.__parent = writableSelfKey;
const newKey = writableNodeToAppend.__key;
writableSelfChildren.push(newKey);
}
return writableSelf;
return this.splice(this.getChildrenSize(), 0, nodesToAppend);
}

setDirection(direction: 'ltr' | 'rtl' | null): this {
errorOnReadOnly();
const self = this.getWritable();
Expand Down Expand Up @@ -363,16 +332,24 @@ export class ElementNode extends LexicalNode {
}

// Remove defined range of children
const nodesToRemoveKeys = writableSelfChildren.splice(
start,
deleteCount,
...nodesToInsertKeys,
);
let nodesToRemoveKeys;

// Using faster push when only appending nodes
if (start === writableSelfChildren.length) {
writableSelfChildren.push(...nodesToInsertKeys);
nodesToRemoveKeys = [];
} else {
nodesToRemoveKeys = writableSelfChildren.splice(
start,
deleteCount,
...nodesToInsertKeys,
);
}

// In case of deletion we need to adjust selection, unlink removed nodes
// and clean up node itself if it becomes empty. None of these needed
// for insertion-only cases
if (deleteCount) {
if (nodesToRemoveKeys.length) {
// Adjusting selection, in case node that was anchor/focus will be deleted
const selection = $getSelection();
if ($isRangeSelection(selection)) {
Expand Down

0 comments on commit 3ee7e12

Please sign in to comment.