import { ChangeDetectionStrategy, Component, computed, inject, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfOutline, withOutline } from '@embedpdf/angular/outline'; import type { OutlineItem, OutlineItemRef } from '@embedpdf/angular/outline'; import { cloudEngine } from '@cloudpdf/engine'; const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' }; const same = (left: OutlineItemRef | null, right: OutlineItemRef | null) => left?.objectNumber === right?.objectNumber; // An outline being built: add items, nest them, fold them, rename and delete them. @Component({ selector: 'demo-root', imports: [EpdfDocumentGate], providers: [ provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: welcome }] }, withOutline(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './nesting.css', encapsulation: ViewEncapsulation.None, template: ` Add Add inside Indent Outdent Rename Delete @if (outline.rows().length === 0) { This document has no outline yet. Add an item. } @else { @for (row of outline.rows(); track row.item.ref.objectNumber) { {{ row.item.title }} } } Loading… `, }) export class App { protected readonly outline = inject(EpdfOutline); protected readonly title = signal('Chapter'); protected readonly item = computed(() => { const [picked] = this.outline.selected(); return this.outline.items().find((each) => same(each.ref, picked ?? null)) ?? null; }); // The item before the picked one among its parent's children: where an indent puts it. protected readonly previous = computed(() => { const item = this.item(); if (!item) return null; const siblings = this.outline.items().filter((each) => same(each.parent, item.parent)); return siblings[siblings.indexOf(item) - 1] ?? null; }); protected readonly parent = computed(() => { const item = this.item(); return this.outline.items().find((each) => same(each.ref, item?.parent ?? null)) ?? null; }); // After the picked item, or at the end. protected add(inside: boolean) { const item = this.item(); const title = this.title(); void this.outline.create( inside && item ? { title, parent: item.ref } : { title, parent: item?.parent ?? null, position: item ? { after: item.ref } : 'end' }, { select: true }, ); } protected indent() { const item = this.item(); const previous = this.previous(); if (!item || !previous) return; void this.outline.move([item.ref], previous.ref, 'end'); this.outline.expand(previous.ref); } protected outdent() { const item = this.item(); const parent = this.parent(); if (item && parent) void this.outline.move([item.ref], parent.parent, { after: parent.ref }); } protected rename() { const item = this.item(); if (item) void this.outline.update(item.ref, { title: this.title() }); } protected remove() { const item = this.item(); if (item) void this.outline.delete([item.ref]); } protected pick(item: OutlineItem) { this.outline.select([item.ref]); this.title.set(item.title); } }
This document has no outline yet. Add an item.
Loading…