import { ChangeDetectionStrategy, Component, computed, inject, Injectable, signal, ViewEncapsulation, } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfActions, withActions, withActionsUi, type ActionContext, type ActionOrigin, type ActionPolicyDecision, type PdfActionTree, } from '@embedpdf/angular/actions'; import { cloudEngine } from '@cloudpdf/engine'; const links: OpenInput = { kind: 'grant', grant: 'shr_xs-9Y5qwuYrJ2BKtWzuo_2xy' }; // A link to a website, as a PDF carries it: what readActionTree() reads from a link. const websiteLink: PdfActionTree = { root: { type: 'uri', subtype: 'URI', uri: 'https://www.w3.org/TR/WCAG22/', isMap: false, next: [], }, incomplete: false, warnings: [], }; // The three ways an action can start, each as the context it runs with. const STARTS: ReadonlyArray<{ label: string; context: ActionContext }> = [ { label: 'A click', context: { origin: 'user', source: { kind: 'api' }, event: { type: 'activate' } }, }, { label: 'The pointer over it', context: { origin: 'hover', source: { kind: 'api' }, event: { type: 'annotation', event: 'cursorEnter' }, }, }, { label: 'The document opening', context: { origin: 'lifecycle', source: { kind: 'api' }, event: { type: 'document', event: 'open' }, }, }, ]; const RULES: readonly ActionPolicyDecision[] = ['allow', 'adapter', 'report', 'block']; /** What happened to the link, newest first. */ @Injectable() export class RunLog { readonly lines = signal([]); note(line: string) { this.lines.update((lines) => [line, ...lines].slice(0, 6)); } } // The rules for websites, changed while the app runs, and what happens when the link runs. @Component({ selector: 'demo-website-rules', changeDetection: ChangeDetectionStrategy.OnPush, template: `
`, }) export class WebsiteRules { protected readonly actions = inject(EpdfActions); protected readonly log = inject(RunLog); protected readonly starts = STARTS; protected readonly rulesToPick = RULES; protected readonly rules = computed(() => this.actions.settings().policy.uri); constructor() { this.actions.diagnosticReported$ .pipe(takeUntilDestroyed()) .subscribe(({ code, action }) => this.log.note(`The ${action} action was not run: ${code}`)); // One click on load, so the log shows what a click does. this.run(STARTS[0].context); } protected run(context: ActionContext) { void this.actions.execute(websiteLink, context); } protected setRule(origin: ActionOrigin, rule: string) { this.actions.updateSettings({ policy: { uri: { [origin]: rule as ActionPolicyDecision } }, }); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, WebsiteRules], providers: [ RunLog, provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: links }] }, // Websites open only on a click, as the page's example registers it. withActions({ policy: { uri: { user: 'adapter', hover: 'block', lifecycle: 'block' } }, }), // Instead of opening a tab, the adapter notes the website it was given. withActionsUi(() => { const log = inject(RunLog); return { openUri: (uri) => log.note(`Your adapter got ${uri}`) }; }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './rules.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}