Skip to content

气泡定位 Popover ​

基于 @floating-ui/dom 的 ProseMirror 插件,监听编辑器内的节点点击并计算气泡菜单的定位(自动 Placement、翻转、偏移、箭头)。

@qingpai/vue 的气泡菜单组件构建于该插件之上。

用法 ​

通过 createPopoverPlugin 创建插件:

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

const plugin = createPopoverPlugin({
  pluginKey: 'myPopover',
  editor,
  element: menuElement,
  shouldShow: ({ node }) => node?.type.name === 'image',
  onNodeChange: ({ node, nodePos }) => {
    // 选中的目标节点变化
  },
})

配置 ​

PopoverPluginProps:

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

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

导出的常量 ​

名称说明
POPOVER_PLUGIN_KEY默认插件 key('popover')

导出的值与类型 ​

  • PopoverView:定位与显隐的视图类(context-menu 等插件复用它)
  • createPopoverPlugin(options: PopoverPluginProps): ProseMirror Plugin:创建插件
ts
interface PopoverPluginNodeChangeInfo {
  view: EditorView
  node: PMNode
  nodePos: number
}

interface Target {
  node: PMNode
  nodePos: number
  el: HTMLElement
}

interface PopoverPluginProps {
  pluginKey: PluginKey | string
  editor: Editor
  element: HTMLElement
  /** 更新延迟(ms),默认 250 */
  updateDelay?: number
  /** 窗口尺寸变化重定位延迟(ms),默认 60 */
  resizeDelay?: number
  /** 返回 false 时菜单隐藏;props 含 editor / element / view / state / oldState / node / nodePos 等 */
  shouldShow?: (props: {
    editor: Editor
    element: HTMLElement
    view: EditorView
    state: EditorState
    oldState?: EditorState
    node?: PMNode
    nodePos?: number
  }) => boolean
  appendTo?: HTMLElement | (() => HTMLElement)
  getReferencedVirtualElement?: () => VirtualElement | null
  /** floating-ui 定位配置(strategy / placement / offset / 中间件等),默认 `{}` */
  options?: ComputePositionConfig
  onNodeChange?: (info: PopoverPluginNodeChangeInfo) => void
}

type PopoverViewProps = PopoverPluginProps & {
  view: EditorView
}

外部控制 ​

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

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

目标节点 ​

Target 结构包含节点、位置与对应 DOM 元素;onNodeChange 回调携带节点变更信息:

ts
interface Target {
  node: PMNode
  nodePos: number
  el: HTMLElement
}

interface PopoverPluginNodeChangeInfo {
  view: EditorView
  node: PMNode
  nodePos: number
}

Source code ​

  • packages/core/src/extensions/popover/

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