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

208 lines
4.7 KiB
TypeScript
Raw Normal View History

import {
Editor,
posToDOMRect,
isTextSelection,
isNodeSelection,
} from '@tiptap/core'
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>,
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 shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({ 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)
if (empty || isEmptyTextBlock) {
return false
}
return true
}
2021-04-16 18:42:56 +08:00
constructor({
editor,
element,
view,
tippyOptions = {},
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
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
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,
})
2021-04-10 04:42:09 +08:00
}
2021-03-30 18:13:55 +08:00
update(view: EditorView, oldState?: EditorState) {
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({
getReferenceClientRect: () => {
if (isNodeSelection(state.selection)) {
const node = view.nodeDOM(from) as HTMLElement
if (node) {
return node.getBoundingClientRect()
}
}
return posToDOMRect(view, from, to)
},
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() {
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
})
}