import { ChangeDetectionStrategy, Component, inject, ViewEncapsulation } from '@angular/core';
import { EpdfDocument, EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime';
import type { OpenInput, PageRef } from '@embedpdf/angular/runtime';
import { EpdfPageView } from '@embedpdf/angular/page-view';
import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render';
import { EpdfPageEdit, withPageEdit } from '@embedpdf/angular/page-edit';
import { cloudEngine } from '@cloudpdf/engine';
const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' };
const sameRef = (left: PageRef, right: PageRef) => left.objectNumber === right.objectNumber;
// Every page as a card. Click cards to select them; the toolbar edits every selected page at once.
@Component({
selector: 'demo-page-organizer',
imports: [EpdfPageView, EpdfRenderLayer],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@let canEdit = pageEdit.canEdit();
@let selected = pageEdit.selected();
@let count = selected.length;
@for (page of document.pages(); track page.ref.objectNumber) {
-
}
`,
})
export class PageOrganizer {
protected readonly pageEdit = inject(EpdfPageEdit);
protected readonly document = inject(EpdfDocument);
constructor() {
// The second and third pages start selected. The selection holds refs: it names the same
// pages after a reorder, and a deleted page leaves it.
this.pageEdit.select([1, 2]);
}
protected isSelected(page: PageRef): boolean {
return this.pageEdit.selected().some((ref) => sameRef(ref, page));
}
protected toggle(page: PageRef) {
const selected = this.pageEdit.selected();
this.pageEdit.select(
this.isSelected(page) ? selected.filter((ref) => !sameRef(ref, page)) : [...selected, page],
);
}
}
@Component({
selector: 'demo-root',
imports: [EpdfDocumentGate, PageOrganizer],
providers: [
provideEmbedPdf(
{ engine: () => cloudEngine(), initialDocuments: [{ source: welcome }] },
withRender(),
withPageEdit(),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './organize.css',
encapsulation: ViewEncapsulation.None,
template: `
Loading…
`,
})
export class App {}