Skip to content

feat(browser): design mode annotation layer for the embedded browser (S2) - #153

Merged
bubbleptr merged 8 commits into
mainfrom
feat/embedded-browser-annotation
Sep 2, 2026
Merged

bubbleptr merged 8 commits into
mainfrom
feat/embedded-browser-annotation

Conversation

@bubbleptr

Copy link
Copy Markdown
Owner

Closes #150. Refs #86. PRD: .scratch/embedded-browser/PRD.md(决策 4/5、「S2 / S3 实现约束」段)。

摘要

给 WebContentsView 挂一个独立的 preload,在 isolated world 里放 closed Shadow DOM 覆盖层,实现 design mode:悬停高亮、点击选中并落序号、点序号展开评论气泡、Esc 退出;标注状态经独立 IPC channel 回传渲染层,浏览器 surface 表头新增 Design 开关 / 计数 / 清空。

  • electron/browser-annotation.ts:channel 常量、消息类型、selector 生成(id / data-testid / nth-of-type 回退)、元素描述、主进程侧消息白名单校验(纯函数)。
  • electron/browser-annotation-overlay.ts:页内覆盖层控制器(纯 DOM,jsdom 可测);标记 position: fixed,scroll(捕获)/ resize 经 rAF 重测。
  • electron/browser-annotation-preload.ts:约 50 行 IPC 接线,不向页面暴露任何 API。
  • main.ts:webPreferences.preload、pigui:browser-annotation(页→主,event.sender 只认该 view)、pigui:browser-annotation-command(主→页)。
  • browser-host.ts:browser_set_design_mode / browser_clear_annotations;每个新文档 ready 后重放 design mode。
  • browser-protocol.ts:BrowserAnnotationElement;BrowserEvent 新增 annotations-changed / design-mode-changed。
  • 渲染层:browser-surface.tsx 工具条(只用普通按钮,避免触发弹层快照冻结)、session-browser-panel.tsx 状态;/design 页登记,docs/self-built-ui.md 对账。
  • E2E:fixture 新增严格 CSP 页(default-src 'none'; script-src 'self'),断言 design mode 可用、页面读不到覆盖层、Design 开关不触发快照。

