/** * The commands plugin's service, features and directives: every action of the viewer, with its * label, icon, state and shortcut, run from buttons, menus and keys. * * withCommands({ commands }) the plugin, for provideEmbedPdf() * withCommandShortcuts() every command's shortcut as a working key * inject(EpdfCommands) `execute()`, `searchCommands()`, `executed$`, the settings * * * It runs and reads the command for this part of the template's document. An unknown id * hides the button. */ @Directive({ selector: '[epdfCommand]', exportAs: 'epdfCommand', host: { '(click)': 'onClick()', '[attr.disabled]': 'enabled() ? null : ""', '[attr.aria-pressed]': 'command().pressable ? active() : null', '[attr.aria-expanded]': 'command().menu ? active() : null', '[attr.title]': 'label() || null', '[hidden]': '!visible()', }, }) export class EpdfCommand { /** The command's id: `'zoom:in'`. */ readonly id = input.required({ alias: 'epdfCommand' }); /** The command; an unknown id is neither visible nor enabled, so its button is hidden. */ readonly command: Signal = inject(EpdfCommands).commandOf(() => this.id()); /** What the user sees. */ readonly label: Signal = computed(() => this.command().label); /** Your icon's name for it, when the command has one. */ readonly icon: Signal = computed(() => this.command().icon); /** Its first shortcut, formatted for the platform (`'⌘K'`), or `null`. */ readonly shortcutLabel: Signal = computed(() => this.command().shortcutLabel); /** Whether it can run now. */ readonly enabled: Signal = computed(() => this.command().enabled); /** Whether it shows as pressed, such as the current tool. */ readonly active: Signal = computed(() => this.command().active); /** Whether it shows at all. */ readonly visible: Signal = computed(() => this.command().visible); /** Run the command for this part of the template's document. Resolves `{ status }`, like `execute()`. */ run(): Promise { return untracked(this.command).run(); } protected onClick(): void { // A command whose `run` throws reports it through `executionFailed$`. this.run().catch(() => {}); } }