import {
ChangeDetectionStrategy,
Component,
computed,
inject,
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 { EpdfSearch, EpdfSearchLayer, withSearch } from '@embedpdf/angular/search';
import type { SearchHit } from '@embedpdf/angular/search';
import { EpdfShell, withShell } from '@embedpdf/angular/shell';
import { cloudEngine } from '@cloudpdf/engine';
const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' };
// The panel shows the match its props name, and steps to the next one without reopening.
@Component({
selector: 'aside[demoMatchPanel]',
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'panel', 'aria-label': 'Match' },
template: `
@if (hit(); as hit) {
Page {{ hit.pageIndex + 1 }}
@if (hit.snippet; as snippet) {
…{{ snippet.before }}{{ snippet.match }}{{ snippet.after }}…
}
} @else {
Searching…
}
`,
})
export class MatchPanel {
private readonly shell = inject(EpdfShell);
protected readonly search = inject(EpdfSearch);
protected readonly panel = this.shell.surface('match');
protected readonly index = computed(() => {
const index = this.panel.props()['index'];
return typeof index === 'number' ? index : 0;
});
protected readonly hit = computed(() => this.search.hits()[this.index()]);
protected show(next: number) {
this.shell.updateSurfaceProps('match', { index: next });
this.search.goToHit(this.search.hits()[next]);
}
}
@Component({
selector: 'div[demoWorkspace]',
imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSearchLayer, MatchPanel],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'workspace' },
template: `
@if (panel.isOpen()) {
}
`,
})
export class Workspace {
private readonly shell = inject(EpdfShell);
private readonly search = inject(EpdfSearch);
protected readonly panel = this.shell.surface('match');
constructor() {
// Every "PDF" on the pages, and the first one open in the panel.
void this.search.search({ text: 'PDF' });
this.shell.open('match', { exclusive: 'right', props: { index: 0 } });
}
protected open(hit: SearchHit) {
const index = this.search.hits().indexOf(hit);
this.shell.open('match', { exclusive: 'right', props: { index } });
}
}
@Component({
selector: 'demo-root',
imports: [EpdfDocumentGate, Workspace],
providers: [
provideEmbedPdf(
{ engine: () => cloudEngine(), initialDocuments: [{ source: welcome }] },
withStage(),
withRender(),
withSearch(),
withShell(),
),
],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrl: './props.css',
encapsulation: ViewEncapsulation.None,
template: `
Loading…
`,
})
export class App {}