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

190 lines
4.3 KiB
TypeScript
Raw Normal View History

import { Editor, posToDOMRect } from '@tiptap/core'
2021-04-01 21:19:31 +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-04-01 21:19:31 +08:00
export interface FloatingMenuPluginProps {
2021-08-13 18:33:06 +08:00
pluginKey: PluginKey | string,
2021-04-01 21:19:31 +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,
}) => boolean) | null,
2021-04-01 21:19:31 +08:00
}
export type FloatingMenuViewProps = FloatingMenuPluginProps & {
view: EditorView,
}
export class FloatingMenuView {
public editor: Editor
public element: HTMLElement
public view: EditorView
public preventHide = false
public tippy: Instance | undefined
2021-04-16 17:12:03 +08:00
public tippyOptions?: Partial<Props>
2021-12-15 16:06:43 +08:00
public shouldShow: Exclude<FloatingMenuPluginProps['shouldShow'], null> = ({ view, state }) => {
const { selection } = state
const { $anchor, empty } = selection
const isRootDepth = $anchor.depth === 1
const isEmptyTextBlock = $anchor.parent.isTextblock
&& !$anchor.parent.type.spec.code
&& !$anchor.parent.textContent
2021-12-15 16:06:43 +08:00
if (
!view.hasFocus()
|| !empty
|| !isRootDepth
|| !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 = {},
shouldShow,
2021-04-16 18:42:56 +08:00
}: FloatingMenuViewProps) {
2021-04-01 21:19:31 +08:00
this.editor = editor
this.element = element
this.view = view
if (shouldShow) {
this.shouldShow = shouldShow
}
2021-04-16 17:34:37 +08:00
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
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-16 17:12:03 +08:00
this.element.style.visibility = 'visible'
2021-04-01 21:19:31 +08:00
}
mousedownHandler = () => {
this.preventHide = true
}
focusHandler = () => {
// we use `setTimeout` to make sure `selection` is already updated
setTimeout(() => this.update(this.editor.view))
}
blurHandler = ({ event }: { event: FocusEvent }) => {
if (this.preventHide) {
this.preventHide = false
return
}
if (
event?.relatedTarget
&& this.element.parentNode?.contains(event.relatedTarget as Node)
) {
return
}
this.hide()
}
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: 'right',
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', event => {
this.blurHandler({ event })
})
}
2021-04-16 17:12:03 +08:00
}
2021-04-01 21:19:31 +08:00
update(view: EditorView, oldState?: EditorState) {
const { state } = view
2021-04-01 21:19:31 +08:00
const { doc, selection } = state
const { from, to } = selection
2021-04-01 21:19:31 +08:00
const isSame = oldState && oldState.doc.eq(doc) && oldState.selection.eq(selection)
if (isSame) {
2021-04-01 21:19:31 +08:00
return
}
this.createTooltip()
const shouldShow = this.shouldShow?.({
editor: this.editor,
view,
state,
oldState,
})
2021-04-01 21:19:31 +08:00
if (!shouldShow) {
2021-04-01 21:19:31 +08:00
this.hide()
return
}
this.tippy?.setProps({
2022-04-01 04:18:40 +08:00
getReferenceClientRect: this.tippyOptions?.getReferenceClientRect || (() => posToDOMRect(view, from, to)),
2021-04-16 17:12:03 +08:00
})
2021-04-01 21:19:31 +08:00
2021-04-16 17:12:03 +08:00
this.show()
2021-04-01 21:19:31 +08:00
}
2021-04-16 17:12:03 +08:00
show() {
this.tippy?.show()
2021-04-01 21:19:31 +08:00
}
hide() {
this.tippy?.hide()
2021-04-01 21:19:31 +08:00
}
destroy() {
this.tippy?.destroy()
this.element.removeEventListener('mousedown', this.mousedownHandler, { capture: true })
2021-04-01 21:19:31 +08:00
this.editor.off('focus', this.focusHandler)
this.editor.off('blur', this.blurHandler)
}
}
export const FloatingMenuPlugin = (options: FloatingMenuPluginProps) => {
return new Plugin({
2021-08-13 18:33:06 +08:00
key: typeof options.pluginKey === 'string'
? new PluginKey(options.pluginKey)
: options.pluginKey,
2021-04-01 21:19:31 +08:00
view: view => new FloatingMenuView({ view, ...options }),
})
}