Skip to content

fix(composer): keep an empty last line's caret without adding a line - #2554

Merged
Tioit-Wang merged 1 commit into
vastsa:mainfrom
G1oow:fix/composer-tail-line-marker
Nov 24, 2026
Merged

Tioit-Wang merged 1 commit into
vastsa:mainfrom
G1oow:fix/composer-tail-line-marker

Conversation

@G1oow

@G1oow G1oow commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

现象

#2433 的修复(#2437)后,在草稿末尾按 Shift+Enter 会出现多一条空行,而且光标还能停到那条空行下面。

根因(Electron 43.6.0 / Chromium 150 实测)

上一版给空末行加的占位是 <br data-composer-tail>。它确实给了这一行行盒,但同时给了 Chromium 一个占位之后的光标位置:

DOM 形态 渲染行盒 空行上的光标盒 占位之后的位置
文本换行(无占位) 3 无 —
文本换行 + <br> 占位 4 无(getClientRects() 为空) 有
文本换行 + 零宽空格 4 有 无

也就是说:占位让空行可见了,但光标在这一行仍然算不出盒,而 <br> 又在它下面开了第二个落点——这就是"多出一条空行、光标能停到它下面"。

顺带确认:ComposerInput 的 onBeforeInput 在 React 19 下对 beforeinput 根本不触发(实测 newlineCalls=0),换行一直是 Chromium 原生插入的,这条路径不受本 PR 影响。

修法

把占位从 "一个换行" 换成 "一个零宽空格字符"(U+200B):

  • 空末行有行盒,也有真实的光标盒,且不会多出落点;
  • Chromium 会把该字符合并进相邻文本节点,所以 readEditorValue / editorIndexAt / editorNodeLength / editorCaretPoint 全部按字符过滤,而不是按节点判断;
  • syncTailMarker 在每次提交后保证草稿以 \n 结尾时行尾带一个标记,行内有内容时去掉;
  • 草稿字符串、索引与光标位置都不变。

验证

  • Electron 43.6.0(Chromium 150)生产 composer 组件实测:空末行行盒存在、光标落在该行、行数只增加 1、ArrowDown 不会跑到该行以下。
  • tsc -p tsconfig.json --noEmit ✅
  • node --test test/composer-*.test.mjs(tail-line / native-deletion / clipboard / agent-mention / ime)16/16 ✅
  • 规格同步:docs/spec/04-ux/08-component-spec.md、docs/spec/06-delivery/e2e/conversation.md E2E-008d。

未覆盖 / 风险

  • 本机无法产生真实键盘事件(sendInputEvent 的 Enter 只发 keydown、CDP 的 \r 会重复插入),所以没有跑真实按键的端到端;上述几何结论来自 Chromium 150 上的直接 DOM 测量与组件级探针。
  • 原生 undo 若重新引入块级包裹(<div>),标记会落在块末尾,行为与上一版占位一致,未额外处理。

Refs #2433

Closes #2555

The placeholder break that gave an empty last line a box also gave Chromium a
second caret position below it, so one Shift+Enter read as one line too many and
ArrowDown could leave the caret under the empty line (vastsa#2433 follow-up). The
editor now marks the line with a zero-width space: Chromium resolves a caret
there with a real box and offers no position of its own below the line.
Chromium can merge the character into the neighbouring text node, so every read,
index and caret path strips it and the draft string keeps its previous shape.

Measured in Electron 43.6.0 (Chromium 150) against the production composer: the
empty last line keeps its box and its caret, the editor grows by exactly one
line per newline, and no caret position exists below that line.

Refs vastsa#2433
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.

[Bug] composer 末尾换行后多出一条空行,且光标能停到它下面(#2437 回归)

2 participants