mirror of
https://github.com/facebook/lexical.git
synced 2025-08-06 16:39:33 +08:00
[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:
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user