与 PRD / issue 的偏差

  • BrowserAnnotationElement 暂放 browser-protocol.ts,不含 reactName(isolated world 读不到 fiber,PRD 实现约束 1);core 类型与 formatBrowserAnnotationPrompt 归 S3(Embedded browser S3:标注载荷回传 composer(core 类型 + 格式化 + 截图 + Send to composer) #151)。
  • 计数用纯文本「N marked」而非 Badge(Astryx Badge 的 Don't 明写计数不该做徽章)。
  • 评论气泡默认收起、点序号展开,避免 Esc 二义与焦点乱跳。
  • 离开 surface(切 Terminal / 收起 inspector / 切 Session)清空标记:页面存活而组件不存活,留着会出现「页面有徽章、工具条计数 0」的搁浅态。

已知限制(S4 写入 ADR-0029)

  • 回传的 rect 是标记时刻的视口坐标,标记本身跟随滚动但 rect 不更新。
  • 加载窗口内的事件会被 navigationId 过滤丢弃(加载期间页内 Esc 退出 design mode,工具条短暂不同步,下一次变更自愈)。
  • 覆盖层只在主框架:iframe(同源或跨源)内元素标不到,design mode 也拦不住其点击。
  • 页面自有 shadow DOM 内元素能选中,但 selector 止于 shadow root,从 document 查不到。
  • design mode 期间页面全部指针事件被吞;text 取 textContent;无单条删除;页面可删覆盖层宿主(读不到内容)。
  • 覆盖层样式全走 CSSOM、结构全走 DOM API(扛严格 style-src 与 Trusted Types),因此没有 hover/focus CSS 态。

审查

/code-review medium 8 条(ready 竞态、Enter 关不掉气泡、卸载后标记搁浅、标记不随滚动重排、open shadow root 选错、markerLayer pointer-events、Project 切换不重置、评论逐键 IPC)已全部修复并各补回归测试;一条全量运行下的不稳定测试已改成确定性等待(根因:Astryx ToggleButton 的 aria-pressed 走 useOptimistic,在 transition 结束前才收敛)。

验证

  • bunx tsc --noEmit -p tsconfig.json 通过
  • bun run test:95 files / 874 tests 通过(连跑 5 次,含并发负载)
  • bun run test:e2e:18 passed
  • out/preload/:两个自包含文件,各只有 require("electron")

The annotation layer's Electron-free half: a CSS selector builder that has to
resolve uniquely however it got there, element description capped at what the
protocol allows, and the whitelist every message from the embedded page passes
through before main believes it.

The overlay controller lives beside them so design mode's interaction rules —
what a click means, what the page is allowed to see of it, when Escape stops
marking — are testable in jsdom instead of only in a real Electron window.
…wn channel

The view finally gets a preload, which is also the moment the page gains a way
to talk to main — so it gets a channel of its own that only answers to that
view's webContents. Reusing pigui:invoke would have handed a hostile page the
whole command surface, sender check and all: it has none.

Design mode is a main-process command, kept in the host so a fresh document can
be put back into it; the page can still leave design mode on its own, and says
so rather than silently disagreeing with the toolbar.

The second preload entry shares no runtime module with the renderer's, because
electron-vite would hoist one into a chunk that a sandboxed preload cannot
require. Verified on the build: out/preload/ holds two files, each requiring
nothing but electron.
Design, a count of what is marked, and Clear — plain buttons, because the
surface already swaps the native view for a still whenever any layer opens, and
a toggle that opened one would leave the user marking up a screenshot.

The marks live in the page; this side only mirrors them, which is also why the
toolbar follows an Escape pressed inside the page instead of insisting design
mode is still on. Leaving the surface leaves design mode: the page outlives the
component, and one left marking would swallow clicks with no toolbar in sight.

Registers the new toolbar state on /design and reconciles the ledger, as
AGENTS.md requires of anything that changes a shared/ui component's variants.
The strict-CSP fixture is what turns two manual checks into automated ones: the
overlay has to build itself with CSSOM and DOM calls on a page whose CSP would
block a <style> element, and the Design toggle must not trip the still-swap
that any layer would.

The page marks itself through evaluate — a native child view is outside the
renderer's hit-test tree — and then probes for what it can read back: the host
element, and nothing inside it.
A pointermove listener already covers mouse movement in Chromium, so the
mousemove twin only did the same work twice; the surface keeps a count rather
than a list it never reads; and the comment bubble writes to the annotation it
was built from instead of looking it up again — clearing the marks takes the
bubbles with them, so there is nothing to look up.
…ype in

Five defects the review found, all in the page-side overlay:

- Enter never closed a comment bubble. The window-capture handler stops every
  overlay-bound keydown so page shortcuts stay out of it, which also means the
  input's own listener can never run; Enter is answered where it can be seen.
- A comment sent an IPC round trip per keystroke. It now travels when the
  bubble is committed — Enter, blur, or closing — which still guarantees the
  final value goes out.
- Markers were pinned to where the element was, so any scroll (window or
  nested container) or reflow left the number over something else, in the page
  and on S3's screenshot alike. They are fixed to the viewport and re-measured
  on scroll and resize, and an element a re-render took away hides its marker
  rather than parking it in the corner.
- Clicks inside a page's own shadow DOM marked the wrapper: event.target is
  retargeted at the window. composedPath()[0] names the real element, and
  still stops at our host for our own closed root.
- Design mode applied before <html> existed left the markers unclickable,
  because the layer was built later with the default pointer-events.
Three ways the toolbar could lie about the page:

- Navigating with marks kept the old count. The new document does announce
  itself, but that announcement is stamped with a navigation id this component
  has not accepted yet, so it was dropped — nothing else was ever going to
  clear it. The side that asks for the navigation clears it.
- Leaving the surface left the marks on the page: the view outlives the
  component, so coming back showed badges over the page with the count at zero
  and Clear disabled. Cleanup now clears them along with design mode.
- A Project switch kept design mode and the count. Main re-applies design mode
  to every document that reports in, so the reset has to reach it too, or the
  page keeps marking while the toolbar says it is not. The count and the
  pressed toggle also stop rendering where no page is live, instead of showing
  a pressed control the user cannot unpress.
Two reasons this test only passed when the machine was idle.

Its wait was keyed on a browser_set_design_mode reset that mounting had already
sent, so it was satisfied before the rerender ran and synchronised nothing; it
now waits for a *new* reset, which is the thing the switch is supposed to
produce.

And it then read aria-pressed once. Astryx renders that from a useOptimistic
value which snaps back to the prop only when the click's transition settles, so
the attribute still read "true" right after the state behind it flipped — a
probe shows it does so even in a quiet run, and passing at all depended on an
incidental flush inside the previous wait. The assertion is polled instead.

Reverting the production reset still fails the test, on the reset count.
@bubbleptr
bubbleptr merged commit 7ed6e4f into main Sep 2, 2026
@bubbleptr
bubbleptr deleted the feat/embedded-browser-annotation branch September 2, 2026 17:15
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.

Embedded browser S2:标注层(preload + isolated world + Shadow DOM 覆盖层,design mode)

1 participant