import { useEffect, useRef } 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 { SelectionLayer, selectionPlugin } from '@embedpdf/react/selection';
import { interactionPlugin } from '@embedpdf/react/interaction';
import { AnnotationLayer, annotationPlugin } from '@embedpdf/react/annotation';
import {
stampPlugin,
useStamp,
useStampAssetPreviewUrl,
useStampAssets,
useStampLibraries,
useStampState,
} from '@embedpdf/react/stamp';
import type { StampAsset } from '@embedpdf/react/stamp';
import { loadDefaultLibrary } from '@embedpdf/default-stamps/library';
import { localEngine } from '@embedpdf/engine';
import './basic.css';
const engine = localEngine();
// [!asset-engine]
const assetEngine = engine; // stamp libraries are PDFs; they open here too
// [!/asset-engine]
const plugins = [
stagePlugin(),
renderPlugin(),
interactionPlugin(),
selectionPlugin(),
annotationPlugin(),
stampPlugin({ assetEngine }),
];
// [!doc-source approvals]
const approvals = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/approvals.pdf');
return { kind: 'bytes', id: 'approvals', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// [!/doc-source]
function StampButton({ asset, armed }: { asset: StampAsset; armed: boolean }) {
const stamp = useStamp();
const url = useStampAssetPreviewUrl(asset.id);
return (
);
}
function StampPicker() {
const stamp = useStamp();
const [library] = useStampLibraries();
const assets = useStampAssets({ libraryId: library?.id });
const { armedAsset } = useStampState(); // the stamp the next click places
const imported = useRef(false);
// On load: the standard stamps, English edition, with "Approved" armed.
useEffect(() => {
if (imported.current) return;
imported.current = true;
void loadDefaultLibrary('en')
.then((bytes) => stamp.importLibrary(bytes))
.then(({ library }) => {
const approved = stamp
.listAssets({ libraryId: library.id })
.find((asset) => asset.name === 'Approved');
if (approved) return stamp.armAsset(approved.id);
});
}, [stamp]);
if (!library) {
return (
);
}
return (
{assets.slice(0, 6).map((asset) => (
))}
);
}
export default function App() {
return (
Loading…
}>
{() => (
<>
>
)}
);
}