2021-06-16 01:06:26 +08:00
|
|
|
import {
|
|
|
|
Editor,
|
|
|
|
isNodeSelection,
|
2022-06-08 20:10:25 +08:00
|
|
|
isTextSelection,
|
|
|
|
posToDOMRect,
|
2021-06-16 01:06:26 +08:00
|
|
|
} from '@tiptap/core'
|
2022-11-04 23:39:41 +08:00
|
|
|
import debounce from 'lodash/debounce'
|
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>,
|
2022-11-04 23:39:41 +08:00
|
|
|
delay?: number,
|
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,
|
|
|
|
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
|
|
|
|
|
2021-08-13 00:03:45 +08:00
|
|
|
public tippy: Instance | undefined
|
2021-04-10 04:42:09 +08:00
|
|
|
|
2021-09-08 02:46:45 +08:00
|
|
|
public tippyOptions?: Partial<Props>
|
|
|
|
|
2022-11-04 23:39:41 +08:00
|
|
|
public delay: number
|
|
|
|
|
2021-12-15 16:06:43 +08:00
|
|
|
public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({
|
|
|
|
view,
|
|
|
|
state,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
}) => {
|
2021-08-11 20:37:58 +08:00
|
|
|
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)
|
|
|
|
|
2022-09-04 02:07:41 +08:00
|
|
|
// When clicking on a element inside the bubble menu the editor "blur" event
|
|
|
|
// is called and the bubble menu item is focussed. In this case we should
|
|
|
|
// consider the menu as part of the ditor and keep showing the menu
|
|
|
|
const isChildOfMenu = this.element.contains(document.activeElement)
|
|
|
|
|
|
|
|
const hasEditorFocus = view.hasFocus() || isChildOfMenu
|
|
|
|
|
2021-12-15 16:06:43 +08:00
|
|
|
if (
|
2022-09-04 02:07:41 +08:00
|
|
|
!hasEditorFocus
|
2021-12-15 16:06:43 +08:00
|
|
|
|| empty
|
|
|
|
|| isEmptyTextBlock
|
2022-09-14 08:05:36 +08:00
|
|
|
|| !this.editor.isEditable
|
2021-12-15 16:06:43 +08:00
|
|
|
) {
|
2021-08-11 20:37:58 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-16 18:42:56 +08:00
|
|
|
constructor({
|
|
|
|
editor,
|
|
|
|
element,
|
|
|
|
view,
|
2021-09-08 02:46:45 +08:00
|
|
|
tippyOptions = {},
|
2022-11-04 23:39:41 +08:00
|
|
|
delay = 250,
|
2021-08-11 20:37:58 +08:00
|
|
|
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
|
2022-11-04 23:39:41 +08:00
|
|
|
this.delay = delay
|
2021-08-11 20:37:58 +08:00
|
|
|
|
|
|
|
if (shouldShow) {
|
|
|
|
this.shouldShow = shouldShow
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:45:56 +08:00
|
|
|
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
|
2021-06-15 00:28:27 +08:00
|
|
|
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)
|
2021-09-08 02:46:45 +08:00
|
|
|
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
|
|
|
|
2021-06-15 00:28:27 +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
|
|
|
|
2022-11-05 00:51:01 +08:00
|
|
|
tippyBlurHandler = (event : FocusEvent) => {
|
|
|
|
this.blurHandler({ event })
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:46:45 +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:46:45 +08:00
|
|
|
|
2021-09-08 02:55:40 +08:00
|
|
|
if (this.tippy || !editorIsAttached) {
|
|
|
|
return
|
2021-09-08 02:46:45 +08:00
|
|
|
}
|
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-12-15 17:12:55 +08:00
|
|
|
|
|
|
|
// maybe we have to hide tippy on its own blur event as well
|
|
|
|
if (this.tippy.popper.firstChild) {
|
2022-11-05 00:51:01 +08:00
|
|
|
(this.tippy.popper.firstChild as HTMLElement).addEventListener('blur', this.tippyBlurHandler)
|
2021-12-15 17:12:55 +08:00
|
|
|
}
|
2021-04-10 04:42:09 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:13:55 +08:00
|
|
|
update(view: EditorView, oldState?: EditorState) {
|
2022-11-04 23:39:41 +08:00
|
|
|
const { state } = view
|
|
|
|
const hasValidSelection = state.selection.$from.pos !== state.selection.$to.pos
|
|
|
|
|
|
|
|
if (hasValidSelection) {
|
|
|
|
if (this.delay > 0) {
|
|
|
|
debounce(this.updateHandler, this.delay)(view, oldState)
|
|
|
|
} else {
|
|
|
|
this.updateHandler(view, oldState)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHandler = (view: EditorView, oldState?: EditorState) => {
|
2021-03-30 18:13:55 +08:00
|
|
|
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-09-08 02:46:45 +08:00
|
|
|
this.createTooltip()
|
|
|
|
|
2021-05-06 15:54:35 +08:00
|
|
|
// support for CellSelections
|
2021-08-11 20:37:58 +08:00
|
|
|
const { ranges } = selection
|
2021-05-06 15:54:35 +08:00
|
|
|
const from = Math.min(...ranges.map(range => range.$from.pos))
|
|
|
|
const to = Math.max(...ranges.map(range => range.$to.pos))
|
2021-04-22 05:38:13 +08:00
|
|
|
|
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,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
})
|
2021-06-15 00:58:10 +08:00
|
|
|
|
2021-08-11 20:37:58 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-13 00:03:45 +08:00
|
|
|
this.tippy?.setProps({
|
2022-04-01 04:18:40 +08:00
|
|
|
getReferenceClientRect: this.tippyOptions?.getReferenceClientRect || (() => {
|
2021-08-11 20:37:58 +08:00
|
|
|
if (isNodeSelection(state.selection)) {
|
2021-05-25 02:40:24 +08:00
|
|
|
const node = view.nodeDOM(from) as HTMLElement
|
|
|
|
|
|
|
|
if (node) {
|
|
|
|
return node.getBoundingClientRect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return posToDOMRect(view, from, to)
|
2022-04-01 04:18:40 +08:00
|
|
|
}),
|
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-08-13 00:03:45 +08:00
|
|
|
this.tippy?.show()
|
2021-03-30 17:36:51 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:56:23 +08:00
|
|
|
hide() {
|
2021-08-13 00:03:45 +08:00
|
|
|
this.tippy?.hide()
|
2021-03-30 17:36:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2022-11-05 00:51:01 +08:00
|
|
|
if (this.tippy?.popper.firstChild) {
|
|
|
|
(this.tippy.popper.firstChild as HTMLElement).removeEventListener('blur', this.tippyBlurHandler)
|
|
|
|
}
|
2021-08-13 00:03:45 +08:00
|
|
|
this.tippy?.destroy()
|
2021-09-22 19:28:52 +08:00
|
|
|
this.element.removeEventListener('mousedown', this.mousedownHandler, { capture: true })
|
2021-06-15 00:28:27 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|