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

169 lines
3.9 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 shouldShow: Exclude<FloatingMenuPluginProps['shouldShow'], null> = ({ 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
if (!empty || !isRootDepth || !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
}: 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)
2021-04-16 17:12:03 +08:00
this.element.style.visibility = 'visible'
// We create tippy asynchronously to make sure that `editor.options.element`
// has already been moved to the right position in the DOM
requestAnimationFrame(() => {
this.createTooltip(tippyOptions)
})
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()
}
2021-04-16 18:42:56 +08:00
createTooltip(options: Partial<Props> = {}) {
this.tippy = tippy(this.editor.options.element, {
2021-04-16 17:12:03 +08:00
duration: 0,
getReferenceClientRect: null,
content: this.element,
interactive: true,
trigger: 'manual',
placement: 'right',
2021-04-16 17:14:30 +08:00
hideOnClick: 'toggle',
2021-04-16 18:42:56 +08:00
...options,
2021-04-16 17:12:03 +08:00
})
}
2021-04-01 21:19:31 +08:00
update(view: EditorView, oldState?: EditorState) {
const { state, composing } = view
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 (composing || isSame) {
return
}
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({
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()
2021-04-01 21:19:31 +08:00
this.element.removeEventListener('mousedown', this.mousedownHandler)
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 }),
})
}