Skip to content

fix(app-client): keep selection quote menu after triple-click - #5375

Merged
liruifengv merged 2 commits into
release/2026-11-25from
chengluyu-fix-triple-click-selection-quote
Nov 25, 2026
Merged

liruifengv merged 2 commits into
release/2026-11-25from
chengluyu-fix-triple-click-selection-quote

Conversation

@chengluyu

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在桌面端和 Web UI 的对话里,双击选中文字后,「评论 / 添加到对话」菜单能正常弹出;三击选中一整段时,菜单会闪一下然后消失,没法引用整段。这个 PR 修好之后,三击选中的段落也能正常引用。

Bug Reproduction Steps

  1. 找一条 assistant 消息,要求某段正文后面紧跟着一个工具调用分组(例如「运行了 1 条命令」)。
  2. 三击这段正文的最后一段。
  3. 选择菜单短暂出现后马上消失。

Root Cause

三击选中段落时,Chrome 会把选区的终点越过段落末尾,延伸到下一个区块里第一个可选中的位置。终点前面会跨过一段 user-select: none 的文字,比如活动分组的标题「运行了 1 条命令」,最后停在工具行「运行」标签的末尾。在 Chrome 里实测,Selection.toString() 只有这段正文,但 Range.toString() 会把不可选中的标题和标签也算进去。

endsAtContainerBoundary 原本只在终点是元素节点、或文本偏移量为 0 时,才认为选区「只是停在段落外侧的边界上」;判断容器后面有没有多选内容时,用的又是 Range.toString()。三击的终点是一个偏移量不为 0 的文本节点,不可选中的文字也被当成了多选的内容,于是选区被判定为跨区块,菜单就被收起了。双击能正常显示的那一下菜单,就是消失前看到的那一下。

这次是根本修复:判断尾部内容时,只统计用户真正能选中的文字。

Code Changes

改动只在一个判断函数里:

 endsAtContainerBoundary(range, container)
-  if end is a text node with offset != 0: return false
   if container and end are in different trees: return false
   trailing = range from the end of container to the end of range
-  return trailing.toString() is blank
+  return trailing has no selectable text
+
+hasSelectableText(range)
+  for each text node intersecting range
+    if the covered slice is not blank
+       and no ancestor has user-select: none or display: none
+      return true
+  return false

去掉偏移量为 0 的前提,是因为三击的终点本来就落在一个文本节点的中间。现在由「尾部有没有可选中的文字」单独决定选区算不算跨区块,所以只要多选了下一个区块里真正可选中的文字,选区仍然会被拒绝。selectionQuoteAnchor 和 selectionOwnedByRoot 都调用这个函数,所以气泡锚点和 selectionchange 的归属判断用的是同一套规则。

getRootNode 的类型判断沿用了这个文件里已有的防御写法。原来的早返回会挡住不带 getRootNode 的测试替身,去掉早返回之后要靠这个判断来保护。

Behavior Changes and Affected Users

行为 改动前 改动后 谁依赖旧行为 退路
三击选中后面紧跟着不可选中区块的段落 菜单闪一下后消失 菜单保持显示,引用内容是整段 无,旧行为是 bug 不需要
选区延伸到下一个区块里可选中的文字 拒绝 拒绝(不变) — —
选区终点在容器之后、尾部只有空白(元素边界或偏移量为 0) 接受 接受(不变) — —
尾部只有不可选中或隐藏的文字(例如屏幕阅读器专用的状态文字、user-select: none 的标签) 拒绝 接受 无,用户看不到也选不中这部分文字 不需要

受影响的人群:桌面端和 kimi web 的用户,两者共用同一套渲染层。TUI、VS Code、ACP 和 SDK 不受影响。

测试覆盖:

  • packages/app-client/test/quote-selection-boundary.test.ts 新增 3 个用例:终点越过不可选中的标题时仍保留锚点(修复前会失败);终点落在下一个区块可选中的代码里时仍拒绝;被跨过的标题本身可选中时仍拒绝。
  • 已有的边界、shadow DOM 和气泡多次点击的用例全部通过。
验证过程
  • 在无头 Chrome 里搭了和对话结构一样的 DOM(段落之后是带 user-select: none 标题的活动分组),用真实鼠标三击:修复前 selectionQuoteAnchor 返回 null,修复后返回整段文字;把选区拉进下一个区块的代码里,仍然返回 null。
  • pnpm exec vitest run --reporter=agent packages/app-client/test packages/app-components/test/selectionActionBubble.test.ts packages/app-components/test/transcriptList.test.ts:128 个文件、2436 个用例全部通过。
  • pnpm --filter kimi-code-app run typecheck:通过。
  • pnpm lint:通过。
  • 只验证了 Chromium 的行为(Electron 和 Chrome 都是 Chromium 内核);没有在 Safari 或 Firefox 上验证。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3a5196e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
kimi-code-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T03:33:47.829704Z 9902c20 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@3a5196e
npx https://pkg.pr.new/@moonshot-ai/kimi-code@3a5196e

commit: 3a5196e

@liruifengv
liruifengv merged commit 48be878 into release/2026-11-25 Nov 25, 2026
36 of 37 checks passed
@liruifengv
liruifengv deleted the chengluyu-fix-triple-click-selection-quote branch November 25, 2026 04:45
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
* fix(app-client): keep selection quote menu after triple-click

* chore: drop CLI changeset for triple-click quote fix

---------

Co-authored-by: liruifengv <[email protected]>
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
* fix(app-client): keep selection quote menu after triple-click

* chore: drop CLI changeset for triple-click quote fix

---------

Co-authored-by: liruifengv <[email protected]>
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.

2 participants