/** * A box drawn by a control of the app's: an `` inside * `` (`templates.ts`). The box is the layer's (`widget-box.ts`): placed and * turned like the field, the widget's event surface, and the cursor's way in, to the first * element inside that Tab stops at. The template draws in a frame of the field's 100% size, * scaled with the page. Its `native` is the field's picture and the layer's own control, drawn * as they are without a control of the app's. A click on the box runs the widget's action, * after a value the click wrote (`createFieldControlPress`). */ import { NgTemplateOutlet } from '@angular/common'; import { ChangeDetectionStrategy, Component, computed, Directive, input } from '@angular/core'; import { EpdfSvgShapes } from '@embedpdf/angular/runtime'; import type { FormFieldValue, FormSetValueResult, FormWidgetItem } from '@embedpdf/plugin-form'; import { createFieldControlPress, fieldControlPlaceOf, isClickClaimed, runsActionOnClick, type FieldControlFrame, } from '@embedpdf/web'; import { FormButtonControl, FormComboControl, FormListControl, FormSignatureControl, FormTextControl, FormToggleControl, } from './controls'; import type { ShownLook } from './field-pictures'; import type { EpdfFieldControlTemplate } from './templates'; import { EPDF_FORM_EMBEDDED, FormWidgetBox } from './widget-box'; /** * The element the layer's own drawing of a box goes in, inside a control of the app's. The * layer's control in it only fills its place ({@link EPDF_FORM_EMBEDDED}). */ @Directive({ selector: 'div[epdfFormNative]', providers: [{ provide: EPDF_FORM_EMBEDDED, useValue: true }], }) export class FormNativeFrame {} const sameFrame = (left: FieldControlFrame, right: FieldControlFrame): boolean => left.width === right.width && left.height === right.height && left.rotation === right.rotation && left.scale === right.scale; @Component({ selector: 'div[epdfFormCustom]', imports: [ NgTemplateOutlet, EpdfSvgShapes, FormNativeFrame, FormTextControl, FormToggleControl, FormComboControl, FormListControl, FormButtonControl, FormSignatureControl, ], changeDetection: ChangeDetectionStrategy.OnPush, host: { '(click)': 'clicked($event)' }, template: ` @let own = item();
@if (look(); as shown) { @if (shown.kind === 'picture') { } @else { } } @switch (own.control) { @case ('text') {
} @case ('toggle') {
} @case ('choice') { @if (own.kind === 'list') {
} @else {
} } @case ('button') {
} @case ('signature') {
} }
`, }) export class FormCustomControl extends FormWidgetBox { /** The app's template that draws this box. */ readonly control = input.required(); /** The widget as it looks now, for `native`; `null` while it has nothing to show. */ readonly look = input(null); // The template draws the box: an edge would be the layer's own look. protected override readonly drawsEdge = false; protected override readonly custom = true; /** The frame the template draws into, and where the layer's own drawing goes inside it. */ protected readonly place = computed(() => fieldControlPlaceOf(this.item(), this.page.transform()), ); /** The frame the template is handed: the same object while its numbers stay the same. */ protected readonly controlFrame = computed(() => this.place().frame, { equal: sameFrame }); /** True while the cursor is in this box. */ protected readonly focused = this.formBinding.select( (form) => form.isWidgetFocused(this.item().ref), false, Object.is, ); /** Where the widget stands, inside `native`. A global `img { max-width: 100% }` reset would clamp it. */ protected readonly pictureStyle = computed(() => ({ ...this.place().pictureStyle, maxWidth: 'none', maxHeight: 'none', pointerEvents: 'none', })); protected readonly paintingStyle = computed(() => ({ ...this.place().pictureStyle, overflow: 'visible', pointerEvents: 'none', })); /** One press for the box's life: the write the template is handed, and the box's click. */ private readonly press = createFieldControlPress( (value: FormFieldValue): Promise => { try { return this.formBinding.require('setValue').setValue(this.item().fieldRef, value); } catch (error) { return Promise.reject(error); } }, () => this.activate(), ); protected readonly setValue = this.press.setValue; /** * A click in the box runs the widget's action, as on the layer's own control, unless that * control, inside the template, dealt with the click itself. */ protected clicked(event: Event): void { if (!isClickClaimed(event) && runsActionOnClick(this.item())) this.press.click(); } }