import { useEffect, useRef, useState } from 'react';
import { Viewer, DocumentGate } from '@embedpdf/react/runtime';
import type { OpenInput } from '@embedpdf/react/runtime';
import {
actionsPlugin,
useActions,
useActionsEvent,
useActionsSettings,
useActionsUiAdapter,
type ActionContext,
type ActionOrigin,
type ActionPolicyDecision,
type PdfActionTree,
} from '@embedpdf/react/actions';
import { localEngine } from '@embedpdf/engine';
import './rules.css';
const engine = localEngine();
// Websites open only on a click, as the page's example registers it.
const plugins = [
actionsPlugin({
policy: { uri: { user: 'adapter', hover: 'block', lifecycle: 'block' } },
}),
];
// [!doc-source links]
const links = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/links.pdf');
return { kind: 'bytes', id: 'links', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// [!/doc-source]
// A link to a website, as a PDF carries it: what readActionTree() reads from a link.
const websiteLink: PdfActionTree = {
root: {
type: 'uri',
subtype: 'URI',
uri: 'https://www.w3.org/TR/WCAG22/',
isMap: false,
next: [],
},
incomplete: false,
warnings: [],
};
// The three ways an action can start, each as the context it runs with.
const STARTS: ReadonlyArray<{ label: string; context: ActionContext }> = [
{
label: 'A click',
context: { origin: 'user', source: { kind: 'api' }, event: { type: 'activate' } },
},
{
label: 'The pointer over it',
context: {
origin: 'hover',
source: { kind: 'api' },
event: { type: 'annotation', event: 'cursorEnter' },
},
},
{
label: 'The document opening',
context: {
origin: 'lifecycle',
source: { kind: 'api' },
event: { type: 'document', event: 'open' },
},
},
];
const RULES: readonly ActionPolicyDecision[] = ['allow', 'adapter', 'report', 'block'];
// The rules for websites, changed while the app runs, and what happens when the link runs.
function WebsiteRules() {
const actions = useActions();
const rules = useActionsSettings((settings) => settings.policy.uri);
const [log, setLog] = useState([]);
const note = (line: string) => setLog((lines) => [line, ...lines].slice(0, 6));
// Instead of opening a tab, the adapter notes the website it was given.
useActionsUiAdapter({ openUri: (uri) => note(`Your adapter got ${uri}`) });
useActionsEvent(
(capability) => capability.onDiagnosticReported,
({ code, action }) => note(`The ${action} action was not run: ${code}`),
);
const run = (context: ActionContext) => actions.execute(websiteLink, context);
const setRule = (origin: ActionOrigin, rule: ActionPolicyDecision) =>
actions.updateSettings({ policy: { uri: { [origin]: rule } } });
// One click on load, so the log shows what a click does.
const clicked = useRef(false);
useEffect(() => {
if (clicked.current) return;
clicked.current = true;
void run(STARTS[0].context);
});
return (
{STARTS.map(({ label, context }) => (
-
{label}
{context.origin}
))}
{log.map((line, index) => (
- {line}
))}
);
}
export default function App() {
return (
Loading…
}>
);
}