import { useEffect } 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, useInteractionState, } from '@embedpdf/react/interaction'; import { AnnotationLayer, annotationPlugin, useAnnotation } from '@embedpdf/react/annotation'; import { localEngine } from '@embedpdf/engine'; import './custom-tools.css'; const engine = localEngine(); // A blue pen, an arrow, and three tools of your own. `meta` is yours: the toolbar reads its label. const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin({ tools: [ { id: 'ink', defaults: { color: '#1e90ff', strokeWidth: 3 }, meta: { label: 'Blue pen' } }, { id: 'arrow', extends: 'line', defaults: { lineEndings: { start: 'none', end: 'closed-arrow' } }, meta: { label: 'Arrow' }, }, { id: 'red-pen', extends: 'ink', defaults: { color: '#ff0000', strokeWidth: 2 }, meta: { label: 'Red pen' }, }, { id: 'marker', extends: 'ink-highlight', defaults: { color: '#ffa500' }, meta: { label: 'Marker' }, }, { id: 'todo', extends: 'note', defaults: { icon: 'key', contents: 'TODO' }, meta: { label: 'To do' }, }, ], }), ]; // [!doc-source sketch] const sketch = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/sketch.pdf'); return { kind: 'bytes', id: 'sketch', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // A toolbar that builds itself from the tools: every tool with a label gets a button. function Toolbar() { const annotation = useAnnotation(); const interaction = useInteraction(); const { activeToolId } = useInteractionState(); const labelled = annotation.tools.list().filter((tool) => typeof tool.meta?.label === 'string'); // On load: the arrow tool is active. useEffect(() => { interaction.activateTool('arrow'); }, [interaction]); return (
{labelled.map((tool) => ( ))}
); } export default function App() { return ( Loading…

}> {() => ( <> )}
); }