import { useEffect, useRef } from 'react';
import { Viewer, DocumentGate } from '@embedpdf/react/runtime';
import type { OpenInput } from '@embedpdf/react/runtime';
import { Stage, stagePlugin } from '@embedpdf/react/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/react/render';
import { interactionPlugin, useInteraction } from '@embedpdf/react/interaction';
import { AnnotationLayer, annotationPlugin, useAnnotation } from '@embedpdf/react/annotation';
import { FormLayer, formPlugin, toFieldRef, useForm, useFormState } from '@embedpdf/react/form';
import { localEngine } from '@embedpdf/engine';
import './field-settings.css';
const engine = localEngine();
const plugins = [
stagePlugin(),
renderPlugin(),
interactionPlugin(),
annotationPlugin(),
formPlugin(),
];
const forms = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/forms.pdf');
return { kind: 'bytes', id: 'forms', bytes: new Uint8Array(await response.arrayBuffer()) };
};
/** On load: design mode, with the name field selected. */
function useDesignForm() {
const form = useForm();
const annotation = useAnnotation();
const interaction = useInteraction();
const status = useFormState((state) => state.status);
const started = useRef(false);
useEffect(() => {
if (status !== 'ready' || started.current) return;
started.current = true;
interaction.activateTool('form-edit');
const widget = form.get(toFieldRef('name'))?.widgets[0]?.ref;
if (widget) annotation.selection.select([widget]);
}, [form, annotation, interaction, status]);
}
/** What a settings panel shows for the selected field, and changes with `update()`. */
function FieldSettings() {
useDesignForm();
const form = useForm();
const { selectedField: field } = useFormState();
if (!field) return Select a field on the page.
;
return (
);
}
export default function App() {
return (
Loading…
}>
);
}