import { useEffect, useState } from 'react'; import { Viewer, DocumentGate, pageRefsEqual } from '@embedpdf/react/runtime'; import type { OpenInput, PageContextValue, PageRef } from '@embedpdf/react/runtime'; import { Stage, stagePlugin } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { interactionPlugin, svgCursor, useInteraction, useInteractionState, } from '@embedpdf/react/interaction'; import { cloudEngine } from '@cloudpdf/engine'; import './pin.css'; const engine = cloudEngine(); const plugins = [stagePlugin(), renderPlugin(), interactionPlugin()]; const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' }; interface Pin { readonly id: number; readonly page: PageRef; readonly point: { readonly x: number; readonly y: number }; } // The cursor is the pin itself; its tip is the point that clicks. const PIN_CURSOR = svgCursor({ svg: ` `, hotspot: { x: 12, y: 23 }, fallback: 'copy', }); // The pins on one page, placed in its pixels at the last moment. function PinLayer({ page, pins }: { page: PageContextValue; pins: readonly Pin[] }) { return ( <> {pins .filter((pin) => pageRefsEqual(pin.page, page.ref)) .map((pin) => { const { x, y } = page.transform.toPixels(pin.point); return ( {pin.id} ); })} ); } function PinBoard() { const interaction = useInteraction(); const { activeToolId } = useInteractionState(); const [pins, setPins] = useState([]); // A tool of your own: a click on a page drops a pin there, in page coordinates. useEffect(() => { const remove = interaction.registerTool({ id: 'pin', cursor: PIN_CURSOR, onPointerDown: ({ page, point }) => { setPins((current) => [...current, { id: current.length + 1, page, point }]); return true; // this tool handled the click }, }); interaction.activateTool('pin'); return remove; }, [interaction]); return ( <>
{pins.length === 0 ? 'Click a page to drop a pin' : `${pins.length} pins`}
{(page) => ( <> )} ); } export default function App() { return ( Loading…

}>
); }