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

Handle NodeViews in BubbleMenu positioning #3881

Merged
merged 4 commits into from
Mar 27, 2023
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
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