import {
ChangeDetectionStrategy,
Component,
computed,
effect,
inject,
input,
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 { withInteraction } from '@embedpdf/angular/interaction';
import {
EpdfAnnotation,
EpdfAnnotationLayer,
withAnnotation,
type AnnotationProperty,
type LineEnding,
} from '@embedpdf/angular/annotation';
import { cloudEngine } from '@cloudpdf/engine';
const layout: OpenInput = { kind: 'grant', grant: 'shr_d2E_LaTuddJbZR00d1CvkqLM' };
// One control per property, by the control it asks for.
@Component({
selector: 'demo-control',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@if (property(); as property) {
@if (property.control === 'color') {
@if (property.allowsNone) {
}
} @else if (property.control === 'number') {
} @else if (property.control === 'choice' && property.key === 'lineEndings') {
} @else if (property.control === 'choice' && property.key === 'borderStyle') {
} @else if (property.control === 'choice') {
} @else if (property.control === 'flag') {
} @else if (property.control === 'textFormat') {
} @else if (property.control === 'text') {
} @else if (property.control === 'link') {
}
}
`,
})
export class Control {
readonly property = input.required();
protected readonly annotation = inject(EpdfAnnotation);
private readonly panel = this.annotation.selection.properties;
protected readonly value = computed(() => this.panel().values[this.property().key]);
protected readonly isMixed = computed(() => this.panel().mixed.includes(this.property().key));
protected readonly stringValue = computed(() => {
const value = this.value();
return typeof value === 'string' ? value : null;
});
protected readonly numberValue = computed(() => {
const value = this.value();
return typeof value === 'number' ? value : null;
});
/** The value as a readout or a `
}
}
`,
})
export class StylePanel {
protected readonly panel = inject(EpdfAnnotation).selection.properties;
}
@Component({
selector: 'demo-root',
imports: [
EpdfDocumentGate,
EpdfStage,
EpdfPageTemplate,
EpdfRenderLayer,
EpdfAnnotationLayer,
StylePanel,
],
providers: [
provideEmbedPdf(
{ engine: () => cloudEngine(), initialDocuments: [{ source: layout }] },
withStage(),
withRender(),
withInteraction(),
withAnnotation(),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './style-panel.css',
encapsulation: ViewEncapsulation.None,
template: `
Loading…
`,
})
export class App {
private readonly annotation = inject(EpdfAnnotation);
private readonly stage = viewChild(EpdfStage);
private done = false;
constructor() {
// On load: the document's square, selected, so the panel shows its style.
effect(() => {
const stage = this.stage();
if (!stage || this.annotation.status() !== 'ready' || this.done) return;
this.done = true;
untracked(() => {
const square = this.annotation.list().find((each) => each.nm === 'square');
if (!square) return;
this.annotation.selection.select([square.ref]);
stage.reveal(square.page, { rect: square.rect });
});
});
}
}