Files
2026-04-13 09:30:59 +08:00

1 line
13 KiB
Plaintext

{"version":3,"file":"index.cjs","sources":["../src/floating-menu-plugin.ts","../src/floating-menu.ts"],"sourcesContent":["import {\n Editor, getText, getTextSerializersFromSchema, posToDOMRect,\n} from '@tiptap/core'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { EditorState, Plugin, PluginKey } from '@tiptap/pm/state'\nimport { EditorView } from '@tiptap/pm/view'\nimport tippy, { Instance, Props } from 'tippy.js'\n\nexport interface FloatingMenuPluginProps {\n /**\n * The plugin key for the floating menu.\n * @default 'floatingMenu'\n */\n pluginKey: PluginKey | string\n\n /**\n * The editor instance.\n * @default null\n */\n editor: Editor\n\n /**\n * The DOM element that contains your menu.\n * @default null\n */\n element: HTMLElement\n\n /**\n * The options for the tippy instance.\n * @default {}\n * @see https://atomiks.github.io/tippyjs/v6/all-props/\n */\n tippyOptions?: Partial<Props>\n\n /**\n * A function that determines whether the menu should be shown or not.\n * If this function returns `false`, the menu will be hidden, otherwise it will be shown.\n * @default null\n */\n shouldShow?:\n | ((props: {\n editor: Editor\n view: EditorView\n state: EditorState\n oldState?: EditorState\n }) => boolean)\n | null\n}\n\nexport type FloatingMenuViewProps = FloatingMenuPluginProps & {\n /**\n * The editor view.\n */\n view: EditorView\n}\n\nexport class FloatingMenuView {\n public editor: Editor\n\n public element: HTMLElement\n\n public view: EditorView\n\n public preventHide = false\n\n public tippy: Instance | undefined\n\n public tippyOptions?: Partial<Props>\n\n private getTextContent(node:ProseMirrorNode) {\n return getText(node, { textSerializers: getTextSerializersFromSchema(this.editor.schema) })\n }\n\n public shouldShow: Exclude<FloatingMenuPluginProps['shouldShow'], null> = ({ view, state }) => {\n const { selection } = state\n const { $anchor, empty } = selection\n const isRootDepth = $anchor.depth === 1\n\n const isEmptyTextBlock = $anchor.parent.isTextblock && !$anchor.parent.type.spec.code && !$anchor.parent.textContent && $anchor.parent.childCount === 0 && !this.getTextContent($anchor.parent)\n\n if (\n !view.hasFocus()\n || !empty\n || !isRootDepth\n || !isEmptyTextBlock\n || !this.editor.isEditable\n ) {\n return false\n }\n\n return true\n }\n\n constructor({\n editor, element, view, tippyOptions = {}, shouldShow,\n }: FloatingMenuViewProps) {\n this.editor = editor\n this.element = element\n this.view = view\n\n if (shouldShow) {\n this.shouldShow = shouldShow\n }\n\n this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })\n this.editor.on('focus', this.focusHandler)\n this.editor.on('blur', this.blurHandler)\n this.tippyOptions = tippyOptions\n // Detaches menu content from its current parent\n this.element.remove()\n this.element.style.visibility = 'visible'\n }\n\n mousedownHandler = () => {\n this.preventHide = true\n }\n\n focusHandler = () => {\n // we use `setTimeout` to make sure `selection` is already updated\n setTimeout(() => this.update(this.editor.view))\n }\n\n blurHandler = ({ event }: { event: FocusEvent }) => {\n if (this.preventHide) {\n this.preventHide = false\n\n return\n }\n\n if (event?.relatedTarget && this.element.parentNode?.contains(event.relatedTarget as Node)) {\n return\n }\n\n if (\n event?.relatedTarget === this.editor.view.dom\n ) {\n return\n }\n\n this.hide()\n }\n\n tippyBlurHandler = (event: FocusEvent) => {\n this.blurHandler({ event })\n }\n\n createTooltip() {\n const { element: editorElement } = this.editor.options\n const editorIsAttached = !!editorElement.parentElement\n\n this.element.tabIndex = 0\n\n if (this.tippy || !editorIsAttached) {\n return\n }\n\n this.tippy = tippy(editorElement, {\n duration: 0,\n getReferenceClientRect: null,\n content: this.element,\n interactive: true,\n trigger: 'manual',\n placement: 'right',\n hideOnClick: 'toggle',\n ...this.tippyOptions,\n })\n\n // maybe we have to hide tippy on its own blur event as well\n if (this.tippy.popper.firstChild) {\n (this.tippy.popper.firstChild as HTMLElement).addEventListener('blur', this.tippyBlurHandler)\n }\n }\n\n update(view: EditorView, oldState?: EditorState) {\n const { state } = view\n const { doc, selection } = state\n const { from, to } = selection\n const isSame = oldState && oldState.doc.eq(doc) && oldState.selection.eq(selection)\n\n if (isSame) {\n return\n }\n\n this.createTooltip()\n\n const shouldShow = this.shouldShow?.({\n editor: this.editor,\n view,\n state,\n oldState,\n })\n\n if (!shouldShow) {\n this.hide()\n\n return\n }\n\n this.tippy?.setProps({\n getReferenceClientRect:\n this.tippyOptions?.getReferenceClientRect || (() => posToDOMRect(view, from, to)),\n })\n\n this.show()\n }\n\n show() {\n this.tippy?.show()\n }\n\n hide() {\n this.tippy?.hide()\n }\n\n destroy() {\n if (this.tippy?.popper.firstChild) {\n (this.tippy.popper.firstChild as HTMLElement).removeEventListener(\n 'blur',\n this.tippyBlurHandler,\n )\n }\n this.tippy?.destroy()\n this.element.removeEventListener('mousedown', this.mousedownHandler, { capture: true })\n this.editor.off('focus', this.focusHandler)\n this.editor.off('blur', this.blurHandler)\n }\n}\n\nexport const FloatingMenuPlugin = (options: FloatingMenuPluginProps) => {\n return new Plugin({\n key:\n typeof options.pluginKey === 'string' ? new PluginKey(options.pluginKey) : options.pluginKey,\n view: view => new FloatingMenuView({ view, ...options }),\n })\n}\n","import { Extension } from '@tiptap/core'\n\nimport { FloatingMenuPlugin, FloatingMenuPluginProps } from './floating-menu-plugin.js'\n\nexport type FloatingMenuOptions = Omit<FloatingMenuPluginProps, 'editor' | 'element'> & {\n /**\n * The DOM element that contains your menu.\n * @type {HTMLElement}\n * @default null\n */\n element: HTMLElement | null,\n}\n\n/**\n * This extension allows you to create a floating menu.\n * @see https://tiptap.dev/api/extensions/floating-menu\n */\nexport const FloatingMenu = Extension.create<FloatingMenuOptions>({\n name: 'floatingMenu',\n\n addOptions() {\n return {\n element: null,\n tippyOptions: {},\n pluginKey: 'floatingMenu',\n shouldShow: null,\n }\n },\n\n addProseMirrorPlugins() {\n if (!this.options.element) {\n return []\n }\n\n return [\n FloatingMenuPlugin({\n pluginKey: this.options.pluginKey,\n editor: this.editor,\n element: this.options.element,\n tippyOptions: this.options.tippyOptions,\n shouldShow: this.options.shouldShow,\n }),\n ]\n },\n})\n"],"names":["getText","getTextSerializersFromSchema","tippy","posToDOMRect","Plugin","PluginKey","Extension"],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