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

Pass tags to onChange for LexicalOnChangePlugin #4091

Merged
merged 1 commit into from
Mar 10, 2023

Conversation

strdr4605
Copy link
Contributor

We are using LexicalOnChangePlugin to save the state in DB.

We also have a plugin for reactive updates:

type Props = {
  serializedEditorState: SerializedEditorState | undefined;
  debounceTime?: number;
};

export const REACTIVE_UPDATE_TAG = 'reactive-update';

/**
 * Plugin to update the editor when the incoming serializedEditorState changes
 * (in cases when several users are editing at the same time)
 */
export const ReactiveUpdatePlugin = ({ serializedEditorState, debounceTime = 0 }: Props) => {
  const [editor] = useLexicalComposerContext();

  const checkDiff = useMemo(
    () =>
      debounce((current: SerializedEditorState, incoming: SerializedEditorState, update: () => void) => {
        if (JSON.stringify(current) === JSON.stringify(incoming)) {
          return;
        }

        update();
      }, debounceTime),
    [debounceTime]
  );

  useEffect(() => {
    if (!serializedEditorState) {
      return;
    }

    checkDiff(editor.getEditorState().toJSON(), serializedEditorState, () => {
      const newSerializedState = editor.parseEditorState(serializedEditorState);
      editor.setEditorState(newSerializedState, { tag: REACTIVE_UPDATE_TAG });
    });
  }, [editor, serializedEditorState, checkDiff]);

  return null;
};

We can identify that an update comes from this plugin by tag.

function onChange(editorState: EditorState, editor: LexicalEditor, tags: Set<string>) {
  if (tags.has(REACTIVE_UPDATE_TAG)) {
    return;
  }

  // do the DB update.
}

...


<OnChangePlugin onChange={onChange} />

We actually create our own OnChangePlugin but thought that other users may also benefit from tags passed to onChange

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 10, 2023
@vercel
Copy link

vercel bot commented Mar 10, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
lexical ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 10, 2023 at 2:45PM (UTC)
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 10, 2023 at 2:45PM (UTC)

Copy link
Member

@zurfyx zurfyx left a comment

Choose a reason for hiding this comment

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

Looks like a reasonable addition to me, thanks!

@zurfyx zurfyx merged commit d76df21 into facebook:main Mar 10, 2023
@strdr4605 strdr4605 deleted the onchange-tags branch March 10, 2023 18:22
@zurfyx zurfyx mentioned this pull request Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants