import { ChangeDetectionStrategy, Component, computed, DestroyRef, inject, input, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, injectPage, pageRefsEqual, provideEmbedPdf, } from '@embedpdf/angular/runtime'; import type { OpenInput, PageRef } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { EpdfInteraction, withInteraction } from '@embedpdf/angular/interaction'; import { cloudEngine } from '@cloudpdf/engine'; const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' }; const COLORS = ['#e5484d', '#2f80ed', '#30a46c', '#1a2748']; // A pen in the current color, with its tip at the bottom left. const penIcon = (color: string) => ` `; interface Dot { readonly id: number; readonly page: PageRef; readonly point: { readonly x: number; readonly y: number }; readonly color: string; } // While this is there, the dot tool's crosshair is a pen in `color`. @Component({ selector: 'demo-pen-cursor', changeDetection: ChangeDetectionStrategy.OnPush, template: '', }) export class PenCursor { readonly color = input.required(); constructor() { inject(EpdfInteraction).overrideCursor(() => ({ toolId: 'dot', cursors: { crosshair: { svg: penIcon(this.color()), hotspot: { x: 3, y: 21 } } }, })); } } @Component({ selector: 'demo-dot-layer', changeDetection: ChangeDetectionStrategy.OnPush, template: ` @for (dot of onThisPage(); track dot.id) { } `, }) export class DotLayer { readonly dots = input.required(); private readonly page = injectPage(''); protected readonly onThisPage = computed(() => this.dots().filter((dot) => pageRefsEqual(dot.page, this.page.ref)), ); protected pixels(dot: Dot) { return this.page.transform().toPixels(dot.point); } } @Component({ selector: 'demo-dots', imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer, PenCursor, DotLayer], changeDetection: ChangeDetectionStrategy.OnPush, template: ` @if (penCursor()) { }
@for (swatch of colors; track swatch) { }
`, }) export class Dots { protected readonly colors = COLORS; protected readonly color = signal(COLORS[0]); protected readonly penCursor = signal(true); protected readonly dots = signal([]); constructor() { const interaction = inject(EpdfInteraction); const remove = interaction.registerTool({ id: 'dot', cursor: 'crosshair', // The tool reads the color at the moment of the click. onPointerDown: ({ page, point }) => { this.dots.update((current) => [ ...current, { id: current.length + 1, page, point, color: this.color() }, ]); return true; }, }); interaction.activateTool('dot'); inject(DestroyRef).onDestroy(remove); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, Dots], providers: [ provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withInteraction(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './cursor.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}