import { ChangeDetectionStrategy, Component, effect, inject, signal, untracked, viewChild, ViewEncapsulation, } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { EpdfDocument, EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput, PageRef } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRender, EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; 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] interface Entry { id: number; text: string; } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './redraws.css', encapsulation: ViewEncapsulation.None, template: ` @let currentPage = stage.currentPage();
render epoch {{ epochOf(currentPage) }}
    @for (entry of entries(); track entry.id) {
  1. invalidated$ {{ entry.text }}
  2. }

Loading…

`, }) export class App { protected readonly render = inject(EpdfRender); private readonly document = inject(EpdfDocument); protected readonly entries = signal([]); private count = 0; constructor() { this.render.invalidated$.pipe(takeUntilDestroyed()).subscribe(({ pages, scope, origin }) => { const what = `${pages.length === 1 ? '1 page' : `${pages.length} pages`} · ${scope}`; const from = origin ? `an edit (${origin.kind})` : 'your code'; const entry = { id: this.count++, text: `${what} · from ${from}` }; this.entries.update((current) => [entry, ...current].slice(0, 4)); }); // Redraw the first page on load, so there's something in the list. effect(() => { if (this.document.status() !== 'ready') return; untracked(() => this.render.invalidate({ pages: [0] })); }); } protected epochOf(page: PageRef | null): number { return page ? this.render.getRenderEpoch(page) : 0; } }