Skip to content

Commit

Permalink
Fix splitText when detached (facebook#6501)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx authored and jkjk822 committed Aug 26, 2024
1 parent ab9d694 commit 568dc9a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
36 changes: 19 additions & 17 deletions packages/lexical/src/nodes/LexicalTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ export class TextNode extends LexicalNode {
return [self];
}
const firstPart = parts[0];
const parent = self.getParentOrThrow();
const parent = self.getParent();
let writableNode;
const format = self.getFormat();
const style = self.getStyle();
Expand Down Expand Up @@ -1005,23 +1005,25 @@ export class TextNode extends LexicalNode {
}

// Insert the nodes into the parent's children
internalMarkSiblingsAsDirty(this);
const writableParent = parent.getWritable();
const insertionIndex = this.getIndexWithinParent();
if (hasReplacedSelf) {
writableParent.splice(insertionIndex, 0, splitNodes);
this.remove();
} else {
writableParent.splice(insertionIndex, 1, splitNodes);
}
if (parent !== null) {
internalMarkSiblingsAsDirty(this);
const writableParent = parent.getWritable();
const insertionIndex = this.getIndexWithinParent();
if (hasReplacedSelf) {
writableParent.splice(insertionIndex, 0, splitNodes);
this.remove();
} else {
writableParent.splice(insertionIndex, 1, splitNodes);
}

if ($isRangeSelection(selection)) {
$updateElementSelectionOnCreateDeleteNode(
selection,
parent,
insertionIndex,
partsLength - 1,
);
if ($isRangeSelection(selection)) {
$updateElementSelectionOnCreateDeleteNode(
selection,
parent,
insertionIndex,
partsLength - 1,
);
}
}

return splitNodes;
Expand Down
12 changes: 12 additions & 0 deletions packages/lexical/src/nodes/__tests__/unit/LexicalTextNode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,18 @@ describe('LexicalTextNode tests', () => {
});
},
);

test('with detached parent', async () => {
await update(() => {
const textNode = $createTextNode('foo');
const splits = textNode.splitText(1, 2);
expect(splits.map((split) => split.getTextContent())).toEqual([
'f',
'o',
'o',
]);
});
});
});

describe('createDOM()', () => {
Expand Down

0 comments on commit 568dc9a

Please sign in to comment.