import {
ChangeDetectionStrategy,
Component,
effect,
inject,
ViewEncapsulation,
} from '@angular/core';
import { EpdfDocument, 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 { EpdfSelection, EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection';
import { cloudEngine } from '@cloudpdf/engine';
const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' };
// Every field of the selection's state, as it changes.
@Component({
selector: 'demo-selection-status',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
- hasSelection
- {{ selection.hasSelection() }}
- isSelecting
- {{ selection.isSelecting() }}
- pages
-
{{ selection.pages().length === 1 ? '1 page' : selection.pages().length + ' pages' }}
- range
-
@if (selection.range(); as range) {
{{ range.start.index }} → {{ range.end.index }}
} @else {
null
}
`,
})
export class SelectionStatus {
protected readonly selection = inject(EpdfSelection);
}
@Component({
selector: 'demo-root',
imports: [
EpdfDocumentGate,
EpdfStage,
EpdfPageTemplate,
EpdfRenderLayer,
EpdfSelectionLayer,
SelectionStatus,
],
providers: [
provideEmbedPdf(
{ engine: () => cloudEngine(), initialDocuments: [{ source: welcome }] },
withStage(),
withRender(),
withInteraction(),
withSelection(),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './state.css',
encapsulation: ViewEncapsulation.None,
template: `
Loading…
`,
})
export class App {
private readonly selection = inject(EpdfSelection);
private readonly document = inject(EpdfDocument);
constructor() {
// Something selected on load: "Documents", the first word on the cover.
effect(() => {
const cover = this.document.pages()[0]?.ref;
if (cover) this.selection.select({ page: cover, start: 36, count: 9 });
});
}
}