/** * The React surface for @embedpdf/plugin-interaction. * * is the one pointer listener per page: it converts events to * page space via PageContext.toPagePoint and forwards normalized samples to the * hub. It binds only to the page context, so it works identically inside a * virtualized page and a standalone . Features never attach * their own pointer listeners: they register handlers with the hub, and a tool * of your own brings its pointer methods (`registerTool`). */ // One-line-per-feature: registration travels with the UI. export * from '@embedpdf/plugin-interaction'; // The tool commands, ready to spread, and the command behind `tool:` to build your own on. export { interactionCommands } from './command-lists'; export { toolCommand } from '@embedpdf/plugin-interaction/commands'; // The browser helpers live in @embedpdf/web (the plugin is DOM-free); they are // re-exported here so app code has one import for the feature. export { createClickCounter, svgCursor, vibrationFeedback, wkFeedback } from '@embedpdf/web'; export type { SvgCursorOptions, ToolCursorImage, ToolCursorSpec } from '@embedpdf/web'; import * as React from 'react'; import { useEffect, useRef } from 'react'; import { InteractionToken as InteractionHostToken } from '@embedpdf/plugin-interaction/contract/host'; import { InteractionToken, interactionState } from '@embedpdf/plugin-interaction'; import type { InteractionCapability } from '@embedpdf/plugin-interaction'; import type { EventHook } from '@embedpdf/core'; import { attachPagePointer, toolCursorsOf } from '@embedpdf/web'; import type { ToolCursorSpec } from '@embedpdf/web'; import { useCapability, useCapabilityEvent, useOptionalCapability, usePage, useSelector, } from './runtime'; import { settingsHook, stateHook } from './state'; export function PagePointerSource() { const page = usePage(); // The pointer source is a host of the hub (it dispatches samples). const interaction = useCapability(InteractionHostToken); const cursor = useSelector(InteractionHostToken, (interaction) => interaction.getCursor()); const ref = useRef(null); useEffect(() => { const element = ref.current; if (!element) return; // The listener is `@embedpdf/web`'s, shared with every framework. return attachPagePointer(element, interaction, () => page); }, [interaction, page]); // Sits on top as the page's event surface; visual layers below use pointerEvents:none. return
; } /** The interaction capability: switch tools, add your own, set their cursors. */ export function useInteraction(): InteractionCapability { return useCapability(InteractionToken); } /** Subscribe to one interaction event for the mounted lifetime: `useInteractionEvent((interaction) => interaction.onGestureStarted, handler)`. */ export function useInteractionEvent( select: (interaction: InteractionCapability) => EventHook, handler: (event: T) => void, ): void { useCapabilityEvent(InteractionToken, select, handler); } /** * The tools' state: the active tool's id and every tool you can switch to * (the page's State table, declared once in `interactionState`). Takes a * selector, and re-renders only when what it returns changes. Without a * document there is no active tool (`null`) and no tool. */ export const useInteractionState = stateHook(interactionState); /** The interaction settings (`defaultTool`, `tools`), with or without a document. Takes a selector. */ export const useInteractionSettings = settingsHook(InteractionToken); /** * Give a tool image cursors — the armed-tool indicator. The cursor is the only * zero-latency pointer-locked pixel the platform has, and the hub already * arbitrates it: unmapped hover claims beat it over annotations/text, page * gaps fall back to the tool's `gapCursor`, and other UI (menus, form fill * controls) carries its own CSS cursor — so nothing chases the pointer in * DOM. `null` installs nothing. A re-render with new content rebuilds the * cursor (live recolor from tool defaults); unmount restores the tool's * declared cursors. Without a document it installs nothing until one is ready. */ export function useToolCursor(spec: ToolCursorSpec | null): void { const interaction = useOptionalCapability(InteractionHostToken); // Key the effect by value: specs are built inline in render, and a // fresh-but-identical object must not thrash the skin. const key = spec && JSON.stringify(spec); const ref = useRef(spec); ref.current = spec; useEffect(() => { const cursorSpec = ref.current; if (!cursorSpec || !interaction) return; interaction.setToolCursor(cursorSpec.toolId, toolCursorsOf(cursorSpec.cursors)); return () => interaction.setToolCursor(cursorSpec.toolId, null); }, [interaction, key]); }