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

refactor(extension/bubble-menu): add debounce to bubble menu updates #3385

Merged
merged 2 commits into from
Nov 4, 2022
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
6 changes: 5 additions & 1 deletion demos/src/Extensions/BubbleMenu/Vue/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<input type="checkbox" :checked="isEditable" @change="() => isEditable = !isEditable">
Editable
</div>
<bubble-menu :editor="editor" :tippy-options="{ duration: 100 }" v-if="editor">
<bubble-menu
:editor="editor"
:tippy-options="{ duration: 100 }"
v-if="editor"
>
<button @click="editor.chain().focus().toggleBold().run()" :class="{ 'is-active': editor.isActive('bold') }">
bold
</button>
Expand Down
8 changes: 8 additions & 0 deletions docs/api/extensions/bubble-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Type: `HTMLElement`

Default: `null`

### delay
The `BubbleMenu` debounces the `update` method to allow the bubble menu to not be updated on every selection update. This can be controlled in milliseconds.
The BubbleMenuPlugin will come with a default delay of 250ms. This can be deactivated, by setting the delay to `0` which deactivates the debounce.

Type: `Number`

Default: `undefined`

### tippyOptions
Under the hood, the `BubbleMenu` uses [tippy.js](https://atomiks.github.io/tippyjs/v6/all-props/). You can directly pass options to it.

Expand Down
25 changes: 23 additions & 2 deletions package-lock.json

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

6 changes: 5 additions & 1 deletion packages/extension-bubble-menu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@
"url": "https://github.com/ueberdosis/tiptap",
"directory": "packages/extension-bubble-menu"
},
"sideEffects": false
"sideEffects": false,
"devDependencies": {
"@types/lodash": "^4.14.187",
"lodash": "^4.17.21"
}
}
21 changes: 21 additions & 0 deletions packages/extension-bubble-menu/src/bubble-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isTextSelection,
posToDOMRect,
} from '@tiptap/core'
import debounce from 'lodash/debounce'
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import tippy, { Instance, Props } from 'tippy.js'
Expand All @@ -13,6 +14,7 @@ export interface BubbleMenuPluginProps {
editor: Editor,
element: HTMLElement,
tippyOptions?: Partial<Props>,
delay?: number,
shouldShow?: ((props: {
editor: Editor,
view: EditorView,
Expand Down Expand Up @@ -40,6 +42,8 @@ export class BubbleMenuView {

public tippyOptions?: Partial<Props>

public delay: number

public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({
view,
state,
Expand Down Expand Up @@ -79,11 +83,13 @@ export class BubbleMenuView {
element,
view,
tippyOptions = {},
delay = 250,
shouldShow,
}: BubbleMenuViewProps) {
this.editor = editor
this.element = element
this.view = view
this.delay = delay

if (shouldShow) {
this.shouldShow = shouldShow
Expand Down Expand Up @@ -157,6 +163,21 @@ export class BubbleMenuView {
}

update(view: EditorView, oldState?: EditorState) {
const { state } = view
const hasValidSelection = state.selection.$from.pos !== state.selection.$to.pos

if (hasValidSelection) {
if (this.delay > 0) {
debounce(this.updateHandler, this.delay)(view, oldState)
} else {
this.updateHandler(view, oldState)
}
} else {
this.hide()
}
}

updateHandler = (view: EditorView, oldState?: EditorState) => {
const { state, composing } = view
const { doc, selection } = state
const isSame = oldState && oldState.doc.eq(doc) && oldState.selection.eq(selection)
Expand Down
2 changes: 2 additions & 0 deletions packages/extension-bubble-menu/src/bubble-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const BubbleMenu = Extension.create<BubbleMenuOptions>({
element: null,
tippyOptions: {},
pluginKey: 'bubbleMenu',
delay: undefined,
shouldShow: null,
}
},
Expand All @@ -29,6 +30,7 @@ export const BubbleMenu = Extension.create<BubbleMenuOptions>({
editor: this.editor,
element: this.options.element,
tippyOptions: this.options.tippyOptions,
delay: this.options.delay,
shouldShow: this.options.shouldShow,
}),
]
Expand Down
28 changes: 11 additions & 17 deletions packages/react/src/BubbleMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
import React, {
useEffect, useState,
} from 'react'
import React, { useEffect, useState } from 'react'

type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;

export type BubbleMenuProps = Omit<Optional<BubbleMenuPluginProps, 'pluginKey'>, 'element'> & {
className?: string,
children: React.ReactNode
}
className?: string;
children: React.ReactNode;
delay?: number;
};

export const BubbleMenu = (props: BubbleMenuProps) => {
const [element, setElement] = useState<HTMLDivElement | null>(null)
Expand All @@ -23,26 +22,21 @@ export const BubbleMenu = (props: BubbleMenuProps) => {
}

const {
pluginKey = 'bubbleMenu',
editor,
tippyOptions = {},
shouldShow = null,
pluginKey = 'bubbleMenu', editor, tippyOptions = {}, delay, shouldShow = null,
} = props

const plugin = BubbleMenuPlugin({
pluginKey,
delay,
editor,
element,
tippyOptions,
pluginKey,
shouldShow,
tippyOptions,
})

editor.registerPlugin(plugin)
return () => editor.unregisterPlugin(pluginKey)
}, [
props.editor,
element,
])
}, [props.editor, element])

return (
<div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
Expand Down
10 changes: 8 additions & 2 deletions packages/vue-2/src/BubbleMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface BubbleMenuInterface extends Vue {
pluginKey: BubbleMenuPluginProps['pluginKey'],
editor: BubbleMenuPluginProps['editor'],
tippyOptions: BubbleMenuPluginProps['tippyOptions'],
delay: BubbleMenuPluginProps['delay'],
shouldShow: BubbleMenuPluginProps['shouldShow'],
}

Expand All @@ -22,6 +23,10 @@ export const BubbleMenu: Component = {
required: true,
},

delay: {
type: Number as PropType<BubbleMenuPluginProps['delay']>,
},

tippyOptions: {
type: Object as PropType<BubbleMenuPluginProps['tippyOptions']>,
default: () => ({}),
Expand All @@ -43,11 +48,12 @@ export const BubbleMenu: Component = {

this.$nextTick(() => {
editor.registerPlugin(BubbleMenuPlugin({
pluginKey: this.pluginKey,
delay: this.delay,
editor,
element: this.$el as HTMLElement,
tippyOptions: this.tippyOptions,
pluginKey: this.pluginKey,
shouldShow: this.shouldShow,
tippyOptions: this.tippyOptions,
}))
})
},
Expand Down
15 changes: 11 additions & 4 deletions packages/vue-3/src/BubbleMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export const BubbleMenu = defineComponent({
required: true,
},

delay: {
type: Number as PropType<BubbleMenuPluginProps['delay']>,
default: undefined,
},

tippyOptions: {
type: Object as PropType<BubbleMenuPluginProps['tippyOptions']>,
default: () => ({}),
Expand All @@ -40,18 +45,20 @@ export const BubbleMenu = defineComponent({

onMounted(() => {
const {
pluginKey,
delay,
editor,
tippyOptions,
pluginKey,
shouldShow,
tippyOptions,
} = props

editor.registerPlugin(BubbleMenuPlugin({
pluginKey,
delay,
editor,
element: root.value as HTMLElement,
tippyOptions,
pluginKey,
shouldShow,
tippyOptions,
}))
})

Expand Down