# @embedpdf/react — ./selection ArmedSelectionHandle [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface ArmedSelectionHandle{ base: Point; drag: SelectionHandleDragSession; } HANDLE_BAR [VariableDeclaration] (from packages/plugin/selection/src/handles.ts): HANDLE_BAR = 2 HANDLE_HEAD [VariableDeclaration] (from packages/plugin/selection/src/handles.ts): HANDLE_HEAD = 12 HANDLE_PAD [VariableDeclaration] (from packages/plugin/selection/src/handles.ts): HANDLE_PAD = 14 SELECTION_DEFAULTS [VariableDeclaration] (from packages/plugin/selection/src/contract.ts): SELECTION_DEFAULTS: SelectionSettings ={ dragThresholdPx: 4, color: null, handles:{ color: null, shadow: '0 1px 4px rgb(0 0 0 / 0.35)'} , } SelectionAnchor [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionAnchor{ readonly page: PageRef; readonly bounds: Rect; } SelectionCapability [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionCapability extends SettingsApi{ canSelect(): boolean; canCopy(): boolean; select(range: SelectionRangeInput): void; selectAll(): void; selectPage(page: PageRef | number): void; selectWordAt(page: PageRef | number, point: Point): boolean; selectLineAt(page: PageRef | number, point: Point): boolean; extendTo(page: PageRef | number, point: Point): void; clear(): void; hasSelection(): boolean; isSelecting(): boolean; getSnapshot(): SelectionSnapshot; getRange(): TextRange | null; listSelectedPages(): readonly PageRef[]; listSegments(page: PageRef | number): readonly SelectionSegment[]; listRects(page: PageRef | number): readonly Rect[]; getAnchor(): SelectionAnchor | null; readText(options?: OperationOptions): Promise; readTextInRange(range: TextRange, options?: OperationOptions): Promise; readonly onChanged: EventHook; readonly onCommitted: EventHook; readonly onCleared: EventHook; } SelectionChangedEvent [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionChangedEvent{ readonly range: TextRange | null; readonly pages: readonly PageRef[]; } SelectionClearedEvent [TypeAliasDeclaration] (from packages/plugin/selection/src/contract.ts): export type SelectionClearedEvent = Record; SelectionClipboard [FunctionDeclaration]: export function SelectionClipboard({ prefetch, onRefused, target} : SelectionClipboardProps ={ } ){ const selection = useOptionalCapability(SelectionToken); const refused = useRef(onRefused); refused.current = onRefused; useEffect(() =>{ const element = target ? target.current : undefined; if (!selection || element === null) return; return wireSelectionClipboard(selection, { ...(prefetch === undefined ?{ } :{ prefetch} ), ...(element ?{ target: element} :{ } ), onRefused: () => refused.current?.(), } ); } , [selection, prefetch, target]); return null; } SelectionClipboardProps [InterfaceDeclaration]: export interface SelectionClipboardProps extends Pick< SelectionClipboardOptions, 'prefetch' | 'onRefused' >{ target?: RefObject; } SelectionCommittedEvent [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionCommittedEvent{ readonly range: TextRange; } SelectionConfig [TypeAliasDeclaration] (from packages/plugin/selection/src/contract.ts): export type SelectionConfig = DeepPartial; SelectionEndpoint [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionEndpoint{ readonly page: PageRef; readonly glyphQuad: Quad; readonly advance: 1 | -1; readonly rect: Rect; } SelectionHandleDragSession [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleDragSession{ move(overlay: Point): void; end(): void; } SelectionHandleEndpoint [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleEndpoint{ page: PageRef; glyphQuad: Quad; advance: 1 | -1; } SelectionHandleEndpoints [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleEndpoints{ start: SelectionHandleEndpoint; end: SelectionHandleEndpoint; } SelectionHandleGeometry [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleGeometry{ bar:{ from: Point; to: Point} ; length: number; rotation: number; upright: boolean; head: Point; } SelectionHandleStage [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleStage{ pageToViewport(page: PageRef, point: Point): Point | null; getPageAt(point: Point):{ ref: PageRef; point: Point} | null; viewportToPage(page: PageRef, point: Point): Point | null; } SelectionHandleTarget [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleTarget{ beginGestureAt(page: PageRef, point: Point): boolean; extendTo(page: PageRef, point: Point): void; endGesture(): void; } SelectionHandleView [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleView{ toOverlay(page: PageRef, point: Point): Point | null; pageAt(overlay: Point):{ ref: PageRef; point: Point} | null; pointOnPage(page: PageRef, overlay: Point): Point | null; } SelectionHandles [FunctionDeclaration]: export function SelectionHandles({ token: explicitToken} : SelectionHandlesProps ={ } ){ const token = useStageToken(explicitToken); const host = useCapability(SelectionHostToken); const stage = useOptionalCapability(token); const surface = useOptionalProjectorBinding(); if (!surface || surface.projector.space !== 'overlay'){ devWarn( 'selection-handles-outside-stage', ' renders nothing here: mount it in the overlay slot ' + '(a has no camera to project the handles through).', ); } const selecting = useSelectionState((state) => state.isSelecting); const visible = useSelector(SelectionHostToken, (selection) => selection.isHighlightVisible()); const handles = useSelectionSettings((settings) => settings.handles); const accent = useViewerSettings((settings) => settings.accent); const endpoints = useSelector( SelectionHostToken, (selection) => selectionHandleEndpointsOf(selection.getSnapshot()), sameSelectionEndpoints, ); useKernelValue(() => stage?.listVisiblePages() ?? null); const [dragging, setDragging] = useState<'start' | 'end' | null>(null); const armSource = useRef<{ stage: StageCapability; endpoints: SelectionHandleEndpoints} | null>( null, ); armSource.current = stage && endpoints ?{ stage, endpoints} : null; const bindHandle = useMemo(() =>{ const detach: Partial void>> ={ } ; const make = (role: 'start' | 'end') => (element: HTMLDivElement | null) =>{ detach[role]?.(); delete detach[role]; if (!element) return; detach[role] = attachSelectionHandle(element, { arm: () =>{ const src = armSource.current; if (!src) return null; const armed = armSelectionHandle( host, selectionHandleViewOf(src.stage), src.endpoints, role, ); if (!armed) return null; setDragging(role); return{ base: armed.base, session:{ move: armed.drag.move, end: () =>{ armed.drag.end(); setDragging(null); } , } , } ; } , } ); } ; return{ start: make('start'), end: make('end')} ; } , [host]); if (!stage || !endpoints || !visible) return null; if (selecting && !dragging) return null; const view = selectionHandleViewOf(stage); const color = paint('text-selection-handle', handles.color ?? accent); const shadow = paint('text-selection-handle-shadow', handles.shadow); const renderHandle = (role: 'start' | 'end') =>{ const geometry = selectionHandleGeometry(view, endpoints[role], role); if (!geometry) return null; const barTop = HANDLE_PAD + (role === 'start' ? HANDLE_HEAD : 0); const pivotX = HANDLE_PAD + HANDLE_BAR / 2; return (
{ }
{ }
); } ; return ( <>{ renderHandle('start')} { renderHandle('end')} ); } SelectionHandlesProps [InterfaceDeclaration]: export interface SelectionHandlesProps{ token?: CapabilityToken; } SelectionLayer [FunctionDeclaration]: export function SelectionLayer(){ const page = usePage(); const selection = useCapability(SelectionHostToken); const segments = useSelector( SelectionHostToken, (selection) => selection.listSegments(page.ref), shallowArray, ); const visible = useSelector(SelectionHostToken, (selection) => selection.isHighlightVisible()); const color = useSelectionSettings((settings) => settings.color); const accent = useViewerSettings((settings) => settings.accent); useEffect(() =>{ void selection.ensureLoaded(page.ref); } , [selection, page.ref]); if (!visible) return null; const fill = paint('text-selection', color ?? mixAccent('text-selection', accent)); return ( { segments.map((segment, i) => ( ))} ); } SelectionMenu [FunctionDeclaration]: export function SelectionMenu({ children, gap = 8, placement = 'top'} : SelectionMenuProps){ const selecting = useSelectionState((state) => state.isSelecting); const anchor = useSelector(SelectionToken, (selection) => selection.getAnchor(), samePageBounds); if (selecting || !anchor) return null; return ( { children} ); } SelectionMenuProps [InterfaceDeclaration]: export interface SelectionMenuProps{ children: React.ReactNode; gap?: number; placement?: AnchoredPlacement; } SelectionRangeInput [TypeAliasDeclaration] (from packages/plugin/selection/src/contract.ts): export type SelectionRangeInput = TextRange |{ page: PageRef; start: number; count: number} ; SelectionSegment [TypeAliasDeclaration] (from packages/plugin/selection/src/geometry.ts): export type SelectionSegment = PdfTextSegment; SelectionSettings [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionSettings{ readonly dragThresholdPx: number; readonly color: string | null; readonly handles:{ readonly color: string | null; readonly shadow: string; } ; } SelectionSnapshot [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionSnapshot{ readonly pages: readonly{ page: PageRef; segments: readonly SelectionSegment[]} []; readonly start: SelectionEndpoint | null; readonly end: SelectionEndpoint | null; readonly direction: 'forward' | 'backward'; readonly range: TextRange | null; } SelectionState [InterfaceDeclaration] (from packages/plugin/selection/src/state.ts): export interface SelectionState{ readonly hasSelection: boolean; readonly isSelecting: boolean; readonly range: TextRange | null; readonly pages: readonly PageRef[]; } SelectionToken [VariableDeclaration] (from packages/plugin/selection/src/token.ts): SelectionToken = createCapabilityToken('selection', { hint: `add selectionPlugin() from '@embedpdf/plugin-selection' to your plugins list`, promises:{ readText: true, readTextInRange: true, } , } ) TextPosition [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface TextPosition{ readonly page: PageRef; readonly index: number; } TextRange [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface TextRange{ readonly start: TextPosition; readonly end: TextPosition; } armSelectionHandle [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function armSelectionHandle( selection: SelectionHandleTarget, view: SelectionHandleView, endpoints: SelectionHandleEndpoints, role: 'start' | 'end', ): ArmedSelectionHandle | null{ const geometry = selectionHandleGeometry(view, endpoints[role], role); if (!geometry) return null; const opposite = endpoints[role === 'start' ? 'end' : 'start']; return{ base: midpoint(geometry.bar.from, geometry.bar.to), drag: createSelectionHandleDrag(selection, view, opposite, endpoints[role].page), } ; } copySelection [FunctionDeclaration] (from packages/framework/web/src/clipboard.ts): export async function copySelection( selection: Pick, ): Promise{ const text = await selection.readText(); if (text !== '') await navigator.clipboard.writeText(text); return text; } createSelectionHandleDrag [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function createSelectionHandleDrag( selection: SelectionHandleTarget, view: SelectionHandleView, opposite: SelectionHandleEndpoint, draggedPage: PageRef, ): SelectionHandleDragSession{ const quad = opposite.glyphQuad; const anchorPoint = midpoint( midpoint(quad.upperLeft, quad.lowerRight), midpoint(quad.upperRight, quad.lowerLeft), ); let begun = false; let lastPage = draggedPage; return{ move: (overlay) =>{ if (!begun){ if (!selection.beginGestureAt(opposite.page, anchorPoint)) return; begun = true; } const hit = view.pageAt(overlay); if (hit){ lastPage = hit.ref; selection.extendTo(hit.ref, hit.point); } else{ const point = view.pointOnPage(lastPage, overlay); if (point) selection.extendTo(lastPage, point); } } , end: () =>{ if (begun) selection.endGesture(); } , } ; } selectionCommands [VariableDeclaration] (from packages/framework/react/src/command-lists.ts): selectionCommands = createSelectionCommands(standardCommandsBrowser) selectionHandleEndpointsOf [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function selectionHandleEndpointsOf( snapshot: Pick, ): SelectionHandleEndpoints | null{ const{ start, end} = snapshot; if (!start || !end) return null; return{ start:{ page: start.page, glyphQuad: start.glyphQuad, advance: start.advance} , end:{ page: end.page, glyphQuad: end.glyphQuad, advance: end.advance} , } ; } selectionHandleGeometry [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function selectionHandleGeometry( view: SelectionHandleView, endpoint: SelectionHandleEndpoint, role: 'start' | 'end', ): SelectionHandleGeometry | null{ const leading = role === 'start' ? endpoint.advance > 0 : endpoint.advance < 0; const [upperPage, lowerPage] = quadEdge(endpoint.glyphQuad, leading ? 'left' : 'right'); const upper = view.toOverlay(endpoint.page, upperPage); const lower = view.toOverlay(endpoint.page, lowerPage); if (!upper || !lower) return null; const dx = lower.x - upper.x; const dy = lower.y - upper.y; const length = Math.hypot(dx, dy); if (length === 0) return null; const rotation = (Math.atan2(dy, dx) * 180) / Math.PI - 90; const upright = Math.abs(rotation) < HANDLE_ROT_EPSILON; const ux = dx / length; const uy = dy / length; const head = role === 'start' ?{ x: upper.x - (ux * HANDLE_HEAD) / 2, y: upper.y - (uy * HANDLE_HEAD) / 2} :{ x: lower.x + (ux * HANDLE_HEAD) / 2, y: lower.y + (uy * HANDLE_HEAD) / 2} ; return{ bar:{ from: upper, to: lower} , length, rotation, upright, head} ; } selectionHandleViewOf [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function selectionHandleViewOf(stage: SelectionHandleStage): SelectionHandleView{ return{ toOverlay: (page, point) => stage.pageToViewport(page, point), pageAt: (overlay) => stage.getPageAt(overlay), pointOnPage: (page, overlay) => stage.viewportToPage(page, overlay), } ; } selectionPlugin [VariableDeclaration] (from packages/plugin/selection/src/selection.plugin.ts): selectionPlugin = (config?: SelectionConfig) => definePlugin({ id: 'selection', token: SelectionToken, scope: 'document', requires: [InteractionToken], optional: [FeedbackToken], state: initialSelectionModel, settings:{ defaults: SELECTION_DEFAULTS, registered: config} , create: createSelectionController, } ) selectionState [VariableDeclaration] (from packages/plugin/selection/src/state.ts): selectionState = defineState(SelectionToken, { read: (selection): SelectionState => ({ hasSelection: selection.hasSelection(), isSelecting: selection.isSelecting(), range: selection.getRange(), pages: selection.listSelectedPages(), } ), empty:{ hasSelection: false, isSelecting: false, range: null, pages: [], } , } ) useSelection [FunctionDeclaration]: export function useSelection(){ return useCapability(SelectionToken); } useSelectionEvent [FunctionDeclaration]: export function useSelectionEvent( select: (selection: SelectionCapability) => EventHook, handler: (event: T) => void, ): void{ useCapabilityEvent(SelectionToken, select, handler); } useSelectionSettings [VariableDeclaration]: useSelectionSettings = settingsHook(SelectionToken) useSelectionState [VariableDeclaration]: useSelectionState = stateHook(selectionState)