Skip to content

fix(reader): 标注编辑器的文本框自己滚动时不再关掉面板 - #4

Merged
2019wakeup merged 1 commit into
2019wakeup:mainfrom
liujieranjerry-lgtm:fix/annotation-editor-scroll
Nov 23, 2026
Merged

2019wakeup merged 1 commit into
2019wakeup:mainfrom
liujieranjerry-lgtm:fix/annotation-editor-scroll

Conversation

@liujieranjerry-lgtm

@liujieranjerry-lgtm liujieranjerry-lgtm commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

症状

在正文里点选一处高亮标注打开「编辑标注」面板后,只要备注写得超过三行(文本框约 96px 高、内部开始出现滚动),继续往下滚或按住光标往下拖,面板会直接消失,正在写的备注一起丢掉。右侧抽屉、卡片自己的滚动也受同一问题影响。

根因

src/client/app.js 的文档级关闭逻辑把滚动监听挂在 window 上,并且用了捕获阶段:

const onScroll = () => { setMenu(null); setChildMenu(null); setEditing(null) }
window.addEventListener('scroll', onScroll, true)

scroll 事件不冒泡,但捕获阶段仍然会从 window 一路走到目标节点,所以任何后代元素自己滚动(备注文本框、.rd-quote、.rd-card 内部滚动)都会触发它,把 editing 清空、面板卸载。点击取消逻辑早就做过区域判断(menu.js 的 shouldDismissOnPointerDown 用 closest('.rd-mini, .rd-card') 放行面板内部),滚动这条路径没有对应判断——这是漏掉了同一类判断,不是新引入的问题。

顺带三个放大器:文本框只有 min-height:64px(约三行,正好对应用户说的「超过三行」);.rd-editor textarea 没有 overscroll-behavior,滚到底会链式带动页面滚动;操作按钮原来是普通行,卡片滚动时会被滚出视野。

改动

  1. src/client/menu.js:新增 shouldDismissOnScroll(target),把滚动关闭策略和已有的点击关闭策略放在一起。目标落在可编辑元素(textarea/input/select/[contenteditable])或面板容器(.rd-mini, .rd-card, .rd-trans-card)内部 ⇒ 不关闭;window/document/null 目标或落在正文 ⇒ 仍然关闭。
  2. src/client/app.js:onScroll 改为 onScroll = (event) => { if (!shouldDismissOnScroll(event.target)) return; … };备注文本框的根节点加上 .rd-annotation-editor 类;操作行改用仓库里已有的 rd-sticky-actions。
  3. src/client/styles.js:备注文本框改成 min-height:96px; max-height:min(34vh,260px); overflow:auto; overscroll-behavior:contain,并且只挂在 .rd-annotation-editor 下——概念编辑表单复用 .rd-editor,不隔离会把它的四个 textarea 一起撑高(第一版就是这么写的,视觉基线 03c-concept-edit 直接 diff 1.44%,见下)。

二阶 / 三阶影响

  • 页面滚动仍然关闭面板:面板锚定在正文某个文本矩形上,正文滚走后继续显示会指向错误位置;这条行为有既有测试(test/editor-flow.test.mjs「scrolling closes the editor」)锁定,本次保留。
  • 输入法/光标自动滚动:中文输入法候选框跟随时文本域会自己滚动,原来同样会把面板关掉;现在两者都走同一条策略,顺带修好。
  • 滚动链:给文本框加 overscroll-behavior:contain 后,滚到文本框底部不会再把滚动传给正文,也就不会出现「滚完面板消失」的连带效应;关闭面板只剩两条明确路径:点正文、或滚正文。
  • 样式作用域:.rd-editor textarea 被概念编辑表单共用,所以高度只挂在新增的 .rd-annotation-editor 上,概念编辑表单的布局不变(03c 基线恢复通过)。
  • 维护成本:关闭策略从「散落在 app.js 里的内联事件」收敛到 menu.js 一处,和点击策略并列,后续新增浮层(翻译卡、卡片菜单)只要进同一组选择器即可,不需要再改事件处理函数。
  • 未做(明确记下):正文滚动关闭面板时草稿仍然不保存。可以后续做「关闭前落草稿」,但那属于另一层改动(涉及 store 写入时机与撤销语义),本次不扩大范围。

验证

  • node --test test/*.test.mjs → 250/250(新增 test/annotation-editor-scroll.test.mjs,jsdom 挂真实 client.js:打开标注编辑器 → 输入 5 行备注 → 在 textarea / .rd-quote 上派发 scroll → 断言面板还在、备注还在 → 在 .rd-main 上派发 scroll → 断言关闭;修复前该用例为红)
  • node eval/run.mjs → 13/13
  • node eval/layout-host.mjs → 6/6
  • node eval/visual.mjs(真实 Chrome + 真实 client.js)→ 121/122,唯一失败项 markdown relative images load through the file route 在未改动的 main 上同样失败(缺图片资源的既有环境问题)。新增三条浏览器断言:备注框自己能滚(scrollTop 102)、滚备注框时面板保持、滚正文时面板关闭。
  • 视觉基线只更新两张:03d-annotation-editor(备注框变高、按钮吸底,预期变化)、03e-note-box(前一步耗时变长,截图里的 transient toast「已收下」已经消失;同时给两张图加了「等 toast 消失」的显式等待,避免以后按运行速度抖动)。
  • node scripts/build-client.mjs 重建后 git status 干净,产物与源码一致。

服务端 index.js 未改动,纯前端改动。

The page-level dismissal listener sits on window in the capture phase, so it
also received scroll events from the annotation editor's own textarea and from
any scrolling card: the first wheel or caret scroll inside the note field
cleared the editing state and the panel vanished mid-typing.

Scroll dismissal now goes through a policy in menu.js, next to the existing
pointer policy: a scroll that starts inside a panel or an editable field is not
the reading surface moving under the anchor, while a window-level scroll (or one
landing in the prose) still closes the panels as before.

Give the annotation note field a usable size (96px) scoped to
.rd-annotation-editor so the concept edit form keeps its compact fields, pin the
save/delete/close row with the existing sticky-action class, and add real-browser
scenarios that type into the field, scroll it, and then scroll the page.
@liujieranjerry-lgtm
liujieranjerry-lgtm force-pushed the fix/annotation-editor-scroll branch from bc5117a to 5bef4b4 Compare November 23, 2026 08:37

@2019wakeup 2019wakeup left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

验证通过(PR 基线即当前 main):node --test 250/250、node eval/run.mjs 13/13、node eval/layout-host.mjs 6/6、node eval/visual.mjs 122/122。新增 DOM 用例真实挂载 client.js,覆盖了 textarea 自滚动、.rd-quote 自滚动、正文滚动关闭三条路径;视觉用例也验证了 102px 的文本框滚动和面板保持。

代码上把滚动关闭策略收敛到 menu.js,和 shouldDismissOnPointerDown 并列,选择器包含可编辑元素与浮动面板;正文/window 滚动仍关闭,保留了既有行为。.rd-annotation-editor 的样式作用域也避开了概念编辑表单。

两个备注(不阻塞合并):1) 滚动右侧抽屉时仍会关闭标注编辑器,这是既有行为,本次未扩大范围;2) 正文滚动关闭时草稿不落盘,作者已在 PR 中明确列为后续项。合并。

@2019wakeup
2019wakeup merged commit 6fc6082 into 2019wakeup:main Nov 23, 2026
1 check passed
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