import { useEffect, useRef, useState } from 'react';
import {
Viewer,
DocumentGate,
saveFile,
useDocument,
useDocuments,
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,
copySelection,
selectionPlugin,
useSelection,
useSelectionState,
} from '@embedpdf/react/selection';
import { SearchLayer, searchPlugin, useSearch } from '@embedpdf/react/search';
import { localEngine } from '@embedpdf/engine';
import './checks.css';
const engine = localEngine();
const plugins = [
stagePlugin(),
renderPlugin(),
interactionPlugin(),
selectionPlugin(),
searchPlugin(),
];
// [!doc-source welcome]
const welcome = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf');
return { kind: 'bytes', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// [!/doc-source]
// What each role may do, as permissions.
const roles = {
viewer: ['doc.open', 'doc.render', 'doc.text.select'],
reviewer: ['doc.open', 'doc.render', 'doc.text.select', 'doc.text.copy', 'doc.text.search'],
owner: [
'doc.open',
'doc.render',
'doc.text.select',
'doc.text.copy',
'doc.text.search',
'doc.download',
],
};
type Role = keyof typeof roles;
// A document keeps the permissions it opened with, so a new role opens it again.
function ReopenOnRoleChange({ role }: { role: Role }) {
const documents = useDocuments();
const { id } = useDocument();
const opened = useRef(role);
useEffect(() => {
if (role === opened.current || !id) return;
opened.current = role;
void documents.close(id).then(() => documents.open(welcome, { name: 'welcome.pdf' }));
}, [role, id, documents]);
return null;
}
// Each control shows only when its check says yes.
function Controls() {
const documents = useDocuments();
const search = useSearch();
const selection = useSelection();
const hasSelection = useSelectionState((state) => state.hasSelection);
const cover = usePageList()[0]?.ref;
const [text, setText] = useState('PDF');
const [copied, setCopied] = useState('');
// On load: a search, and some text selected, so every check has something to act on.
useEffect(() => {
if (search.canSearch()) void search.search({ text });
}, [search, text]);
useEffect(() => {
// [!doc-range welcome firstWord]
if (cover) selection.select({ page: cover, start: 28, count: 9 });
// [!/doc-range]
}, [selection, cover]);
const copy = async () => {
await copySelection(selection).catch(() => {}); // the browser may refuse the clipboard
setCopied(await selection.readText());
};
return (
<>
{search.canSearch() && (
setText(event.target.value)}
/>
)}
{selection.canCopy() && (
)}
{documents.canDownload() && (
)}
{copied ? `Copied: “${copied}”` : ' '}
>
);
}
export default function App() {
const [role, setRole] = useState('reviewer');
return (
Opening…
}>
{() => (
<>
>
)}
);
}