Skip to content

Commit

Permalink
Handle NodeViews in BubbleMenu positioning (#3881)
Browse files Browse the repository at this point in the history
* fix(bubble-menu): use correct children of node view renderers for clientRect

* fix(bubble-menu): remove lodash

* fix(bubble-menu): support vue node views

* fix(demos): revert bubble menu demo
  • Loading branch information
bdbch authored Mar 27, 2023
1 parent 00aad1f commit 65371b7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
4 changes: 3 additions & 1 deletion demos/src/Extensions/BubbleMenu/React/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import './styles.scss'

import { BubbleMenu, EditorContent, useEditor } from '@tiptap/react'
import {
BubbleMenu, EditorContent, useEditor,
} from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import React, { useEffect } from 'react'

Expand Down
24 changes: 5 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions packages/extension-bubble-menu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@tiptap/pm": "^2.0.0-beta.209"
},
"dependencies": {
"lodash": "^4.17.21",
"tippy.js": "^6.3.7"
},
"repository": {
Expand All @@ -43,8 +42,7 @@
},
"sideEffects": false,
"devDependencies": {
"@tiptap/pm": "^2.0.0-beta.220",
"@types/lodash": "^4.14.191"
"@tiptap/pm": "^2.0.0-beta.220"
},
"scripts": {
"clean": "rm -rf dist",
Expand Down
28 changes: 23 additions & 5 deletions packages/extension-bubble-menu/src/bubble-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
} from '@tiptap/core'
import { EditorState, Plugin, PluginKey } from '@tiptap/pm/state'
import { EditorView } from '@tiptap/pm/view'
import debounce from 'lodash/debounce'
import tippy, { Instance, Props } from 'tippy.js'

export interface BubbleMenuPluginProps {
Expand Down Expand Up @@ -43,6 +42,8 @@ export class BubbleMenuView {

public updateDelay: number

private updateDebounceTimer: number | undefined

public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({
view,
state,
Expand Down Expand Up @@ -159,10 +160,21 @@ export class BubbleMenuView {
const hasValidSelection = state.selection.$from.pos !== state.selection.$to.pos

if (this.updateDelay > 0 && hasValidSelection) {
debounce(this.updateHandler, this.updateDelay)(view, oldState)
} else {
this.updateHandler(view, oldState)
this.handleDebouncedUpdate(view, oldState)
return
}

this.updateHandler(view, oldState)
}

handleDebouncedUpdate = (view: EditorView, oldState?: EditorState) => {
if (this.updateDebounceTimer) {
clearTimeout(this.updateDebounceTimer)
}

this.updateDebounceTimer = window.setTimeout(() => {
this.updateHandler(view, oldState)
}, this.updateDelay)
}

updateHandler = (view: EditorView, oldState?: EditorState) => {
Expand Down Expand Up @@ -201,7 +213,13 @@ export class BubbleMenuView {
this.tippyOptions?.getReferenceClientRect
|| (() => {
if (isNodeSelection(state.selection)) {
const node = view.nodeDOM(from) as HTMLElement
let node = view.nodeDOM(from) as HTMLElement

const nodeViewWrapper = node.dataset.nodeViewWrapper ? node : node.querySelector('[data-node-view-wrapper]')

if (nodeViewWrapper) {
node = nodeViewWrapper.firstChild as HTMLElement
}

if (node) {
return node.getBoundingClientRect()
Expand Down

0 comments on commit 65371b7

Please sign in to comment.