import { useEffect, useRef, useState } from 'react'; import { Viewer, DocumentGate } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin, useStageState } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { interactionPlugin } from '@embedpdf/react/interaction'; import { SelectionLayer, selectionPlugin, useSelectionState } from '@embedpdf/react/selection'; import { searchPlugin } from '@embedpdf/react/search'; import { AnnotationLayer, annotationPlugin, useAnnotationState } from '@embedpdf/react/annotation'; import { redactionPlugin, useRedaction, useRedactionState } from '@embedpdf/react/redaction'; import { localEngine } from '@embedpdf/engine'; import './from-code.css'; const engine = localEngine(); // Marking matches runs a search; marking the selected text needs a selection. const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin(), searchPlugin(), annotationPlugin(), redactionPlugin(), ]; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; function MarkFromCode() { const redaction = useRedaction(); const { pendingCount } = useRedactionState(); const { hasSelection } = useSelectionState(); const currentPage = useStageState((state) => state.currentPageIndex); const ready = useAnnotationState((state) => state.status === 'ready'); const [text, setText] = useState('John Warnock'); const started = useRef(false); // On load: every "John Warnock" in the document. useEffect(() => { if (!ready || started.current) return; started.current = true; void redaction.markMatches({ text: 'John Warnock' }); }, [redaction, ready]); return (
{ event.preventDefault(); if (text.trim()) void redaction.markMatches({ text: text.trim() }); }} > setText(event.target.value)} />
{pendingCount} {pendingCount === 1 ? 'mark' : 'marks'}
); } export default function App() { return ( Loading…

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