Skip to content

fix(composer): keep an empty last line's caret on that line - #2437

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

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

Conversation

@G1oow

@G1oow G1oow commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary / 概要

#2433:草稿最后一行为空时,这一行在 DOM 里没有任何布局盒,Chromium 会把「末尾换行之后」的光标归属到上一行,导致后续按键与 IME 组合落到上一行(草稿字符串本身仍正确)。在真实 Electron 43.6.0(Chromium 150)上用生产 composer 组件复现:

未修复场景 结果
空末行上按键 1test2test\nx
Backspace 删空空末行后按键 1testy\n
空末行上 IME 组合 ni 提交 你 1testni\n → 1testn你\n

在 editor.ts 里为空末行补一个占位换行(<br data-composer-tail aria-hidden="true">)提供布局盒,所有读取 / 索引 / 光标路径跳过它,草稿字符串与索引不变;在每次草稿提交后的 layout effect 里同步(不在 input 事件内改 DOM——实测那样会让 Chromium 把换行插两次);Shift+Enter 新行改为按草稿索引放光标。

修复后同一套探针:1test\n2test\nx、1test\ny、IME 1test\nni → 1test\n你;Backspace 合并空末行后 1test\n2test 且无残留占位;中间行删空行为不变(1test\nz\n3test,Chromium 150 本来就正常,规格已注明)。

Requirement link / 需求关联

  • Linked requirement issue / 已关联对应的需求 Issue:Closes [Bug] 输入框多行编辑:删掉某行最后一个字符后,光标自动跳到上一行末尾 #2433
  • Issue explains the background and the expected outcome / Issue 已说明需求背景和期望效果(含 DOM 快照 1test\n2test\n、光标位于末尾 "\n" 节点 offset=1)
  • UI changes carry a fitting reference / 本改动无界面与布局变化,仅光标归属;以修复前后输入结果表作为说明材料
  • UI changes include before / after screenshots / 不适用:无任何界面变化,因此无截图

Verification / 验证

  • 候选基线:上游 main 0699f6aca(已 rebase,git diff --check ✔)
  • tsc -p apps/desktop --noEmit ✔;biome lint ✔
  • node --test apps/desktop/test/composer-*.test.mjs:268/270 通过,其中包含真实 Electron 的 user-path 用例(composer-agent-mention-user-path、composer-deep-file-search-user-path)
  • 唯一失败 composer-file-references 为 Windows symlink EPERM,改动前同样失败,属环境限制而非本次改动
  • 受影响规格与 E2E 场景:docs/spec/04-ux/08-component-spec.md(Native deletion 段)、docs/spec/06-delivery/e2e/conversation.md(E2E-008d)
  • 新增回归用例:apps/desktop/test/composer-tail-line.test.mjs(占位判定 + 读取 / 索引 / 绘制契约)
  • NOT RUN:pnpm test:e2e:*(live-model 车道,需要测试用 API Key)与 verify:ui:*(未获授权);替代验证为 Electron 真机探针(真实键盘与 IME 事件)
  • 剩余风险:撤销 / 重做与占位换行的交互未覆盖;占位只在草稿以换行结尾时存在,中间空行不涉及

Closes #2433

Chromium has no layout box for a line that holds nothing, so a caret left
after the final line break resolves to the line above: the next keystroke,
and an IME composition, land on the previous line while the draft string
stays correct. The composer's own DOM evidence in vastsa#2433 ("1test\n2test\n"
with the caret in the trailing "\n" node) reproduces it on Electron 43.6.0.

Paint a placeholder break on an empty last line and skip it from every read,
index and caret path, so the draft string and its indices do not change.
Re-sync it after each commit instead of inside the input event, where
mutating the editor makes Chromium insert the line break twice. Place a new
Shift+Enter line's caret by draft index rather than a DOM offset.

fixes vastsa#2433
@Tioit-Wang

Copy link
Copy Markdown
Collaborator

优质PR , 已合并。

49e48680-3349-409a-aaa2-f88eaa8a126d

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] 输入框多行编辑:删掉某行最后一个字符后,光标自动跳到上一行末尾

2 participants