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

Fix: Don’t initialize tippy on requestAnimationFrame to avoid race conditions #1820

Conversation

enriquecastl
Copy link
Contributor

This PR defers the initialization of the tippy library in the bubble and floating menu extensions to the moment when the update hook is first invoked by ProseMirror. The goal of this change is initializing the tippy library only when the Editor’s element is attached to the document. The update hook provides a convenient place to check whether the aforementioned condition is met.

Why is this change necessary

We are implementing a WYSIWYG Editor using Tiptap’s Vue 2 extension. We ran into a problem where the requestAnimationFrame call is executed without delay right after the bubble menu plugin is initialized. This behavior causes a race condition where tippy is initialized before the Vue component is fully mounted.

How to reproduce the bug?

I can’t explain why requestAnimationFrame is invoked differently from the demos you provide in this repository. I couldn’t create an isolated sandbox where the problem can be reproduced either. However, you can reproduce the problem if you try to edit a Project Wiki in https://gitlab.com.

This approach ensures that a race condition can’t exist because the menu is only initialized when the editor’s state changes which is a sign that Tiptap’s Editor is fully mounted.

Instead of initializting tippy when
the bubble menu and floating menu plugins are initialized,
defer the initialization of tippy to the moment when
the the editor should display the floating or bubble menu
@@ -74,13 +76,10 @@ export class BubbleMenuView {
this.view.dom.addEventListener('dragstart', this.dragstartHandler)
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
this.tippyOptions = tippyOptions
// Detaches menu content from its current parent
this.element.remove()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: I considered safe to detach the menu element because tippy also detaches the element on mount.

@enriquecastl enriquecastl changed the title Defer initialization of tippy in bubble and floating menus Fix: Don’t initialize tippy on requestAnimationFrame to avoid race conditions Aug 31, 2021
@philippkuehn philippkuehn merged commit ca3763d into ueberdosis:main Sep 7, 2021
@philippkuehn
Copy link
Contributor

That looks great. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants