tiptap/packages/extension-bubble-menu/src/bubble-menu-plugin.ts

254 lines
6.1 KiB
TypeScript
Raw Normal View History

import {
Editor,
isNodeSelection,
isTextSelection,
posToDOMRect,
} from '@tiptap/core'
import debounce from 'lodash/debounce'
2021-03-30 17:36:51 +08:00
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
2021-04-16 18:42:56 +08:00
import tippy, { Instance, Props } from 'tippy.js'
2021-03-30 17:36:51 +08:00
2021-03-30 20:07:18 +08:00
export interface BubbleMenuPluginProps {
2021-08-13 18:33:06 +08:00
pluginKey: PluginKey | string,
2021-03-30 18:45:56 +08:00
editor: Editor,
element: HTMLElement,
2021-04-16 18:42:56 +08:00
tippyOptions?: Partial<Props>,
updateDelay?: number,
shouldShow?: ((props: {
editor: Editor,
view: EditorView,
state: EditorState,
oldState?: EditorState,
from: number,
to: number,
}) => boolean) | null,
2021-03-30 17:36:51 +08:00
}
2021-03-31 16:22:54 +08:00
export type BubbleMenuViewProps = BubbleMenuPluginProps & {
2021-03-30 18:45:56 +08:00
view: EditorView,
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
export class BubbleMenuView {
public editor: Editor
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public element: HTMLElement
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public view: EditorView
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
public preventHide = false
public tippy: Instance | undefined
2021-04-10 04:42:09 +08:00
public tippyOptions?: Partial<Props>
public updateDelay: number
2021-12-15 16:06:43 +08:00
public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({
view,
state,
from,
to,
}) => {
const { doc, selection } = state
const { empty } = selection
// Sometime check for `empty` is not enough.
// Doubleclick an empty paragraph returns a node size of 2.
// So we check also for an empty text size.
const isEmptyTextBlock = !doc.textBetween(from, to).length
&& isTextSelection(state.selection)
// When clicking on a element inside the bubble menu the editor "blur" event
// is called and the bubble menu item is focussed. In this case we should
2022-11-08 04:41:00 +08:00
// consider the menu as part of the editor and keep showing the menu
const isChildOfMenu = this.element.contains(document.activeElement)
const hasEditorFocus = view.hasFocus() || isChildOfMenu
2021-12-15 16:06:43 +08:00
if (
!hasEditorFocus
2021-12-15 16:06:43 +08:00
|| empty
|| isEmptyTextBlock
|| !this.editor.isEditable
2021-12-15 16:06:43 +08:00
) {
return false
}
return true
}
2021-04-16 18:42:56 +08:00
constructor({
editor,
element,
view,
tippyOptions = {},
updateDelay = 250,
shouldShow,
2021-04-16 18:42:56 +08:00
}: BubbleMenuViewProps) {
2021-03-30 18:45:56 +08:00
this.editor = editor
this.element = element
this.view = view
this.updateDelay = updateDelay
if (shouldShow) {
this.shouldShow = shouldShow
}
2021-03-30 18:45:56 +08:00
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
this.view.dom.addEventListener('dragstart', this.dragstartHandler)
2021-03-30 18:45:56 +08:00
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()
2021-04-10 04:42:09 +08:00
this.element.style.visibility = 'visible'
2021-03-30 17:36:51 +08:00
}
2021-03-30 18:45:56 +08:00
mousedownHandler = () => {
this.preventHide = true
}
2021-03-30 17:36:51 +08:00
dragstartHandler = () => {
this.hide()
}
2021-03-30 18:45:56 +08:00
focusHandler = () => {
2021-03-31 16:58:05 +08:00
// we use `setTimeout` to make sure `selection` is already updated
setTimeout(() => this.update(this.editor.view))
2021-03-30 18:45:56 +08:00
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:45:56 +08:00
blurHandler = ({ event }: { event: FocusEvent }) => {
if (this.preventHide) {
this.preventHide = false
2021-03-30 18:56:23 +08:00
2021-03-30 18:45:56 +08:00
return
}
2021-03-30 17:36:51 +08:00
2021-03-30 18:56:23 +08:00
if (
event?.relatedTarget
&& this.element.parentNode?.contains(event.relatedTarget as Node)
) {
return
}
this.hide()
2021-03-30 18:45:56 +08:00
}
2021-03-30 17:36:51 +08:00
tippyBlurHandler = (event : FocusEvent) => {
this.blurHandler({ event })
}
createTooltip() {
const { element: editorElement } = this.editor.options
2021-09-08 02:55:40 +08:00
const editorIsAttached = !!editorElement.parentElement
2021-09-08 02:55:40 +08:00
if (this.tippy || !editorIsAttached) {
return
}
2021-09-08 02:55:40 +08:00
this.tippy = tippy(editorElement, {
duration: 0,
getReferenceClientRect: null,
content: this.element,
interactive: true,
trigger: 'manual',
placement: 'top',
hideOnClick: 'toggle',
...this.tippyOptions,
})
// maybe we have to hide tippy on its own blur event as well
if (this.tippy.popper.firstChild) {
(this.tippy.popper.firstChild as HTMLElement).addEventListener('blur', this.tippyBlurHandler)
}
2021-04-10 04:42:09 +08:00
}
2021-03-30 18:13:55 +08:00
update(view: EditorView, oldState?: EditorState) {
const { state } = view
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)
}
}
updateHandler = (view: EditorView, oldState?: EditorState) => {
2021-03-30 18:13:55 +08:00
const { state, composing } = view
const { doc, selection } = state
2021-03-31 16:58:05 +08:00
const isSame = oldState && oldState.doc.eq(doc) && oldState.selection.eq(selection)
2021-03-30 17:36:51 +08:00
2021-03-31 16:58:05 +08:00
if (composing || isSame) {
2021-03-30 17:36:51 +08:00
return
}
this.createTooltip()
// support for CellSelections
const { ranges } = selection
const from = Math.min(...ranges.map(range => range.$from.pos))
const to = Math.max(...ranges.map(range => range.$to.pos))
const shouldShow = this.shouldShow?.({
editor: this.editor,
view,
state,
oldState,
from,
to,
})
if (!shouldShow) {
2021-03-30 17:36:51 +08:00
this.hide()
2021-03-30 18:13:55 +08:00
2021-03-30 17:36:51 +08:00
return
}
this.tippy?.setProps({
2022-04-01 04:18:40 +08:00
getReferenceClientRect: this.tippyOptions?.getReferenceClientRect || (() => {
if (isNodeSelection(state.selection)) {
const node = view.nodeDOM(from) as HTMLElement
if (node) {
return node.getBoundingClientRect()
}
}
return posToDOMRect(view, from, to)
2022-04-01 04:18:40 +08:00
}),
2021-04-10 04:42:09 +08:00
})
this.show()
2021-03-30 17:36:51 +08:00
}
2021-04-10 04:42:09 +08:00
show() {
this.tippy?.show()
2021-03-30 17:36:51 +08:00
}
2021-03-30 18:56:23 +08:00
hide() {
this.tippy?.hide()
2021-03-30 17:36:51 +08:00
}
destroy() {
if (this.tippy?.popper.firstChild) {
(this.tippy.popper.firstChild as HTMLElement).removeEventListener('blur', this.tippyBlurHandler)
}
this.tippy?.destroy()
this.element.removeEventListener('mousedown', this.mousedownHandler, { capture: true })
this.view.dom.removeEventListener('dragstart', this.dragstartHandler)
2021-03-30 18:45:56 +08:00
this.editor.off('focus', this.focusHandler)
this.editor.off('blur', this.blurHandler)
2021-03-30 17:36:51 +08:00
}
}
2021-03-30 20:07:18 +08:00
export const BubbleMenuPlugin = (options: BubbleMenuPluginProps) => {
2021-03-30 17:36:51 +08:00
return new Plugin({
2021-08-13 18:33:06 +08:00
key: typeof options.pluginKey === 'string'
? new PluginKey(options.pluginKey)
: options.pluginKey,
2021-03-30 18:45:56 +08:00
view: view => new BubbleMenuView({ view, ...options }),
2021-03-30 17:36:51 +08:00
})
}