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 { localEngine } from '@embedpdf/engine';
import './menu.css';
const engine = localEngine();
const plugins = [
stagePlugin(),
renderPlugin(),
interactionPlugin(),
selectionPlugin(),
annotationPlugin(),
];
// [!doc-source notice]
const notice = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/notice.pdf');
return { kind: 'bytes', id: 'notice', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// [!/doc-source]
// 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 (
{allText && (
)}
{allText && selected.length === 1 && first && (
)}
);
}
export default function App() {
return (
Loading…
}>
}
>
{() => (
<>
>
)}
);
}