Skip to content

Commit

Permalink
[RFC] Add prev editor state for mutation listener (#4796)
Browse files Browse the repository at this point in the history
  • Loading branch information
fantactuka authored Jul 18, 2023
1 parent ccf8bf3 commit 57b6d23
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ type TextContentListener = (text: string) => void;
type ErrorHandler = (error: Error) => void;
export type MutationListener = (
nodes: Map<NodeKey, NodeMutation>,
{updateTags: Set<string>, dirtyLeaves: Set<string>},
{
updateTags: Set<string>,
dirtyLeaves: Set<string>,
prevEditorState: EditorState,
},
) => void;
export type EditableListener = (editable: boolean) => void;
type Listeners = {
Expand Down
6 changes: 5 additions & 1 deletion packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ export type TextContentListener = (text: string) => void;

export type MutationListener = (
nodes: Map<NodeKey, NodeMutation>,
payload: {updateTags: Set<string>; dirtyLeaves: Set<string>},
payload: {
updateTags: Set<string>;
dirtyLeaves: Set<string>;
prevEditorState: EditorState;
},
) => void;

export type CommandListener<P> = (payload: P, editor: LexicalEditor) => boolean;
Expand Down
10 changes: 9 additions & 1 deletion packages/lexical/src/LexicalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,13 @@ export function commitPendingUpdates(
}

if (mutatedNodes !== null) {
triggerMutationListeners(editor, mutatedNodes, tags, dirtyLeaves);
triggerMutationListeners(
editor,
mutatedNodes,
tags,
dirtyLeaves,
currentEditorState,
);
}
if (
!$isRangeSelection(pendingSelection) &&
Expand Down Expand Up @@ -653,6 +659,7 @@ function triggerMutationListeners(
mutatedNodes: MutatedNodes,
updateTags: Set<string>,
dirtyLeaves: Set<string>,
prevEditorState: EditorState,
): void {
const listeners = Array.from(editor._listeners.mutation);
const listenersLength = listeners.length;
Expand All @@ -663,6 +670,7 @@ function triggerMutationListeners(
if (mutatedNodesByType !== undefined) {
listener(mutatedNodesByType, {
dirtyLeaves,
prevEditorState,
updateTags,
});
}
Expand Down

2 comments on commit 57b6d23

@vercel
Copy link

@vercel vercel bot commented on 57b6d23 Jul 18, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-git-main-fbopensource.vercel.app
lexical.dev
lexical-fbopensource.vercel.app
lexicaljs.com
www.lexical.dev
lexicaljs.org

@vercel
Copy link

@vercel vercel bot commented on 57b6d23 Jul 18, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-git-main-fbopensource.vercel.app
playground.lexical.dev
lexical-playground.vercel.app
lexical-playground-fbopensource.vercel.app

Please sign in to comment.