import { useRef, useState } 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 { SelectionLayer, selectionPlugin } from '@embedpdf/react/selection';
import {
interactionPlugin,
useInteraction,
useInteractionState,
} from '@embedpdf/react/interaction';
import {
AnnotationLayer,
annotationPlugin,
useAnnotation,
useAnnotationEvent,
useAnnotationState,
} from '@embedpdf/react/annotation';
import { localEngine } from '@embedpdf/engine';
import './events.css';
const engine = localEngine();
const plugins = [
stagePlugin(),
renderPlugin(),
interactionPlugin(),
selectionPlugin(),
annotationPlugin(),
];
// [!doc-source report]
const report = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/report.pdf');
return { kind: 'bytes', id: 'report', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// [!/doc-source]
interface Entry {
id: number;
text: string;
origin: string;
}
// Every change, once the engine has saved it, newest first.
function ActivityLog() {
const [entries, setEntries] = useState([]);
const nextId = useRef(0);
const log = (text: string, origin: string) =>
setEntries((current) => [{ id: nextId.current++, text, origin }, ...current].slice(0, 30));
useAnnotationEvent(
(annotation) => annotation.onCreated,
({ annotation, origin }) =>
log(`${annotation.author ?? 'Someone'} added a ${annotation.subtype}`, origin.kind),
);
useAnnotationEvent(
(annotation) => annotation.onUpdated,
({ annotation, origin }) => log(`Changed a ${annotation.subtype}`, origin.kind),
);
useAnnotationEvent(
(annotation) => annotation.onDeleted,
({ refs, origin }) =>
log(
`Deleted ${refs.length === 1 ? 'one annotation' : `${refs.length} annotations`}`,
origin.kind,
),
);
useAnnotationEvent(
(annotation) => annotation.onReordered,
({ order, origin }) => log(`Restacked ${order.length} on a page`, origin.kind),
);
return (
{entries.length === 0 && - Nothing yet
}
{entries.map((entry) => (
-
{entry.text}
{entry.origin}
))}
);
}
const TOOLS = [
{ id: 'pointer', label: 'Select' },
{ id: 'square', label: 'Rectangle' },
{ id: 'ink', label: 'Pen' },
];
function Toolbar() {
const annotation = useAnnotation();
const interaction = useInteraction();
const { activeToolId } = useInteractionState();
const { selected } = useAnnotationState();
return (
{TOOLS.map((tool) => (
))}
);
}
export default function App() {
return (
Loading…
}>
);
}