Skip to content

feat(ui): add ui.selection.restore for capture round-trip (SD-2936)#3146

Merged
caio-pizzol merged 8 commits intomainfrom
caio/sd-2936-selection-restore
May 5, 2026
Merged

feat(ui): add ui.selection.restore for capture round-trip (SD-2936)#3146
caio-pizzol merged 8 commits intomainfrom
caio/sd-2936-selection-restore

Conversation

@caio-pizzol
Copy link
Copy Markdown
Contributor

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.

const cap = ui.selection.capture();
openComposer(cap);
// ...user types and submits, focus returns to the editor...
const result = ui.selection.restore(cap);
if (!result.success) {
  // 'not-ready' | 'read-only' | 'missing-target' | 'stale'
}

Returns a typed result rather than void because 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. The reason discriminator 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 restore returns 'stale' rather than placing the selection on the wrong surface β€” same posture as getRects(capture). The setTextSelection command 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.

…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.
@caio-pizzol caio-pizzol requested a review from a team as a code owner May 5, 2026 10:46
@linear
Copy link
Copy Markdown

linear Bot commented May 5, 2026

Base automatically changed from caio/sd-2936-context-menu to main May 5, 2026 10:51
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.

πŸ“’ Thoughts on this report? Let us know!

@caio-pizzol caio-pizzol merged commit 806451a into main May 5, 2026
69 checks passed
@caio-pizzol caio-pizzol deleted the caio/sd-2936-selection-restore branch May 5, 2026 11:22
@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot Bot commented May 5, 2026

πŸŽ‰ This PR is included in @superdoc-dev/mcp v0.3.0-next.49

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot Bot commented May 5, 2026

πŸŽ‰ This PR is included in @superdoc-dev/react v1.2.0-next.91

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot Bot commented May 5, 2026

πŸŽ‰ This PR is included in vscode-ext v2.3.0-next.93

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot Bot commented May 5, 2026

πŸŽ‰ This PR is included in superdoc-cli v0.8.0-next.67

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot Bot commented May 5, 2026

πŸŽ‰ This PR is included in superdoc v1.30.0-next.50

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot Bot commented May 5, 2026

πŸŽ‰ This PR is included in superdoc-sdk v1.8.0-next.52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants