import { useEffect, useRef, useState } from 'react';
import { Viewer, DocumentGate } from '@embedpdf/react/runtime';
import type { OpenInput } from '@embedpdf/react/runtime';
import {
actionsPlugin,
useActions,
useActionsEvent,
useActionsUiAdapter,
type ActionContext,
type PdfActionTree,
} from '@embedpdf/react/actions';
import { localEngine } from '@embedpdf/engine';
import './javascript.css';
const engine = localEngine();
// JavaScript on, and two fields scripts often ask for about the user.
const plugins = [
actionsPlugin({
javascript: { enabled: true, identity: { name: 'Dana Smith', corporation: 'Acme' } },
}),
];
// [!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 JavaScript action, as a PDF carries it behind a button or a link.
const script = (code: string): PdfActionTree => ({
root: { type: 'javascript', subtype: 'JavaScript', script: code, next: [] },
incomplete: false,
warnings: [],
});
const click: ActionContext = {
origin: 'user',
source: { kind: 'api' },
event: { type: 'activate' },
};
const GREETING = "app.alert('Hello, ' + identity.name + ' from ' + identity.corporation + '!');";
// Edit the script and run it. Its alerts and its errors show below it.
function ScriptConsole() {
const actions = useActions();
const [code, setCode] = useState(GREETING);
const [output, setOutput] = useState>([]);
const show = (kind: 'alert' | 'error', text: string) =>
setOutput((lines) => [{ kind, text }, ...lines].slice(0, 5));
// A script's alert goes to your UI adapter: here, a line below the script.
useActionsUiAdapter({ alert: (message) => show('alert', message) });
useActionsEvent(
(capability) => capability.onScriptFailed,
({ error }) => show('error', error.message),
);
// The greeting runs once on load.
const greeted = useRef(false);
useEffect(() => {
if (greeted.current) return;
greeted.current = true;
void actions.execute(script(GREETING), click);
});
return (
);
}
export default function App() {
return (
Loading…
}>
);
}