import { ChangeDetectionStrategy, Component, computed, DOCUMENT, effect, ElementRef, inject, viewChild, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { EpdfInteraction, withInteraction } from '@embedpdf/angular/interaction'; import { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection'; import { EpdfShell, withShell } from '@embedpdf/angular/shell'; import { localEngine } from '@embedpdf/engine'; // [!doc-source welcome] 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()) }; }; // [!/doc-source] const TOOLS = [ { id: 'pointer', label: 'Select text' }, { id: 'pan', label: 'Scroll with the hand' }, ]; // A menu with a submenu: opening the submenu leaves its menu open. @Component({ selector: 'div[demoViewMenu]', changeDetection: ChangeDetectionStrategy.OnPush, host: { class: 'toolbar' }, template: ` Open menus: {{ anyOpen() ? shell.openMenus().join(' › ') : 'none' }} `, }) export class ViewMenu { protected readonly shell = inject(EpdfShell); protected readonly interaction = inject(EpdfInteraction); protected readonly tools = TOOLS; protected readonly anyOpen = computed(() => this.shell.openMenus().length > 0); private readonly bar = viewChild.required>('bar'); constructor() { const document = inject(DOCUMENT); // A press outside the menus, or Escape, closes all of them. effect((onCleanup) => { if (!this.anyOpen()) return; const outside = (event: PointerEvent) => { if (!this.bar().nativeElement.contains(event.target as Node)) this.shell.closeAllMenus(); }; const escape = (event: KeyboardEvent) => { if (event.key === 'Escape') this.shell.closeAllMenus(); }; document.addEventListener('pointerdown', outside, true); document.addEventListener('keydown', escape); onCleanup(() => { document.removeEventListener('pointerdown', outside, true); document.removeEventListener('keydown', escape); }); }); } protected isOpen(id: string) { return this.shell.openMenus().includes(id); } protected choose(toolId: string) { this.interaction.activateTool(toolId); this.shell.closeAllMenus(); } } @Component({ selector: 'demo-workspace', imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, ViewMenu], changeDetection: ChangeDetectionStrategy.OnPush, template: `
`, }) export class Workspace { constructor() { // The menu and its submenu are open when the document is. const shell = inject(EpdfShell); shell.openMenu('view'); shell.openMenu('view-tool'); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, Workspace], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withInteraction(), withSelection(), withShell(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './menus.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}