Skip to content

Commit

Permalink
Revert "Revert [Breaking Change][lexical] Bug Fix: Commit updates on …
Browse files Browse the repository at this point in the history
…editorSetRootElement(null) (#7023) (#7052)"

This reverts commit 29d733c.
  • Loading branch information
potatowagon committed Jan 20, 2025
1 parent 2e4a63e commit 542e850
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 @@ -1101,11 +1101,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 @@ -1021,7 +1021,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 @@ -1036,7 +1036,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 542e850

Please sign in to comment.