From 57b6d234351e45c60227c5c02ff6d3cc932617b0 Mon Sep 17 00:00:00 2001 From: Maksim Horbachevsky Date: Mon, 17 Jul 2023 22:47:40 -0400 Subject: [PATCH] [RFC] Add prev editor state for mutation listener (#4796) --- packages/lexical/flow/Lexical.js.flow | 6 +++++- packages/lexical/src/LexicalEditor.ts | 6 +++++- packages/lexical/src/LexicalUpdates.ts | 10 +++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/lexical/flow/Lexical.js.flow b/packages/lexical/flow/Lexical.js.flow index 30c41039fbb..5530cf29570 100644 --- a/packages/lexical/flow/Lexical.js.flow +++ b/packages/lexical/flow/Lexical.js.flow @@ -88,7 +88,11 @@ type TextContentListener = (text: string) => void; type ErrorHandler = (error: Error) => void; export type MutationListener = ( nodes: Map, - {updateTags: Set, dirtyLeaves: Set}, + { + updateTags: Set, + dirtyLeaves: Set, + prevEditorState: EditorState, + }, ) => void; export type EditableListener = (editable: boolean) => void; type Listeners = { diff --git a/packages/lexical/src/LexicalEditor.ts b/packages/lexical/src/LexicalEditor.ts index 128923f0a24..47a97c1106b 100644 --- a/packages/lexical/src/LexicalEditor.ts +++ b/packages/lexical/src/LexicalEditor.ts @@ -205,7 +205,11 @@ export type TextContentListener = (text: string) => void; export type MutationListener = ( nodes: Map, - payload: {updateTags: Set; dirtyLeaves: Set}, + payload: { + updateTags: Set; + dirtyLeaves: Set; + prevEditorState: EditorState; + }, ) => void; export type CommandListener

= (payload: P, editor: LexicalEditor) => boolean; diff --git a/packages/lexical/src/LexicalUpdates.ts b/packages/lexical/src/LexicalUpdates.ts index f90b25ca98c..006f060ebd6 100644 --- a/packages/lexical/src/LexicalUpdates.ts +++ b/packages/lexical/src/LexicalUpdates.ts @@ -594,7 +594,13 @@ export function commitPendingUpdates( } if (mutatedNodes !== null) { - triggerMutationListeners(editor, mutatedNodes, tags, dirtyLeaves); + triggerMutationListeners( + editor, + mutatedNodes, + tags, + dirtyLeaves, + currentEditorState, + ); } if ( !$isRangeSelection(pendingSelection) && @@ -653,6 +659,7 @@ function triggerMutationListeners( mutatedNodes: MutatedNodes, updateTags: Set, dirtyLeaves: Set, + prevEditorState: EditorState, ): void { const listeners = Array.from(editor._listeners.mutation); const listenersLength = listeners.length; @@ -663,6 +670,7 @@ function triggerMutationListeners( if (mutatedNodesByType !== undefined) { listener(mutatedNodesByType, { dirtyLeaves, + prevEditorState, updateTags, }); }