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 { SearchLayer, searchPlugin, useSearch, useSearchHits } from '@embedpdf/react/search';
import { shellPlugin, useShell, useSurface } from '@embedpdf/react/shell';
import { localEngine } from '@embedpdf/engine';
import './props.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), searchPlugin(), shellPlugin()];
// [!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]
// The panel shows the match its props name, and steps to the next one without reopening.
function MatchPanel() {
const panel = useSurface('match');
const shell = useShell();
const search = useSearch();
const hits = useSearchHits();
const index = typeof panel.props.index === 'number' ? panel.props.index : 0;
const hit = hits[index];
if (!panel.isOpen) return null;
const show = (next: number) => {
shell.updateSurfaceProps('match', { index: next });
search.goToHit(hits[next]);
};
return (
);
}
function Workspace() {
const shell = useShell();
const search = useSearch();
const hits = useSearchHits();
// Every "PDF" on the pages, and the first one open in the panel.
useEffect(() => {
void search.search({ text: 'PDF' });
shell.open('match', { exclusive: 'right', props: { index: 0 } });
}, [search, shell]);
return (
{() => (
<>
{/* A click on a match opens it in the panel. */}
shell.open('match', { exclusive: 'right', props: { index: hits.indexOf(hit) } })
}
/>
>
)}
);
}
export default function App() {
return (
Loading…