import { useRef, useState } 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 { interactionPlugin } from '@embedpdf/react/interaction';
import {
commandsPlugin,
formatShortcut,
standardCommands,
useCommand,
useCommandShortcuts,
useCommands,
useCommandsEvent,
} from '@embedpdf/react/commands';
import { localEngine } from '@embedpdf/engine';
import './shortcuts.css';
const engine = localEngine();
const plugins = [
stagePlugin(),
renderPlugin(),
interactionPlugin(),
commandsPlugin({ commands: standardCommands }),
];
const welcome = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf');
return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// One row of the sheet: the key, and what it does. A command that can't run now is greyed out.
function ShortcutRow({ id, shortcut }: { id: string; shortcut: string }) {
const command = useCommand(id);
if (!command.visible) return null;
return (
{formatShortcut(shortcut)}{command.label}
);
}
// The sheet lists every shortcut, and shows the last command a key ran.
function KeyboardHelp() {
const commands = useCommands();
const [last, setLast] = useState(null);
useCommandsEvent(
(commands) => commands.onExecuted,
({ commandId }) => setLast(commands.resolveCommand(commandId)?.label ?? commandId),
);
return (
);
}
// The keys work while focus is inside this viewer, so they leave the rest of the page alone. A
// click anywhere in it gives it focus: `tabIndex={-1}` makes it a place for keys, not a stop in the
// Tab order.
function ShortcutArea() {
const area = useRef(null);
useCommandShortcuts({ target: area });
return (
{() => }
);
}
export default function App() {
return (
Loading…