import {
ChangeDetectionStrategy,
Component,
computed,
effect,
inject,
input,
output,
signal,
untracked,
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 { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection';
import { withInteraction } from '@embedpdf/angular/interaction';
import { EpdfAnchored } from '@embedpdf/angular/anchored';
import {
EpdfAnnotation,
EpdfAnnotationLayer,
withAnnotation,
type Annotation,
} from '@embedpdf/angular/annotation';
import { cloudEngine } from '@cloudpdf/engine';
const approvals: OpenInput = { kind: 'grant', grant: 'shr_lu5Sb4r9PYjKrHS3qfNeUywM' };
// One stamp's status, pinned to it: a check on its corner once it's signed off,
// and a status with a button under it. Both stay there while the stamp moves.
@Component({
selector: 'demo-approval-status',
imports: [EpdfAnchored],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@if (signedOff()) {
✓
}
{{ signedOff() ? 'Signed off' : 'Waiting' }}
`,
})
export class ApprovalStatus {
readonly stamp = input.required();
readonly signedOff = input.required();
readonly toggle = output();
protected readonly anchor = inject(EpdfAnnotation).anchorOf(() => this.stamp().ref);
}
// An approval stamp's name, which the stamp keeps in every PDF app; null for any other annotation.
const approvalName = (annotation: Annotation): string | null =>
annotation.subtype === 'stamp' && annotation.name?.startsWith('approval-')
? annotation.name
: null;
// Every approval stamp's status, inside . Your own data for each
// stamp is kept by its name.
@Component({
selector: 'demo-approvals',
imports: [ApprovalStatus],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (approval of approvals(); track approval.name) {
}
`,
})
export class Approvals {
private readonly stamps = inject(EpdfAnnotation).watch({ subtype: 'stamp' });
protected readonly signedOff = signal>>({
'approval-budget': true,
});
protected readonly approvals = computed(() =>
this.stamps().flatMap((stamp) => {
const name = approvalName(stamp);
return name ? [{ name, stamp }] : [];
}),
);
protected toggle(name: string) {
this.signedOff.update((current) => ({ ...current, [name]: !current[name] }));
}
}
@Component({
selector: 'demo-root',
imports: [
EpdfDocumentGate,
EpdfStage,
EpdfPageTemplate,
EpdfRenderLayer,
EpdfSelectionLayer,
EpdfAnnotationLayer,
Approvals,
],
providers: [
provideEmbedPdf(
{ engine: () => cloudEngine(), initialDocuments: [{ source: approvals }] },
withStage(),
withRender(),
withInteraction(),
withSelection(),
withAnnotation(),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './stamp-status.css',
encapsulation: ViewEncapsulation.None,
template: `
Sign a stamp off, then move it: its status goes with it.
Loading…
`,
})
export class App {
private readonly annotation = inject(EpdfAnnotation);
private readonly stage = viewChild(EpdfStage);
private done = false;
constructor() {
// On load: the page with the document's two approval stamps, the budget one already signed off.
effect(() => {
const stage = this.stage();
if (!stage || this.annotation.status() !== 'ready' || this.done) return;
this.done = true;
untracked(() => {
const found = this.annotation
.list()
.filter((each) => each.nm === 'approval-budget' || each.nm === 'approval-contract');
if (found.length === 0) return;
stage.reveal(found[0].page, { rect: found[0].rect });
});
});
}
}