import { useEffect, useRef } 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 { SelectionLayer, selectionPlugin } from '@embedpdf/react/selection'; import { interactionPlugin } from '@embedpdf/react/interaction'; import { AnnotationLayer, AnnotationMenu, annotationPlugin, useAnnotation, useAnnotationState, } from '@embedpdf/react/annotation'; import { cloudEngine } from '@cloudpdf/engine'; import './menu.css'; const engine = cloudEngine(); const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin(), annotationPlugin(), ]; const notice: OpenInput = { kind: 'grant', grant: 'shr_LfeeUer1ogBI9mz8eYkWXa0d' }; // On load: the document's text box, selected, so its menu shows. function SelectTextBox() { 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 textBox = annotation.list().find((each) => each.nm === 'text-box'); if (!textBox) return; annotation.selection.select([textBox.ref]); stage.reveal(textBox.page, { rect: textBox.rect }); }, [annotation, stage, ready]); return null; } // What's in the menu depends on what's selected: text boxes get Bold and Edit. function SelectionActions() { const annotation = useAnnotation(); const { selected } = useAnnotationState(); const allText = selected.every((a) => a.subtype === 'free-text'); const [first] = selected; return (