Skip to content

气泡菜单 BubbleMenu ​

基于 @floating-ui/dom 的 ProseMirror 插件,监听编辑器选区变化并计算气泡菜单的定位(Placement、翻转、偏移、箭头),选区存在且 shouldShow 通过时展示菜单。

@qingpai/vue 的 BaseBubbleMenu 组件构建于该插件之上,一般经组件使用而非直接创建插件。

用法 ​

通过 createBubbleMenuPlugin 创建插件:

ts
import { createBubbleMenuPlugin } from '@qingpai/core'

const plugin = createBubbleMenuPlugin({
  editor,
  element: menuElement,
  shouldShow: ({ from, to }) => to - from > 0,
})

配置 ​

BubbleMenuPluginProps:

名称类型默认值描述
pluginKeyPluginKey | stringBUBBLE_MENU_PLUGIN_KEY插件 key
editorEditor—编辑器实例(必传)
elementHTMLElement—菜单 DOM 元素(必传)
updateDelaynumber250菜单更新的延迟(毫秒),防止性能问题
resizeDelaynumber60窗口 resize 时位置更新的延迟(毫秒)
shouldShow(props) => boolean见下是否展示菜单,返回 false 则隐藏
appendToHTMLElement | (() => HTMLElement)编辑器父元素菜单挂载目标(处理 accessibility、裁剪、z-index 场景)
getReferencedVirtualElement() => VirtualElement | null基于选区定位返回定位参照的虚拟元素
optionsBubbleMenuPluginProps['options']{}传给 Floating UI 的定位选项,字段见下文

shouldShow 的默认实现:编辑器有焦点(或焦点在菜单内)、选区非空、选区不是空文本块、且编辑器可编辑(isEditable)时展示。props 含 editor / element / view / state / oldState / from / to。

options 支持 Floating UI 的定位配置(strategy 默认 'absolute'、placement 默认 'top'、offset 默认 8、中间件 flip / shift 默认开启,arrow / size / autoPlacement / hide / inline 默认关闭),以及 onShow / onHide / onUpdate / onDestroy 生命周期回调与 scrollTarget(滚动的监听元素,默认 window)。

导出的常量 ​

名称说明
BUBBLE_MENU_PLUGIN_KEY默认插件 key('bubbleMenu')

导出的值与类型 ​

  • BubbleMenuView:定位与显隐的视图类
  • createBubbleMenuPlugin(options: BubbleMenuPluginProps): ProseMirror Plugin:创建插件
ts
interface BubbleMenuPluginProps {
  pluginKey?: PluginKey | string
  editor: Editor
  element: HTMLElement
  /** 更新延迟(ms),默认 250 */
  updateDelay?: number
  /** 窗口尺寸变化重定位延迟(ms),默认 60 */
  resizeDelay?: number
  /** 返回 false 时菜单隐藏;props 含 editor / element / view / state / oldState / from / to */
  shouldShow?: ((props: {
    editor: Editor
    element: HTMLElement
    view: EditorView
    state: EditorState
    oldState?: EditorState
    from: number
    to: number
  }) => boolean) | null
  appendTo?: HTMLElement | (() => HTMLElement)
  getReferencedVirtualElement?: () => VirtualElement | null
  /** floating-ui 定位配置(strategy / placement / offset / 中间件等) */
  options?: {
    strategy?: 'absolute' | 'fixed'
    placement?: 'top' | 'right' | 'bottom' | 'left' | 'top-start' | 'top-end'
      | 'right-start' | 'right-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end'
    offset?: Parameters<typeof offset>[0] | boolean
    flip?: Parameters<typeof flip>[0] | boolean
    shift?: Parameters<typeof shift>[0] | boolean
    arrow?: Parameters<typeof arrow>[0] | false
    size?: Parameters<typeof size>[0] | boolean
    autoPlacement?: Parameters<typeof autoPlacement>[0] | boolean
    hide?: Parameters<typeof hide>[0] | boolean
    inline?: Parameters<typeof inline>[0] | boolean
    onShow?: () => void
    onHide?: () => void
    onUpdate?: () => void
    onDestroy?: () => void
    scrollTarget?: HTMLElement | Window
  }
}

type BubbleMenuViewProps = BubbleMenuPluginProps & {
  view: EditorView
}

外部控制 ​

外部代码可通过 tr.setMeta(pluginKey, ...) 控制菜单显隐与更新:

ts
// 更新位置
tr.setMeta(pluginKey, 'updatePosition')
// 更新配置(Partial<BubbleMenuPluginProps>,不含 editor / element / pluginKey)
tr.setMeta(pluginKey, { type: 'updateOptions', options: { resizeDelay: 100 } })
// 隐藏 / 展示
tr.setMeta(pluginKey, 'hide')
tr.setMeta(pluginKey, 'show')

定位细节 ​

默认以选区为参照:普通选区取 posToDOMRect 的包围盒;NodeSelection 优先取 nodeView 包裹元素(data-node-view-wrapper)的实际矩形;表格 CellSelection 取锚点/焦点单元格矩形的合并包围盒。滚动(窗口 capture 阶段,含编辑器祖先容器)时菜单隐藏,由后续的选区/焦点更新路径重新展示。

Source code ​

  • packages/core/src/extensions/bubble-menu/

轻排编辑器 — 教育领域富文本编辑器