@embedpdf/react 3.0.0-next.6 → 3.0.0-next.7
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.
- package/dist/annotation.d.cts +1 -1
- package/dist/annotation.d.ts +1 -1
- package/dist/index.cjs +14 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/interaction.cjs +14 -1
- package/dist/interaction.cjs.map +1 -1
- package/dist/interaction.d.cts +2 -2
- package/dist/interaction.d.ts +2 -2
- package/dist/interaction.js +4 -3
- package/dist/interaction.js.map +1 -1
- package/dist/page-view.cjs +5 -3
- package/dist/page-view.cjs.map +1 -1
- package/dist/page-view.js +6 -4
- package/dist/page-view.js.map +1 -1
- package/dist/render.cjs +5 -4
- package/dist/render.cjs.map +1 -1
- package/dist/render.js +5 -4
- package/dist/render.js.map +1 -1
- package/dist/{runtime-CMxIuF2O.d.cts → runtime-Bh_9OtWo.d.ts} +12 -3
- package/dist/{runtime-DV98q7I0.d.ts → runtime-D4TXEICX.d.cts} +12 -3
- package/dist/runtime.cjs +2 -1
- package/dist/runtime.cjs.map +1 -1
- package/dist/runtime.d.cts +1 -1
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.js +2 -1
- package/dist/runtime.js.map +1 -1
- package/dist/selection.cjs +144 -0
- package/dist/selection.cjs.map +1 -1
- package/dist/selection.d.cts +26 -1
- package/dist/selection.d.ts +26 -1
- package/dist/selection.js +149 -6
- package/dist/selection.js.map +1 -1
- package/dist/stage.cjs +14 -145
- package/dist/stage.cjs.map +1 -1
- package/dist/stage.d.cts +18 -5
- package/dist/stage.d.ts +18 -5
- package/dist/stage.js +15 -146
- package/dist/stage.js.map +1 -1
- package/package.json +23 -23
package/dist/selection.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selection.cjs","names":["usePage","useCapability","SelectionHostToken","useSelector","shallowArray","SelectionToken","Anchored"],"sources":["../src/selection.tsx"],"sourcesContent":["/**\n * The React surface for @embedpdf/plugin-selection.\n *\n * <SelectionLayer> is a dumb renderer: it warms the page's geometry on mount,\n * reads the content-space highlight rects from the capability, and paints them —\n * mapping each rect through PageContext.pageToContent (the same path markers use).\n * Zero pointer handling here; that's the PagePointerSource + the hub.\n *\n * The layer resolves the HOST lens (`/internal`: geometry warming, the\n * highlight handshake) — the adapter is exactly what that entry exists for.\n * `useSelection()` hands app code the PUBLIC lens only.\n */\n\n// One-line-per-feature: registration travels with the UI.\nexport * from '@embedpdf/plugin-selection';\n// The clipboard side effect lives in @embedpdf/web (the plugin is DOM-free);\n// re-exported here so app code has one import for the whole feature.\nexport { copySelection } from '@embedpdf/web';\nimport * as React from 'react';\nimport { useEffect } from 'react';\nimport { SelectionToken, type SelectionMenuAnchor } from '@embedpdf/plugin-selection';\nimport { SelectionToken as SelectionHostToken } from '@embedpdf/plugin-selection/internal';\nimport { wireSelectionClipboard, type SelectionClipboardOptions } from '@embedpdf/web';\nimport { Anchored, type AnchoredPlacement } from './anchored';\nimport { shallowArray, useCapability, usePage, useSelector } from './runtime';\n\nexport interface SelectionLayerProps {\n /** Highlight colour (default: translucent blue). */\n color?: string;\n}\n\nexport function SelectionLayer({ color = 'rgba(33, 150, 243, 0.35)' }: SelectionLayerProps) {\n const page = usePage();\n const selection = useCapability(SelectionHostToken);\n const segments = useSelector(\n SelectionHostToken,\n (c) => c.segmentsForPage(page.pon),\n shallowArray,\n );\n // A consumer (e.g. a markup tool drawing its own preview) can take over the\n // selection visual; when it does, we render nothing so the two never overlap.\n const visible = useSelector(SelectionHostToken, (c) => c.highlightVisible());\n\n // Warm this page's text geometry as soon as it's on screen, so the first\n // pointer-down can hit-test without waiting on the engine round-trip.\n // (A no-op without doc.text.select — nothing warms, nothing renders.)\n useEffect(() => {\n selection.ensurePage(page.pon);\n }, [selection, page.pon]);\n\n if (!visible) return null;\n\n return (\n <svg\n style={{\n position: 'absolute',\n inset: 0,\n width: '100%',\n height: '100%',\n overflow: 'visible',\n pointerEvents: 'none',\n }}\n >\n {segments.map((s, i) => {\n // content space → un-rotated content view px (rides the page's CSS\n // rotation). An affine map, so mapping the four corners is exact —\n // upright segments render pixel-identical to the old div-per-rect.\n const ring = [s.quad.upperStart, s.quad.upperEnd, s.quad.lowerEnd, s.quad.lowerStart].map(\n (p) => page.transform.pageToContent(p),\n );\n return (\n <polygon key={i} points={ring.map((p) => `${p.x},${p.y}`).join(' ')} fill={color} />\n );\n })}\n </svg>\n );\n}\n\n/** The PUBLIC selection capability (select(), readText(), canCopy(), …) for\n * app chrome — toolbars, context menus, automation. */\nexport function useSelection() {\n return useCapability(SelectionToken);\n}\n\n/** Structural equality for the selection's menu anchor — keeps the menu from\n * re-rendering on unrelated dispatches (the capability returns a fresh\n * object each call). */\nconst sameMenuAnchor = (\n a: SelectionMenuAnchor | null,\n b: SelectionMenuAnchor | null,\n): boolean => {\n if (a === b) return true;\n if (!a || !b) return false;\n return (\n a.pon === b.pon &&\n a.bounds.x === b.bounds.x &&\n a.bounds.y === b.bounds.y &&\n a.bounds.width === b.bounds.width &&\n a.bounds.height === b.bounds.height\n );\n};\n\nexport interface SelectionMenuProps {\n children: React.ReactNode;\n /** Gap in screen px between the selection and the menu (default 8). */\n gap?: number;\n /** Where to place the menu relative to the selection. Default 'top'. */\n placement?: AnchoredPlacement;\n}\n\n/**\n * Floats over the current TEXT selection (one anchor regardless of\n * cross-page selection; it rides the gesture's end page) — and only once the\n * selection SETTLES: hidden while `isSelecting()` (mid-drag), it appears at\n * pointer-up; programmatic selections show immediately (born settled). Works\n * under `<Stage>` (mount in the overlay slot) and `<PageView>` alike — the\n * surface provides the projection. Compose the contents from hooks:\n * `useSelection()` for copy/clear, `useAnnotation()` for\n * `markupFromSelection('highlight')`, `copySelection` for the clipboard.\n * For live-follow UI during the drag, compose `<Anchored>` with\n * `menuAnchor()` yourself — the primitive carries no policy.\n */\nexport function SelectionMenu({ children, gap = 8, placement = 'top' }: SelectionMenuProps) {\n const selecting = useSelector(SelectionToken, (c) => c.isSelecting());\n const anchor = useSelector(SelectionToken, (c) => c.menuAnchor(), sameMenuAnchor);\n if (selecting || !anchor) return null;\n return (\n <Anchored anchor={anchor} placement={placement} gap={gap}>\n {children}\n </Anchored>\n );\n}\n\nexport type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;\n\n/**\n * Mount ONCE per viewer to wire clipboard copy: prefetches the selected text\n * when the selection settles, answers the native `copy` event synchronously,\n * and falls back to the async Clipboard API for ctrl/cmd+C when the page has\n * no DOM selection. Renders nothing. For a toolbar Copy button, call\n * `copySelection(useSelection())` from its click handler instead.\n */\nexport function SelectionClipboard({ prefetch }: SelectionClipboardProps = {}) {\n const selection = useCapability(SelectionToken);\n useEffect(\n () => wireSelectionClipboard(selection, prefetch === undefined ? {} : { prefetch }),\n [selection, prefetch],\n );\n return null;\n}\n"],"mappings":";;;;;;;;;;;AA+BA,SAAgB,eAAe,EAAE,QAAQ,8BAAmD;CAC1F,MAAM,OAAOA,gBAAAA,QAAQ;CACrB,MAAM,YAAYC,gBAAAA,cAAcC,oCAAAA,cAAkB;CAClD,MAAM,WAAWC,gBAAAA,YACfD,oCAAAA,iBACC,MAAM,EAAE,gBAAgB,KAAK,GAAG,GACjCE,gBAAAA,YACF;CAGA,MAAM,UAAUD,gBAAAA,YAAYD,oCAAAA,iBAAqB,MAAM,EAAE,iBAAiB,CAAC;CAK3E,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,UAAU,WAAW,KAAK,GAAG;CAC/B,GAAG,CAAC,WAAW,KAAK,GAAG,CAAC;CAExB,IAAI,CAAC,SAAS,OAAO;CAErB,OACE,iBAAA,GAAA,kBAAA,IAAA,CAAC,OAAD;EACE,OAAO;GACL,UAAU;GACV,OAAO;GACP,OAAO;GACP,QAAQ;GACR,UAAU;GACV,eAAe;EACjB;EAEC,UAAA,SAAS,KAAK,GAAG,MAAM;GAOtB,OACE,iBAAA,GAAA,kBAAA,IAAA,C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AAI,EAAE,SAAS,CAAC,GAClF,CAAC,WAAW,QAAQ,CACtB;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"selection.cjs","names":["usePage","useCapability","SelectionHostToken","useSelector","shallowArray","SelectionToken","Anchored","StageToken","useOptionalCapability","HANDLE_PAD","HANDLE_HEAD","HANDLE_BAR"],"sources":["../src/selection.tsx"],"sourcesContent":["/**\n * The React surface for @embedpdf/plugin-selection.\n *\n * <SelectionLayer> is a dumb renderer: it warms the page's geometry on mount,\n * reads the content-space highlight rects from the capability, and paints them —\n * mapping each rect through PageContext.pageToContent (the same path markers use).\n * Zero pointer handling here; that's the PagePointerSource + the hub.\n *\n * The layer resolves the HOST lens (`/internal`: geometry warming, the\n * highlight handshake) — the adapter is exactly what that entry exists for.\n * `useSelection()` hands app code the PUBLIC lens only.\n */\n\n// One-line-per-feature: registration travels with the UI.\nexport * from '@embedpdf/plugin-selection';\n// The clipboard side effect lives in @embedpdf/web (the plugin is DOM-free);\n// re-exported here so app code has one import for the whole feature.\nexport { copySelection } from '@embedpdf/web';\nimport * as React from 'react';\nimport { useEffect, useMemo, useRef, useState } from 'react';\nimport { textQuadEquals } from '@embedpdf/core-geometry';\nimport type { CapabilityToken } from '@embedpdf/core';\nimport {\n HANDLE_BAR,\n HANDLE_HEAD,\n HANDLE_PAD,\n SelectionToken,\n createSelectionHandleDrag,\n selectionHandleGeom,\n type SelectionHandleEndpoint,\n type SelectionHandleView,\n type SelectionMenuAnchor,\n} from '@embedpdf/plugin-selection';\nimport { SelectionToken as SelectionHostToken } from '@embedpdf/plugin-selection/internal';\nimport { StageToken, type StageCapability } from '@embedpdf/plugin-stage';\nimport {\n attachSelectionHandle,\n wireSelectionClipboard,\n type SelectionClipboardOptions,\n} from '@embedpdf/web';\nimport { Anchored, type AnchoredPlacement } from './anchored';\nimport {\n shallowArray,\n useCapability,\n useKernelValue,\n useOptionalCapability,\n usePage,\n useSelector,\n} from './runtime';\n\nexport interface SelectionLayerProps {\n /** Highlight colour (default: translucent blue). */\n color?: string;\n}\n\nexport function SelectionLayer({ color = 'rgba(33, 150, 243, 0.35)' }: SelectionLayerProps) {\n const page = usePage();\n const selection = useCapability(SelectionHostToken);\n const segments = useSelector(\n SelectionHostToken,\n (c) => c.segmentsForPage(page.pon),\n shallowArray,\n );\n // A consumer (e.g. a markup tool drawing its own preview) can take over the\n // selection visual; when it does, we render nothing so the two never overlap.\n const visible = useSelector(SelectionHostToken, (c) => c.highlightVisible());\n\n // Warm this page's text geometry as soon as it's on screen, so the first\n // pointer-down can hit-test without waiting on the engine round-trip.\n // (A no-op without doc.text.select — nothing warms, nothing renders.)\n useEffect(() => {\n selection.ensurePage(page.pon);\n }, [selection, page.pon]);\n\n if (!visible) return null;\n\n return (\n <svg\n style={{\n position: 'absolute',\n inset: 0,\n width: '100%',\n height: '100%',\n overflow: 'visible',\n pointerEvents: 'none',\n }}\n >\n {segments.map((s, i) => {\n // content space → un-rotated content view px (rides the page's CSS\n // rotation). An affine map, so mapping the four corners is exact —\n // upright segments render pixel-identical to the old div-per-rect.\n const ring = [s.quad.upperStart, s.quad.upperEnd, s.quad.lowerEnd, s.quad.lowerStart].map(\n (p) => page.transform.pageToContent(p),\n );\n return (\n <polygon key={i} points={ring.map((p) => `${p.x},${p.y}`).join(' ')} fill={color} />\n );\n })}\n </svg>\n );\n}\n\n/** The PUBLIC selection capability (select(), readText(), canCopy(), …) for\n * app chrome — toolbars, context menus, automation. */\nexport function useSelection() {\n return useCapability(SelectionToken);\n}\n\n/** Structural equality for the selection's menu anchor — keeps the menu from\n * re-rendering on unrelated dispatches (the capability returns a fresh\n * object each call). */\nconst sameMenuAnchor = (\n a: SelectionMenuAnchor | null,\n b: SelectionMenuAnchor | null,\n): boolean => {\n if (a === b) return true;\n if (!a || !b) return false;\n return (\n a.pon === b.pon &&\n a.bounds.x === b.bounds.x &&\n a.bounds.y === b.bounds.y &&\n a.bounds.width === b.bounds.width &&\n a.bounds.height === b.bounds.height\n );\n};\n\nexport interface SelectionMenuProps {\n children: React.ReactNode;\n /** Gap in screen px between the selection and the menu (default 8). */\n gap?: number;\n /** Where to place the menu relative to the selection. Default 'top'. */\n placement?: AnchoredPlacement;\n}\n\n/**\n * Floats over the current TEXT selection (one anchor regardless of\n * cross-page selection; it rides the gesture's end page) — and only once the\n * selection SETTLES: hidden while `isSelecting()` (mid-drag), it appears at\n * pointer-up; programmatic selections show immediately (born settled). Works\n * under `<Stage>` (mount in the overlay slot) and `<PageView>` alike — the\n * surface provides the projection. Compose the contents from hooks:\n * `useSelection()` for copy/clear, `useAnnotation()` for\n * `markupFromSelection('highlight')`, `copySelection` for the clipboard.\n * For live-follow UI during the drag, compose `<Anchored>` with\n * `menuAnchor()` yourself — the primitive carries no policy.\n */\nexport function SelectionMenu({ children, gap = 8, placement = 'top' }: SelectionMenuProps) {\n const selecting = useSelector(SelectionToken, (c) => c.isSelecting());\n const anchor = useSelector(SelectionToken, (c) => c.menuAnchor(), sameMenuAnchor);\n if (selecting || !anchor) return null;\n return (\n <Anchored anchor={anchor} placement={placement} gap={gap}>\n {children}\n </Anchored>\n );\n}\n\n// ── selection handles (the touch affordance) ────────────────────────────────\n//\n// Policy lives out of this file, per the layering razor: the geometry and the\n// drag session are `@embedpdf/plugin-selection`'s pure `handles` module (one\n// source for every adapter), the native listener mechanics are\n// `@embedpdf/web`'s `attachSelectionHandle` (the down-shield timing subtlety\n// lives ONCE), and this component keeps what only React can do —\n// subscriptions, markup, theming.\n\ninterface Endpoints {\n start: SelectionHandleEndpoint;\n end: SelectionHandleEndpoint;\n}\nconst sameEndpoints = (a: Endpoints | null, b: Endpoints | null): boolean => {\n if (a === b) return true;\n if (!a || !b) return false;\n return (\n a.start.pon === b.start.pon &&\n a.end.pon === b.end.pon &&\n a.start.advance === b.start.advance &&\n a.end.advance === b.end.advance &&\n // corner-wise, so a boundary that ROTATES without moving its bounding box\n // still re-renders (an AABB comparison would call that \"unchanged\")\n textQuadEquals(a.start.glyphQuad, b.start.glyphQuad) &&\n textQuadEquals(a.end.glyphQuad, b.end.glyphQuad)\n );\n};\n\n/** The plugin's structural view over this Stage: point-exact projection in,\n * page resolution out. (`pageRectToScreen` is the AABB projector — upright\n * overlays only — and would collapse exactly the orientation handles need.) */\nconst handleView = (stage: StageCapability): SelectionHandleView => ({\n toOverlay: (pon, pt) => {\n const world = stage.pageToWorld(pon, pt);\n return world ? stage.toScreen(world) : null;\n },\n pageAt: (overlay) => stage.pageAt(overlay),\n pointOnPage: (pon, overlay) => stage.pointOnPage(pon, overlay),\n});\n\nexport interface SelectionHandlesProps {\n /** Handle colour (default: the selection blue). */\n color?: string;\n /** The stage lens hosting this overlay (default: the main StageToken). */\n token?: CapabilityToken<StageCapability>;\n}\n\n/**\n * iOS-style draggable selection handles. Each is drawn the way the platform\n * draws them: a thin caret BAR that forms the selection's own start/end edge\n * — the boundary glyph's oriented edge, so rotated text and rotated pages\n * carry the handle with them — capped by a screen-constant circle, above the\n * first line at the start, below the last line at the end.\n *\n * On touch, where a caret drag isn't available, the handles ARE the way to\n * grow or shrink a selection: a long-press selects a word, then each handle\n * extends from the OPPOSITE endpoint, snapping to glyphs and crossing pages\n * exactly like a pointer drag — it rides the same `beginAt`/`extendTo`\n * gesture path, so highlights, menus, and commit signals all behave\n * identically. Mount in the `<Stage>` overlay slot next to `<SelectionMenu>`;\n * outside a Stage it renders nothing (a `PageView` has no camera to project\n * through). Pointer-isolated, so grabbing a handle never pans the stage.\n */\nexport function SelectionHandles({ color = '#2196f3', token = StageToken }: SelectionHandlesProps) {\n const host = useCapability(SelectionHostToken);\n const stage = useOptionalCapability(token);\n const selecting = useSelector(SelectionToken, (c) => c.isSelecting());\n const visible = useSelector(SelectionHostToken, (c) => c.highlightVisible());\n const endpoints = useSelector(\n SelectionHostToken,\n (c): Endpoints | null => {\n const s = c.snapshot();\n if (!s.start || !s.end) return null;\n return {\n start: { pon: s.start.pon, glyphQuad: s.start.glyphQuad, advance: s.start.advance },\n end: { pon: s.end.pon, glyphQuad: s.end.glyphQuad, advance: s.end.advance },\n };\n },\n sameEndpoints,\n );\n // The handles are positioned by PROJECTING the endpoint corners through the\n // camera, so they must re-render whenever the camera moves — visiblePages is\n // the stage's reference-stable revision for exactly that (the same value the\n // page surfaces re-render on, so handle and highlight move in one commit).\n useKernelValue(() => stage?.visiblePages() ?? null);\n const [dragging, setDragging] = useState<'start' | 'end' | null>(null);\n // The web binder's `arm` must read the CURRENT endpoints/stage at pointer\n // down, not the ones captured when the listener attached — a stable ref\n // callback with a live arm-source is the standard escape from that.\n const armSource = useRef<{ stage: StageCapability; endpoints: Endpoints } | null>(null);\n armSource.current = stage && endpoints ? { stage, endpoints } : null;\n const bindHandle = useMemo(() => {\n const detach: Partial<Record<'start' | 'end', () => void>> = {};\n const make = (role: 'start' | 'end') => (el: HTMLDivElement | null) => {\n detach[role]?.();\n delete detach[role];\n if (!el) return;\n detach[role] = attachSelectionHandle(el, {\n arm: () => {\n const src = armSource.current;\n if (!src) return null;\n const view = handleView(src.stage);\n const geom = selectionHandleGeom(view, src.endpoints[role], role);\n if (!geom) return null;\n const opposite = src.endpoints[role === 'start' ? 'end' : 'start'];\n const drag = createSelectionHandleDrag(\n host,\n view,\n opposite,\n src.endpoints[role].pon,\n );\n setDragging(role);\n return {\n // the point the user grabbed: the bar's midpoint\n base: {\n x: (geom.bar.from.x + geom.bar.to.x) / 2,\n y: (geom.bar.from.y + geom.bar.to.y) / 2,\n },\n session: {\n move: drag.move,\n end: () => {\n drag.end(); // settle → menu reappears, onCommit fires\n setDragging(null);\n },\n },\n };\n },\n });\n };\n return { start: make('start'), end: make('end') };\n }, [host]);\n\n if (!stage || !endpoints || !visible) return null;\n // Hidden while a pointer drag-select is in flight (like the menu) — but a\n // HANDLE drag is itself a selection gesture, so it keeps its handles.\n if (selecting && !dragging) return null;\n const view = handleView(stage);\n\n const renderHandle = (role: 'start' | 'end') => {\n const geom = selectionHandleGeom(view, endpoints[role], role);\n if (!geom) return null; // endpoint page not laid out right now\n // The shell is laid out UPRIGHT in its own frame — bar of the edge's\n // length, head stacked above (start) or below (end) — then rotated onto\n // the projected edge. The pivot is the bar's ascent-side tip, the one\n // point that must land exactly on the glyph corner.\n const barTop = HANDLE_PAD + (role === 'start' ? HANDLE_HEAD : 0);\n const pivotX = HANDLE_PAD + HANDLE_BAR / 2;\n return (\n <div\n key={role}\n ref={bindHandle[role]}\n style={{\n position: 'absolute',\n left: geom.bar.from.x - pivotX,\n top: geom.bar.from.y - barTop,\n width: HANDLE_BAR + 2 * HANDLE_PAD,\n height: geom.length + HANDLE_HEAD + 2 * HANDLE_PAD,\n // Upright text carries NO transform — pixel-identical to an\n // axis-aligned box (the geometry's float-noise guard decides).\n ...(geom.upright\n ? null\n : {\n transform: `rotate(${geom.rotation}deg)`,\n transformOrigin: `${pivotX}px ${barTop}px`,\n }),\n touchAction: 'none',\n cursor: 'grab',\n pointerEvents: 'auto',\n }}\n >\n {/* the caret bar — the selection's own edge, spanning the glyph's ink\n height (which is why it stays the text's height at any tilt) */}\n <div\n style={{\n position: 'absolute',\n left: HANDLE_PAD,\n top: barTop,\n width: HANDLE_BAR,\n height: geom.length,\n background: color,\n borderRadius: HANDLE_BAR / 2,\n }}\n />\n {/* the head — flush against the bar, beyond the ascent at the start and\n past the baseline at the end, in the TEXT's frame */}\n <div\n style={{\n position: 'absolute',\n left: pivotX - HANDLE_HEAD / 2,\n top: role === 'start' ? HANDLE_PAD : HANDLE_PAD + geom.length,\n width: HANDLE_HEAD,\n height: HANDLE_HEAD,\n borderRadius: '50%',\n background: color,\n boxShadow: '0 1px 4px rgba(0, 0, 0, 0.35)',\n }}\n />\n </div>\n );\n };\n\n return (\n <>\n {renderHandle('start')}\n {renderHandle('end')}\n </>\n );\n}\n\n\nexport type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;\n\n/**\n * Mount ONCE per viewer to wire clipboard copy: prefetches the selected text\n * when the selection settles, answers the native `copy` event synchronously,\n * and falls back to the async Clipboard API for ctrl/cmd+C when the page has\n * no DOM selection. Renders nothing. For a toolbar Copy button, call\n * `copySelection(useSelection())` from its click handler instead.\n */\nexport function SelectionClipboard({ prefetch }: SelectionClipboardProps = {}) {\n const selection = useCapability(SelectionToken);\n useEffect(\n () => wireSelectionClipboard(selection, prefetch === undefined ? {} : { prefetch }),\n [selection, prefetch],\n );\n return null;\n}\n"],"mappings":";;;;;;;;;;;;;AAuDA,SAAgB,eAAe,EAAE,QAAQ,8BAAmD;CAC1F,MAAM,OAAOA,gBAAAA,QAAQ;CACrB,MAAM,YAAYC,gBAAAA,cAAcC,oCAAAA,cAAkB;CAClD,MAAM,WAAWC,gBAAAA,YACfD,oCAAAA,iBACC,MAAM,EAAE,gBAAgB,KAAK,GAAG,GACjCE,gBAAAA,YACF;CAGA,MAAM,UAAUD,gBAAAA,YAAYD,oCAAAA,iBAAqB,MAAM,EAAE,iBAAiB,CAAC;CAK3E,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,UAAU,WAAW,KAAK,GAAG;CAC/B,GAAG,CAAC,WAAW,KAAK,GAAG,CAAC;CAExB,IAAI,CAAC,SAAS,OAAO;CAErB,OACE,iBAAA,GAAA,kBAAA,IAAA,CAAC,OAAD;EACE,OAAO;GACL,UAAU;GACV,OAAO;GACP,OAAO;GACP,QAAQ;GACR,UAAU;GACV,eAAe;EACjB;EAEC,UAAA,SAAS,KAAK,GAAG,MAAM;GAOtB,OACE,iBAAA,GAAA,kBAAA,IAAA,CAAC,WAAD;IAAiB,QAJN;KAAC,EAAE,KAAK;KAAY,EAAE,KAAK;KAAU,EAAE,KAAK;KAAU,EAAE,KAAK;IAAU,CAAC,CAAC,KACnF,MAAM,KAAK,UAAU,cAAc,CAAC,CAGT,CAAC,CAAC,KAAK,MAAM,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,KAAK,GAAG;IAAG,MAAM;GAAQ,GAArE,CAAqE;EAEvF,CAAC;CACE,CAAA;AAET;;;AAIA,SAAgB,eAAe;CAC7B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.cjs";
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import { SelectionClipboardOptions, copySelection } from "@embedpdf/web";
|
|
4
|
+
import { CapabilityToken } from "@embedpdf/core";
|
|
5
|
+
import { StageCapability } from "@embedpdf/plugin-stage";
|
|
4
6
|
export * from "@embedpdf/plugin-selection";
|
|
5
7
|
//#region src/selection.d.ts
|
|
6
8
|
interface SelectionLayerProps {
|
|
@@ -31,6 +33,29 @@ interface SelectionMenuProps {
|
|
|
31
33
|
* `menuAnchor()` yourself — the primitive carries no policy.
|
|
32
34
|
*/
|
|
33
35
|
declare function SelectionMenu({ children, gap, placement }: SelectionMenuProps): import("react/jsx-runtime").JSX.Element | null;
|
|
36
|
+
interface SelectionHandlesProps {
|
|
37
|
+
/** Handle colour (default: the selection blue). */
|
|
38
|
+
color?: string;
|
|
39
|
+
/** The stage lens hosting this overlay (default: the main StageToken). */
|
|
40
|
+
token?: CapabilityToken<StageCapability>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* iOS-style draggable selection handles. Each is drawn the way the platform
|
|
44
|
+
* draws them: a thin caret BAR that forms the selection's own start/end edge
|
|
45
|
+
* — the boundary glyph's oriented edge, so rotated text and rotated pages
|
|
46
|
+
* carry the handle with them — capped by a screen-constant circle, above the
|
|
47
|
+
* first line at the start, below the last line at the end.
|
|
48
|
+
*
|
|
49
|
+
* On touch, where a caret drag isn't available, the handles ARE the way to
|
|
50
|
+
* grow or shrink a selection: a long-press selects a word, then each handle
|
|
51
|
+
* extends from the OPPOSITE endpoint, snapping to glyphs and crossing pages
|
|
52
|
+
* exactly like a pointer drag — it rides the same `beginAt`/`extendTo`
|
|
53
|
+
* gesture path, so highlights, menus, and commit signals all behave
|
|
54
|
+
* identically. Mount in the `<Stage>` overlay slot next to `<SelectionMenu>`;
|
|
55
|
+
* outside a Stage it renders nothing (a `PageView` has no camera to project
|
|
56
|
+
* through). Pointer-isolated, so grabbing a handle never pans the stage.
|
|
57
|
+
*/
|
|
58
|
+
declare function SelectionHandles({ color, token }: SelectionHandlesProps): import("react/jsx-runtime").JSX.Element | null;
|
|
34
59
|
type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;
|
|
35
60
|
/**
|
|
36
61
|
* Mount ONCE per viewer to wire clipboard copy: prefetches the selected text
|
|
@@ -41,5 +66,5 @@ type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;
|
|
|
41
66
|
*/
|
|
42
67
|
declare function SelectionClipboard({ prefetch }?: SelectionClipboardProps): null;
|
|
43
68
|
//#endregion
|
|
44
|
-
export { SelectionClipboard, SelectionClipboardProps, SelectionLayer, SelectionLayerProps, SelectionMenu, SelectionMenuProps, copySelection, useSelection };
|
|
69
|
+
export { SelectionClipboard, SelectionClipboardProps, SelectionHandles, SelectionHandlesProps, SelectionLayer, SelectionLayerProps, SelectionMenu, SelectionMenuProps, copySelection, useSelection };
|
|
45
70
|
//# sourceMappingURL=selection.d.cts.map
|
package/dist/selection.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { AnchoredPlacement as AnchoredPlacement$1 } from "./anchored.js";
|
|
2
|
+
import { CapabilityToken } from "@embedpdf/core";
|
|
2
3
|
import * as React from "react";
|
|
3
4
|
import { SelectionClipboardOptions, copySelection } from "@embedpdf/web";
|
|
5
|
+
import { StageCapability } from "@embedpdf/plugin-stage";
|
|
4
6
|
export * from "@embedpdf/plugin-selection";
|
|
5
7
|
//#region src/selection.d.ts
|
|
6
8
|
interface SelectionLayerProps {
|
|
@@ -31,6 +33,29 @@ interface SelectionMenuProps {
|
|
|
31
33
|
* `menuAnchor()` yourself — the primitive carries no policy.
|
|
32
34
|
*/
|
|
33
35
|
declare function SelectionMenu({ children, gap, placement }: SelectionMenuProps): import("react/jsx-runtime").JSX.Element | null;
|
|
36
|
+
interface SelectionHandlesProps {
|
|
37
|
+
/** Handle colour (default: the selection blue). */
|
|
38
|
+
color?: string;
|
|
39
|
+
/** The stage lens hosting this overlay (default: the main StageToken). */
|
|
40
|
+
token?: CapabilityToken<StageCapability>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* iOS-style draggable selection handles. Each is drawn the way the platform
|
|
44
|
+
* draws them: a thin caret BAR that forms the selection's own start/end edge
|
|
45
|
+
* — the boundary glyph's oriented edge, so rotated text and rotated pages
|
|
46
|
+
* carry the handle with them — capped by a screen-constant circle, above the
|
|
47
|
+
* first line at the start, below the last line at the end.
|
|
48
|
+
*
|
|
49
|
+
* On touch, where a caret drag isn't available, the handles ARE the way to
|
|
50
|
+
* grow or shrink a selection: a long-press selects a word, then each handle
|
|
51
|
+
* extends from the OPPOSITE endpoint, snapping to glyphs and crossing pages
|
|
52
|
+
* exactly like a pointer drag — it rides the same `beginAt`/`extendTo`
|
|
53
|
+
* gesture path, so highlights, menus, and commit signals all behave
|
|
54
|
+
* identically. Mount in the `<Stage>` overlay slot next to `<SelectionMenu>`;
|
|
55
|
+
* outside a Stage it renders nothing (a `PageView` has no camera to project
|
|
56
|
+
* through). Pointer-isolated, so grabbing a handle never pans the stage.
|
|
57
|
+
*/
|
|
58
|
+
declare function SelectionHandles({ color, token }: SelectionHandlesProps): import("react/jsx-runtime").JSX.Element | null;
|
|
34
59
|
type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;
|
|
35
60
|
/**
|
|
36
61
|
* Mount ONCE per viewer to wire clipboard copy: prefetches the selected text
|
|
@@ -41,5 +66,5 @@ type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;
|
|
|
41
66
|
*/
|
|
42
67
|
declare function SelectionClipboard({ prefetch }?: SelectionClipboardProps): null;
|
|
43
68
|
//#endregion
|
|
44
|
-
export { SelectionClipboard, SelectionClipboardProps, SelectionLayer, SelectionLayerProps, SelectionMenu, SelectionMenuProps, copySelection, useSelection };
|
|
69
|
+
export { SelectionClipboard, SelectionClipboardProps, SelectionHandles, SelectionHandlesProps, SelectionLayer, SelectionLayerProps, SelectionMenu, SelectionMenuProps, copySelection, useSelection };
|
|
45
70
|
//# sourceMappingURL=selection.d.ts.map
|
package/dist/selection.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { shallowArray, useCapability, usePage, useSelector } from "./runtime.js";
|
|
1
|
+
import { shallowArray, useCapability, useKernelValue, useOptionalCapability, usePage, useSelector } from "./runtime.js";
|
|
2
2
|
import { Anchored } from "./anchored.js";
|
|
3
|
-
import { jsx } from "react/jsx-runtime";
|
|
4
|
-
import { useEffect } from "react";
|
|
5
|
-
import { copySelection, wireSelectionClipboard } from "@embedpdf/web";
|
|
6
|
-
import {
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { attachSelectionHandle, copySelection, wireSelectionClipboard } from "@embedpdf/web";
|
|
6
|
+
import { StageToken } from "@embedpdf/plugin-stage";
|
|
7
|
+
import { textQuadEquals } from "@embedpdf/core-geometry";
|
|
8
|
+
import { HANDLE_BAR, HANDLE_HEAD, HANDLE_PAD, SelectionToken, createSelectionHandleDrag, selectionHandleGeom } from "@embedpdf/plugin-selection";
|
|
7
9
|
import { SelectionToken as SelectionToken$1 } from "@embedpdf/plugin-selection/internal";
|
|
8
10
|
export * from "@embedpdf/plugin-selection";
|
|
9
11
|
//#region src/selection.tsx
|
|
@@ -74,6 +76,147 @@ function SelectionMenu({ children, gap = 8, placement = "top" }) {
|
|
|
74
76
|
children
|
|
75
77
|
});
|
|
76
78
|
}
|
|
79
|
+
const sameEndpoints = (a, b) => {
|
|
80
|
+
if (a === b) return true;
|
|
81
|
+
if (!a || !b) return false;
|
|
82
|
+
return a.start.pon === b.start.pon && a.end.pon === b.end.pon && a.start.advance === b.start.advance && a.end.advance === b.end.advance && textQuadEquals(a.start.glyphQuad, b.start.glyphQuad) && textQuadEquals(a.end.glyphQuad, b.end.glyphQuad);
|
|
83
|
+
};
|
|
84
|
+
/** The plugin's structural view over this Stage: point-exact projection in,
|
|
85
|
+
* page resolution out. (`pageRectToScreen` is the AABB projector — upright
|
|
86
|
+
* overlays only — and would collapse exactly the orientation handles need.) */
|
|
87
|
+
const handleView = (stage) => ({
|
|
88
|
+
toOverlay: (pon, pt) => {
|
|
89
|
+
const world = stage.pageToWorld(pon, pt);
|
|
90
|
+
return world ? stage.toScreen(world) : null;
|
|
91
|
+
},
|
|
92
|
+
pageAt: (overlay) => stage.pageAt(overlay),
|
|
93
|
+
pointOnPage: (pon, overlay) => stage.pointOnPage(pon, overlay)
|
|
94
|
+
});
|
|
95
|
+
/**
|
|
96
|
+
* iOS-style draggable selection handles. Each is drawn the way the platform
|
|
97
|
+
* draws them: a thin caret BAR that forms the selection's own start/end edge
|
|
98
|
+
* — the boundary glyph's oriented edge, so rotated text and rotated pages
|
|
99
|
+
* carry the handle with them — capped by a screen-constant circle, above the
|
|
100
|
+
* first line at the start, below the last line at the end.
|
|
101
|
+
*
|
|
102
|
+
* On touch, where a caret drag isn't available, the handles ARE the way to
|
|
103
|
+
* grow or shrink a selection: a long-press selects a word, then each handle
|
|
104
|
+
* extends from the OPPOSITE endpoint, snapping to glyphs and crossing pages
|
|
105
|
+
* exactly like a pointer drag — it rides the same `beginAt`/`extendTo`
|
|
106
|
+
* gesture path, so highlights, menus, and commit signals all behave
|
|
107
|
+
* identically. Mount in the `<Stage>` overlay slot next to `<SelectionMenu>`;
|
|
108
|
+
* outside a Stage it renders nothing (a `PageView` has no camera to project
|
|
109
|
+
* through). Pointer-isolated, so grabbing a handle never pans the stage.
|
|
110
|
+
*/
|
|
111
|
+
function SelectionHandles({ color = "#2196f3", token = StageToken }) {
|
|
112
|
+
const host = useCapability(SelectionToken$1);
|
|
113
|
+
const stage = useOptionalCapability(token);
|
|
114
|
+
const selecting = useSelector(SelectionToken, (c) => c.isSelecting());
|
|
115
|
+
const visible = useSelector(SelectionToken$1, (c) => c.highlightVisible());
|
|
116
|
+
const endpoints = useSelector(SelectionToken$1, (c) => {
|
|
117
|
+
const s = c.snapshot();
|
|
118
|
+
if (!s.start || !s.end) return null;
|
|
119
|
+
return {
|
|
120
|
+
start: {
|
|
121
|
+
pon: s.start.pon,
|
|
122
|
+
glyphQuad: s.start.glyphQuad,
|
|
123
|
+
advance: s.start.advance
|
|
124
|
+
},
|
|
125
|
+
end: {
|
|
126
|
+
pon: s.end.pon,
|
|
127
|
+
glyphQuad: s.end.glyphQuad,
|
|
128
|
+
advance: s.end.advance
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}, sameEndpoints);
|
|
132
|
+
useKernelValue(() => stage?.visiblePages() ?? null);
|
|
133
|
+
const [dragging, setDragging] = useState(null);
|
|
134
|
+
const armSource = useRef(null);
|
|
135
|
+
armSource.current = stage && endpoints ? {
|
|
136
|
+
stage,
|
|
137
|
+
endpoints
|
|
138
|
+
} : null;
|
|
139
|
+
const bindHandle = useMemo(() => {
|
|
140
|
+
const detach = {};
|
|
141
|
+
const make = (role) => (el) => {
|
|
142
|
+
detach[role]?.();
|
|
143
|
+
delete detach[role];
|
|
144
|
+
if (!el) return;
|
|
145
|
+
detach[role] = attachSelectionHandle(el, { arm: () => {
|
|
146
|
+
const src = armSource.current;
|
|
147
|
+
if (!src) return null;
|
|
148
|
+
const view = handleView(src.stage);
|
|
149
|
+
const geom = selectionHandleGeom(view, src.endpoints[role], role);
|
|
150
|
+
if (!geom) return null;
|
|
151
|
+
const opposite = src.endpoints[role === "start" ? "end" : "start"];
|
|
152
|
+
const drag = createSelectionHandleDrag(host, view, opposite, src.endpoints[role].pon);
|
|
153
|
+
setDragging(role);
|
|
154
|
+
return {
|
|
155
|
+
base: {
|
|
156
|
+
x: (geom.bar.from.x + geom.bar.to.x) / 2,
|
|
157
|
+
y: (geom.bar.from.y + geom.bar.to.y) / 2
|
|
158
|
+
},
|
|
159
|
+
session: {
|
|
160
|
+
move: drag.move,
|
|
161
|
+
end: () => {
|
|
162
|
+
drag.end();
|
|
163
|
+
setDragging(null);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
} });
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
start: make("start"),
|
|
171
|
+
end: make("end")
|
|
172
|
+
};
|
|
173
|
+
}, [host]);
|
|
174
|
+
if (!stage || !endpoints || !visible) return null;
|
|
175
|
+
if (selecting && !dragging) return null;
|
|
176
|
+
const view = handleView(stage);
|
|
177
|
+
const renderHandle = (role) => {
|
|
178
|
+
const geom = selectionHandleGeom(view, endpoints[role], role);
|
|
179
|
+
if (!geom) return null;
|
|
180
|
+
const barTop = HANDLE_PAD + (role === "start" ? HANDLE_HEAD : 0);
|
|
181
|
+
const pivotX = HANDLE_PAD + HANDLE_BAR / 2;
|
|
182
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
183
|
+
ref: bindHandle[role],
|
|
184
|
+
style: {
|
|
185
|
+
position: "absolute",
|
|
186
|
+
left: geom.bar.from.x - pivotX,
|
|
187
|
+
top: geom.bar.from.y - barTop,
|
|
188
|
+
width: HANDLE_BAR + 2 * HANDLE_PAD,
|
|
189
|
+
height: geom.length + HANDLE_HEAD + 2 * HANDLE_PAD,
|
|
190
|
+
...geom.upright ? null : {
|
|
191
|
+
transform: `rotate(${geom.rotation}deg)`,
|
|
192
|
+
transformOrigin: `${pivotX}px ${barTop}px`
|
|
193
|
+
},
|
|
194
|
+
touchAction: "none",
|
|
195
|
+
cursor: "grab",
|
|
196
|
+
pointerEvents: "auto"
|
|
197
|
+
},
|
|
198
|
+
children: [/* @__PURE__ */ jsx("div", { style: {
|
|
199
|
+
position: "absolute",
|
|
200
|
+
left: HANDLE_PAD,
|
|
201
|
+
top: barTop,
|
|
202
|
+
width: HANDLE_BAR,
|
|
203
|
+
height: geom.length,
|
|
204
|
+
background: color,
|
|
205
|
+
borderRadius: HANDLE_BAR / 2
|
|
206
|
+
} }), /* @__PURE__ */ jsx("div", { style: {
|
|
207
|
+
position: "absolute",
|
|
208
|
+
left: pivotX - HANDLE_HEAD / 2,
|
|
209
|
+
top: role === "start" ? HANDLE_PAD : HANDLE_PAD + geom.length,
|
|
210
|
+
width: HANDLE_HEAD,
|
|
211
|
+
height: HANDLE_HEAD,
|
|
212
|
+
borderRadius: "50%",
|
|
213
|
+
background: color,
|
|
214
|
+
boxShadow: "0 1px 4px rgba(0, 0, 0, 0.35)"
|
|
215
|
+
} })]
|
|
216
|
+
}, role);
|
|
217
|
+
};
|
|
218
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [renderHandle("start"), renderHandle("end")] });
|
|
219
|
+
}
|
|
77
220
|
/**
|
|
78
221
|
* Mount ONCE per viewer to wire clipboard copy: prefetches the selected text
|
|
79
222
|
* when the selection settles, answers the native `copy` event synchronously,
|
|
@@ -87,6 +230,6 @@ function SelectionClipboard({ prefetch } = {}) {
|
|
|
87
230
|
return null;
|
|
88
231
|
}
|
|
89
232
|
//#endregion
|
|
90
|
-
export { SelectionClipboard, SelectionLayer, SelectionMenu, copySelection, useSelection };
|
|
233
|
+
export { SelectionClipboard, SelectionHandles, SelectionLayer, SelectionMenu, copySelection, useSelection };
|
|
91
234
|
|
|
92
235
|
//# sourceMappingURL=selection.js.map
|
package/dist/selection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selection.js","names":["SelectionHostToken"],"sources":["../src/selection.tsx"],"sourcesContent":["/**\n * The React surface for @embedpdf/plugin-selection.\n *\n * <SelectionLayer> is a dumb renderer: it warms the page's geometry on mount,\n * reads the content-space highlight rects from the capability, and paints them —\n * mapping each rect through PageContext.pageToContent (the same path markers use).\n * Zero pointer handling here; that's the PagePointerSource + the hub.\n *\n * The layer resolves the HOST lens (`/internal`: geometry warming, the\n * highlight handshake) — the adapter is exactly what that entry exists for.\n * `useSelection()` hands app code the PUBLIC lens only.\n */\n\n// One-line-per-feature: registration travels with the UI.\nexport * from '@embedpdf/plugin-selection';\n// The clipboard side effect lives in @embedpdf/web (the plugin is DOM-free);\n// re-exported here so app code has one import for the whole feature.\nexport { copySelection } from '@embedpdf/web';\nimport * as React from 'react';\nimport { useEffect } from 'react';\nimport { SelectionToken, type SelectionMenuAnchor } from '@embedpdf/plugin-selection';\nimport { SelectionToken as SelectionHostToken } from '@embedpdf/plugin-selection/internal';\nimport { wireSelectionClipboard, type SelectionClipboardOptions } from '@embedpdf/web';\nimport { Anchored, type AnchoredPlacement } from './anchored';\nimport { shallowArray, useCapability, usePage, useSelector } from './runtime';\n\nexport interface SelectionLayerProps {\n /** Highlight colour (default: translucent blue). */\n color?: string;\n}\n\nexport function SelectionLayer({ color = 'rgba(33, 150, 243, 0.35)' }: SelectionLayerProps) {\n const page = usePage();\n const selection = useCapability(SelectionHostToken);\n const segments = useSelector(\n SelectionHostToken,\n (c) => c.segmentsForPage(page.pon),\n shallowArray,\n );\n // A consumer (e.g. a markup tool drawing its own preview) can take over the\n // selection visual; when it does, we render nothing so the two never overlap.\n const visible = useSelector(SelectionHostToken, (c) => c.highlightVisible());\n\n // Warm this page's text geometry as soon as it's on screen, so the first\n // pointer-down can hit-test without waiting on the engine round-trip.\n // (A no-op without doc.text.select — nothing warms, nothing renders.)\n useEffect(() => {\n selection.ensurePage(page.pon);\n }, [selection, page.pon]);\n\n if (!visible) return null;\n\n return (\n <svg\n style={{\n position: 'absolute',\n inset: 0,\n width: '100%',\n height: '100%',\n overflow: 'visible',\n pointerEvents: 'none',\n }}\n >\n {segments.map((s, i) => {\n // content space → un-rotated content view px (rides the page's CSS\n // rotation). An affine map, so mapping the four corners is exact —\n // upright segments render pixel-identical to the old div-per-rect.\n const ring = [s.quad.upperStart, s.quad.upperEnd, s.quad.lowerEnd, s.quad.lowerStart].map(\n (p) => page.transform.pageToContent(p),\n );\n return (\n <polygon key={i} points={ring.map((p) => `${p.x},${p.y}`).join(' ')} fill={color} />\n );\n })}\n </svg>\n );\n}\n\n/** The PUBLIC selection capability (select(), readText(), canCopy(), …) for\n * app chrome — toolbars, context menus, automation. */\nexport function useSelection() {\n return useCapability(SelectionToken);\n}\n\n/** Structural equality for the selection's menu anchor — keeps the menu from\n * re-rendering on unrelated dispatches (the capability returns a fresh\n * object each call). */\nconst sameMenuAnchor = (\n a: SelectionMenuAnchor | null,\n b: SelectionMenuAnchor | null,\n): boolean => {\n if (a === b) return true;\n if (!a || !b) return false;\n return (\n a.pon === b.pon &&\n a.bounds.x === b.bounds.x &&\n a.bounds.y === b.bounds.y &&\n a.bounds.width === b.bounds.width &&\n a.bounds.height === b.bounds.height\n );\n};\n\nexport interface SelectionMenuProps {\n children: React.ReactNode;\n /** Gap in screen px between the selection and the menu (default 8). */\n gap?: number;\n /** Where to place the menu relative to the selection. Default 'top'. */\n placement?: AnchoredPlacement;\n}\n\n/**\n * Floats over the current TEXT selection (one anchor regardless of\n * cross-page selection; it rides the gesture's end page) — and only once the\n * selection SETTLES: hidden while `isSelecting()` (mid-drag), it appears at\n * pointer-up; programmatic selections show immediately (born settled). Works\n * under `<Stage>` (mount in the overlay slot) and `<PageView>` alike — the\n * surface provides the projection. Compose the contents from hooks:\n * `useSelection()` for copy/clear, `useAnnotation()` for\n * `markupFromSelection('highlight')`, `copySelection` for the clipboard.\n * For live-follow UI during the drag, compose `<Anchored>` with\n * `menuAnchor()` yourself — the primitive carries no policy.\n */\nexport function SelectionMenu({ children, gap = 8, placement = 'top' }: SelectionMenuProps) {\n const selecting = useSelector(SelectionToken, (c) => c.isSelecting());\n const anchor = useSelector(SelectionToken, (c) => c.menuAnchor(), sameMenuAnchor);\n if (selecting || !anchor) return null;\n return (\n <Anchored anchor={anchor} placement={placement} gap={gap}>\n {children}\n </Anchored>\n );\n}\n\nexport type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;\n\n/**\n * Mount ONCE per viewer to wire clipboard copy: prefetches the selected text\n * when the selection settles, answers the native `copy` event synchronously,\n * and falls back to the async Clipboard API for ctrl/cmd+C when the page has\n * no DOM selection. Renders nothing. For a toolbar Copy button, call\n * `copySelection(useSelection())` from its click handler instead.\n */\nexport function SelectionClipboard({ prefetch }: SelectionClipboardProps = {}) {\n const selection = useCapability(SelectionToken);\n useEffect(\n () => wireSelectionClipboard(selection, prefetch === undefined ? {} : { prefetch }),\n [selection, prefetch],\n );\n return null;\n}\n"],"mappings":";;;;;;;;;AA+BA,SAAgB,eAAe,EAAE,QAAQ,8BAAmD;CAC1F,MAAM,OAAO,QAAQ;CACrB,MAAM,YAAY,cAAcA,gBAAkB;CAClD,MAAM,WAAW,YACfA,mBACC,MAAM,EAAE,gBAAgB,KAAK,GAAG,GACjC,YACF;CAGA,MAAM,UAAU,YAAYA,mBAAqB,MAAM,EAAE,iBAAiB,CAAC;CAK3E,gBAAgB;EACd,UAAU,WAAW,KAAK,GAAG;CAC/B,GAAG,CAAC,WAAW,KAAK,GAAG,CAAC;CAExB,IAAI,CAAC,SAAS,OAAO;CAErB,OACE,oBAAC,OAAD;EACE,OAAO;GACL,UAAU;GACV,OAAO;GACP,OAAO;GACP,QAAQ;GACR,UAAU;GACV,eAAe;EACjB;EAEC,UAAA,SAAS,KAAK,GAAG,MAAM;GAOtB,OACE,oBAAC,WAAD;IAAiB,QAJN;KAAC,EAAE,KAAK;KAAY,EAAE,KAAK;KAAU,EAAE,KAAK;KAAU,EAAE,KAAK;IAAU,CAAC,CAAC,KACnF,MAAM,KAAK,UAAU,cAAc,CAAC,CAGT,CAAC,CAAC,KAAK,MAAM,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,KAAK,GAAG;IAAG,MAAM;GAAQ,GAArE,CAAqE;EAEvF,CAAC;CACE,CAAA;AAET;;;AAIA,SAAgB,eAAe;CAC7B,OAAO,cAAc,cAAc;AACrC;;;;AAKA,MAAM,kBACJ,GACA,MACY;CACZ,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,CAAC,KAAK,CAAC,GAAG,OAAO;CACrB,OACE,EAAE,QAAQ,EAAE,OACZ,EAAE,OAAO,MAAM,EAAE,OAAO,KACxB,EAAE,OAAO,MAAM,EAAE,OAAO,KACxB,EAAE,OAAO,UAAU,EAAE,OAAO,SAC5B,EAAE,OAAO,WAAW,EAAE,OAAO;AAEjC;;;;;;;;;;;;;AAsBA,SAAgB,cAAc,EAAE,UAAU,MAAM,GAAG,YAAY,SAA6B;CAC1F,MAAM,YAAY,YAAY,iBAAiB,MAAM,EAAE,YAAY,CAAC;CACpE,MAAM,SAAS,YAAY,iBAAiB,MAAM,EAAE,WAAW,GAAG,cAAc;CAChF,IAAI,aAAa,CAAC,QAAQ,OAAO;CACjC,OACE,oBAAC,UAAD;EAAkB;EAAmB;EAAgB;EAClD;CACO,CAAA;AAEd;;;;;;;;AAWA,SAAgB,mBAAmB,EAAE,aAAsC,CAAC,GAAG;CAC7E,MAAM,YAAY,cAAc,cAAc;CAC9C,gBACQ,uBAAuB,WAAW,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS,CAAC,GAClF,CAAC,WAAW,QAAQ,CACtB;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"selection.js","names":["SelectionHostToken"],"sources":["../src/selection.tsx"],"sourcesContent":["/**\n * The React surface for @embedpdf/plugin-selection.\n *\n * <SelectionLayer> is a dumb renderer: it warms the page's geometry on mount,\n * reads the content-space highlight rects from the capability, and paints them —\n * mapping each rect through PageContext.pageToContent (the same path markers use).\n * Zero pointer handling here; that's the PagePointerSource + the hub.\n *\n * The layer resolves the HOST lens (`/internal`: geometry warming, the\n * highlight handshake) — the adapter is exactly what that entry exists for.\n * `useSelection()` hands app code the PUBLIC lens only.\n */\n\n// One-line-per-feature: registration travels with the UI.\nexport * from '@embedpdf/plugin-selection';\n// The clipboard side effect lives in @embedpdf/web (the plugin is DOM-free);\n// re-exported here so app code has one import for the whole feature.\nexport { copySelection } from '@embedpdf/web';\nimport * as React from 'react';\nimport { useEffect, useMemo, useRef, useState } from 'react';\nimport { textQuadEquals } from '@embedpdf/core-geometry';\nimport type { CapabilityToken } from '@embedpdf/core';\nimport {\n HANDLE_BAR,\n HANDLE_HEAD,\n HANDLE_PAD,\n SelectionToken,\n createSelectionHandleDrag,\n selectionHandleGeom,\n type SelectionHandleEndpoint,\n type SelectionHandleView,\n type SelectionMenuAnchor,\n} from '@embedpdf/plugin-selection';\nimport { SelectionToken as SelectionHostToken } from '@embedpdf/plugin-selection/internal';\nimport { StageToken, type StageCapability } from '@embedpdf/plugin-stage';\nimport {\n attachSelectionHandle,\n wireSelectionClipboard,\n type SelectionClipboardOptions,\n} from '@embedpdf/web';\nimport { Anchored, type AnchoredPlacement } from './anchored';\nimport {\n shallowArray,\n useCapability,\n useKernelValue,\n useOptionalCapability,\n usePage,\n useSelector,\n} from './runtime';\n\nexport interface SelectionLayerProps {\n /** Highlight colour (default: translucent blue). */\n color?: string;\n}\n\nexport function SelectionLayer({ color = 'rgba(33, 150, 243, 0.35)' }: SelectionLayerProps) {\n const page = usePage();\n const selection = useCapability(SelectionHostToken);\n const segments = useSelector(\n SelectionHostToken,\n (c) => c.segmentsForPage(page.pon),\n shallowArray,\n );\n // A consumer (e.g. a markup tool drawing its own preview) can take over the\n // selection visual; when it does, we render nothing so the two never overlap.\n const visible = useSelector(SelectionHostToken, (c) => c.highlightVisible());\n\n // Warm this page's text geometry as soon as it's on screen, so the first\n // pointer-down can hit-test without waiting on the engine round-trip.\n // (A no-op without doc.text.select — nothing warms, nothing renders.)\n useEffect(() => {\n selection.ensurePage(page.pon);\n }, [selection, page.pon]);\n\n if (!visible) return null;\n\n return (\n <svg\n style={{\n position: 'absolute',\n inset: 0,\n width: '100%',\n height: '100%',\n overflow: 'visible',\n pointerEvents: 'none',\n }}\n >\n {segments.map((s, i) => {\n // content space → un-rotated content view px (rides the page's CSS\n // rotation). An affine map, so mapping the four corners is exact —\n // upright segments render pixel-identical to the old div-per-rect.\n const ring = [s.quad.upperStart, s.quad.upperEnd, s.quad.lowerEnd, s.quad.lowerStart].map(\n (p) => page.transform.pageToContent(p),\n );\n return (\n <polygon key={i} points={ring.map((p) => `${p.x},${p.y}`).join(' ')} fill={color} />\n );\n })}\n </svg>\n );\n}\n\n/** The PUBLIC selection capability (select(), readText(), canCopy(), …) for\n * app chrome — toolbars, context menus, automation. */\nexport function useSelection() {\n return useCapability(SelectionToken);\n}\n\n/** Structural equality for the selection's menu anchor — keeps the menu from\n * re-rendering on unrelated dispatches (the capability returns a fresh\n * object each call). */\nconst sameMenuAnchor = (\n a: SelectionMenuAnchor | null,\n b: SelectionMenuAnchor | null,\n): boolean => {\n if (a === b) return true;\n if (!a || !b) return false;\n return (\n a.pon === b.pon &&\n a.bounds.x === b.bounds.x &&\n a.bounds.y === b.bounds.y &&\n a.bounds.width === b.bounds.width &&\n a.bounds.height === b.bounds.height\n );\n};\n\nexport interface SelectionMenuProps {\n children: React.ReactNode;\n /** Gap in screen px between the selection and the menu (default 8). */\n gap?: number;\n /** Where to place the menu relative to the selection. Default 'top'. */\n placement?: AnchoredPlacement;\n}\n\n/**\n * Floats over the current TEXT selection (one anchor regardless of\n * cross-page selection; it rides the gesture's end page) — and only once the\n * selection SETTLES: hidden while `isSelecting()` (mid-drag), it appears at\n * pointer-up; programmatic selections show immediately (born settled). Works\n * under `<Stage>` (mount in the overlay slot) and `<PageView>` alike — the\n * surface provides the projection. Compose the contents from hooks:\n * `useSelection()` for copy/clear, `useAnnotation()` for\n * `markupFromSelection('highlight')`, `copySelection` for the clipboard.\n * For live-follow UI during the drag, compose `<Anchored>` with\n * `menuAnchor()` yourself — the primitive carries no policy.\n */\nexport function SelectionMenu({ children, gap = 8, placement = 'top' }: SelectionMenuProps) {\n const selecting = useSelector(SelectionToken, (c) => c.isSelecting());\n const anchor = useSelector(SelectionToken, (c) => c.menuAnchor(), sameMenuAnchor);\n if (selecting || !anchor) return null;\n return (\n <Anchored anchor={anchor} placement={placement} gap={gap}>\n {children}\n </Anchored>\n );\n}\n\n// ── selection handles (the touch affordance) ────────────────────────────────\n//\n// Policy lives out of this file, per the layering razor: the geometry and the\n// drag session are `@embedpdf/plugin-selection`'s pure `handles` module (one\n// source for every adapter), the native listener mechanics are\n// `@embedpdf/web`'s `attachSelectionHandle` (the down-shield timing subtlety\n// lives ONCE), and this component keeps what only React can do —\n// subscriptions, markup, theming.\n\ninterface Endpoints {\n start: SelectionHandleEndpoint;\n end: SelectionHandleEndpoint;\n}\nconst sameEndpoints = (a: Endpoints | null, b: Endpoints | null): boolean => {\n if (a === b) return true;\n if (!a || !b) return false;\n return (\n a.start.pon === b.start.pon &&\n a.end.pon === b.end.pon &&\n a.start.advance === b.start.advance &&\n a.end.advance === b.end.advance &&\n // corner-wise, so a boundary that ROTATES without moving its bounding box\n // still re-renders (an AABB comparison would call that \"unchanged\")\n textQuadEquals(a.start.glyphQuad, b.start.glyphQuad) &&\n textQuadEquals(a.end.glyphQuad, b.end.glyphQuad)\n );\n};\n\n/** The plugin's structural view over this Stage: point-exact projection in,\n * page resolution out. (`pageRectToScreen` is the AABB projector — upright\n * overlays only — and would collapse exactly the orientation handles need.) */\nconst handleView = (stage: StageCapability): SelectionHandleView => ({\n toOverlay: (pon, pt) => {\n const world = stage.pageToWorld(pon, pt);\n return world ? stage.toScreen(world) : null;\n },\n pageAt: (overlay) => stage.pageAt(overlay),\n pointOnPage: (pon, overlay) => stage.pointOnPage(pon, overlay),\n});\n\nexport interface SelectionHandlesProps {\n /** Handle colour (default: the selection blue). */\n color?: string;\n /** The stage lens hosting this overlay (default: the main StageToken). */\n token?: CapabilityToken<StageCapability>;\n}\n\n/**\n * iOS-style draggable selection handles. Each is drawn the way the platform\n * draws them: a thin caret BAR that forms the selection's own start/end edge\n * — the boundary glyph's oriented edge, so rotated text and rotated pages\n * carry the handle with them — capped by a screen-constant circle, above the\n * first line at the start, below the last line at the end.\n *\n * On touch, where a caret drag isn't available, the handles ARE the way to\n * grow or shrink a selection: a long-press selects a word, then each handle\n * extends from the OPPOSITE endpoint, snapping to glyphs and crossing pages\n * exactly like a pointer drag — it rides the same `beginAt`/`extendTo`\n * gesture path, so highlights, menus, and commit signals all behave\n * identically. Mount in the `<Stage>` overlay slot next to `<SelectionMenu>`;\n * outside a Stage it renders nothing (a `PageView` has no camera to project\n * through). Pointer-isolated, so grabbing a handle never pans the stage.\n */\nexport function SelectionHandles({ color = '#2196f3', token = StageToken }: SelectionHandlesProps) {\n const host = useCapability(SelectionHostToken);\n const stage = useOptionalCapability(token);\n const selecting = useSelector(SelectionToken, (c) => c.isSelecting());\n const visible = useSelector(SelectionHostToken, (c) => c.highlightVisible());\n const endpoints = useSelector(\n SelectionHostToken,\n (c): Endpoints | null => {\n const s = c.snapshot();\n if (!s.start || !s.end) return null;\n return {\n start: { pon: s.start.pon, glyphQuad: s.start.glyphQuad, advance: s.start.advance },\n end: { pon: s.end.pon, glyphQuad: s.end.glyphQuad, advance: s.end.advance },\n };\n },\n sameEndpoints,\n );\n // The handles are positioned by PROJECTING the endpoint corners through the\n // camera, so they must re-render whenever the camera moves — visiblePages is\n // the stage's reference-stable revision for exactly that (the same value the\n // page surfaces re-render on, so handle and highlight move in one commit).\n useKernelValue(() => stage?.visiblePages() ?? null);\n const [dragging, setDragging] = useState<'start' | 'end' | null>(null);\n // The web binder's `arm` must read the CURRENT endpoints/stage at pointer\n // down, not the ones captured when the listener attached — a stable ref\n // callback with a live arm-source is the standard escape from that.\n const armSource = useRef<{ stage: StageCapability; endpoints: Endpoints } | null>(null);\n armSource.current = stage && endpoints ? { stage, endpoints } : null;\n const bindHandle = useMemo(() => {\n const detach: Partial<Record<'start' | 'end', () => void>> = {};\n const make = (role: 'start' | 'end') => (el: HTMLDivElement | null) => {\n detach[role]?.();\n delete detach[role];\n if (!el) return;\n detach[role] = attachSelectionHandle(el, {\n arm: () => {\n const src = armSource.current;\n if (!src) return null;\n const view = handleView(src.stage);\n const geom = selectionHandleGeom(view, src.endpoints[role], role);\n if (!geom) return null;\n const opposite = src.endpoints[role === 'start' ? 'end' : 'start'];\n const drag = createSelectionHandleDrag(\n host,\n view,\n opposite,\n src.endpoints[role].pon,\n );\n setDragging(role);\n return {\n // the point the user grabbed: the bar's midpoint\n base: {\n x: (geom.bar.from.x + geom.bar.to.x) / 2,\n y: (geom.bar.from.y + geom.bar.to.y) / 2,\n },\n session: {\n move: drag.move,\n end: () => {\n drag.end(); // settle → menu reappears, onCommit fires\n setDragging(null);\n },\n },\n };\n },\n });\n };\n return { start: make('start'), end: make('end') };\n }, [host]);\n\n if (!stage || !endpoints || !visible) return null;\n // Hidden while a pointer drag-select is in flight (like the menu) — but a\n // HANDLE drag is itself a selection gesture, so it keeps its handles.\n if (selecting && !dragging) return null;\n const view = handleView(stage);\n\n const renderHandle = (role: 'start' | 'end') => {\n const geom = selectionHandleGeom(view, endpoints[role], role);\n if (!geom) return null; // endpoint page not laid out right now\n // The shell is laid out UPRIGHT in its own frame — bar of the edge's\n // length, head stacked above (start) or below (end) — then rotated onto\n // the projected edge. The pivot is the bar's ascent-side tip, the one\n // point that must land exactly on the glyph corner.\n const barTop = HANDLE_PAD + (role === 'start' ? HANDLE_HEAD : 0);\n const pivotX = HANDLE_PAD + HANDLE_BAR / 2;\n return (\n <div\n key={role}\n ref={bindHandle[role]}\n style={{\n position: 'absolute',\n left: geom.bar.from.x - pivotX,\n top: geom.bar.from.y - barTop,\n width: HANDLE_BAR + 2 * HANDLE_PAD,\n height: geom.length + HANDLE_HEAD + 2 * HANDLE_PAD,\n // Upright text carries NO transform — pixel-identical to an\n // axis-aligned box (the geometry's float-noise guard decides).\n ...(geom.upright\n ? null\n : {\n transform: `rotate(${geom.rotation}deg)`,\n transformOrigin: `${pivotX}px ${barTop}px`,\n }),\n touchAction: 'none',\n cursor: 'grab',\n pointerEvents: 'auto',\n }}\n >\n {/* the caret bar — the selection's own edge, spanning the glyph's ink\n height (which is why it stays the text's height at any tilt) */}\n <div\n style={{\n position: 'absolute',\n left: HANDLE_PAD,\n top: barTop,\n width: HANDLE_BAR,\n height: geom.length,\n background: color,\n borderRadius: HANDLE_BAR / 2,\n }}\n />\n {/* the head — flush against the bar, beyond the ascent at the start and\n past the baseline at the end, in the TEXT's frame */}\n <div\n style={{\n position: 'absolute',\n left: pivotX - HANDLE_HEAD / 2,\n top: role === 'start' ? HANDLE_PAD : HANDLE_PAD + geom.length,\n width: HANDLE_HEAD,\n height: HANDLE_HEAD,\n borderRadius: '50%',\n background: color,\n boxShadow: '0 1px 4px rgba(0, 0, 0, 0.35)',\n }}\n />\n </div>\n );\n };\n\n return (\n <>\n {renderHandle('start')}\n {renderHandle('end')}\n </>\n );\n}\n\n\nexport type SelectionClipboardProps = Pick<SelectionClipboardOptions, 'prefetch'>;\n\n/**\n * Mount ONCE per viewer to wire clipboard copy: prefetches the selected text\n * when the selection settles, answers the native `copy` event synchronously,\n * and falls back to the async Clipboard API for ctrl/cmd+C when the page has\n * no DOM selection. Renders nothing. For a toolbar Copy button, call\n * `copySelection(useSelection())` from its click handler instead.\n */\nexport function SelectionClipboard({ prefetch }: SelectionClipboardProps = {}) {\n const selection = useCapability(SelectionToken);\n useEffect(\n () => wireSelectionClipboard(selection, prefetch === undefined ? {} : { prefetch }),\n [selection, prefetch],\n );\n return null;\n}\n"],"mappings":";;;;;;;;;;;AAuDA,SAAgB,eAAe,EAAE,QAAQ,8BAAmD;CAC1F,MAAM,OAAO,QAAQ;CACrB,MAAM,YAAY,cAAcA,gBAAkB;CAClD,MAAM,WAAW,YACfA,mBACC,MAAM,EAAE,gBAAgB,KAAK,GAAG,GACjC,YACF;CAGA,MAAM,UAAU,YAAYA,mBAAqB,MAAM,EAAE,iBAAiB,CAAC;CAK3E,gBAAgB;EACd,UAAU,WAAW,KAAK,GAAG;CAC/B,GAAG,CAAC,WAAW,KAAK,GAAG,CAAC;CAExB,IAAI,CAAC,SAAS,OAAO;CAErB,OACE,oBAAC,OAAD;EACE,OAAO;GACL,UAAU;GACV,OAAO;GACP,OAAO;GACP,QAAQ;GACR,UAAU;GACV,eAAe;EACjB;EAEC,UAAA,SAAS,KAAK,GAAG,MAAM;GAOtB,OACE,oBAAC,WAAD;IAAiB,QAJN;KAAC,EAAE,KAAK;KAAY,EAAE,KAAK;KAAU,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AK,OAAO;IACzC;IACJ,aAAa;IACb,QAAQ;IACR,eAAe;GACjB;GApBF,UAAA,CAwBE,oBAAC,OAAD,EACE,OAAO;IACL,UAAU;IACV,MAAM;IACN,KAAK;IACL,OAAO;IACP,QAAQ,KAAK;IACb,YAAY;IACZ,cAAc,aAAa;GAC7B,EACD,CAAA,GAGD,oBAAC,OAAD,EACE,OAAO;IACL,UAAU;IACV,MAAM,SAAS,cAAc;IAC7B,KAAK,SAAS,UAAU,aAAa,aAAa,KAAK;IACvD,OAAO;IACP,QAAQ;IACR,cAAc;IACd,YAAY;IACZ,WAAW;GACb,EACD,CAAA,CACE;EAhDE,GAAA,IAgDF;CAET;CAEA,OACE,qBAAA,UAAA,EAAA,UAAA,CACG,aAAa,OAAO,GACpB,aAAa,KAAK,CACnB,EAAA,CAAA;AAEN;;;;;;;;AAYA,SAAgB,mBAAmB,EAAE,aAAsC,CAAC,GAAG;CAC7E,MAAM,YAAY,cAAc,cAAc;CAC9C,gBACQ,uBAAuB,WAAW,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS,CAAC,GAClF,CAAC,WAAW,QAAQ,CACtB;CACA,OAAO;AACT"}
|
package/dist/stage.cjs
CHANGED
|
@@ -2,10 +2,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_rolldown_runtime = require("./rolldown-runtime-DDXJien1.cjs");
|
|
3
3
|
const require_runtime = require("./runtime.cjs");
|
|
4
4
|
const require_anchored = require("./anchored.cjs");
|
|
5
|
-
const require_interaction = require("./interaction.cjs");
|
|
6
5
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
7
6
|
let react = require("react");
|
|
8
7
|
react = require_rolldown_runtime.__toESM(react, 1);
|
|
8
|
+
let _embedpdf_web = require("@embedpdf/web");
|
|
9
9
|
let _embedpdf_plugin_stage = require("@embedpdf/plugin-stage");
|
|
10
10
|
let _embedpdf_plugin_interaction = require("@embedpdf/plugin-interaction");
|
|
11
11
|
//#region src/stage.tsx
|
|
@@ -19,7 +19,7 @@ function PageSurface({ documentId, page, frame, stage, render, chrome }) {
|
|
|
19
19
|
const top = page.screenY - frame.top;
|
|
20
20
|
const contentLeft = frame.left + (t.viewWidth - t.contentWidth) / 2;
|
|
21
21
|
const contentTop = frame.top + (t.viewHeight - t.contentHeight) / 2;
|
|
22
|
-
const ctx = (0, react.useMemo)(() => require_runtime.makePageContext(documentId, page.pon, page.pageIndex, frame, t, () => ref.current.getBoundingClientRect(), () => {
|
|
22
|
+
const ctx = (0, react.useMemo)(() => require_runtime.makePageContext(documentId, stage.lensId(), page.pon, page.pageIndex, frame, t, () => ref.current.getBoundingClientRect(), () => {
|
|
23
23
|
const live = stage.visiblePages().find((p) => p.pon === page.pon);
|
|
24
24
|
return live ? {
|
|
25
25
|
desiredDeviceWidth: live.transform.deviceWidth,
|
|
@@ -58,7 +58,7 @@ function PageSurface({ documentId, page, frame, stage, render, chrome }) {
|
|
|
58
58
|
top: frame.top,
|
|
59
59
|
width: t.viewWidth,
|
|
60
60
|
height: t.viewHeight,
|
|
61
|
-
boxShadow: "0 6px 18px rgba(0,0,0,.18)"
|
|
61
|
+
boxShadow: "var(--epdf-page-shadow, 0 6px 18px rgba(0,0,0,.18))"
|
|
62
62
|
} }),
|
|
63
63
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
64
64
|
ref,
|
|
@@ -80,7 +80,7 @@ function PageSurface({ documentId, page, frame, stage, render, chrome }) {
|
|
|
80
80
|
})
|
|
81
81
|
});
|
|
82
82
|
}
|
|
83
|
-
function Stage({ children, pageChrome, overlay, interaction =
|
|
83
|
+
function Stage({ children, pageChrome, overlay, interaction = true, panFallback = true, zoomGestures = true, token = _embedpdf_plugin_stage.StageToken, className, style }) {
|
|
84
84
|
const stage = require_runtime.useCapability(token);
|
|
85
85
|
const ix = require_runtime.useOptionalCapability(_embedpdf_plugin_interaction.InteractionToken);
|
|
86
86
|
const useHub = interaction && !!ix;
|
|
@@ -119,153 +119,22 @@ function Stage({ children, pageChrome, overlay, interaction = false, zoomGesture
|
|
|
119
119
|
}), [projector, pages]);
|
|
120
120
|
(0, react.useEffect)(() => {
|
|
121
121
|
const el = ref.current;
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
const detachSurface = (0, _embedpdf_web.createStageSurface)(el, stage, {
|
|
123
|
+
hub: useHub ? ix : null,
|
|
124
|
+
source: stage.lensId(),
|
|
125
|
+
zoomGestures
|
|
125
126
|
});
|
|
126
|
-
const
|
|
127
|
-
ro.observe(el);
|
|
128
|
-
setVp();
|
|
129
|
-
let mq = null;
|
|
130
|
-
const reportDpr = () => {
|
|
131
|
-
stage.setDevicePixelRatio(window.devicePixelRatio || 1);
|
|
132
|
-
mq?.removeEventListener("change", reportDpr);
|
|
133
|
-
mq = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
|
|
134
|
-
mq.addEventListener("change", reportDpr);
|
|
135
|
-
};
|
|
136
|
-
reportDpr();
|
|
137
|
-
const onWheel = (e) => {
|
|
138
|
-
e.preventDefault();
|
|
139
|
-
const r = el.getBoundingClientRect();
|
|
140
|
-
if (zoomGestures && (e.ctrlKey || e.metaKey)) stage.zoomAround({
|
|
141
|
-
x: e.clientX - r.left,
|
|
142
|
-
y: e.clientY - r.top
|
|
143
|
-
}, (0, _embedpdf_plugin_stage.wheelZoomFactor)(e));
|
|
144
|
-
else {
|
|
145
|
-
const dx = e.shiftKey ? e.deltaY : e.deltaX;
|
|
146
|
-
const dy = e.shiftKey ? e.deltaX : e.deltaY;
|
|
147
|
-
stage.panBy(-dx, -dy);
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
el.addEventListener("wheel", onWheel, { passive: false });
|
|
151
|
-
let lastScale = 1;
|
|
152
|
-
const onGestureStart = (e) => {
|
|
153
|
-
e.preventDefault();
|
|
154
|
-
lastScale = e.scale ?? 1;
|
|
155
|
-
};
|
|
156
|
-
const onGestureChange = (e) => {
|
|
157
|
-
e.preventDefault();
|
|
158
|
-
const g = e;
|
|
159
|
-
const scale = g.scale ?? 1;
|
|
160
|
-
if (zoomGestures && scale > 0) {
|
|
161
|
-
const r = el.getBoundingClientRect();
|
|
162
|
-
stage.zoomAround({
|
|
163
|
-
x: g.clientX - r.left,
|
|
164
|
-
y: g.clientY - r.top
|
|
165
|
-
}, scale / lastScale);
|
|
166
|
-
}
|
|
167
|
-
lastScale = scale;
|
|
168
|
-
};
|
|
169
|
-
const hasGestureEvents = "GestureEvent" in window;
|
|
170
|
-
if (hasGestureEvents) {
|
|
171
|
-
el.addEventListener("gesturestart", onGestureStart);
|
|
172
|
-
el.addEventListener("gesturechange", onGestureChange);
|
|
173
|
-
el.addEventListener("gestureend", onGestureStart);
|
|
174
|
-
}
|
|
175
|
-
const cleanups = [];
|
|
176
|
-
if (useHub && ix) {
|
|
177
|
-
const clicks = require_interaction.createClickCounter();
|
|
178
|
-
const forward = (phase, e, clickCount = 1) => {
|
|
179
|
-
const r = el.getBoundingClientRect();
|
|
180
|
-
const vpt = {
|
|
181
|
-
x: e.clientX - r.left,
|
|
182
|
-
y: e.clientY - r.top
|
|
183
|
-
};
|
|
184
|
-
ix.dispatch({
|
|
185
|
-
phase,
|
|
186
|
-
viewport: vpt,
|
|
187
|
-
page: stage.pageAt(vpt) ?? void 0,
|
|
188
|
-
project: (pon) => stage.pointOnPage(pon, vpt),
|
|
189
|
-
modifiers: {
|
|
190
|
-
shift: e.shiftKey,
|
|
191
|
-
alt: e.altKey,
|
|
192
|
-
ctrl: e.ctrlKey,
|
|
193
|
-
meta: e.metaKey
|
|
194
|
-
},
|
|
195
|
-
clickCount
|
|
196
|
-
});
|
|
197
|
-
};
|
|
198
|
-
let dragging = false;
|
|
199
|
-
const down = (e) => {
|
|
200
|
-
if (e.button !== 0) return;
|
|
201
|
-
dragging = true;
|
|
202
|
-
forward("down", e, clicks(Date.now(), e.clientX, e.clientY));
|
|
203
|
-
};
|
|
204
|
-
const hover = (e) => {
|
|
205
|
-
if (!dragging) forward("move", e);
|
|
206
|
-
};
|
|
207
|
-
const winMove = (e) => {
|
|
208
|
-
if (dragging) forward("move", e);
|
|
209
|
-
};
|
|
210
|
-
const up = (e) => {
|
|
211
|
-
if (!dragging) return;
|
|
212
|
-
dragging = false;
|
|
213
|
-
forward("up", e);
|
|
214
|
-
};
|
|
215
|
-
el.addEventListener("pointerdown", down);
|
|
216
|
-
el.addEventListener("pointermove", hover);
|
|
217
|
-
window.addEventListener("pointermove", winMove);
|
|
218
|
-
window.addEventListener("pointerup", up);
|
|
219
|
-
cleanups.push(() => {
|
|
220
|
-
el.removeEventListener("pointerdown", down);
|
|
221
|
-
el.removeEventListener("pointermove", hover);
|
|
222
|
-
window.removeEventListener("pointermove", winMove);
|
|
223
|
-
window.removeEventListener("pointerup", up);
|
|
224
|
-
});
|
|
225
|
-
} else {
|
|
226
|
-
let dragging = false;
|
|
227
|
-
let lx = 0;
|
|
228
|
-
let ly = 0;
|
|
229
|
-
const down = (e) => {
|
|
230
|
-
if (e.button !== 0) return;
|
|
231
|
-
dragging = true;
|
|
232
|
-
lx = e.clientX;
|
|
233
|
-
ly = e.clientY;
|
|
234
|
-
};
|
|
235
|
-
const move = (e) => {
|
|
236
|
-
if (!dragging) return;
|
|
237
|
-
stage.panBy(e.clientX - lx, e.clientY - ly);
|
|
238
|
-
lx = e.clientX;
|
|
239
|
-
ly = e.clientY;
|
|
240
|
-
};
|
|
241
|
-
const up = () => {
|
|
242
|
-
dragging = false;
|
|
243
|
-
};
|
|
244
|
-
el.addEventListener("pointerdown", down);
|
|
245
|
-
window.addEventListener("pointermove", move);
|
|
246
|
-
window.addEventListener("pointerup", up);
|
|
247
|
-
cleanups.push(() => {
|
|
248
|
-
el.removeEventListener("pointerdown", down);
|
|
249
|
-
window.removeEventListener("pointermove", move);
|
|
250
|
-
window.removeEventListener("pointerup", up);
|
|
251
|
-
});
|
|
252
|
-
}
|
|
127
|
+
const offScroll = useHub && ix ? ix.registerHandler((0, _embedpdf_plugin_stage.createScrollHandler)(stage, ix, { panFallback }), { source: stage.lensId() }) : null;
|
|
253
128
|
return () => {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
el.removeEventListener("wheel", onWheel);
|
|
257
|
-
if (hasGestureEvents) {
|
|
258
|
-
el.removeEventListener("gesturestart", onGestureStart);
|
|
259
|
-
el.removeEventListener("gesturechange", onGestureChange);
|
|
260
|
-
el.removeEventListener("gestureend", onGestureStart);
|
|
261
|
-
}
|
|
262
|
-
cleanups.forEach((fn) => fn());
|
|
129
|
+
offScroll?.();
|
|
130
|
+
detachSurface();
|
|
263
131
|
};
|
|
264
132
|
}, [
|
|
265
133
|
stage,
|
|
266
134
|
ix,
|
|
267
135
|
useHub,
|
|
268
|
-
zoomGestures
|
|
136
|
+
zoomGestures,
|
|
137
|
+
panFallback
|
|
269
138
|
]);
|
|
270
139
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
271
140
|
ref,
|
|
@@ -284,7 +153,7 @@ function Stage({ children, pageChrome, overlay, interaction = false, zoomGesture
|
|
|
284
153
|
stage,
|
|
285
154
|
render: children,
|
|
286
155
|
chrome: pageChrome
|
|
287
|
-
}, p.
|
|
156
|
+
}, p.pon)), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_anchored.ProjectorProvider, {
|
|
288
157
|
value: projectorBinding,
|
|
289
158
|
children: overlay
|
|
290
159
|
})]
|