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

123 lines
2.7 KiB
TypeScript
Raw Normal View History

import { Editor, posToClientRect } 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 17:34:37 +08:00
import tippy, { Instance } from 'tippy.js'
2021-03-30 17:36:51 +08:00
2021-03-30 20:07:18 +08:00
export interface BubbleMenuPluginProps {
2021-03-30 18:45:56 +08:00
editor: Editor,
element: HTMLElement,
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
2021-04-16 17:34:37 +08:00
public tippy!: Instance
2021-04-10 04:42:09 +08:00
2021-04-16 17:11:41 +08:00
constructor({ editor, element, view }: BubbleMenuViewProps) {
2021-03-30 18:45:56 +08:00
this.editor = editor
this.element = element
this.view = view
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
2021-04-10 04:42:09 +08:00
this.createTooltip()
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
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
2021-04-10 04:42:09 +08:00
createTooltip() {
2021-04-16 17:34:37 +08:00
this.tippy = tippy(this.view.dom, {
2021-04-10 04:42:09 +08:00
duration: 0,
getReferenceClientRect: null,
content: this.element,
interactive: true,
trigger: 'manual',
placement: 'top',
hideOnClick: 'toggle',
})
}
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
}
2021-03-30 18:13:55 +08:00
const { from, to, empty } = selection
2021-03-30 17:36:51 +08:00
2021-04-10 04:42:09 +08:00
if (empty) {
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
}
2021-04-16 17:34:37 +08:00
this.tippy.setProps({
getReferenceClientRect: () => posToClientRect(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() {
2021-04-16 17:34:37 +08:00
this.tippy.show()
2021-03-30 17:36:51 +08:00
}
2021-03-30 18:56:23 +08:00
hide() {
2021-04-16 17:34:37 +08:00
this.tippy.hide()
2021-03-30 17:36:51 +08:00
}
destroy() {
2021-04-16 17:34:37 +08:00
this.tippy.destroy()
2021-03-30 18:45:56 +08:00
this.element.removeEventListener('mousedown', this.mousedownHandler)
this.editor.off('focus', this.focusHandler)
this.editor.off('blur', this.blurHandler)
2021-03-30 17:36:51 +08:00
}
}
2021-03-30 18:56:23 +08:00
export const BubbleMenuPluginKey = new PluginKey('menuBubble')
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-03-30 18:56:23 +08:00
key: BubbleMenuPluginKey,
2021-03-30 18:45:56 +08:00
view: view => new BubbleMenuView({ view, ...options }),
2021-03-30 17:36:51 +08:00
})
}