探索 Tiptap V3 的最新功能

浮动菜单扩展

版本下载次数

在 Tiptap 中使用浮动菜单扩展,使菜单在空行上出现。

安装扩展

安装浮动菜单扩展和 Floating UI 库。

npm install @tiptap/extension-floating-menu @floating-ui/dom@^1.6.0

设置

element

包含您菜单的 DOM 元素。

类型: HTMLElement

默认值: null

options

在底层,FloatingMenu 使用 Floating UI。您可以使用这些选项控制浮动菜单的中间件和定位。

类型: Object

默认值: { strategy: 'absolute', placement: 'right' }

选项类型描述
strategystring定位策略。请参见 这里
placementstring菜单的位置。请参见 这里
offsetOffsetOptionsboolean偏移中间件选项。如果为 true 使用默认选项,如果为 false 禁用中间件
flipFlipOptionsboolean翻转中间件选项。如果为 true 使用默认选项,如果为 false 禁用中间件
shiftShiftOptionsboolean移动中间件选项。如果为 true 使用默认选项,如果为 false 禁用中间件
arrowArrowOptionsfalse箭头中间件选项。如果为 false 禁用中间件
sizeSizeOptionsboolean大小中间件选项。如果为 true 使用默认选项,如果为 false 禁用中间件
autoPlacementAutoPlacementOptionsboolean自动放置中间件选项。如果为 true 使用默认选项,如果为 false 禁用中间件
hideHideOptionsboolean隐藏中间件选项。如果为 true 使用默认选项,如果为 false 禁用中间件
inlineInlineOptionsboolean内联中间件选项。如果为 true 使用默认选项,如果为 false 禁用中间件
onShowFunction or undefinedA callback that is called when the menu is shown. This can be used to add custom logic or styles when the menu is displayed.
onHideFunction or undefinedA callback that is called when the menu is hidden. This can be used to add custom logic or styles when the menu is hidden.
onUpdateFunction or undefinedA callback that is called when the menu is updated. This can be used to add custom logic or styles when the menu is updated.
onDestroyFunction or undefinedA callback that is called when the menu is destroyed. This can be used to add custom logic or styles when the menu is removed.

pluginKey

底层 ProseMirror 插件的键。如果您添加多个实例,请确保使用不同的键。

类型: string | PluginKey

默认值: 'floatingMenu'

shouldShow

一个回调,用于控制菜单是否应显示。

类型: (props) => boolean

源代码

packages/extension-floating-menu/

在 Vanilla JavaScript 中使用

import { Editor } from '@tiptap/core'
import FloatingMenu from '@tiptap/extension-floating-menu'

new Editor({
  extensions: [
    FloatingMenu.configure({
      element: document.querySelector('.menu'),
    }),
  ],
})

其他框架

请查看此页顶部的演示 #,了解如何将浮动菜单扩展与 React 或 Vue 集成。

自定义逻辑

使用 shouldShow 选项自定义显示菜单的逻辑。对于组件,shouldShow 可以作为 prop 传递。

FloatingMenu.configure({
  shouldShow: ({ editor, view, state, oldState }) => {
    // 在任何段落中显示浮动菜单
    return editor.isActive('paragraph')
  },
})

多个菜单

通过设置唯一的 pluginKey 使用多个菜单。

import { Editor } from '@tiptap/core'
import FloatingMenu from '@tiptap/extension-floating-menu'

new Editor({
  extensions: [
    FloatingMenu.configure({
      pluginKey: 'floatingMenuOne',
      element: document.querySelector('.menu-one'),
    }),
    FloatingMenu.configure({
      pluginKey: 'floatingMenuTwo',
      element: document.querySelector('.menu-two'),
    }),
  ],
})

或者,您可以传递一个 ProseMirror PluginKey

import { Editor } from '@tiptap/core'
import FloatingMenu from '@tiptap/extension-floating-menu'
import { PluginKey } from '@tiptap/pm/state'

new Editor({
  extensions: [
    FloatingMenu.configure({
      pluginKey: new PluginKey('floatingMenuOne'),
      element: document.querySelector('.menu-one'),
    }),
    FloatingMenu.configure({
      pluginKey: new PluginKey('floatingMenuOne'),
      element: document.querySelector('.menu-two'),
    }),
  ],
})