2021-04-24 05:01:53 +08:00
|
|
|
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>,
|
2021-08-24 00:44:40 +08:00
|
|
|
shouldShow?: ((props: {
|
2021-08-11 20:37:58 +08:00
|
|
|
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
|
|
|
|
|
2021-08-13 00:03:45 +08:00
|
|
|
public tippy: Instance | undefined
|
2021-04-16 17:12:03 +08:00
|
|
|
|
2021-08-11 20:37:58 +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,
|
2021-08-11 20:37:58 +08:00
|
|
|
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
|
2021-08-11 20:37:58 +08:00
|
|
|
|
|
|
|
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'
|
2021-08-13 00:03:45 +08:00
|
|
|
|
|
|
|
// 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> = {}) {
|
2021-08-13 00:03:45 +08:00
|
|
|
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
|
2021-08-11 20:37:58 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-24 00:44:40 +08:00
|
|
|
const shouldShow = this.shouldShow?.({
|
2021-08-11 20:37:58 +08:00
|
|
|
editor: this.editor,
|
|
|
|
view,
|
|
|
|
state,
|
|
|
|
oldState,
|
|
|
|
})
|
2021-04-01 21:19:31 +08:00
|
|
|
|
2021-08-11 20:37:58 +08:00
|
|
|
if (!shouldShow) {
|
2021-04-01 21:19:31 +08:00
|
|
|
this.hide()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-13 00:03:45 +08:00
|
|
|
this.tippy?.setProps({
|
2021-04-16 20:44:10 +08:00
|
|
|
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() {
|
2021-08-13 00:03:45 +08:00
|
|
|
this.tippy?.show()
|
2021-04-01 21:19:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
2021-08-13 00:03:45 +08:00
|
|
|
this.tippy?.hide()
|
2021-04-01 21:19:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2021-08-13 00:03:45 +08:00
|
|
|
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 }),
|
|
|
|
})
|
|
|
|
}
|