import { useEffect, 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 {
SearchLayer,
searchPlugin,
useSearch,
useSearchHits,
useSearchState,
} from '@embedpdf/react/search';
import { localEngine } from '@embedpdf/engine';
import './results-list.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), searchPlugin()];
// [!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]
function SearchBox() {
const search = useSearch();
const hitCount = useSearchState((state) => state.hitCount);
const [text, setText] = useState('PDF');
useEffect(() => {
void search.search({ text });
}, [search, text]);
return (
setText(event.target.value)}
/>
{hitCount} matches
);
}
// Every match with the words around it. Clicking one makes it the active match and scrolls to it.
function Results() {
const search = useSearch();
const hits = useSearchHits();
const activeHitIndex = useSearchState((state) => state.activeHitIndex);
return (
{hits.map((hit, index) => (
search.goToHit(hit)}
>
Page {hit.pageIndex + 1}
{/* A match has no snippet when the user may search but not copy text. */}
{hit.snippet && (
…{hit.snippet.before}
{hit.snippet.match}
{hit.snippet.after}…
)}
))}
);
}
export default function App() {
return (
Loading…
}>
{() => (
<>
>
)}
);
}