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

[Bug]: Update event gets triggered unexpectedly without the document changing #4828

Open
2 tasks done
yyjlincoln opened this issue Jan 24, 2024 · 2 comments
Open
2 tasks done
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@yyjlincoln
Copy link
Contributor

yyjlincoln commented Jan 24, 2024

Which packages did you experience the bug in?

core

What Tiptap version are you using?

2.1.16

What’s the bug you are facing?

The update event is triggered by editor.setEditable by default, where the content had not been updated, which contradicts with the doc description (and to my opinion, the intended usage of) the update event.

At https://tiptap.dev/docs/editor/api/events#update: "The content has changed."
At https://tiptap.dev/docs/editor/api/editor#set-editable: "setEditable: emitUpdate: defaults to true."
First introduced by: #2850 (PR #2935)

What browser are you using?

Chrome

Code example

No response

What did you expect to happen?

The update event is not triggered if it is implemented per doc, or the doc needs to be updated to reflect it.

Anything to add? (optional)

We are using @tiptap/react, and we wrap the editor around a component that accepts isEditable, content and onChange to make it behave like a React controlled component. It behaves more or less like:

export const EditorComponent = ({ isEditable, content, onChange }) => {
  // ...  
  useEffect(() => {
    const fn = () => {
      setContent(editor.getContent());
    };

    editor.on("update", fn);
    return () => {
      editor.off(fn);
    };
  }, [editor]);

  useEffect(() => {
    queueMicrotask(() => {
      // This unexpectedly emits an update event.
      editor.setEditable(isEditable);
    });
  }, [editor, isEditable]);

  useEffect(() => {
    queueMicrotask(() => {
      // Ths does not emit an update event by default.
      editor.setContent(content);
    });
  }, [editor, content]);

  useEffect(() => {
    queueMicrotask(() => {
      editor.setContent(content);
    });
  }, [editor, content]);
}

When there are lots to render on the page, in a ~20% chance, editor.setEditable gets called first, emitting an update event which updates the parent state with an empty document (which is the default prior to setContent). This causes the prop content to change, setting the content on the editor again to nothing. In the end, the content in parent state is replaced by an empty document.

This can be fixed by setting .setEditable(content, false), however it would still be nice to discuss how the update event should be used / documented to avoid confusion like this in the future.

Notes: I understand that using beta versions do come with disadvantages like this. setEditable(content)'s behaviour was changed from the beta (we locked to ^2.0.0-beta.109) thanks to the issue linked above, but was not documented and cannot be found in any upgrade guides (although I might be wrong).

Would probably be nice to document this somewhere, or to put a clear warning on setEditable as its second param has a different default value than the other commonly used command, setContent (setEditable defaults to true and setContent defaults to false).

*The company that I work in is a Github Sponsor of this project.

Did you update your dependencies?

  • Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖
@yyjlincoln yyjlincoln added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Jan 24, 2024
@github-project-automation github-project-automation bot moved this to Triage open in Tiptap Jan 24, 2024
@hadiqaiser
Copy link

hadiqaiser commented Sep 10, 2024

Facing same issue, did you find any fix.

@yyjlincoln
Copy link
Contributor Author

yyjlincoln commented Sep 10, 2024 via email

@github-project-automation github-project-automation bot moved this to Needs Triage in Tiptap: Issues Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
Status: Needs Triage
Status: Triage open
Development

No branches or pull requests

2 participants