/** * @embedpdf/vue/commands: the Vue surface of `@embedpdf/plugin-commands`. The * standard commands, a command as a button needs it, the settings, and keyboard * shortcuts. A command's state is derived from live state on read, so * `useCommand` re-reads it on the kernel's one change stream and its ref * changes only when what the command shows changed. */ // One line per feature: registration travels with the UI. export * from '@embedpdf/plugin-commands'; import { computed, toValue, watch } from 'vue'; import type { MaybeRefOrGetter, Ref } from 'vue'; import { formatShortcut as formatKeys } from '@embedpdf/core-ui'; import { CommandsToken, boundCommandOf, resolvedCommandsEqual, unknownCommandWarning, unregisteredCommand, } from '@embedpdf/plugin-commands'; import type { BoundCommand, CommandId, CommandRefusal, CommandsCapability, } from '@embedpdf/plugin-commands'; import { CommandsToken as CommandsHostToken } from '@embedpdf/plugin-commands/contract/host'; import type { EventHook } from '@embedpdf/core'; import { bindCommandShortcuts, isMacPlatform } from '@embedpdf/web'; import { devWarn } from './dev'; import { useCapability, useCapabilityEvent } from './runtime/capabilities'; import { useDocumentId, useKernelValue } from './runtime/kernel'; import { settingsComposable } from './state'; export { standardCommands } from './command-lists'; /** The commands API: `execute`, `searchCommands`, `registerCommand`, the categories, the settings calls. The object never changes. */ export function useCommands(): CommandsCapability { return useCapability(CommandsToken); } /** Subscribe to one commands event while the component lives: `useCommandsEvent((commands) => commands.onExecuted, handler)`. */ export function useCommandsEvent( select: (commands: CommandsCapability) => EventHook, handler: (event: Event) => void, ): void { useCapabilityEvent(CommandsToken, select, handler); } /** * The commands settings (`commands`, `disabledCategories`, `disabledCommands`, `notAllowed`) as refs, * with or without a document. Takes a selector. */ export const useCommandsSettings = settingsComposable(CommandsToken); /** A shortcut as a tooltip shows it, for this platform: `'⌘K'` on a Mac, `'Ctrl+K'` elsewhere. */ export function formatShortcut(shortcut: string, options: { isMac?: boolean } = {}): string { return formatKeys(shortcut, { isMac: options.isMac ?? isMacPlatform() }); } /** * A command for this component's document (the nearest ``, else * the active one), as a ref: its label, icon, state, `run` and `shortcut`. Pass * a getter to follow a prop: `useCommand(() => props.id)`. The ref changes only * when what the command shows does. Always a command: an id no plugin * registered is labelled with its id, neither visible nor enabled, and warns * once in development. */ export function useCommand(id: MaybeRefOrGetter): Readonly> { const commands = useCapability(CommandsToken); const documentId = useDocumentId(); const resolved = useKernelValue( () => commands.resolveCommand(toValue(id), documentId.value ?? undefined), resolvedCommandsEqual, ); return computed(() => { const command = resolved.value; const requested = toValue(id); if (command === null) { devWarn( `useCommand:${requested}`, `useCommand(): ${unknownCommandWarning(requested, commands.listCommandIds())}`, ); } const scope = documentId.value ?? undefined; return boundCommandOf( command ?? unregisteredCommand(requested), (commandId) => commands.execute(commandId, { documentId: scope }), { isMac: isMacPlatform() }, ); }); } /** Where {@link useCommandShortcuts} listens, and how it reads `Mod`. */ export interface CommandShortcutsOptions { /** Whether `Mod` is Cmd (a Mac) or Ctrl. Default: what the browser says. */ isMac?: MaybeRefOrGetter; /** * Listen only while focus is inside this element (a template ref), for a * page with more than one viewer. The keys work once it has mounted. */ target?: MaybeRefOrGetter; /** Called when a key's command is turned off or not allowed, with why: say so to the user. */ onRefused?: (commandId: CommandId, refusal: CommandRefusal) => void; } /** * Turn every command's shortcut into a working key, for the active document. * Call it once, near the top of your viewer. Keys typed into text fields are * left alone, and so is a key whose command can't run right now. A command * that's turned off or not allowed still takes its key, and `onRefused` says * why. The keys work anywhere on the page; with `target`, only while focus is * inside that element. * They stop when the component unmounts. */ export function useCommandShortcuts(options: CommandShortcutsOptions = {}): void { const commands = useCapability(CommandsHostToken); const scoped = options.target !== undefined; watch( () => [toValue(options.isMac), toValue(options.target)] as const, ([isMac, target], _previous, onCleanup) => { // A target that hasn't mounted yet binds nothing; its ref being set runs this again. if (scoped && !target) return; onCleanup( bindCommandShortcuts(commands, { isMac, target: target ?? undefined, onRefused: (commandId, refusal) => options.onRefused?.(commandId, refusal), }), ); }, { immediate: true, flush: 'post' }, ); }