feat(plugin-selection): add programmatic setSelection() to apply/restore selections#685
Open
simonmysun wants to merge 1 commit into
Open
feat(plugin-selection): add programmatic setSelection() to apply/restore selections#685simonmysun wants to merge 1 commit into
simonmysun wants to merge 1 commit into
Conversation
…ore selections
The selection capability only exposed clear(), so consumers could not
programmatically set or restore a text selection (e.g. persist a selection
and reapply it after reload, or drive selection from search results).
Add setSelection(range, documentId?) to both the SelectionCapability and the
document-scoped SelectionScope. It accepts a SelectionRangeX — the same
{ start, end } glyph-pointer shape emitted by onSelectionChange — so a value
captured from that event can be passed straight back in to restore a
selection. Passing null clears the selection (equivalent to clear()).
Behavior:
- Loads page geometry on demand from the engine for every page the range
spans, so the returned PdfTask only resolves once highlight rects are
computed (works when restoring on a freshly opened document).
- Normalizes the range so start/end may be supplied in any order.
- Validates input up front: rejects malformed ranges, non-integer or
negative indices, and out-of-bounds pages, so a bogus page is never handed
to the engine.
- Clamps glyph indices to the actual per-page glyph count, keeping a stale
persisted selection usable instead of erroring.
- Repaints the union of previously-highlighted pages and the new range, so
switching to a disjoint selection clears stale highlights on other pages.
|
@simonmysun is attempting to deploy a commit to the OpenBook Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary
@embedpdf/plugin-selectioncould onlyclear()a selection — there was no wayto set or restore one programmatically. This adds
setSelection()to thepublic capability so consumers can persist a selection and reapply it (after
reload, tab switch, etc.) or drive selection from code (e.g. search results,
"jump to citation").
The PR and the example is majorly made by vibe coding with human reviewing.
API
Added to both
SelectionCapabilityand the document-scopedSelectionScope:SelectionRangeXis the exact{ start, end }glyph-pointer shape alreadyemitted by
onSelectionChange, so a saved value round-trips back in:Scope / notes
marquee (rectangular) feature is event-only and unaffected.
clear(), this does not touch the nativebrowser selection (apps suppress that via
user-select: none, as the officialexamples do).
Changes
packages/plugin-selection/src/lib/selection-plugin.ts—applySelection(wired to
setSelectionon capability + scope),validateRange,clampRangeToGeometry,normalizeRange.packages/plugin-selection/src/lib/types.ts—setSelectiononSelectionCapabilityandSelectionScopewith JSDoc.Testing
Verified against the built
distwith a headless registry + mock engine, andwith runnable React / Vue / Svelte / Preact demo apps temporarily deployed
at https://makelove.expert/tmp/sel-demo-dist/:
onSelectionChangenullclearsNo public type removals or breaking changes (additive only).