import { useEffect, useRef, useState } from 'react'; import { Viewer, DocumentGate, usePageList } 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, useAnnotationState, } from '@embedpdf/react/annotation'; import { cloudEngine } from '@cloudpdf/engine'; import './copy.css'; const engine = cloudEngine(); const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()]; const layout: OpenInput = { kind: 'grant', grant: 'shr_d2E_LaTuddJbZR00d1CvkqLM' }; // On load: the document's square, selected, ready to copy. 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; } // duplicate() copies the selected annotation, here onto the next page. function CopyButton() { const annotation = useAnnotation(); const stage = useStage(); const pages = usePageList(); const { selected } = useAnnotationState(); const [status, setStatus] = useState(''); const [first] = selected; const index = first ? pages.findIndex((page) => page.ref.objectNumber === first.page.objectNumber) : -1; const next = pages[index + 1]; const copyToNextPage = async () => { if (!first || !next) return; const { annotations } = await annotation.duplicate([first.ref], { page: next.ref, select: true, // so the next click copies the copy, a page further }); const [made] = annotations; if (made) stage.reveal(next.ref, { rect: made.rect }); setStatus(`Copied to page ${next.index + 1}`); }; return (