Skip to content

🐛 fix(common): normalize caret parked at root start before a block decorator - #209

Open
arvinxx wants to merge 1 commit into
masterfrom
fix/cursor-edge-case-block-start
Open

arvinxx wants to merge 1 commit into
masterfrom
fix/cursor-edge-case-block-start

Conversation

@arvinxx

@arvinxx arvinxx commented Sep 7, 2026

Copy link
Copy Markdown
Member

🔗 Related issue / 背景

来自 @lobehub 主仓库 dev 频道的边缘 case 反馈:

这应该是一个边缘 case,像 linear 这种第一行是图片,向左唤出光标,会自动顶一行空行。这个应该没处理顶一个空行,直接就加载在上面了

像是编辑器层面的 bug,我在正文中粘贴图片的时候也会先横向光标,然后再转成纵向

🐛 问题分析

当文档第一个子节点是 非 inline 的 DecoratorNode(BlockImageNode、HorizontalRuleNode 等)时:

  1. 向左 / 向上移动光标越过块边界,selection 会落到 root offset 0 —— 这个位置没有任何文本锚点
  2. 浏览器只能把光标画在块节点上,产生 横向光标 / 光标闪烁 的视觉 artifact
  3. 此时光标不可用、输入行为异常,且不像 Linear 那样自动顶一行空行

现有的 CursorNode 机制只覆盖了 inline 装饰节点(行内图片)的光标锚点,块级装饰节点前没有对应处理。

✨ 修复方案

在 registerCursorNode 中新增一个被动规范化 listener(Linear 同款行为):

  • () — 检测「collapsed selection 停在 root offset 0 且第一个子节点是非 inline 装饰节点」
  • () — 在块前插入空段落并把 selection 锚定进去
  • listener 在每次 update 后被动触发(带重入 guard + queueMicrotask 延迟避免 update 循环),因此所有光标落点路径都会被规范化:
    • 块后段落按 ArrowLeft / ArrowUp
    • 点击顶部空白区域
    • NodeSelection 上按 ArrowLeft
    • 直接 load 进这个状态的文档

关键细节:caret 停在 root offset 0 的 selection 在 DOM reconcile 时本身无法保留,所以 update 事务内只做结构校验、不重新检查 selection,插入的段落自带全新合法 selection。

✅ 验证

  • 新增 5 个单测(cursor-root-start.test.ts):detector 正/反例、插入后结构与 caret 落点、ArrowLeft 跨块边界
  • vitest run src/plugins/common:8 个文件 50 个测试全部通过,无回归
  • tsc --noEmit 通过;循环依赖检查通过

📌 备注

粘贴图片时「先横向光标再转纵向」是同一根因的另一种表现:块级图片插入后 selection 短暂落在不可表达的 DOM 位置。本次的被动规范化会在下一次 update 时把这种情况收敛掉。

…corator

When the first child of the document is a non-inline decorator node
(block image, horizontal rule, ...), moving the caret left/up past the
boundary parks the selection at root offset 0 — a position with no text
anchor. The browser then renders a floating caret across the block (the
'horizontal cursor' artifact), and the empty leading paragraph that
editors like Linear insert in this case never appears.

This adds a passive normalization listener to registerCursorNode:

- $isCaretAtRootStartBeforeBlockDecorator detects the parked caret
- $insertParagraphBeforeRootStartBlock inserts a fresh empty paragraph
  and anchors the selection inside it
- the listener runs on every update (with a re-entrancy guard + microtask
  deferral to avoid update loops), so every caret path — ArrowLeft from
  the block below, ArrowUp, clicking the top gap, NodeSelection + ArrowLeft —
  lands in a real text anchor
@vercel

vercel Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lobe-editor Ready Ready Preview Sep 7, 2026 9:37am UTC

Request Review

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

👍 @arvinxx


Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

@pkg-pr-new

pkg-pr-new Bot commented Sep 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@lobehub/editor@209

commit: 33605a9

This branch was successfully deployed

1 active deployment
Preview — 33605a91 Deployed Sep 7, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant