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 { cloudEngine } from '@cloudpdf/engine';
import './javascript.css';
const engine = cloudEngine();
// JavaScript on, and two fields scripts often ask for about the user.
const plugins = [
actionsPlugin({
javascript: { enabled: true, identity: { name: 'Dana Smith', corporation: 'Acme' } },
}),
];
const links: OpenInput = { kind: 'grant', grant: 'shr_xs-9Y5qwuYrJ2BKtWzuo_2xy' };
// 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