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] Bug Fix: Mutation listeners not fired for replacement nodes #6076

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
25 changes: 23 additions & 2 deletions packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ export type Transform<T extends LexicalNode> = (node: T) => void;

export type ErrorHandler = (error: Error) => void;

export type MutationListeners = Map<MutationListener, Klass<LexicalNode>>;
export type MutationListeners = Map<
MutationListener,
ReadonlyArray<Klass<LexicalNode>>
Copy link
Contributor Author

@aleksandr-lapushkin aleksandr-lapushkin May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite like this change (Klass => Klass[]). I was thinking of migrating this to nodes like with transforms, but this is a relatively quick and inexpensive fix 🤷🏻 . Happy to iterate further.

>;

export type MutatedNodes = Map<Klass<LexicalNode>, Map<NodeKey, NodeMutation>>;

Expand Down Expand Up @@ -823,9 +826,27 @@ export class LexicalEditor {
klass.name,
);
}
const replaceWithKlass = registeredNode.replaceWithKlass;
const registeredNodes = [registeredNode];
if (replaceWithKlass != null) {
const replaceWithNode = this._nodes.get(replaceWithKlass.getType());
if (replaceWithNode === undefined) {
invariant(
false,
'Node %s has not been registered. Ensure node has been passed to createEditor.',
replaceWithKlass.name,
);
}

registeredNodes.push(replaceWithNode);
}

const mutations = this._listeners.mutation;
mutations.set(listener, klass);
mutations.set(
listener,
registeredNodes.map((node) => node.klass),
);

return () => {
mutations.delete(listener);
};
Expand Down
44 changes: 25 additions & 19 deletions packages/lexical/src/LexicalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ function handleDEVOnlyPendingUpdateGuarantees(
};
}

export function commitPendingUpdates(
export function $commitPendingUpdates(
editor: LexicalEditor,
recoveryEditorState?: EditorState,
): void {
Expand Down Expand Up @@ -494,7 +494,7 @@ export function commitPendingUpdates(
initMutationObserver(editor);
editor._dirtyType = FULL_RECONCILE;
isAttemptingToRecoverFromReconcilerError = true;
commitPendingUpdates(editor, currentEditorState);
$commitPendingUpdates(editor, currentEditorState);
isAttemptingToRecoverFromReconcilerError = false;
} else {
// To avoid a possible situation of infinite loops, lets throw
Expand Down Expand Up @@ -638,8 +638,10 @@ export function commitPendingUpdates(
tags,
});
triggerDeferredUpdateCallbacks(editor, deferred);
triggerEnqueuedUpdates(editor);
$triggerEnqueuedUpdates(editor);
}
/** @deprecated renamed to $commitPendingUpdates by @lexical/eslint-plugin rules-of-lexical */
export const commitPendingUpdates = $commitPendingUpdates;

function triggerTextContentListeners(
editor: LexicalEditor,
Expand All @@ -665,14 +667,16 @@ function triggerMutationListeners(
const listenersLength = listeners.length;

for (let i = 0; i < listenersLength; i++) {
const [listener, klass] = listeners[i];
const mutatedNodesByType = mutatedNodes.get(klass);
if (mutatedNodesByType !== undefined) {
listener(mutatedNodesByType, {
dirtyLeaves,
prevEditorState,
updateTags,
});
const [listener, klasses] = listeners[i];
for (let j = 0; j < klasses.length; j++) {
const mutatedNodesByType = mutatedNodes.get(klasses[j]);
if (mutatedNodesByType !== undefined) {
listener(mutatedNodesByType, {
dirtyLeaves,
prevEditorState,
updateTags,
});
}
}
}
}
Expand Down Expand Up @@ -740,14 +744,14 @@ export function triggerCommandListeners<
return false;
}

function triggerEnqueuedUpdates(editor: LexicalEditor): void {
function $triggerEnqueuedUpdates(editor: LexicalEditor): void {
const queuedUpdates = editor._updates;

if (queuedUpdates.length !== 0) {
const queuedUpdate = queuedUpdates.shift();
if (queuedUpdate) {
const [updateFn, options] = queuedUpdate;
beginUpdate(editor, updateFn, options);
$beginUpdate(editor, updateFn, options);
}
}
}
Expand Down Expand Up @@ -814,7 +818,7 @@ function processNestedUpdates(
return skipTransforms;
}

function beginUpdate(
function $beginUpdate(
editor: LexicalEditor,
updateFn: () => void,
options?: EditorUpdateOptions,
Expand Down Expand Up @@ -939,7 +943,7 @@ function beginUpdate(

editor._dirtyElements.clear();

commitPendingUpdates(editor);
$commitPendingUpdates(editor);
return;
} finally {
activeEditorState = previousActiveEditorState;
Expand All @@ -956,10 +960,10 @@ function beginUpdate(
if (shouldUpdate) {
if (pendingEditorState._flushSync) {
pendingEditorState._flushSync = false;
commitPendingUpdates(editor);
$commitPendingUpdates(editor);
} else if (editorStateWasCloned) {
scheduleMicroTask(() => {
commitPendingUpdates(editor);
$commitPendingUpdates(editor);
});
}
} else {
Expand All @@ -973,14 +977,16 @@ function beginUpdate(
}
}

export function updateEditor(
export function $updateEditor(
editor: LexicalEditor,
updateFn: () => void,
options?: EditorUpdateOptions,
): void {
if (editor._updating) {
editor._updates.push([updateFn, options]);
} else {
beginUpdate(editor, updateFn, options);
$beginUpdate(editor, updateFn, options);
}
}
/** @deprecated renamed to $updateEditor by @lexical/eslint-plugin rules-of-lexical */
export const updateEditor = $updateEditor;