[Breaking Change][lexical] Bug Fix: Only select RootNode on removal of last child if there was an existing selection (#7351)

This commit is contained in:
Bob Ippolito
2025-03-20 07:44:41 -07:00
committed by GitHub
parent 25993505f7
commit 9a851da209
3 changed files with 34 additions and 3 deletions

View File

@ -1280,7 +1280,11 @@ export class LexicalEditor {
}
/**
* Focuses the editor
* Focuses the editor by marking the existing selection as dirty, or by
* creating a new selection at `defaultSelection` if one does not already
* exist. If you want to force a specific selection, you should call
* `root.selectStart()` or `root.selectEnd()` in an update.
*
* @param callbackFn - A function to run after the editor is focused.
* @param options - A bag of options
* @param options.defaultSelection - Where to move selection when the editor is

View File

@ -148,7 +148,12 @@ export function $removeNode(
) {
$removeNode(parent, restoreSelection);
}
if (restoreSelection && $isRootNode(parent) && parent.isEmpty()) {
if (
restoreSelection &&
selection &&
$isRootNode(parent) &&
parent.isEmpty()
) {
parent.selectEnd();
}
}

View File

@ -157,7 +157,7 @@ describe('LexicalRootNode tests', () => {
});
});
test('RootNode is selected when its only child removed', async () => {
test('RootNode is selected when its selected child is removed', async () => {
const {editor} = testEnv;
await editor.update(() => {
@ -187,6 +187,28 @@ describe('LexicalRootNode tests', () => {
});
});
test('RootNode is not selected when all children are removed with no selection', async () => {
const {editor} = testEnv;
await editor.update(() => {
expect($getSelection()).toBe(null);
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
expect($getSelection()).toBe(null);
});
await editor.update(() => {
expect($getSelection()).toBe(null);
$getRoot().clear();
expect($getSelection()).toBe(null);
});
await editor.update(() => {
expect($getSelection()).toBe(null);
});
});
test('RootNode __cachedText', async () => {
const {editor} = testEnv;