/** * Script world building (a page-scoped prefetch: the form as the user sees it * plus the annotations of the page the action runs on) and the event bridge between * two standards: the /AA keys are ISO 32000-2 Table 200; the event names * live in the JavaScript API layer (ISO 21757-1, Acrobat). */ import { scriptFieldsFromSnapshot } from '@embedpdf/core-acrojs'; import type { ScriptAnnotInput, ScriptColorArray, ScriptEventInput, ScriptWorldInput, } from '@embedpdf/core-acrojs'; import { pdfRectOf, rgbOf, toPageRef } from '@embedpdf/engine-core/runtime'; import type { FormSnapshot, PageObjectNumber } from '@embedpdf/engine-core/runtime'; import type { ActionContext } from '../contract'; import type { ActionsContext, ActionsServices } from '../services'; export function createScriptWorld(ctx: ActionsContext, { form }: Pick) { // Acrobat's JavaScript keeps colors as color arrays, so they convert here, at its edge. const engineColorToArray = (color: string | null | undefined): ScriptColorArray => { if (!color) return ['T']; const { r, g, b } = rgbOf(color); return ['RGB', r / 255, g / 255, b / 255]; }; const scriptWorldFor = async ( pageObjectNumber: PageObjectNumber, ): Promise<{ snapshot: FormSnapshot; world: Omit }> => { const page = toPageRef(pageObjectNumber); const snapshot = await form.read(); const layout = ctx.getPage(page); const pageIndex = Math.max(0, layout?.index ?? -1); const { annotations } = await ctx.doc.page(page).annotations.list(); const annots: ScriptAnnotInput[] = annotations // Script-addressable = everything except links and widgets (they are // Link and Field objects in Acrobat and separate planes here). .filter( (annotation) => annotation.subtype !== 'link' && !annotation.subtype.startsWith('widget'), ) .map((annotation) => { // Acrobat's JavaScript keeps an annotation's rect in the file's // numbers, [x_ll, y_ll, x_ur, y_ur], so it converts here, at its edge. const inFile = layout ? pdfRectOf(annotation.rect, layout.pdfCropBox) : null; const styled = annotation as unknown as { color?: string; interiorColor?: string | null; opacity?: number; strokeWidth?: number; borderStyle?: string; dashArray?: number[]; }; return { ref: annotation.ref, name: annotation.nm ?? '', subtype: annotation.subtype, page: pageIndex, rect: (inFile ? [inFile.left, inFile.bottom, inFile.right, inFile.top] : [0, 0, 0, 0]) as [number, number, number, number], contents: annotation.contents ?? '', author: annotation.author ?? '', subject: annotation.subject ?? '', strokeColor: engineColorToArray(styled.color), fillColor: engineColorToArray(styled.interiorColor), opacity: styled.opacity ?? 1, width: styled.strokeWidth ?? 1, borderStyle: styled.borderStyle === 'dashed' ? ('D' as const) : ('S' as const), dash: styled.dashArray ?? [], hidden: annotation.hidden, print: annotation.print, readOnly: annotation.readOnly, locked: annotation.locked, noView: annotation.noView, toggleNoView: annotation.toggleNoView, opaqueBody: annotation.subtype === 'stamp', }; }); return { snapshot, world: { fields: scriptFieldsFromSnapshot(snapshot), annots, annotPages: [pageIndex], annotsCoverDocument: (ctx.document()?.pageCount ?? 0) <= 1, }, }; }; const ANNOTATION_EVENT_NAMES: Record = { cursorEnter: 'Mouse Enter', cursorExit: 'Mouse Exit', mouseDown: 'Mouse Down', mouseUp: 'Mouse Up', focus: 'Focus', blur: 'Blur', }; const PAGE_EVENT_NAMES: Record = { open: 'Open', close: 'Close', // /PV and /PI have no Acrobat event name: these two are an EmbedPDF extension. visible: 'Visible', invisible: 'Invisible', }; // The /AA keys are ISO 32000-2 Table 200; the event names live in the // JavaScript API layer (ISO 21757-1, Acrobat). const DOC_EVENT_NAMES: Record = { open: 'Open', 'will-save': 'WillSave', 'did-save': 'DidSave', 'will-print': 'WillPrint', 'did-print': 'DidPrint', 'will-close': 'WillClose', }; const scriptEventFor = ( actionContext: ActionContext, snapshot: FormSnapshot, ): ScriptEventInput => { const provenance = actionContext.event; const type = provenance.type === 'page' ? 'Page' : provenance.type === 'document' ? 'Doc' : actionContext.source.kind === 'widget' ? 'Field' : actionContext.source.kind === 'link' ? 'Link' : actionContext.source.kind === 'outline' ? 'Bookmark' : 'Annot'; // EmbedPDF extension — Acrobat has no plain-annot type const name = provenance.type === 'activate' ? 'Mouse Up' : provenance.type === 'annotation' ? (ANNOTATION_EVENT_NAMES[provenance.event] ?? 'Mouse Up') : provenance.type === 'page' ? (PAGE_EVENT_NAMES[provenance.event] ?? 'Open') : (DOC_EVENT_NAMES[provenance.event] ?? 'Open'); const targetRef = actionContext.source.kind === 'widget' ? actionContext.source.field : undefined; const targetField = targetRef ? snapshot.fields.find((field) => targetRef.kind === 'objectNumber' && field.ref.kind === 'objectNumber' ? field.ref.objectNumber === targetRef.objectNumber : targetRef.kind === 'fqn' && field.name === targetRef.name, ) : undefined; return { kind: 'widget-activate', type, name, ...(targetRef ? { target: targetRef, source: targetRef } : {}), ...(targetField && targetField.valueEntry.kind === 'scalar' ? { value: targetField.valueEntry.value } : {}), }; }; return { scriptWorldFor, scriptEventFor }; }