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

[lexical] temporarily set editor to not writable when calling getCachedTypeToNodeMap #7022

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,9 +909,15 @@ export class LexicalEditor {
klass: Klass<LexicalNode>,
): void {
const prevEditorState = this._editorState;
const originalIsEditable = this.isEditable();
// Temporarily freeze the editor before calling getCachedTypeToNodeMap
if (originalIsEditable) {
this.setEditable(false);
}
const nodeMap = getCachedTypeToNodeMap(prevEditorState).get(
klass.getType(),
);
this.setEditable(originalIsEditable);
if (!nodeMap) {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,16 @@ export function markNodesWithTypesAsDirty(
editor: LexicalEditor,
types: string[],
): void {
// Temporarily freeze the editor to call getCachedTypeToNodeMap.
// It cannot be called on a writable editor.
const originalIsEditable = editor.isEditable();
if (originalIsEditable) {
editor.setEditable(false);
}
// We only need to mark nodes dirty if they were in the previous state.
// If they aren't, then they are by definition dirty already.
const cachedMap = getCachedTypeToNodeMap(editor.getEditorState());
editor.setEditable(originalIsEditable);
const dirtyNodeMaps: NodeMap[] = [];
for (const type of types) {
const nodeMap = cachedMap.get(type);
Expand Down
Loading