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

[Breaking Change][lexical] Bug Fix: Commit updates on editor.setRootElement(null) #7023

Merged
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
12 changes: 8 additions & 4 deletions packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,15 @@ export class LexicalEditor {
}
}
} else {
// If content editable is unmounted we'll reset editor state back to original
// (or pending) editor state since there will be no reconciliation
this._editorState = pendingEditorState;
this._pendingEditorState = null;
// When the content editable is unmounted we will still trigger a
// reconciliation so that any pending updates are flushed,
// to match the previous state change when
// `_editorState = pendingEditorState` was used, but by
// using a commit we preserve the readOnly invariant
// for editor.getEditorState().
this._window = null;
this._updateTags.add('history-merge');
$commitPendingUpdates(this);
}

triggerListeners('root', this, false, nextRootElement, prevRootElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ describe('LexicalEditor tests', () => {
});

expect(rootListener).toHaveBeenCalledTimes(3);
expect(updateListener).toHaveBeenCalledTimes(3);
expect(updateListener).toHaveBeenCalledTimes(4);
expect(container.innerHTML).toBe(
'<span contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><p dir="ltr"><span data-lexical-text="true">Change successful</span></p></span>',
);
Expand All @@ -1032,7 +1032,13 @@ describe('LexicalEditor tests', () => {
init();
const contentEditable = editor.getRootElement();
editor.setEditable(editable);
editor.update(() => {
// Cause the editor to become dirty, so we can ensure
// that the getEditorState()._readOnly invariant holds
$getRoot().markDirty();
});
editor.setRootElement(null);
expect(editor.getEditorState()._readOnly).toBe(true);
const editorState = editor.parseEditorState(JSON_EDITOR_STATE);
editor.setEditorState(editorState);
editor.update(() => {
Expand Down
Loading