Skip to content

缝隙修复 FixGapCursor ​

把卡在"父节点内两个相邻子节点之间"的选择区(GapCursor 为主)自动前推到后方节点内的最近合法文本位置。

用法 ​

ts
import { FixGapCursor } from '@qingpai/core'
import { Editor } from '@tiptap/core'

const editor = new Editor({
  extensions: [
    FixGapCursor.configure({
      targets: [
        // 题号与题干之间
        { between: ['questionNumber', 'questionMain'] },
        // 父节点开头(题号之前)
        { between: [null, 'questionNumber'] },
      ],
    }),
  ],
})

StartKit 已内置注册本扩展,默认修复 question 的题号之前、题号与题干之间、题干之后三处缝隙,可经 StartKit.configure({ fixGapCursor }) 追加自定义缝隙或传 false 关闭,无需手写注册。

配置 ​

名称类型默认值描述
targetsFixGapCursorTarget[]要修复的缝隙列表,支持多组

FixGapCursorTarget ​

ts
interface FixGapCursorTarget {
  /**
   * 缝隙两侧的相邻子节点名:[前方, 后方];
   * null 表示父节点边界(最前 / 最后)
   */
  between: [string | null, string | null]
}

修复方向固定向后:命中缝隙后从当前位置用 Selection.near(pos, 1) 寻找最近的合法选择区,且要求结果不是 GapCursor、位置严格在缝隙之后——已合法的文本选择区不会被移动。

机制与 atom 无关:开头 atom 后的缝隙只是典型成因,缝隙按运行时父节点内实际的相邻子节点对匹配,前置节点的节点大小不影响配置。

Source code ​

  • packages/core/src/extensions/fix-gap-cursor/

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