Skip to content

Commit

Permalink
[Breaking Change][lexical] Bug Fix: Commit updates on editor.setRootE…
Browse files Browse the repository at this point in the history
…lement(null) (#7023)
  • Loading branch information
etrepum authored Jan 9, 2025
1 parent 6add515 commit d7abafd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
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
8 changes: 7 additions & 1 deletion packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
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

0 comments on commit d7abafd

Please sign in to comment.