feat(ui): add ui.selection.restore for capture round-trip (SD-2936)#3146
Merged
caio-pizzol merged 8 commits intomainfrom May 5, 2026
Merged
feat(ui): add ui.selection.restore for capture round-trip (SD-2936)#3146caio-pizzol merged 8 commits intomainfrom
caio-pizzol merged 8 commits intomainfrom
Conversation
β¦SD-2936)
Right-click menus, hover tooltips, and any UI that asks "what's under
this point?" today read `data-track-change-id` and `data-comment-ids`
off `event.target.closest(...)` themselves. The attribute layout is
an implementation detail of the painter that consumers shouldn't
depend on, and the closest() walk makes id collisions silent (a
trackedChange inside a comment highlight surfaces only the innermost
hit).
ui.viewport.entityAt({ x, y }) takes viewport coordinates (matching
`MouseEvent.clientX/clientY` and `ViewportRect`) and returns
`ViewportEntityHit[]` β every painted entity in the chain, innermost
first. Supports `comment` and `trackedChange` today; `link`, `image`,
and `tableCell` are reserved as additive union members so consumers
switching on `hit.type` with a default branch stay forward-compatible.
The DOM walk is a pure helper (`collectEntityHitsFromChain`) so it's
testable without stubbing `document.elementFromPoint` (happy-dom in
this repo doesn't ship the method natively, and per-realm prototype
mutation didn't survive between the test and source files). The
controller method composes the helper with `elementFromPoint`.
Stacks on top of caio/sd-2936-selection-rects.
Two review issues from PR #3139: 1. entityAt previously called document.elementFromPoint globally and walked all ancestors with no check that the controller had a mounted editor or that the hit landed inside this instance's painted DOM. A page mounting two SuperDoc instances would have one's entityAt return ids from the other; post-destroy calls would return stale ids from cached painted nodes. Now resolves the host editor via resolveHostEditor, reads presentationEditor.visibleHost (newly added to the structural type), and returns [] when the host is missing or the hit element isn't inside it. 2. The published `superdoc/ui` declaration barrel at packages/superdoc/src/ui.d.ts didn't list the new public types, so `import type { ViewportEntityHit, ViewportEntityAtInput } from 'superdoc/ui'` failed for consumers. Same gap existed for SelectionAnchorRectOptions from PR #3134. Added all three.
Codecov Reportβ All modified and coverable lines are covered by tests. π’ Thoughts on this report? Let us know! |
Contributor
|
π This PR is included in @superdoc-dev/mcp v0.3.0-next.49 The release is available on GitHub release |
Contributor
|
π This PR is included in @superdoc-dev/react v1.2.0-next.91 The release is available on GitHub release |
Contributor
|
π This PR is included in vscode-ext v2.3.0-next.93 |
Contributor
|
π This PR is included in superdoc-cli v0.8.0-next.67 The release is available on GitHub release |
Contributor
|
π This PR is included in superdoc v1.30.0-next.50 The release is available on GitHub release |
Contributor
|
π This PR is included in superdoc-sdk v1.8.0-next.52 |
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.
Inverse of
ui.selection.capture(). Closes the round-trip a sidebar composer needs: capture on open β submit β restore on close so the user sees the editor with the same range highlighted, instead of the visible selection clearing the moment the textarea takes focus.Returns a typed result rather than
voidbecause captures go stale: an edit between capture and call can move or delete the captured block, the editor can switch into viewing mode, the captured target may have been a non-text selection. Thereasondiscriminator lets consumers branch on "the editor hasn't mounted yet" vs "the doc has changed under us" without inspecting state separately.Resolution routes through the routed editor for the same reason the captured-rect path does: non-body block ids only resolve in their own story editor's PM doc. When focus has moved to the body by call time, the captured non-body ids no longer resolve there and
restorereturns'stale'rather than placing the selection on the wrong surface β same posture asgetRects(capture). ThesetTextSelectioncommand exists on the routed editor's command surface and is dispatched directly.Stacks on PR #3145.
Verified:
pnpm exec vitest run src/uiβ 228 passed (15 files, +5 new);pnpm exec tsc -b tsconfig.references.jsonβ clean.