import { pageAnnotationOf } from './annotations'; import type { PageCoordinates, PdfCoordinates } from './coordinates'; import { mapFieldActions, mapTriggerActions, pageDestinationOf, pdfDestinationOf, type VisibleBoxOf, } from './destinations'; import type { WidgetAnnotation } from '../annotation/kinds/widget'; import type { PdfFieldActions } from '../dto/PdfAction'; import type { PageDestination, PdfDestination } from '../dto/PdfDestination'; import type { FormFieldDraft, WidgetPlacement } from '../forms/draft'; import type { FormField } from '../forms/field'; import type { FormSnapshot } from '../forms/snapshot'; import { pdfRectOf } from '../geometry/pageSpace'; /** A page-space widget placement in the file's coordinates, measured on its page. */ export function pdfWidgetPlacementOf( placement: WidgetPlacement, boxOf: VisibleBoxOf, ): WidgetPlacement { const { actions, ...rest } = placement; const visible = boxOf(placement.page); return { ...rest, ...(placement.rect ? { rect: pdfRectOf(placement.rect, visible) } : {}), ...(placement.box ? { box: pdfRectOf(placement.box, visible) } : {}), ...(actions ? { actions: mapTriggerActions(actions, (destination: PageDestination) => pdfDestinationOf(destination, boxOf), ), } : {}), } as WidgetPlacement; } /** A field draft in the file's coordinates: each widget measured on its own page. */ export function pdfFormFieldDraftOf( draft: FormFieldDraft, boxOf: VisibleBoxOf, ): FormFieldDraft { if (!draft.widgets) return draft as FormFieldDraft; return { ...draft, widgets: draft.widgets.map((widget) => pdfWidgetPlacementOf(widget, boxOf)), } as FormFieldDraft; } /** A field's actions in page space, each destination measured on its page. */ export function pageFieldActionsOf( actions: PdfFieldActions, boxOf: VisibleBoxOf, ): PdfFieldActions { return mapFieldActions(actions, (destination) => pageDestinationOf(destination, boxOf)); } /** A form field in page space: each destination of its actions on the page it goes to. */ export function pageFormFieldOf(field: FormField, boxOf: VisibleBoxOf): FormField { if (!field.actions) return field as FormField; return { ...field, actions: pageFieldActionsOf(field.actions, boxOf) } as FormField; } /** A widget row in page space, measured on its page. */ export function pageWidgetOf( widget: WidgetAnnotation, boxOf: VisibleBoxOf, ): WidgetAnnotation { return pageAnnotationOf(widget, boxOf(widget.page), boxOf) as WidgetAnnotation; } /** A form in page space: its fields, and each widget row on its page. */ export function pageFormSnapshotOf( snapshot: FormSnapshot, boxOf: VisibleBoxOf, ): FormSnapshot { return { ...snapshot, fields: snapshot.fields.map((field) => pageFormFieldOf(field, boxOf)), widgets: snapshot.widgets.map((widget) => pageWidgetOf(widget, boxOf)), }; }