import { useEffect } from 'react'; import { Viewer, DocumentGate, usePageList } 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 } from '@embedpdf/react/interaction'; import { SelectionLayer, selectionPlugin, useSelection, useSelectionState, } from '@embedpdf/react/selection'; import { localEngine } from '@embedpdf/engine'; import './programmatic.css'; const engine = localEngine(); const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin()]; // On the cover: the characters of its first word, "Documents", and a point on its word "web", // in page coordinates. // [!doc-range welcome firstWord] const FIRST_WORD = { start: 28, count: 9 }; // [!/doc-range] const POINT = { x: 260, y: 242 }; // [!doc-source welcome] 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()) }; }; // [!/doc-source] // Every button selects on the cover, the first page: by its ref, or by its index, 0. function SelectionToolbar() { const selection = useSelection(); const { hasSelection, range, pages } = useSelectionState(); const cover = usePageList()[0]?.ref; // The first word is selected on load. useEffect(() => { if (cover) selection.select({ page: cover, ...FIRST_WORD }); }, [selection, cover]); const canSelect = selection.canSelect(); let summary = 'Nothing selected'; if (pages.length > 1) summary = `On ${pages.length} pages`; else if (range) summary = `${range.end.index - range.start.index} characters`; return (
{summary}
); } export default function App() { return ( Loading…

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