Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix splitText when detached #6501

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment on lines +1012 to +1015
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't the code that's changing in this PR but it seems like there might be a hasReplacedSelf selection bug, the above code to fixup selections only handles parts>0 and the splice selection fixup code won't do anything either because the node removal is happening separately. It also seems a bit odd that the special case only happens with IS_SEGMENTED and not IS_TOKEN

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
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
Loading