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

OnChangePlugin ignoreInitialChange -> ignoreHistoryMergeTagChange #2706

Merged
merged 3 commits into from
Jul 26, 2022
Merged
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
2 changes: 2 additions & 0 deletions packages/lexical-react/flow/LexicalOnChangePlugin.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import type {EditorState, LexicalEditor} from 'lexical';

declare export function OnChangePlugin({
ignoreHistoryMergeTagChange?: boolean,
// TODO 0.4 remove
ignoreInitialChange?: boolean,
ignoreSelectionChange?: boolean,
onChange: (editorState: EditorState, editor: LexicalEditor) => void,
Expand Down
21 changes: 16 additions & 5 deletions packages/lexical-react/src/LexicalOnChangePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import useLayoutEffect from 'shared/useLayoutEffect';

export function OnChangePlugin({
// TODO 0.4 flip to true
ignoreHistoryMergeTagChange = false,
ignoreInitialChange = true,
ignoreSelectionChange = false,
onChange,
}: {
ignoreHistoryMergeTagChange?: boolean;
// TODO 0.4 remove
ignoreInitialChange?: boolean;
ignoreSelectionChange?: boolean;
onChange: (editorState: EditorState, editor: LexicalEditor) => void;
Expand All @@ -25,11 +29,12 @@ export function OnChangePlugin({
useLayoutEffect(() => {
if (onChange) {
return editor.registerUpdateListener(
({editorState, dirtyElements, dirtyLeaves, prevEditorState}) => {
({editorState, dirtyElements, dirtyLeaves, prevEditorState, tags}) => {
if (
ignoreSelectionChange &&
dirtyElements.size === 0 &&
dirtyLeaves.size === 0
(ignoreSelectionChange &&
dirtyElements.size === 0 &&
dirtyLeaves.size === 0) ||
(ignoreHistoryMergeTagChange && tags.has('history-merge'))
) {
return;
}
Expand All @@ -42,7 +47,13 @@ export function OnChangePlugin({
},
);
}
}, [editor, ignoreInitialChange, ignoreSelectionChange, onChange]);
}, [
editor,
ignoreHistoryMergeTagChange,
ignoreInitialChange,
ignoreSelectionChange,
onChange,
]);

return null;
}