@embedpdf/plugin-selection 3.0.0-next.0 → 3.0.0-next.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reducer-Dy0YPuX0.js","names":[],"sources":["../src/types.ts","../src/geometry.ts","../src/capability.ts","../src/handler.ts","../src/reducer.ts"],"sourcesContent":["import { createCapabilityToken, type PageObjectNumber } from '@embedpdf/core';\nimport type { Point, Rect, TextQuad } from '@embedpdf/core-geometry';\nimport type { SelectionSegment } from './geometry';\n\nexport type { SelectionSegment } from './geometry';\n\n// ── the public range vocabulary ─────────────────────────────────────────────\n\n/**\n * A position in a page's CHARACTER space — the space the engine's geometry\n * runs tile (`PageGeometryRun.charStart`) and search hits address\n * (`SearchMatch.charStart`). NOT a string offset into extracted text; the\n * two spaces are joined by the engine's `charMap` (see engine-core\n * `text/charmap.ts`), which `readText()` applies for you.\n */\nexport interface TextPosition {\n pon: PageObjectNumber;\n /** Character index, 0-based. */\n index: number;\n}\n\n/**\n * A half-open range in character space: `start` inclusive, `end` exclusive,\n * in document order (`start` at or before `end`). `end.index` may be 0 to\n * end a range exactly at a page boundary.\n */\nexport interface TextRange {\n start: TextPosition;\n end: TextPosition;\n}\n\n/**\n * What {@link SelectionCapability.select} accepts: a cross-page\n * {@link TextRange}, or a single-page span — the shape of a search hit, so\n * `select({ pon: hit.pageObjectNumber, start: hit.charStart, count: hit.charCount })`\n * needs no conversion.\n */\nexport type SelectionRangeInput =\n | TextRange\n | { pon: PageObjectNumber; start: number; count: number };\n\n// ── gesture-state vocabulary (host/internal) ────────────────────────────────\n\n/** A glyph address: a page + a flat character index within that page. */\nexport interface GlyphPointer {\n pon: PageObjectNumber;\n glyph: number;\n}\n\n/** Anchor = where the drag began, focus = the current end. Inclusive. */\nexport interface SelectionRange {\n anchor: GlyphPointer;\n focus: GlyphPointer;\n}\n\n/**\n * A selection boundary, anchored to the boundary GLYPH's own oriented cell.\n * `advance` is the reading direction of the segment it belongs to (+1 = the\n * frame's +x), so caret consumers place at the trailing edge without\n * re-deriving bidi from geometry. `rect` is the AABB (scroll targets).\n */\nexport interface SelectionEndpoint {\n pon: PageObjectNumber;\n glyphQuad: TextQuad;\n advance: 1 | -1;\n rect: Rect;\n}\n\n/** The selection's floating-UI anchor: a page + the selection's union box on\n * it, in CONTENT space. See {@link SelectionCapability.menuAnchor}. */\nexport interface SelectionMenuAnchor {\n pon: PageObjectNumber;\n bounds: Rect;\n}\n\nexport interface SelectionSnapshot {\n /** Per-page canonical segments — the ONE geometry consumers act on.\n * Boxes are derived views (`segment.rect`, or `rectsForPage()`). */\n pages: Array<{ pon: PageObjectNumber; segments: SelectionSegment[] }>;\n start: SelectionEndpoint | null;\n end: SelectionEndpoint | null;\n direction: 'forward' | 'backward';\n /**\n * The selection as a half-open character range in document order — a\n * valid {@link SelectionCapability.select} input (persist and restore).\n * Null when nothing is selected. Until a boundary page's text geometry\n * has loaded, its index may still exceed the page's character count\n * (`selectAll` settles as geometry arrives); consumers of the range\n * (`select`, `readText`) clamp, so round-tripping is always safe.\n */\n range: TextRange | null;\n}\n\nexport interface SelectionState {\n selection: SelectionRange | null;\n /** Derived per-line segments per page, in CONTENT space (y-down, PDF units). */\n segments: Record<number, SelectionSegment[]>;\n /** Pages whose text geometry has loaded (so the layer re-renders when ready). */\n loaded: Record<number, boolean>;\n /** When a consumer owns the selection visual (e.g. a markup tool draws its own\n * preview), the default highlight rects are suppressed. */\n highlightHidden: boolean;\n /** A selection GESTURE is in flight (drag / double- / triple-click, between\n * pointer-down and pointer-up). A readable FACT, not an event: derived\n * recomputes (page load, rotate, move) never touch it, and programmatic\n * selections are born settled. Consumers apply policy (e.g. a menu hides\n * while true). */\n selecting: boolean;\n}\n\nexport type SelectionAction =\n | { type: 'PAGE_LOADED'; pon: PageObjectNumber }\n | { type: 'SET'; selection: SelectionRange; segments: Record<number, SelectionSegment[]> }\n | { type: 'CLEAR' }\n | { type: 'SET_HIGHLIGHT_HIDDEN'; hidden: boolean }\n | { type: 'SET_SELECTING'; selecting: boolean };\n\n// ── the PUBLIC capability ───────────────────────────────────────────────────\n\n/**\n * The PUBLIC selection API — the documented, stable surface for application\n * code (toolbars, context menus, automation). Resolve it with the token\n * re-exported from the package root (`@embedpdf/plugin-selection`).\n *\n * The permission model: GEOMETRY enables selection (`doc.text.select`),\n * TEXT enables extraction (`doc.text.copy`) — neither implies the other.\n * Everything here except `readText()` works with the select scope alone, so\n * a deployment can allow selecting and highlighting while denying copy.\n *\n * Framework-only plumbing (pointer-gesture intents, geometry warming, the\n * highlight-visibility handshake) lives on {@link SelectionHostCapability},\n * reachable only through `@embedpdf/plugin-selection/internal`. Both are\n * the SAME runtime object — two typed lenses on one token.\n */\nexport interface SelectionCapability {\n // ── authorization (mirrors the engine's own enforcement) ──\n /** Whether this caller may create selections at all (`doc.text.select`).\n * Authorization only — not \"is something selected\" ({@link hasSelection})\n * and not geometry readiness (a host concern). */\n canSelect(): boolean;\n /** Whether this caller may extract literal text (`doc.text.copy`) —\n * gates {@link readText} and any copy UI. */\n canCopy(): boolean;\n\n // ── programmatic selection (character space, half-open) ──\n /**\n * Select a range programmatically — the same path gestures use, so the\n * highlight, `onChange`, and markup bridges all behave identically. An\n * empty range clears. Throws the engine's `PermissionDenied` when\n * `doc.text.select` is not granted (gate UI with {@link canSelect}).\n */\n select(range: SelectionRangeInput): void;\n /** Select every character of the document. Same gating as {@link select}. */\n selectAll(): void;\n /** Clear the selection. Always allowed. */\n clear(): void;\n\n // ── reads ──\n /** Coherent read-model: per-page segments, endpoints, direction, and the\n * half-open {@link TextRange} (a valid {@link select} input). */\n snapshot(): SelectionSnapshot;\n /**\n * Whether a selection GESTURE is in flight (drag, double- or triple-click\n * — between pointer-down and pointer-up). Programmatic {@link select} /\n * {@link selectAll} never set it (a programmatic selection is born\n * settled), and derived recomputes (page geometry loading, rotate, move)\n * never touch it. THE fact selection-scoped UI applies policy to — e.g.\n * `<SelectionMenu>` hides while true and appears at pointer-up.\n */\n isSelecting(): boolean;\n /**\n * Where selection-scoped floating UI should attach: the union box of the\n * selection's segments on its END page (where the gesture finished),\n * falling back to the last page with materialized segments while a\n * boundary page's geometry is still loading. One anchor regardless of\n * cross-page selection — the shape every anchored-overlay consumer\n * positions on. Null when nothing is selected (or nothing has\n * materialized yet).\n */\n menuAnchor(): SelectionMenuAnchor | null;\n hasSelection(): boolean;\n /** The pages the current selection covers (those with at least one\n * segment) — so a cross-page action (e.g. markup creation) can fan out\n * per page. */\n selectedPages(): PageObjectNumber[];\n /** Per-line oriented segments for a page, in content space — build your\n * own highlight layer from these. */\n segmentsForPage(pon: PageObjectNumber): SelectionSegment[];\n /** The segments' AABBs — for consumers that genuinely want boxes (scroll,\n * conservative regions). Never a substitute for the oriented quads in\n * geometry that gets drawn or persisted. */\n rectsForPage(pon: PageObjectNumber): Rect[];\n\n // ── text extraction (requires doc.text.copy) ──\n /**\n * The selected text. Fetches each selected page's text snapshot (cached\n * per page, immutable per content version) and slices it by the\n * selection's character range through the engine's `charMap` — at most\n * one text read per page per content version, amortized across every\n * call. Pages are joined with `\\n`. Resolves `''` when nothing is\n * selected; rejects with `PermissionDenied` when `doc.text.copy` is not\n * granted. Clipboard writes are deliberately NOT here — this package is\n * DOM-free; use `@embedpdf/web`'s clipboard helpers (or your platform's)\n * on top of this.\n */\n readText(): Promise<string>;\n\n // ── signals ──\n /** Fires whenever the selection segments change (drag-extend, programmatic\n * select, recompute) — for live preview. Returns an unsubscriber. */\n onChange(cb: () => void): () => void;\n /** Fires when a selection gesture ends (pointer-up) — the commit point\n * consumers (markup creation, copy prefetch) act on. */\n onCommit(cb: () => void): () => void;\n}\n\n// ── the HOST capability ─────────────────────────────────────────────────────\n\n/**\n * The HOST (framework) surface: what render layers, the interaction hub,\n * and sibling plugins need on top of the public lens. Import the token from\n * `@embedpdf/plugin-selection/internal`, never from application code. All\n * point-based functions are gesture vocabulary (`…At` = point-addressed);\n * the programmatic equivalent is the public range-based {@link\n * SelectionCapability.select}. Every function here is INERT (no requests,\n * no state) when `canSelect()` is false — the pointer handler needs no\n * permission special-casing.\n */\nexport interface SelectionHostCapability extends SelectionCapability {\n /** Warm a page's text geometry (idempotent; no-op without\n * `doc.text.select`). Layers call this when a page mounts. */\n ensurePage(pon: PageObjectNumber): void;\n isLoaded(pon: PageObjectNumber): boolean;\n /** Is a content-space point on (or near) text? Drives the I-beam cursor. */\n isOverText(pon: PageObjectNumber, point: Point): boolean;\n /** Begin a caret selection at a page point. Returns false if not near any\n * text — the caller deselects instead of capturing. */\n beginAt(pon: PageObjectNumber, point: Point): boolean;\n /** Double-click / touch long-press: select the word around the point.\n * Returns false when the point has no selectable text (geometry not\n * loaded, or no glyph there) — nothing was selected. */\n selectWordAt(pon: PageObjectNumber, point: Point): boolean;\n /** Triple-click: select the whole visual line around the point. Same\n * success contract as {@link selectWordAt}. */\n selectLineAt(pon: PageObjectNumber, point: Point): boolean;\n /** Extend the current selection to a page point (drag). */\n extendTo(pon: PageObjectNumber, point: Point): void;\n /** The gesture ended (pointer-up) → notify `onCommit` consumers. */\n end(): void;\n /** Suppress / restore the default highlight visual (a consumer drawing its\n * own preview — the markup ghost). */\n setHighlightVisible(visible: boolean): void;\n highlightVisible(): boolean;\n}\n\n/**\n * The selection capability token. Typed to the full\n * {@link SelectionHostCapability} for this module and `/internal`; the\n * package root re-exports the SAME token narrowed to\n * {@link SelectionCapability}.\n */\nexport const SelectionToken = createCapabilityToken<SelectionHostCapability>('selection');\n","/**\n * The selection plugin's COORDINATE SEAM — and nothing else.\n *\n * Segmentation, hit-testing, and word/line expansion live in the canonical\n * engine-core text layout (`@embedpdf/engine-core` `text/layout`): a text\n * range has exactly one segmentation whether it came from a drag or a\n * search. This module only converts between the viewer's content space\n * (y-down, crop-relative) and the engine's PDF space (y-up) — pointer\n * points inward, canonical segments outward. The PDF↔content y-flip is the\n * geometry package's `pageGeometry` (the one place that math lives), never\n * hand-rolled here.\n */\nimport {\n applyPoint,\n invert,\n pageGeometry,\n textQuadBounds,\n type Mat2D,\n type Point,\n type PointIn,\n type Rect,\n type TextQuad,\n} from '@embedpdf/core-geometry';\nimport {\n buildPageTextLayout,\n type PageGeometrySnapshot,\n type PageTextLayout,\n type PdfPoint,\n type PdfQuad,\n type PdfRect,\n type PdfTextSegment,\n} from '@embedpdf/engine-core/runtime';\n\n/**\n * One merged visual line of a selection, in content space — the structural\n * twin of the engine's `PdfTextSegment`. `quad` carries frame-geometric\n * corner semantics (upper = ascent side, start = the frame's −x); `advance`\n * is the READING direction along the baseline (+1 = toward `end`), derived\n * from the glyph sequence — geometry and bidi stay separate concerns.\n * `rect` is the content-space AABB.\n */\nexport interface SelectionSegment {\n quad: TextQuad;\n rect: Rect;\n advance: 1 | -1;\n}\n\n/** A page's cached selection geometry: the canonical layout + the seam. */\nexport interface SelectionPageGeometry {\n layout: PageTextLayout;\n toContent: Mat2D<'pdf', 'content'>;\n fromContent: Mat2D<'content', 'pdf'>;\n}\n\n/**\n * Build a page's selection geometry. Page display rotation does NOT enter\n * here — the overlay rides the page's CSS rotation, so content space stays\n * un-rotated (the crop-relative y-flip is the only conversion; zoom = 1,\n * the viewer's zoom is applied later by `PageTransform.toPixels`).\n */\nexport function buildSelectionPageGeometry(\n snapshot: PageGeometrySnapshot,\n crop: PdfRect,\n rotation: 0 | 90 | 180 | 270,\n userUnit: number,\n): SelectionPageGeometry {\n const { pdfToContent } = pageGeometry({ crop, rotation, userUnit }, 1);\n return {\n layout: buildPageTextLayout(snapshot),\n toContent: pdfToContent,\n fromContent: invert(pdfToContent),\n };\n}\n\n/** A content-space pointer point in the layout's PDF space. */\nexport function contentPointToPdf(geom: SelectionPageGeometry, p: Point): PdfPoint {\n return applyPoint(geom.fromContent, p as PointIn<'content'>);\n}\n\n/**\n * A canonical PDF quad in content space. Engine quads carry frame-geometric\n * slot semantics (`p1..p4` = upper-start, upper-end, lower-start,\n * lower-end) — a trusted producer, so the y-flip maps corners straight onto\n * their names; imported-annotation normalization does NOT apply here.\n */\nexport function toContentTextQuad(geom: SelectionPageGeometry, quad: PdfQuad): TextQuad {\n const pt = (p: PdfPoint): Point => applyPoint(geom.toContent, p as PointIn<'pdf'>);\n return {\n upperStart: pt(quad.p1),\n upperEnd: pt(quad.p2),\n lowerStart: pt(quad.p3),\n lowerEnd: pt(quad.p4),\n };\n}\n\n/** A canonical segment in content space (`rect` recomputed from the mapped\n * quad so the pair can never drift). */\nexport function toContentSegment(\n geom: SelectionPageGeometry,\n segment: PdfTextSegment,\n): SelectionSegment {\n const quad = toContentTextQuad(geom, segment.quad);\n return { quad, rect: textQuadBounds(quad), advance: segment.advance };\n}\n","import type { PageObjectNumber, PluginContext } from '@embedpdf/core';\nimport { textQuadBounds, type Point, type Rect } from '@embedpdf/core-geometry';\nimport {\n expandTextRangeToLine,\n expandTextRangeToWord,\n PermissionDenied,\n sliceTextByChars,\n textGlyphAt,\n textGlyphQuad,\n textSegmentsForRange,\n type PageGeometrySnapshot,\n type PageTextSnapshot,\n} from '@embedpdf/engine-core/runtime';\nimport {\n buildSelectionPageGeometry,\n contentPointToPdf,\n toContentSegment,\n toContentTextQuad,\n type SelectionPageGeometry,\n type SelectionSegment,\n} from './geometry';\nimport type {\n GlyphPointer,\n SelectionAction,\n SelectionEndpoint,\n SelectionHostCapability,\n SelectionMenuAnchor,\n SelectionRange,\n SelectionRangeInput,\n SelectionSnapshot,\n SelectionState,\n TextRange,\n} from './types';\n\nconst EMPTY_SEGMENTS: SelectionSegment[] = [];\n\n/** How many page-text reads `readText()` keeps in flight at once. */\nconst TEXT_READ_CONCURRENCY = 8;\n\n/**\n * The selection capability — ONE closure returning the host lens (the\n * public lens is the same object, narrowed at the package root).\n *\n * State model: the reducer holds the selection range and the derived\n * content-space segments; the large, non-serializable per-page caches live\n * HERE in the closure, split by what invalidates them:\n *\n * - `rawGeometry` — PDF-space geometry snapshots, rotation-independent\n * (pages load normalized; see `PageRotateResult`). Dropped only when a\n * page leaves the registry or page CONTENT changes (redaction apply /\n * flatten).\n * - `derived` — the content-space transform + layout, keyed by the\n * layout params (crop/rotation/userUnit) so a view-rotate re-derives\n * from the cached raw snapshot with NO refetch.\n * - `textSnapshots` — per-page text (for `readText`), same content-only\n * invalidation as `rawGeometry`.\n *\n * Permission model: `doc.text.select` gates geometry (selection exists),\n * `doc.text.copy` gates text (extraction) — neither implies the other.\n * Host gesture functions are INERT when select is denied (`ensurePage`\n * no-ops, so nothing warms and nothing hit-tests: the pointer handler\n * needs no special-casing); the public `select()` THROWS `PermissionDenied`\n * so programmatic misuse is loud; `clear()` is always allowed. The engine\n * independently enforces both scopes — these gates exist so the plugin\n * never issues a request that is guaranteed to fail.\n *\n * Selection is cross-page: glyphs are ordered globally by (pageIndex,\n * glyph), so a drag from page 2 into page 4 selects the tail of 2, all of\n * 3, and the head of 4. `recompute` rebuilds the merged line segments for\n * every loaded page in the span, re-runs whenever a mid-span page finishes\n * loading, and CLAMPS stored endpoints once their page's geometry is known\n * (so `selectAll`'s open-ended focus settles to the real last character).\n */\nexport function createSelectionCapability(\n ctx: PluginContext<SelectionState, SelectionAction>,\n): SelectionHostCapability {\n const rawGeometry = new Map<PageObjectNumber, PageGeometrySnapshot>();\n const derived = new Map<PageObjectNumber, { key: string; geom: SelectionPageGeometry }>();\n const pending = new Set<PageObjectNumber>();\n const textSnapshots = new Map<PageObjectNumber, Promise<PageTextSnapshot>>();\n /** Bumped on content invalidation; in-flight reads from before are discarded. */\n let epoch = 0;\n // Consumers (e.g. text-markup) observe the selection without selection knowing\n // about them: `change` fires whenever the segments change, `commit` when a\n // gesture ends (pointer-up). One typed callback each — not an event bus.\n const changeCbs = new Set<() => void>();\n const commitCbs = new Set<() => void>();\n const fireChange = (): void => changeCbs.forEach((cb) => cb());\n\n const canSelect = (): boolean => ctx.doc?.security.allows('doc.text.select') ?? false;\n const canCopy = (): boolean => ctx.doc?.security.allows('doc.text.copy') ?? false;\n\n const layoutOf = (pon: PageObjectNumber) =>\n ctx.document()?.pages.find((p) => p.pageObjectNumber === pon);\n const pageIndexOf = (pon: PageObjectNumber): number =>\n ctx.document()?.pages.findIndex((p) => p.pageObjectNumber === pon) ?? -1;\n const ponAtIndex = (i: number): PageObjectNumber | undefined =>\n ctx.document()?.pages[i]?.pageObjectNumber;\n\n /**\n * The content-space geometry for a page, derived on demand from the raw\n * snapshot + the CURRENT layout params. A view-rotate (or crop change)\n * changes the key and re-derives — raw caches and the engine are never\n * touched (rotation is presentation metadata).\n */\n function geometryFor(pon: PageObjectNumber): SelectionPageGeometry | null {\n const raw = rawGeometry.get(pon);\n const layout = layoutOf(pon);\n if (!raw || !layout) return null;\n const crop = layout.boxes.crop;\n const key = `${layout.rotation}|${layout.userUnit}|${crop.left},${crop.bottom},${crop.right},${crop.top}`;\n const hit = derived.get(pon);\n if (hit && hit.key === key) return hit.geom;\n const geom = buildSelectionPageGeometry(raw, crop, layout.rotation, layout.userUnit);\n derived.set(pon, { key, geom });\n return geom;\n }\n\n const glyphAt = (geom: SelectionPageGeometry, point: Point): number | null =>\n textGlyphAt(geom.layout, contentPointToPdf(geom, point));\n\n // Order the two ends of a selection by document position (page, then glyph).\n function orderedEnds(sel: SelectionRange): {\n start: GlyphPointer;\n end: GlyphPointer;\n direction: 'forward' | 'backward';\n } {\n const ai = pageIndexOf(sel.anchor.pon);\n const fi = pageIndexOf(sel.focus.pon);\n const anchorFirst = ai < fi || (ai === fi && sel.anchor.glyph <= sel.focus.glyph);\n return anchorFirst\n ? { start: sel.anchor, end: sel.focus, direction: 'forward' }\n : { start: sel.focus, end: sel.anchor, direction: 'backward' };\n }\n\n function endpointFor(ptr: GlyphPointer, which: 'start' | 'end'): SelectionEndpoint | null {\n const segments = ctx.getState().segments[ptr.pon] ?? EMPTY_SEGMENTS;\n if (!segments.length) return null;\n const segment = which === 'start' ? segments[0] : segments[segments.length - 1];\n\n // Anchor the endpoint to the boundary GLYPH's own oriented cell so caret\n // placement lands on the exact character edge; fall back to the segment\n // when the glyph is degenerate (e.g. a generated space).\n const geom = geometryFor(ptr.pon);\n const cell = geom ? textGlyphQuad(geom.layout, ptr.glyph) : null;\n if (geom && cell) {\n const glyphQuad = toContentTextQuad(geom, cell);\n return {\n pon: ptr.pon,\n glyphQuad,\n advance: segment.advance,\n rect: textQuadBounds(glyphQuad),\n };\n }\n return { pon: ptr.pon, glyphQuad: segment.quad, advance: segment.advance, rect: segment.rect };\n }\n\n /** Union box of a page's segments, or null when none have materialized. */\n function pageBounds(pon: PageObjectNumber): Rect | null {\n const segs = ctx.getState().segments[pon];\n if (!segs || segs.length === 0) return null;\n let x1 = Infinity;\n let y1 = Infinity;\n let x2 = -Infinity;\n let y2 = -Infinity;\n for (const s of segs) {\n x1 = Math.min(x1, s.rect.x);\n y1 = Math.min(y1, s.rect.y);\n x2 = Math.max(x2, s.rect.x + s.rect.width);\n y2 = Math.max(y2, s.rect.y + s.rect.height);\n }\n return { x: x1, y: y1, width: x2 - x1, height: y2 - y1 };\n }\n\n function menuAnchor(): SelectionMenuAnchor | null {\n const sel = ctx.getState().selection;\n if (!sel) return null;\n const { end } = orderedEnds(sel);\n // Prefer the gesture's end page; while its geometry is still loading,\n // fall back to the LAST page (document order) with materialized segments\n // so the anchor never teleports backwards mid-drag.\n let bounds = pageBounds(end.pon);\n if (bounds) return { pon: end.pon, bounds };\n const pages = ctx.document()?.pages ?? [];\n for (let i = pages.length - 1; i >= 0; i--) {\n const pon = pages[i].pageObjectNumber;\n bounds = pageBounds(pon);\n if (bounds) return { pon, bounds };\n }\n return null;\n }\n\n function snapshot(): SelectionSnapshot {\n const { selection, segments } = ctx.getState();\n const pages = Object.keys(segments)\n .map(Number)\n .filter((pon) => (segments[pon]?.length ?? 0) > 0)\n .map((pon) => ({ pon: pon as PageObjectNumber, segments: segments[pon] }));\n if (!selection) return { pages, start: null, end: null, direction: 'forward', range: null };\n const { start, end, direction } = orderedEnds(selection);\n return {\n pages,\n start: endpointFor(start, 'start'),\n end: endpointFor(end, 'end'),\n direction,\n range: {\n start: { pon: start.pon, index: start.glyph },\n end: { pon: end.pon, index: end.glyph + 1 },\n },\n };\n }\n\n function ensurePage(pon: PageObjectNumber): void {\n if (rawGeometry.has(pon) || pending.has(pon)) return;\n // Authorized-only warming: without doc.text.select the read is\n // guaranteed to be rejected — don't issue it. The engine guard stays\n // the security boundary either way.\n if (!canSelect()) return;\n const doc = ctx.doc;\n if (!doc || !layoutOf(pon)) return;\n pending.add(pon);\n const at = epoch;\n doc\n .page(pon)\n .geometry.read()\n .then(\n (snapshot) => {\n pending.delete(pon);\n if (at !== epoch) return; // content changed while in flight — stale\n rawGeometry.set(pon, snapshot);\n ctx.dispatch({ type: 'PAGE_LOADED', pon });\n if (ctx.getState().selection) recompute(); // a mid-span page arrived → fill its segments\n },\n () => {\n pending.delete(pon); // doc closed / read aborted / denied — ignore\n },\n );\n }\n\n /** Clamp a pointer into its page's real character range once geometry is\n * known — how `selectAll`'s open-ended focus settles. */\n function clampPointer(ptr: GlyphPointer): GlyphPointer {\n const geom = geometryFor(ptr.pon);\n if (!geom) return ptr;\n const max = Math.max(geom.layout.glyphs.length - 1, 0);\n const glyph = Math.max(0, Math.min(ptr.glyph, max));\n return glyph === ptr.glyph ? ptr : { pon: ptr.pon, glyph };\n }\n\n // Rebuild merged line segments for every loaded page in the span; warm the\n // rest (they recompute on arrival). Clears when an endpoint's page has left\n // the registry — a range across a structural edit is meaningless.\n function recompute(sel: SelectionRange | null = ctx.getState().selection): void {\n if (!sel) return;\n const clamped: SelectionRange = {\n anchor: clampPointer(sel.anchor),\n focus: clampPointer(sel.focus),\n };\n const { start, end } = orderedEnds(clamped);\n const si = pageIndexOf(start.pon);\n const ei = pageIndexOf(end.pon);\n if (si < 0 || ei < 0) {\n clearSelection();\n return;\n }\n const segments: Record<number, SelectionSegment[]> = {};\n for (let i = si; i <= ei; i++) {\n const pon = ponAtIndex(i);\n if (pon == null) continue;\n const geom = geometryFor(pon);\n if (!geom) {\n ensurePage(pon); // not loaded yet — it'll recompute when ready\n continue;\n }\n const from = i === si ? start.glyph : 0;\n const to = i === ei ? end.glyph : geom.layout.glyphs.length - 1;\n segments[pon] = textSegmentsForRange(geom.layout, from, to - from + 1).map((s) =>\n toContentSegment(geom, s),\n );\n }\n ctx.dispatch({ type: 'SET', selection: clamped, segments });\n fireChange();\n }\n\n /** Flip the gesture-in-flight FACT (see `SelectionState.selecting`). Set\n * BEFORE the recompute that fires `onChange`, so change listeners always\n * observe a coherent (selecting, segments) pair. */\n function setSelecting(selecting: boolean): void {\n ctx.dispatch({ type: 'SET_SELECTING', selecting });\n }\n\n function clearSelection(): void {\n setSelecting(false);\n ctx.dispatch({ type: 'CLEAR' });\n fireChange();\n }\n\n // Set the selection to a flat [from,to] glyph span on one page (word/line).\n // These are GESTURE vocabulary (double-/triple-click) — the commit arrives\n // at the pointer-up that follows, so the span counts as in-flight until then.\n // Returns whether a span actually engaged (geometry present AND a glyph\n // under the point) — the fact haptics and other success-gated feedback key\n // on, so nothing ever buzzes over blank space.\n function selectSpanAt(pon: PageObjectNumber, point: Point, expand: 'word' | 'line'): boolean {\n const geom = geometryFor(pon);\n if (!geom) return false;\n const i = glyphAt(geom, point);\n if (i == null) return false;\n const [from, to] =\n expand === 'word'\n ? expandTextRangeToWord(geom.layout, i)\n : expandTextRangeToLine(geom.layout, i);\n setSelecting(true);\n recompute({ anchor: { pon, glyph: from }, focus: { pon, glyph: to } });\n return true;\n }\n\n function assertCanSelect(context: string): void {\n if (!canSelect()) throw new PermissionDenied('doc.text.select', context);\n }\n\n /** The public programmatic entry: a half-open character range in. */\n function select(input: SelectionRangeInput): void {\n assertCanSelect('selection.select');\n const range: TextRange =\n 'pon' in input\n ? {\n start: { pon: input.pon, index: input.start },\n end: { pon: input.pon, index: input.start + input.count },\n }\n : input;\n const si = pageIndexOf(range.start.pon);\n let ei = pageIndexOf(range.end.pon);\n if (si < 0 || ei < 0) {\n throw new Error(\n `select: unknown page object ${si < 0 ? range.start.pon : range.end.pon}`,\n );\n }\n let endIndex = range.end.index;\n if (ei < si || (ei === si && endIndex <= range.start.index)) {\n clearSelection(); // empty range = no selection\n return;\n }\n if (endIndex === 0) {\n // Half-open end exactly at a page boundary: the last included\n // character is the previous page's last one. Its index settles via\n // clamping once that page's geometry loads.\n ei -= 1;\n if (ei < si) {\n clearSelection();\n return;\n }\n endIndex = Number.MAX_SAFE_INTEGER;\n }\n const focusPon = ponAtIndex(ei);\n if (focusPon == null) throw new Error(`select: unknown page at index ${ei}`);\n setSelecting(false); // programmatic selections are born settled\n recompute({\n anchor: { pon: range.start.pon, glyph: Math.max(0, range.start.index) },\n focus: { pon: focusPon, glyph: endIndex - 1 },\n });\n }\n\n function selectAll(): void {\n assertCanSelect('selection.selectAll');\n const pages = ctx.document()?.pages ?? [];\n if (pages.length === 0) return;\n setSelecting(false); // programmatic selections are born settled\n recompute({\n anchor: { pon: pages[0].pageObjectNumber, glyph: 0 },\n focus: {\n pon: pages[pages.length - 1].pageObjectNumber,\n glyph: Number.MAX_SAFE_INTEGER, // settles to the real last character on load\n },\n });\n }\n\n /** Per-page text snapshot, cached as a PROMISE so concurrent readers\n * share one fetch. Rejections are evicted (a denied or failed read must\n * not poison the cache for a later authorized call). */\n function pageTextSnapshot(pon: PageObjectNumber): Promise<PageTextSnapshot> {\n let p = textSnapshots.get(pon);\n if (!p) {\n const doc = ctx.doc;\n if (!doc) return Promise.reject(new Error('readText: document is not open'));\n p = Promise.resolve(doc.page(pon).text.read());\n p.catch(() => textSnapshots.delete(pon));\n textSnapshots.set(pon, p);\n }\n return p;\n }\n\n async function readText(): Promise<string> {\n const sel = ctx.getState().selection;\n if (!sel) return '';\n if (!canCopy()) throw new PermissionDenied('doc.text.copy', 'selection.readText');\n const { start, end } = orderedEnds(sel);\n const si = pageIndexOf(start.pon);\n const ei = pageIndexOf(end.pon);\n if (si < 0 || ei < 0) return '';\n // Per-page half-open character spans. Geometry is NOT needed here —\n // boundary offsets come from the range, interior pages span their whole\n // charCount (known from the text snapshot itself).\n const spans: Array<{ pon: PageObjectNumber; from: number; to: number }> = [];\n for (let i = si; i <= ei; i++) {\n const pon = ponAtIndex(i);\n if (pon == null) continue;\n spans.push({\n pon,\n from: i === si ? start.glyph : 0,\n to: i === ei ? end.glyph + 1 : Number.MAX_SAFE_INTEGER, // slice clamps to charCount\n });\n }\n const parts: string[] = new Array(spans.length);\n for (let base = 0; base < spans.length; base += TEXT_READ_CONCURRENCY) {\n const batch = spans.slice(base, base + TEXT_READ_CONCURRENCY);\n const snapshots = await Promise.all(batch.map((s) => pageTextSnapshot(s.pon)));\n snapshots.forEach((snap, j) => {\n parts[base + j] = sliceTextByChars(snap, batch[j].from, batch[j].to);\n });\n }\n return parts.join('\\n');\n }\n\n /** Structural registry change (rotate/move/delete/insert — `revision`\n * bumped): drop caches for pages that left, then recompute. Rotation\n * re-derives transforms via the layout key; a deleted endpoint page\n * clears via recompute's registry check. Raw snapshots are NEVER\n * refetched here — they are rotation-independent. */\n function onPagesUpdated(): void {\n const alive = new Set((ctx.document()?.pages ?? []).map((p) => p.pageObjectNumber));\n for (const pon of [...rawGeometry.keys()]) {\n if (!alive.has(pon)) {\n rawGeometry.delete(pon);\n derived.delete(pon);\n textSnapshots.delete(pon);\n }\n }\n if (ctx.getState().selection) recompute();\n }\n\n /** Page CONTENT changed (redaction apply / flatten): every cached\n * snapshot — geometry AND text — is stale, and any live range points\n * into a character space that no longer exists. Drop everything, clear. */\n function invalidateContent(): void {\n epoch++;\n rawGeometry.clear();\n derived.clear();\n pending.clear();\n textSnapshots.clear();\n if (ctx.getState().selection) clearSelection();\n }\n\n // ── lifecycle wiring (registry + content invalidation) ──\n let lastRevision = ctx.document()?.revision ?? -1;\n const offStore = ctx.subscribe(() => {\n const revision = ctx.document()?.revision;\n if (revision === undefined || revision === lastRevision) return;\n lastRevision = revision;\n onPagesUpdated();\n });\n ctx.cleanup(offStore);\n const offEvents = ctx.doc?.events.subscribe((event) => {\n if (event.type === 'redaction.applied' || event.type === 'pages.flattened') {\n invalidateContent();\n }\n });\n if (offEvents) ctx.cleanup(offEvents);\n\n return {\n // ── public lens ──\n canSelect,\n canCopy,\n select,\n selectAll,\n clear: clearSelection,\n snapshot,\n menuAnchor,\n isSelecting: () => ctx.getState().selecting,\n hasSelection: () => ctx.getState().selection != null,\n selectedPages: () => {\n const { segments } = ctx.getState();\n return Object.keys(segments)\n .map(Number)\n .filter((pon) => (segments[pon]?.length ?? 0) > 0) as PageObjectNumber[];\n },\n segmentsForPage: (pon) => ctx.getState().segments[pon] ?? EMPTY_SEGMENTS,\n rectsForPage: (pon) =>\n (ctx.getState().segments[pon] ?? EMPTY_SEGMENTS).map((s) => s.rect),\n readText,\n onChange: (cb) => {\n changeCbs.add(cb);\n return () => changeCbs.delete(cb);\n },\n onCommit: (cb) => {\n commitCbs.add(cb);\n return () => commitCbs.delete(cb);\n },\n\n // ── host lens ──\n ensurePage,\n isLoaded: (pon) => !!ctx.getState().loaded[pon],\n isOverText: (pon, point: Point) => {\n const geom = geometryFor(pon);\n return geom ? glyphAt(geom, point) != null : false;\n },\n beginAt: (pon, point: Point) => {\n const geom = geometryFor(pon);\n if (!geom) return false;\n const i = glyphAt(geom, point);\n if (i == null) return false; // not near text — caller deselects, doesn't capture\n setSelecting(true); // the gesture opened\n recompute({ anchor: { pon, glyph: i }, focus: { pon, glyph: i } });\n return true;\n },\n selectWordAt: (pon, point: Point) => selectSpanAt(pon, point, 'word'),\n selectLineAt: (pon, point: Point) => selectSpanAt(pon, point, 'line'),\n extendTo: (pon, point: Point) => {\n const cur = ctx.getState().selection;\n if (!cur) return;\n const geom = geometryFor(pon);\n if (!geom) {\n ensurePage(pon); // dragged onto a not-yet-loaded page — warm it, recompute on load\n return;\n }\n const i = glyphAt(geom, point);\n if (i == null) return; // off-text — keep the last focus\n recompute({ anchor: cur.anchor, focus: { pon, glyph: i } });\n },\n end: () => {\n // The gesture ended (pointer-up): settle FIRST, so commit listeners\n // (menus, clipboard prefetch) observe isSelecting() === false.\n setSelecting(false);\n commitCbs.forEach((cb) => cb());\n },\n setHighlightVisible: (visible) =>\n ctx.dispatch({ type: 'SET_HIGHLIGHT_HIDDEN', hidden: !visible }),\n highlightVisible: () => !ctx.getState().highlightHidden,\n };\n}\n","import type {\n InteractionCapability,\n InteractionHandler,\n PlatformFeedback,\n} from '@embedpdf/plugin-interaction';\nimport type { SelectionHostCapability } from './types';\n\nconst CURSOR_TOKEN = 'selection-text';\nconst DRAG_THRESHOLD_PX = 4; // viewport px the pointer must move before a drag-select begins\n\n/**\n * The text-selection pointer handler. It is live only under tools that enable the\n * `'text-select'` tag (the built-in `pointer` tool does; `pan` does not — so\n * switching to pan disables text selection with zero special-casing here).\n *\n * - single click → deselect; records an anchor but selects NOTHING until\n * the pointer moves past the drag threshold (Chrome/Acrobat feel)\n * - single-click + drag → caret selection from the anchor (extends across pages)\n * - double-click → select the word\n * - triple-click → select the visual line\n * - down off-text → deselect, and DON'T capture (so it can't block)\n * - hover → I-beam only when over text, else the pointer cursor\n */\nexport function createTextSelectHandler(\n selection: SelectionHostCapability,\n interaction: InteractionCapability,\n feedback?: PlatformFeedback,\n): InteractionHandler {\n // Per-gesture drag-threshold state (one active gesture at a time — the hub owner).\n let anchor: { pon: number; point: { x: number; y: number }; vx: number; vy: number } | null =\n null;\n let dragging = false;\n\n return {\n id: 'text-select',\n priority: 60,\n enabledFor: (tool) => tool.enables.has('text-select'),\n onDown: (s) => {\n anchor = null;\n dragging = false;\n if (!s.page) return false; // over a gap — let a lower handler (e.g. scroll) try\n const { pon, point } = s.page;\n const clicks = s.clickCount ?? 1;\n if (clicks >= 3) {\n selection.selectLineAt(pon, point);\n return true;\n }\n if (clicks === 2) {\n const selected = selection.selectWordAt(pon, point);\n // The platform's \"selection changed\" tick — ONLY when a touch\n // long-press actually engaged a word (never on blank space, never for\n // a mouse double-click). The gesture marker is the honest signal; a\n // raw double-click sample never carries it.\n if (selected && s.gesture === 'long-press') feedback?.selection();\n return true;\n }\n // Single click: clear immediately (clicking deselects), then record an anchor\n // ONLY if over text — but begin no selection until the drag threshold is met.\n selection.clear();\n if (!selection.isOverText(pon, point)) return false; // empty space → don't capture\n anchor = { pon, point, vx: s.viewport.x, vy: s.viewport.y };\n return true;\n },\n onMove: (s) => {\n if (!s.page || !anchor) return;\n if (!dragging) {\n if (Math.hypot(s.viewport.x - anchor.vx, s.viewport.y - anchor.vy) < DRAG_THRESHOLD_PX) {\n return; // still a click, not a drag — select nothing yet\n }\n dragging = true;\n selection.beginAt(anchor.pon, anchor.point); // open the selection at the anchor\n }\n selection.extendTo(s.page.pon, s.page.point);\n },\n onUp: () => {\n anchor = null;\n dragging = false;\n selection.end();\n },\n onCancel: () => {\n // Aborted (second finger → pinch, or a system cancel): drop the\n // in-flight selection instead of committing a sliver of it. end() after\n // clear() settles the `selecting` flag without anything to commit.\n anchor = null;\n dragging = false;\n selection.clear();\n selection.end();\n },\n onHover: (s) => {\n if (!s.page) {\n interaction.setCursor(CURSOR_TOKEN, null); // off the page → pointer\n return;\n }\n // Warm geometry on first hover; show the I-beam ONLY when actually over text.\n selection.ensurePage(s.page.pon);\n const overText = selection.isOverText(s.page.pon, s.page.point);\n interaction.setCursor(CURSOR_TOKEN, overText ? 'text' : null, 10);\n },\n };\n}\n","import type { SelectionAction, SelectionState } from './types';\n\nexport const initialSelectionState: SelectionState = {\n selection: null,\n segments: {},\n loaded: {},\n highlightHidden: false,\n selecting: false,\n};\n\nexport const selectionReducer = (\n state: SelectionState,\n action: SelectionAction,\n): SelectionState => {\n switch (action.type) {\n case 'PAGE_LOADED':\n return state.loaded[action.pon]\n ? state\n : { ...state, loaded: { ...state.loaded, [action.pon]: true } };\n case 'SET':\n return { ...state, selection: action.selection, segments: action.segments };\n case 'CLEAR':\n return state.selection === null && Object.keys(state.segments).length === 0\n ? state\n : { ...state, selection: null, segments: {} };\n case 'SET_HIGHLIGHT_HIDDEN':\n return state.highlightHidden === action.hidden\n ? state\n : { ...state, highlightHidden: action.hidden };\n case 'SET_SELECTING':\n return state.selecting === action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