import { useEffect, useRef, useState } from 'react'; import { Viewer, DocumentGate } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin, useStage } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { interactionPlugin } from '@embedpdf/react/interaction'; import { AnnotationLayer, annotationPlugin, useAnnotation, useAnnotationSettings, useAnnotationState, type AnnotationLayerComponents, type HandleProps, type RotationHandleProps, } from '@embedpdf/react/annotation'; import { localEngine } from '@embedpdf/engine'; import './handles.css'; const engine = localEngine(); const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()]; const layout = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/layout.pdf'); return { kind: 'bytes', id: 'layout', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // On load: the document's square, selected, on its page. function SelectSquare() { const annotation = useAnnotation(); const stage = useStage(); const ready = useAnnotationState((state) => state.status === 'ready'); const done = useRef(false); useEffect(() => { if (!ready || done.current) return; done.current = true; const square = annotation.list().find((each) => each.nm === 'square'); if (!square) return; annotation.selection.select([square.ref]); stage.reveal(square.page, { rect: square.rect }); }, [annotation, stage, ready]); return null; } // Your own handles: the layer places them, and still decides where they can be grabbed. function Handle({ at, size, rotation, kind, active }: HandleProps) { return (
); } function RotationHandle({ at, size, active }: RotationHandleProps) { return (
↻
); } const OWN_HANDLES: AnnotationLayerComponents = { Handle, RotationHandle }; const ACCENTS = ['#054fb3', '#e91e63', '#0f6e56']; function ChromeControls({ own, onOwn }: { own: boolean; onOwn: (own: boolean) => void }) { const annotation = useAnnotation(); const chrome = useAnnotationSettings((settings) => settings.chrome); return (
{ACCENTS.map((accent) => (
); } export default function App() { const [own, setOwn] = useState(false); return ( Loading…

}> {() => ( <> )}
); }