Repository navigation
feat(browser): design mode annotation layer for the embedded browser (S2) - #153
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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对账。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)。已知限制(S4 写入 ADR-0029)
rect是标记时刻的视口坐标,标记本身跟随滚动但 rect 不更新。text取textContent;无单条删除;页面可删覆盖层宿主(读不到内容)。style-src与 Trusted Types),因此没有 hover/focus CSS 态。审查
/code-review medium8 条(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 passedout/preload/:两个自包含文件,各只有require("electron")