@embedpdf/plugin-form 3.0.0-next.10 → 3.0.0-next.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/contract/host.cjs +3 -0
- package/dist/contract/host.d.cts +2 -0
- package/dist/contract/host.d.ts +2 -0
- package/dist/contract/host.js +2 -0
- package/dist/contract.cjs +7 -0
- package/dist/contract.cjs.map +1 -0
- package/dist/contract.d.cts +8 -0
- package/dist/contract.d.ts +8 -0
- package/dist/contract.js +7 -0
- package/dist/contract.js.map +1 -0
- package/dist/index.cjs +395 -558
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -247
- package/dist/index.d.ts +10 -247
- package/dist/index.js +383 -546
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +4 -0
- package/dist/internal.d.cts +3 -0
- package/dist/internal.d.ts +3 -0
- package/dist/internal.js +3 -0
- package/dist/scripting.cjs +485 -0
- package/dist/scripting.cjs.map +1 -0
- package/dist/scripting.d.cts +47 -0
- package/dist/scripting.d.ts +47 -0
- package/dist/scripting.js +483 -0
- package/dist/scripting.js.map +1 -0
- package/dist/types-C1uIIYfn.cjs +16 -0
- package/dist/types-C1uIIYfn.cjs.map +1 -0
- package/dist/types-CnUW7z1W.d.cts +257 -0
- package/dist/types-DkQmmcTG.js +12 -0
- package/dist/types-DkQmmcTG.js.map +1 -0
- package/dist/types-NGRZjJNj.d.ts +257 -0
- package/package.json +25 -8
package/dist/index.js
CHANGED
|
@@ -1,9 +1,104 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { FormScriptingController, createFormScriptingController } from "./scripting.js";
|
|
2
|
+
import { t as FormToken$1 } from "./types-DkQmmcTG.js";
|
|
3
|
+
import { FormToken } from "./contract.js";
|
|
4
|
+
import { createSerialQueue, definePlugin } from "@embedpdf/core";
|
|
5
|
+
import { ActionsToken, createHoverPump } from "@embedpdf/plugin-actions/contract";
|
|
6
|
+
import { ActionsToken as ActionsToken$1 } from "@embedpdf/plugin-actions/contract/host";
|
|
7
|
+
import { AnnotationToken } from "@embedpdf/plugin-annotation/contract";
|
|
8
|
+
import { AnnotationToken as AnnotationToken$1 } from "@embedpdf/plugin-annotation/contract/host";
|
|
9
|
+
import { InteractionToken, samplePointOn } from "@embedpdf/plugin-interaction/contract";
|
|
5
10
|
import { PermissionDenied } from "@embedpdf/engine-core/runtime";
|
|
6
|
-
import {
|
|
11
|
+
import { MIN_DRAG, resolveClickPlacement, widgetAppearanceFromProps } from "@embedpdf/plugin-annotation/authoring";
|
|
12
|
+
//#region src/field-selection.ts
|
|
13
|
+
/**
|
|
14
|
+
* ISO field selection, written ONCE for ResetForm (Tables 241/242) and
|
|
15
|
+
* SubmitForm (Tables 239/240): a NAME target selects the field AND its
|
|
16
|
+
* descendants (FQN dot-prefix — `parent` selects `parent.c1`, the rule the
|
|
17
|
+
* old exact-match resolution violated); an objectNumber target selects that
|
|
18
|
+
* field dictionary. Include mode keeps the selection; exclude mode keeps
|
|
19
|
+
* the complement (a name excludes its subtree the same way it includes
|
|
20
|
+
* it). `targets === null` (the key ABSENT) selects everything and the flag
|
|
21
|
+
* is ignored; `[]` + include selects NOTHING while `[]` + exclude selects
|
|
22
|
+
* everything — presence and emptiness are different states.
|
|
23
|
+
*
|
|
24
|
+
* `listed` reports whether a field was ADDRESSED by the target list
|
|
25
|
+
* (directly or through its subtree) — the "explicitly listed" predicate the
|
|
26
|
+
* submit honesty diagnostics key on.
|
|
27
|
+
*/
|
|
28
|
+
const resolveFieldSelection = (all, targets, exclude) => {
|
|
29
|
+
if (targets === null) return {
|
|
30
|
+
selected: [...all],
|
|
31
|
+
listed: () => false
|
|
32
|
+
};
|
|
33
|
+
const names = targets.filter((t) => t.kind === "name").map((t) => t.name);
|
|
34
|
+
const objectNumbers = new Set(targets.filter((t) => t.kind === "objectNumber").map((t) => t.objectNumber));
|
|
35
|
+
const listed = (field) => field.ref.kind === "objectNumber" && objectNumbers.has(field.ref.fieldObjectNumber) || names.some((name) => field.name === name || field.name.startsWith(`${name}.`));
|
|
36
|
+
return {
|
|
37
|
+
selected: exclude ? all.filter((f) => !listed(f)) : all.filter(listed),
|
|
38
|
+
listed
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Build the resolved submission dataset from a live field list (Tables
|
|
43
|
+
* 239/240 semantics; pure — the capability supplies a FRESH engine read so
|
|
44
|
+
* no model-lag class exists):
|
|
45
|
+
*
|
|
46
|
+
* - the unconditional **NoExport veto** — even an explicit include cannot
|
|
47
|
+
* override it (diagnosed when explicitly listed, so the veto is
|
|
48
|
+
* observable, silent when swept in implicitly);
|
|
49
|
+
* - **push-buttons and signatures** never contribute entries in v1 —
|
|
50
|
+
* ISO's explicit-pushbutton-`/AP`-in-FDF rule is unrepresentable in the
|
|
51
|
+
* current DTO, so an EXPLICITLY listed one is diagnosed
|
|
52
|
+
* (`submit-entry-unsupported`), never silently dropped;
|
|
53
|
+
* - an **unsupported `/V` shape** is always diagnosed (silent omission
|
|
54
|
+
* would be data loss);
|
|
55
|
+
* - a valueless field becomes a NAME-ONLY entry (`value: null`) only under
|
|
56
|
+
* IncludeNoValueFields, else it is skipped (the ISO default);
|
|
57
|
+
* - values are entry-faithful: scalar → string, array (multi-select list
|
|
58
|
+
* box) → string[]; the submitted name is the fully qualified name.
|
|
59
|
+
*/
|
|
60
|
+
const buildSubmitEntries = (fields, intent, diagnose) => {
|
|
61
|
+
const { selected, listed } = resolveFieldSelection(fields, intent.fields, intent.exclude);
|
|
62
|
+
const explicitInclude = intent.fields !== null && !intent.exclude;
|
|
63
|
+
const entries = [];
|
|
64
|
+
for (const field of selected) {
|
|
65
|
+
const explicit = explicitInclude && listed(field);
|
|
66
|
+
if (field.family === "pushbutton" || field.family === "signature") {
|
|
67
|
+
if (explicit) diagnose({
|
|
68
|
+
code: "submit-entry-unsupported",
|
|
69
|
+
message: `submit: '${field.name}' (${field.family}) has no representable submission value in v1 — skipped`
|
|
70
|
+
});
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (field.flags.noExport) {
|
|
74
|
+
if (explicit) diagnose({
|
|
75
|
+
code: "submit-entry-unsupported",
|
|
76
|
+
message: `submit: '${field.name}' carries the NoExport flag — the veto is unconditional (ISO 12.7.6.2), skipped`
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const entry = field.valueEntry;
|
|
81
|
+
if (entry.kind === "scalar") entries.push({
|
|
82
|
+
name: field.name,
|
|
83
|
+
value: entry.value
|
|
84
|
+
});
|
|
85
|
+
else if (entry.kind === "array") entries.push({
|
|
86
|
+
name: field.name,
|
|
87
|
+
value: entry.values
|
|
88
|
+
});
|
|
89
|
+
else if (entry.kind === "none") {
|
|
90
|
+
if (intent.includeNoValueFields) entries.push({
|
|
91
|
+
name: field.name,
|
|
92
|
+
value: null
|
|
93
|
+
});
|
|
94
|
+
} else diagnose({
|
|
95
|
+
code: "submit-entry-unsupported",
|
|
96
|
+
message: `submit: '${field.name}' has an unsupported /V shape — skipped (would otherwise be silent data loss)`
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return entries;
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
7
102
|
//#region src/core/model.ts
|
|
8
103
|
const fieldKeyOf = (field) => field.fieldObjectNumber > 0 ? `obj:${field.fieldObjectNumber}` : `fqn:${field.name}`;
|
|
9
104
|
const initialModel = () => ({
|
|
@@ -217,507 +312,8 @@ function fillItemForWidget(model, annotObjectNumber) {
|
|
|
217
312
|
}
|
|
218
313
|
//#endregion
|
|
219
314
|
//#region src/mutationQueue.ts
|
|
220
|
-
/**
|
|
221
|
-
|
|
222
|
-
let tail = Promise.resolve();
|
|
223
|
-
return (operation) => {
|
|
224
|
-
const result = tail.then(operation, operation);
|
|
225
|
-
tail = result.then(() => void 0, () => void 0);
|
|
226
|
-
return result;
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
//#endregion
|
|
230
|
-
//#region src/scripting.ts
|
|
231
|
-
const refKey = (ref) => ref.kind === "objectNumber" ? `obj:${ref.fieldObjectNumber}` : `fqn:${ref.name}`;
|
|
232
|
-
const sameRef = (left, right) => refKey(left) === refKey(right);
|
|
233
|
-
const cloneValue = (value) => Array.isArray(value) ? [...value] : value;
|
|
234
|
-
const sameValue = (left, right) => Array.isArray(left) && Array.isArray(right) ? left.length === right.length && left.every((value, index) => value === right[index]) : left === right;
|
|
235
|
-
function cloneFields(fields) {
|
|
236
|
-
return fields.map((field) => ({
|
|
237
|
-
...field,
|
|
238
|
-
ref: { ...field.ref },
|
|
239
|
-
value: cloneValue(field.value),
|
|
240
|
-
defaultValue: cloneValue(field.defaultValue),
|
|
241
|
-
...field.options ? { options: field.options.map((option) => ({ ...option })) } : {}
|
|
242
|
-
}));
|
|
243
|
-
}
|
|
244
|
-
function fieldByRef(fields, ref) {
|
|
245
|
-
return fields.find((field) => sameRef(field.ref, ref));
|
|
246
|
-
}
|
|
247
|
-
function snapshotField(snapshot, ref) {
|
|
248
|
-
return snapshot.fields.find((field) => sameRef(field.ref, ref));
|
|
249
|
-
}
|
|
250
|
-
function scriptValueFromFormValue(field, value) {
|
|
251
|
-
if (field.family === "text" && value.type === "text") return value.value;
|
|
252
|
-
if ((field.family === "checkbox" || field.family === "radio") && value.type === "toggle") return value.state ?? "Off";
|
|
253
|
-
if (field.family === "combobox" && value.type === "choice") return value.values[0] ?? "";
|
|
254
|
-
if (field.family === "listbox" && value.type === "choice") return [...value.values];
|
|
255
|
-
throw new Error(`Form value type '${value.type}' does not match field family '${field.family}'`);
|
|
256
|
-
}
|
|
257
|
-
function formValueFromScriptValue(field) {
|
|
258
|
-
switch (field.family) {
|
|
259
|
-
case "text": return {
|
|
260
|
-
type: "text",
|
|
261
|
-
value: String(field.value ?? "")
|
|
262
|
-
};
|
|
263
|
-
case "checkbox":
|
|
264
|
-
case "radio": return {
|
|
265
|
-
type: "toggle",
|
|
266
|
-
state: field.value === null || field.value === void 0 || String(field.value) === "Off" ? null : String(field.value)
|
|
267
|
-
};
|
|
268
|
-
case "combobox": return {
|
|
269
|
-
type: "choice",
|
|
270
|
-
values: Array.isArray(field.value) ? field.value.slice(0, 1) : [String(field.value ?? "")]
|
|
271
|
-
};
|
|
272
|
-
case "listbox": return {
|
|
273
|
-
type: "choice",
|
|
274
|
-
values: Array.isArray(field.value) ? [...field.value] : [String(field.value ?? "")]
|
|
275
|
-
};
|
|
276
|
-
default: return null;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
function applyEffects(overlay, effects) {
|
|
280
|
-
for (const effect of effects) {
|
|
281
|
-
if (effect.kind === "reset") {
|
|
282
|
-
for (const ref of effect.refs) {
|
|
283
|
-
const field = fieldByRef(overlay.fields, ref);
|
|
284
|
-
if (!field) continue;
|
|
285
|
-
field.value = cloneValue(field.defaultValue);
|
|
286
|
-
overlay.resetKeys.add(refKey(ref));
|
|
287
|
-
}
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
const field = fieldByRef(overlay.fields, effect.ref);
|
|
291
|
-
if (!field) continue;
|
|
292
|
-
const key = refKey(effect.ref);
|
|
293
|
-
if (effect.kind === "setValue") {
|
|
294
|
-
field.value = scriptValueFromFormValue(field, effect.value);
|
|
295
|
-
overlay.resetKeys.delete(key);
|
|
296
|
-
} else if (effect.kind === "setDisplay") field.display = effect.display;
|
|
297
|
-
else overlay.appearances.set(key, {
|
|
298
|
-
ref: effect.ref,
|
|
299
|
-
text: effect.text
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
function canonicalEffects(overlay) {
|
|
304
|
-
const effects = [];
|
|
305
|
-
const resets = [];
|
|
306
|
-
for (const field of overlay.fields) {
|
|
307
|
-
const original = fieldByRef(overlay.original, field.ref);
|
|
308
|
-
if (!original) continue;
|
|
309
|
-
if (!sameValue(original.value, field.value)) {
|
|
310
|
-
const key = refKey(field.ref);
|
|
311
|
-
if (overlay.resetKeys.has(key) && sameValue(field.value, field.defaultValue)) resets.push(field.ref);
|
|
312
|
-
else {
|
|
313
|
-
const value = formValueFromScriptValue(field);
|
|
314
|
-
if (value) effects.push({
|
|
315
|
-
kind: "setValue",
|
|
316
|
-
ref: field.ref,
|
|
317
|
-
value
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
if (original.display !== field.display) effects.push({
|
|
322
|
-
kind: "setDisplay",
|
|
323
|
-
ref: field.ref,
|
|
324
|
-
display: field.display
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
if (resets.length > 0) effects.unshift({
|
|
328
|
-
kind: "reset",
|
|
329
|
-
refs: resets
|
|
330
|
-
});
|
|
331
|
-
effects.push(...Array.from(overlay.appearances.values(), ({ ref, text }) => ({
|
|
332
|
-
kind: "setAppearanceText",
|
|
333
|
-
ref,
|
|
334
|
-
text
|
|
335
|
-
})));
|
|
336
|
-
return effects;
|
|
337
|
-
}
|
|
338
|
-
function scriptError(message) {
|
|
339
|
-
return {
|
|
340
|
-
kind: "invalid-output",
|
|
341
|
-
message
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
function utf8Length(value) {
|
|
345
|
-
let length = 0;
|
|
346
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
347
|
-
const code = value.charCodeAt(index);
|
|
348
|
-
if (code < 128) length += 1;
|
|
349
|
-
else if (code < 2048) length += 2;
|
|
350
|
-
else if (code >= 55296 && code <= 56319 && index + 1 < value.length) {
|
|
351
|
-
length += 4;
|
|
352
|
-
index += 1;
|
|
353
|
-
} else length += 3;
|
|
354
|
-
}
|
|
355
|
-
return length;
|
|
356
|
-
}
|
|
357
|
-
function seedFrom(documentId, sequence) {
|
|
358
|
-
let hash = 2166136261;
|
|
359
|
-
for (const char of `${documentId}:${sequence}`) {
|
|
360
|
-
hash ^= char.charCodeAt(0);
|
|
361
|
-
hash = Math.imul(hash, 16777619);
|
|
362
|
-
}
|
|
363
|
-
return hash >>> 0;
|
|
364
|
-
}
|
|
365
|
-
function resolvedIdentity(doc, config) {
|
|
366
|
-
const claims = doc.security.identity;
|
|
367
|
-
const supplied = typeof config.identity === "function" ? config.identity() : config.identity ?? {};
|
|
368
|
-
return {
|
|
369
|
-
name: supplied.name ?? claims?.display_name ?? claims?.user_id ?? "",
|
|
370
|
-
loginName: supplied.loginName ?? claims?.user_id ?? "",
|
|
371
|
-
corporation: supplied.corporation ?? claims?.group_id ?? "",
|
|
372
|
-
email: supplied.email ?? ""
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
function pageNumberFor(meta, field) {
|
|
376
|
-
const pon = field.widgets.find((widget) => widget.pageObjectNumber > 0)?.pageObjectNumber;
|
|
377
|
-
if (!meta || pon === void 0) return 0;
|
|
378
|
-
const index = meta.pages.findIndex((page) => page.pageObjectNumber === pon);
|
|
379
|
-
return Math.max(0, index);
|
|
380
|
-
}
|
|
381
|
-
function statusFromEffects(result) {
|
|
382
|
-
if (result.results.some(({ status }) => status === "failed" || status === "skipped")) return "failed";
|
|
383
|
-
if (result.results.some(({ status }) => status === "rejected")) return "failed";
|
|
384
|
-
return result.results.some(({ status }) => status === "applied") ? "applied" : "unchanged";
|
|
385
|
-
}
|
|
386
|
-
var FormScriptingController = class {
|
|
387
|
-
constructor(options) {
|
|
388
|
-
this.options = options;
|
|
389
|
-
this.sandboxPromise = null;
|
|
390
|
-
this.booted = false;
|
|
391
|
-
this.disposed = false;
|
|
392
|
-
this.sequence = 0;
|
|
393
|
-
}
|
|
394
|
-
dispose() {
|
|
395
|
-
if (this.disposed) return;
|
|
396
|
-
this.disposed = true;
|
|
397
|
-
this.sandboxPromise?.then((sandbox) => sandbox.dispose()).catch(() => void 0);
|
|
398
|
-
}
|
|
399
|
-
async commit(snapshot, ref, proposed) {
|
|
400
|
-
return this.transact(snapshot, ref, proposed);
|
|
401
|
-
}
|
|
402
|
-
/** Execute one originating widget's activation action (`/A`, including `/Next`). */
|
|
403
|
-
async activate(snapshot, ref, action) {
|
|
404
|
-
return this.transact(snapshot, ref, void 0, action);
|
|
405
|
-
}
|
|
406
|
-
/** Run lazy document boot plus the `/CO` chain without a user field change. */
|
|
407
|
-
async recalculate(snapshot) {
|
|
408
|
-
const target = snapshot.calculationOrder.find((ref) => ref !== null && snapshotField(snapshot, ref) !== void 0) ?? snapshot.fields[0]?.ref;
|
|
409
|
-
if (!target) return {
|
|
410
|
-
status: "unchanged",
|
|
411
|
-
scripted: true,
|
|
412
|
-
effectsResult: null,
|
|
413
|
-
uiEffects: [],
|
|
414
|
-
diagnostics: []
|
|
415
|
-
};
|
|
416
|
-
return this.transact(snapshot, target);
|
|
417
|
-
}
|
|
418
|
-
async transact(snapshot, ref, proposed, activation) {
|
|
419
|
-
if (this.disposed) throw new Error("Form scripting controller is disposed");
|
|
420
|
-
const target = snapshotField(snapshot, ref);
|
|
421
|
-
if (!target) return this.failed([], [], scriptError(`Form field '${refKey(ref)}' no longer exists`));
|
|
422
|
-
const original = scriptFieldsFromSnapshot(snapshot);
|
|
423
|
-
const overlay = {
|
|
424
|
-
original,
|
|
425
|
-
fields: cloneFields(original),
|
|
426
|
-
resetKeys: /* @__PURE__ */ new Set(),
|
|
427
|
-
appearances: /* @__PURE__ */ new Map()
|
|
428
|
-
};
|
|
429
|
-
const targetInput = fieldByRef(overlay.fields, ref);
|
|
430
|
-
if (!targetInput) return this.failed([], [], scriptError(`Form field '${refKey(ref)}' has no script view`));
|
|
431
|
-
const hasProposedValue = proposed !== void 0;
|
|
432
|
-
let proposedValue = cloneValue(targetInput.value);
|
|
433
|
-
if (proposed) try {
|
|
434
|
-
proposedValue = scriptValueFromFormValue(targetInput, proposed);
|
|
435
|
-
} catch (error) {
|
|
436
|
-
return this.failed([], [], scriptError(error instanceof Error ? error.message : String(error)));
|
|
437
|
-
}
|
|
438
|
-
const uiEffects = [];
|
|
439
|
-
const diagnostics = [];
|
|
440
|
-
const meta = this.options.document();
|
|
441
|
-
const nowMs = this.options.config.now?.() ?? Date.now();
|
|
442
|
-
const inputBase = {
|
|
443
|
-
document: {
|
|
444
|
-
id: this.options.doc.id,
|
|
445
|
-
fileName: this.options.config.fileName?.() ?? meta?.name ?? this.options.doc.id,
|
|
446
|
-
pageCount: meta?.pageCount ?? 0,
|
|
447
|
-
pageNumber: pageNumberFor(meta, target)
|
|
448
|
-
},
|
|
449
|
-
identity: resolvedIdentity(this.options.doc, this.options.config),
|
|
450
|
-
environment: {
|
|
451
|
-
nowMs,
|
|
452
|
-
utcOffsetMinutes: this.options.config.utcOffsetMinutes?.() ?? -new Date(nowMs).getTimezoneOffset(),
|
|
453
|
-
randomSeed: this.options.config.randomSeed?.() ?? seedFrom(this.options.doc.id, this.sequence++)
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
const budget = this.options.config.budget ?? DEFAULT_SCRIPT_BUDGET;
|
|
457
|
-
let executionMs = 0;
|
|
458
|
-
let aggregateOutputSize = 0;
|
|
459
|
-
const consume = (output, phase) => {
|
|
460
|
-
aggregateOutputSize += utf8Length(JSON.stringify(output));
|
|
461
|
-
uiEffects.push(...output.uiEffects.map((effect) => ({
|
|
462
|
-
...effect,
|
|
463
|
-
phase
|
|
464
|
-
})));
|
|
465
|
-
diagnostics.push(...output.diagnostics);
|
|
466
|
-
if (aggregateOutputSize > budget.maxOutputBytes) return {
|
|
467
|
-
kind: "budget",
|
|
468
|
-
message: `Form script transaction output exceeded ${budget.maxOutputBytes} bytes`
|
|
469
|
-
};
|
|
470
|
-
return output.error ?? null;
|
|
471
|
-
};
|
|
472
|
-
const remainingBudget = () => {
|
|
473
|
-
const remaining = budget.maxExecutionMs - executionMs;
|
|
474
|
-
return remaining <= 0 ? null : {
|
|
475
|
-
...budget,
|
|
476
|
-
maxExecutionMs: remaining
|
|
477
|
-
};
|
|
478
|
-
};
|
|
479
|
-
const run = async (kind, source, input) => {
|
|
480
|
-
const remaining = remainingBudget();
|
|
481
|
-
if (!remaining) return { error: {
|
|
482
|
-
kind: "budget",
|
|
483
|
-
message: "Form script transaction timed out"
|
|
484
|
-
} };
|
|
485
|
-
let output;
|
|
486
|
-
try {
|
|
487
|
-
const sandbox = await this.sandbox();
|
|
488
|
-
const startedAt = Date.now();
|
|
489
|
-
output = kind === "boot" ? sandbox.boot(source, input, remaining) : sandbox.run(source, input, remaining);
|
|
490
|
-
executionMs += Date.now() - startedAt;
|
|
491
|
-
} catch (error) {
|
|
492
|
-
return { error: scriptError(error instanceof Error ? error.message : String(error)) };
|
|
493
|
-
}
|
|
494
|
-
const error = consume(output, kind === "boot" ? "boot" : "user");
|
|
495
|
-
return error ? { error } : { output };
|
|
496
|
-
};
|
|
497
|
-
if (!this.booted) {
|
|
498
|
-
this.booted = true;
|
|
499
|
-
let sources = [];
|
|
500
|
-
let bootError = null;
|
|
501
|
-
try {
|
|
502
|
-
sources = (this.options.doc.actions ? await this.options.doc.actions.read() : null)?.nameTreeScripts.map(({ action }) => javaScriptProgramFromActionTree(action)) ?? [];
|
|
503
|
-
} catch (error) {
|
|
504
|
-
bootError = scriptError(error instanceof Error ? error.message : String(error));
|
|
505
|
-
}
|
|
506
|
-
if (!bootError && sources.length > 0) {
|
|
507
|
-
const boot = await run("boot", sources, {
|
|
508
|
-
...inputBase,
|
|
509
|
-
fields: overlay.fields,
|
|
510
|
-
event: { kind: "name-tree-boot" }
|
|
511
|
-
});
|
|
512
|
-
if (boot.error) bootError = boot.error;
|
|
513
|
-
else applyEffects(overlay, boot.output.formEffects);
|
|
514
|
-
}
|
|
515
|
-
if (bootError) diagnostics.push({
|
|
516
|
-
code: "script-error",
|
|
517
|
-
message: `Document boot script failed (continuing without it): ${bootError.message}`
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
const bootEffects = canonicalEffects(overlay);
|
|
521
|
-
if (activation) {
|
|
522
|
-
let program;
|
|
523
|
-
try {
|
|
524
|
-
program = javaScriptProgramFromActionTree(activation);
|
|
525
|
-
} catch (error) {
|
|
526
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
527
|
-
}
|
|
528
|
-
if (program) {
|
|
529
|
-
const activated = await run("run", program, {
|
|
530
|
-
...inputBase,
|
|
531
|
-
fields: overlay.fields,
|
|
532
|
-
event: {
|
|
533
|
-
kind: "widget-activate",
|
|
534
|
-
target: ref,
|
|
535
|
-
source: ref,
|
|
536
|
-
value: targetInput.value
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
if (activated.error) return this.failed(uiEffects, diagnostics, activated.error);
|
|
540
|
-
applyEffects(overlay, activated.output.formEffects);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
if (!activation && hasProposedValue && target.actions?.keystroke && target.family === "text") {
|
|
544
|
-
let program;
|
|
545
|
-
try {
|
|
546
|
-
program = javaScriptProgramFromActionTree(target.actions.keystroke);
|
|
547
|
-
} catch (error) {
|
|
548
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
549
|
-
}
|
|
550
|
-
if (program) {
|
|
551
|
-
const oldValue = String(targetInput.value ?? "");
|
|
552
|
-
const key = await run("run", program, {
|
|
553
|
-
...inputBase,
|
|
554
|
-
fields: overlay.fields,
|
|
555
|
-
event: {
|
|
556
|
-
kind: "field-keystroke",
|
|
557
|
-
target: ref,
|
|
558
|
-
source: ref,
|
|
559
|
-
value: oldValue,
|
|
560
|
-
change: String(proposedValue ?? ""),
|
|
561
|
-
selStart: 0,
|
|
562
|
-
selEnd: oldValue.length,
|
|
563
|
-
willCommit: true
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
if (key.error) return this.failed(uiEffects, diagnostics, key.error);
|
|
567
|
-
if (!key.output.event.rc) return this.finishRejected(bootEffects, uiEffects, diagnostics);
|
|
568
|
-
applyEffects(overlay, key.output.formEffects);
|
|
569
|
-
const event = key.output.event;
|
|
570
|
-
const base = String(event.value ?? "");
|
|
571
|
-
proposedValue = base.slice(0, event.selStart) + event.change + base.slice(Math.max(event.selStart, event.selEnd));
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
if (!activation && hasProposedValue) {
|
|
575
|
-
targetInput.value = cloneValue(proposedValue);
|
|
576
|
-
overlay.resetKeys.delete(refKey(ref));
|
|
577
|
-
}
|
|
578
|
-
if (!activation && hasProposedValue && target.actions?.validate) {
|
|
579
|
-
let program;
|
|
580
|
-
try {
|
|
581
|
-
program = javaScriptProgramFromActionTree(target.actions.validate);
|
|
582
|
-
} catch (error) {
|
|
583
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
584
|
-
}
|
|
585
|
-
if (program) {
|
|
586
|
-
const validation = await run("run", program, {
|
|
587
|
-
...inputBase,
|
|
588
|
-
fields: overlay.fields,
|
|
589
|
-
event: {
|
|
590
|
-
kind: "field-validate",
|
|
591
|
-
target: ref,
|
|
592
|
-
source: ref,
|
|
593
|
-
value: proposedValue,
|
|
594
|
-
willCommit: true
|
|
595
|
-
}
|
|
596
|
-
});
|
|
597
|
-
if (validation.error) return this.failed(uiEffects, diagnostics, validation.error);
|
|
598
|
-
if (!validation.output.event.rc) return this.finishRejected(bootEffects, uiEffects, diagnostics);
|
|
599
|
-
targetInput.value = cloneValue(validation.output.event.value);
|
|
600
|
-
applyEffects(overlay, validation.output.formEffects);
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
const formatRefs = !activation && hasProposedValue ? [ref] : [];
|
|
604
|
-
for (const calculationRef of activation ? [] : snapshot.calculationOrder) {
|
|
605
|
-
if (!calculationRef) continue;
|
|
606
|
-
const calculated = snapshotField(snapshot, calculationRef);
|
|
607
|
-
const tree = calculated?.actions?.calculate;
|
|
608
|
-
if (!calculated || !tree) continue;
|
|
609
|
-
let program;
|
|
610
|
-
try {
|
|
611
|
-
program = javaScriptProgramFromActionTree(tree);
|
|
612
|
-
} catch (error) {
|
|
613
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
614
|
-
}
|
|
615
|
-
const calculatedInput = fieldByRef(overlay.fields, calculationRef);
|
|
616
|
-
if (program && calculatedInput) {
|
|
617
|
-
const calculation = await run("run", program, {
|
|
618
|
-
...inputBase,
|
|
619
|
-
fields: overlay.fields,
|
|
620
|
-
event: {
|
|
621
|
-
kind: "field-calculate",
|
|
622
|
-
target: calculationRef,
|
|
623
|
-
...hasProposedValue ? { source: ref } : {},
|
|
624
|
-
value: calculatedInput.value
|
|
625
|
-
}
|
|
626
|
-
});
|
|
627
|
-
if (calculation.error) return this.failed(uiEffects, diagnostics, calculation.error);
|
|
628
|
-
applyEffects(overlay, calculation.output.formEffects);
|
|
629
|
-
}
|
|
630
|
-
if (!formatRefs.some((candidate) => sameRef(candidate, calculationRef))) formatRefs.push(calculationRef);
|
|
631
|
-
}
|
|
632
|
-
for (const formatRef of formatRefs) {
|
|
633
|
-
const field = snapshotField(snapshot, formatRef);
|
|
634
|
-
const tree = field?.actions?.format;
|
|
635
|
-
const overlayField = fieldByRef(overlay.fields, formatRef);
|
|
636
|
-
if (!field || !tree || !overlayField) continue;
|
|
637
|
-
let program;
|
|
638
|
-
try {
|
|
639
|
-
program = javaScriptProgramFromActionTree(tree);
|
|
640
|
-
} catch (error) {
|
|
641
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
642
|
-
}
|
|
643
|
-
if (!program) continue;
|
|
644
|
-
const format = await run("run", program, {
|
|
645
|
-
...inputBase,
|
|
646
|
-
fields: overlay.fields,
|
|
647
|
-
event: {
|
|
648
|
-
kind: "field-format",
|
|
649
|
-
target: formatRef,
|
|
650
|
-
...hasProposedValue ? { source: ref } : {},
|
|
651
|
-
value: overlayField.value
|
|
652
|
-
}
|
|
653
|
-
});
|
|
654
|
-
if (format.error) return this.failed(uiEffects, diagnostics, format.error);
|
|
655
|
-
applyEffects(overlay, format.output.formEffects);
|
|
656
|
-
}
|
|
657
|
-
const effects = canonicalEffects(overlay);
|
|
658
|
-
if (effects.length + uiEffects.length > budget.maxEffects) return this.failed(uiEffects, diagnostics, {
|
|
659
|
-
kind: "budget",
|
|
660
|
-
message: `Form script transaction exceeded ${budget.maxEffects} effects`
|
|
661
|
-
});
|
|
662
|
-
if (effects.length === 0) return {
|
|
663
|
-
status: "unchanged",
|
|
664
|
-
scripted: true,
|
|
665
|
-
effectsResult: null,
|
|
666
|
-
uiEffects,
|
|
667
|
-
diagnostics
|
|
668
|
-
};
|
|
669
|
-
if (!this.options.doc.forms.applyEffects) return this.failed(uiEffects, diagnostics, scriptError("This engine does not support batched form effects"));
|
|
670
|
-
const effectsResult = await this.options.doc.forms.applyEffects(effects);
|
|
671
|
-
const status = statusFromEffects(effectsResult);
|
|
672
|
-
return {
|
|
673
|
-
status,
|
|
674
|
-
scripted: true,
|
|
675
|
-
effectsResult,
|
|
676
|
-
uiEffects,
|
|
677
|
-
diagnostics,
|
|
678
|
-
...status === "failed" ? { error: scriptError("One or more native form effects failed") } : {}
|
|
679
|
-
};
|
|
680
|
-
}
|
|
681
|
-
async sandbox() {
|
|
682
|
-
this.sandboxPromise ?? (this.sandboxPromise = this.options.sandboxFactory().then((sandbox) => {
|
|
683
|
-
if (this.disposed) sandbox.dispose();
|
|
684
|
-
return sandbox;
|
|
685
|
-
}));
|
|
686
|
-
return this.sandboxPromise;
|
|
687
|
-
}
|
|
688
|
-
failed(uiEffects, diagnostics, error) {
|
|
689
|
-
return {
|
|
690
|
-
status: "failed",
|
|
691
|
-
scripted: true,
|
|
692
|
-
effectsResult: null,
|
|
693
|
-
uiEffects,
|
|
694
|
-
diagnostics,
|
|
695
|
-
error
|
|
696
|
-
};
|
|
697
|
-
}
|
|
698
|
-
async finishRejected(bootEffects, uiEffects, diagnostics) {
|
|
699
|
-
if (bootEffects.length === 0) return {
|
|
700
|
-
status: "rejected",
|
|
701
|
-
scripted: true,
|
|
702
|
-
effectsResult: null,
|
|
703
|
-
uiEffects,
|
|
704
|
-
diagnostics
|
|
705
|
-
};
|
|
706
|
-
if (!this.options.doc.forms.applyEffects) return this.failed(uiEffects, diagnostics, scriptError("Batched form effects are unavailable"));
|
|
707
|
-
const effectsResult = await this.options.doc.forms.applyEffects(bootEffects);
|
|
708
|
-
if (statusFromEffects(effectsResult) === "failed") return this.failed(uiEffects, diagnostics, scriptError("A name-tree boot effect failed"));
|
|
709
|
-
return {
|
|
710
|
-
status: "rejected",
|
|
711
|
-
scripted: true,
|
|
712
|
-
effectsResult,
|
|
713
|
-
uiEffects,
|
|
714
|
-
diagnostics
|
|
715
|
-
};
|
|
716
|
-
}
|
|
717
|
-
};
|
|
718
|
-
function createFormScriptingController(options) {
|
|
719
|
-
return new FormScriptingController(options);
|
|
720
|
-
}
|
|
315
|
+
/** The core serial queue under its historical form-plugin name. */
|
|
316
|
+
const createSerialMutationQueue = createSerialQueue;
|
|
721
317
|
//#endregion
|
|
722
318
|
//#region src/capability.ts
|
|
723
319
|
/** PDF user-space rect (y-up) → content-space box (y-down, crop-relative). */
|
|
@@ -757,29 +353,22 @@ function createFormCapability(ctx, options = {}) {
|
|
|
757
353
|
};
|
|
758
354
|
};
|
|
759
355
|
const enqueueMutation = createSerialMutationQueue();
|
|
760
|
-
const
|
|
356
|
+
const actionsHost = ctx.tryGet(ActionsToken$1);
|
|
357
|
+
const realm = actionsHost?.scriptRealm ?? null;
|
|
358
|
+
const scripting = realm && ctx.doc ? createFormScriptingController({
|
|
761
359
|
doc: ctx.doc,
|
|
762
360
|
document: () => ctx.document(),
|
|
763
|
-
|
|
764
|
-
|
|
361
|
+
transaction: realm.transaction.bind(realm),
|
|
362
|
+
budget: realm.budget
|
|
765
363
|
}) : null;
|
|
766
364
|
if (scripting) ctx.cleanup(() => scripting.dispose());
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
globalThis.console?.error("[form] scripting observer failed:", error);
|
|
775
|
-
}
|
|
776
|
-
};
|
|
777
|
-
for (const effect of result.uiEffects) {
|
|
778
|
-
observe(options.scripting?.onUiEffect ? () => options.scripting.onUiEffect(effect) : void 0);
|
|
779
|
-
observe(uiEffectProvider ? () => uiEffectProvider(effect) : void 0);
|
|
780
|
-
}
|
|
781
|
-
for (const diagnostic of result.diagnostics) observe(options.scripting?.onDiagnostic ? () => options.scripting.onDiagnostic(diagnostic) : void 0);
|
|
782
|
-
if (result.error) observe(options.scripting?.onError ? () => options.scripting.onError(result.error) : void 0);
|
|
365
|
+
/** Every script surface (UI effects, diagnostics, errors) flows through
|
|
366
|
+
* the actions plugin's ONE port — origin/phase attached (D9). */
|
|
367
|
+
const surfaceViaActions = (result, origin) => {
|
|
368
|
+
actionsHost?.surfaceScriptCommit(result, {
|
|
369
|
+
origin,
|
|
370
|
+
realm: "document"
|
|
371
|
+
});
|
|
783
372
|
};
|
|
784
373
|
const can = (cap) => ctx.doc?.security.allows(cap) ?? false;
|
|
785
374
|
let refreshPromise = null;
|
|
@@ -860,8 +449,8 @@ function createFormCapability(ctx, options = {}) {
|
|
|
860
449
|
const doc = ctx.doc;
|
|
861
450
|
if (!doc) throw new Error("no document");
|
|
862
451
|
if (scripting) {
|
|
863
|
-
const result = await scripting.commit(
|
|
864
|
-
|
|
452
|
+
const result = await scripting.commit(ref, value);
|
|
453
|
+
surfaceViaActions(result, "user");
|
|
865
454
|
if (result.effectsResult !== null) await refresh(true);
|
|
866
455
|
return result;
|
|
867
456
|
}
|
|
@@ -913,8 +502,7 @@ function createFormCapability(ctx, options = {}) {
|
|
|
913
502
|
return annotation?.subtype === "widget" ? annotation.actions?.activate ?? null : null;
|
|
914
503
|
};
|
|
915
504
|
const activateWidgetNow = async (key, annotationRef) => {
|
|
916
|
-
|
|
917
|
-
if (!doc) throw new Error("no document");
|
|
505
|
+
if (!ctx.doc) throw new Error("no document");
|
|
918
506
|
if (!scripting) return {
|
|
919
507
|
status: "unchanged",
|
|
920
508
|
scripted: false,
|
|
@@ -930,11 +518,118 @@ function createFormCapability(ctx, options = {}) {
|
|
|
930
518
|
uiEffects: [],
|
|
931
519
|
diagnostics: []
|
|
932
520
|
};
|
|
933
|
-
const result = await scripting.activate(
|
|
934
|
-
|
|
521
|
+
const result = await scripting.activate(refKeyOf(key), action);
|
|
522
|
+
surfaceViaActions(result, "user");
|
|
935
523
|
if (result.effectsResult !== null) await refresh(true);
|
|
936
524
|
return result;
|
|
937
525
|
};
|
|
526
|
+
const NOT_SCRIPTED = {
|
|
527
|
+
status: "unchanged",
|
|
528
|
+
scripted: false,
|
|
529
|
+
effectsResult: null,
|
|
530
|
+
uiEffects: [],
|
|
531
|
+
diagnostics: []
|
|
532
|
+
};
|
|
533
|
+
/**
|
|
534
|
+
* The shared reset core: one effects batch → refresh → V/C/F
|
|
535
|
+
* recalculation when the transaction port is present. Ridden by BOTH
|
|
536
|
+
* doors — the /ResetForm executor and the public `reset(key)` — so the
|
|
537
|
+
* two can never diverge again (the "reset() asymmetry" fix). `origin` is
|
|
538
|
+
* PRESERVED through recalculation surfacing: a lifecycle/hover ResetForm
|
|
539
|
+
* can no longer launder its alerts into user origin.
|
|
540
|
+
*/
|
|
541
|
+
const applyResetBatch = async (refs, origin) => {
|
|
542
|
+
const doc = ctx.doc;
|
|
543
|
+
if (!doc) throw new Error("no document");
|
|
544
|
+
if (!doc.forms.applyEffects) {
|
|
545
|
+
const result = {
|
|
546
|
+
...NOT_SCRIPTED,
|
|
547
|
+
diagnostics: [{
|
|
548
|
+
code: "unsupported-api",
|
|
549
|
+
message: "this engine has no form-effects batch door"
|
|
550
|
+
}]
|
|
551
|
+
};
|
|
552
|
+
surfaceViaActions(result, origin);
|
|
553
|
+
return result;
|
|
554
|
+
}
|
|
555
|
+
if (refs.length === 0) return NOT_SCRIPTED;
|
|
556
|
+
const effectsResult = await doc.forms.applyEffects([{
|
|
557
|
+
kind: "reset",
|
|
558
|
+
refs
|
|
559
|
+
}]);
|
|
560
|
+
const resetFailed = effectsResult.results.some((entry) => entry.status === "failed" || entry.status === "rejected");
|
|
561
|
+
await refresh(true);
|
|
562
|
+
if (resetFailed) return {
|
|
563
|
+
status: "failed",
|
|
564
|
+
scripted: false,
|
|
565
|
+
effectsResult,
|
|
566
|
+
uiEffects: [],
|
|
567
|
+
diagnostics: []
|
|
568
|
+
};
|
|
569
|
+
let recalc = null;
|
|
570
|
+
if (scripting) {
|
|
571
|
+
recalc = await scripting.recalculate();
|
|
572
|
+
surfaceViaActions(recalc, origin);
|
|
573
|
+
if (recalc.effectsResult !== null) await refresh(true);
|
|
574
|
+
}
|
|
575
|
+
return {
|
|
576
|
+
status: "applied",
|
|
577
|
+
scripted: recalc !== null,
|
|
578
|
+
effectsResult,
|
|
579
|
+
uiEffects: recalc?.uiEffects ?? [],
|
|
580
|
+
diagnostics: recalc?.diagnostics ?? [],
|
|
581
|
+
...recalc?.error ? { error: recalc.error } : {}
|
|
582
|
+
};
|
|
583
|
+
};
|
|
584
|
+
const resetFormAction = (targets, exclude, origin = "user") => enqueueMutation(async () => {
|
|
585
|
+
const doc = ctx.doc;
|
|
586
|
+
if (!doc) throw new Error("no document");
|
|
587
|
+
const { selected } = resolveFieldSelection((await doc.forms.list()).fields, targets, exclude);
|
|
588
|
+
const resettable = selected.filter((f) => f.family !== "pushbutton" && f.family !== "signature");
|
|
589
|
+
return applyResetBatch(resettable.map((f) => f.ref), origin);
|
|
590
|
+
});
|
|
591
|
+
/**
|
|
592
|
+
* The submit dataset resolver (Phase 4, D7): a FRESH engine read — never
|
|
593
|
+
* the cached model, so no staleness class exists — then the pure ISO
|
|
594
|
+
* builder. Selection/veto/value semantics live in `field-selection.ts`;
|
|
595
|
+
* this door only supplies the live fields and assembles the request.
|
|
596
|
+
*/
|
|
597
|
+
const resolveSubmitDataset = async (intent, actionCtx, diagnose) => {
|
|
598
|
+
const doc = ctx.doc;
|
|
599
|
+
if (!doc) throw new Error("no document");
|
|
600
|
+
const snapshot = await doc.forms.list();
|
|
601
|
+
return {
|
|
602
|
+
url: intent.url,
|
|
603
|
+
method: intent.method,
|
|
604
|
+
format: intent.format,
|
|
605
|
+
flagsRaw: intent.flagsRaw,
|
|
606
|
+
...intent.charSet === void 0 ? {} : { charSet: intent.charSet },
|
|
607
|
+
entries: buildSubmitEntries(snapshot.fields, intent, diagnose),
|
|
608
|
+
origin: actionCtx.origin,
|
|
609
|
+
event: actionCtx.event
|
|
610
|
+
};
|
|
611
|
+
};
|
|
612
|
+
const widgetSource = (key, annotationRef) => ({
|
|
613
|
+
kind: "widget",
|
|
614
|
+
field: refKeyOf(key),
|
|
615
|
+
annotation: annotationRef,
|
|
616
|
+
pon: annotationRef.pageObjectNumber
|
|
617
|
+
});
|
|
618
|
+
/** One shared hover pump (Exit→Enter as one ordered pair; intermediates
|
|
619
|
+
* skipped), created on first use against the resolved actions capability. */
|
|
620
|
+
let widgetPump = null;
|
|
621
|
+
const widgetHoverPump = (actions) => {
|
|
622
|
+
widgetPump ?? (widgetPump = createHoverPump(actions.dispatch));
|
|
623
|
+
return widgetPump;
|
|
624
|
+
};
|
|
625
|
+
const widgetHoverFlags = (annotationRef) => {
|
|
626
|
+
const loaded = annotationHost?.get(annotationRef);
|
|
627
|
+
if (loaded?.subtype !== "widget") return null;
|
|
628
|
+
return {
|
|
629
|
+
enter: Boolean(loaded.actions?.cursorEnter?.root),
|
|
630
|
+
exit: Boolean(loaded.actions?.cursorExit?.root)
|
|
631
|
+
};
|
|
632
|
+
};
|
|
938
633
|
const nudgeAnnotations = (pons) => {
|
|
939
634
|
if (!annotationHost) return;
|
|
940
635
|
for (const pon of new Set(pons)) annotationHost.reloadPage(pon);
|
|
@@ -1088,12 +783,27 @@ function createFormCapability(ctx, options = {}) {
|
|
|
1088
783
|
key
|
|
1089
784
|
});
|
|
1090
785
|
try {
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
key
|
|
1095
|
-
field
|
|
1096
|
-
|
|
786
|
+
if (doc.forms.applyEffects) {
|
|
787
|
+
const result = await applyResetBatch([refKeyOf(key)], "user");
|
|
788
|
+
if (result.status === "failed") throw new Error(result.effectsResult?.results.find((entry) => entry.status === "failed" || entry.status === "rejected")?.error?.message ?? "reset failed");
|
|
789
|
+
const field = fieldByKey(model(), key);
|
|
790
|
+
if (field) apply({
|
|
791
|
+
t: "writeDone",
|
|
792
|
+
key,
|
|
793
|
+
field
|
|
794
|
+
});
|
|
795
|
+
else apply({
|
|
796
|
+
t: "writeFailed",
|
|
797
|
+
key
|
|
798
|
+
});
|
|
799
|
+
} else {
|
|
800
|
+
const result = await doc.forms.reset(refKeyOf(key));
|
|
801
|
+
apply({
|
|
802
|
+
t: "writeDone",
|
|
803
|
+
key,
|
|
804
|
+
field: result.field
|
|
805
|
+
});
|
|
806
|
+
}
|
|
1097
807
|
} catch (err) {
|
|
1098
808
|
apply({
|
|
1099
809
|
t: "writeFailed",
|
|
@@ -1104,9 +814,96 @@ function createFormCapability(ctx, options = {}) {
|
|
|
1104
814
|
});
|
|
1105
815
|
},
|
|
1106
816
|
commitValue: (ref, value) => enqueueMutation(() => commitValueNow(ref, value)),
|
|
1107
|
-
activateWidget: (key, annotationRef) =>
|
|
1108
|
-
|
|
1109
|
-
|
|
817
|
+
activateWidget: async (key, annotationRef) => {
|
|
818
|
+
const actions = ctx.tryGet(ActionsToken);
|
|
819
|
+
if (actions) {
|
|
820
|
+
const result = await actions.dispatch({
|
|
821
|
+
scope: "activate",
|
|
822
|
+
ref: annotationRef,
|
|
823
|
+
pon: annotationRef.pageObjectNumber,
|
|
824
|
+
source: widgetSource(key, annotationRef)
|
|
825
|
+
});
|
|
826
|
+
if (!(result.status === "inert" && result.steps.length === 0 && result.diagnostics.length === 0)) return {
|
|
827
|
+
kind: "dispatched",
|
|
828
|
+
result
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
return {
|
|
832
|
+
kind: "form",
|
|
833
|
+
result: await enqueueMutation(() => activateWidgetNow(key, annotationRef))
|
|
834
|
+
};
|
|
835
|
+
},
|
|
836
|
+
notifyWidgetEvent: (key, annotationRef, event) => {
|
|
837
|
+
const actions = ctx.tryGet(ActionsToken);
|
|
838
|
+
if (!actions) return;
|
|
839
|
+
const source = widgetSource(key, annotationRef);
|
|
840
|
+
if (event === "cursorEnter" || event === "cursorExit") {
|
|
841
|
+
if (event === "cursorExit") {
|
|
842
|
+
widgetHoverPump(actions).hover(null);
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
const flags = widgetHoverFlags(annotationRef);
|
|
846
|
+
if (flags && !flags.enter && !flags.exit) return;
|
|
847
|
+
widgetHoverPump(actions).hover({
|
|
848
|
+
ref: annotationRef,
|
|
849
|
+
pon: annotationRef.pageObjectNumber,
|
|
850
|
+
source,
|
|
851
|
+
...flags ? { events: flags } : {}
|
|
852
|
+
});
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
actions.dispatch({
|
|
856
|
+
scope: "annotation",
|
|
857
|
+
event,
|
|
858
|
+
ref: annotationRef,
|
|
859
|
+
pon: annotationRef.pageObjectNumber,
|
|
860
|
+
source
|
|
861
|
+
});
|
|
862
|
+
},
|
|
863
|
+
resetFormAction,
|
|
864
|
+
resolveSubmitDataset,
|
|
865
|
+
commitScriptFormEffects: async (effects) => {
|
|
866
|
+
const doc = ctx.doc;
|
|
867
|
+
if (!doc?.forms.applyEffects) return {
|
|
868
|
+
results: effects.map((_, index) => ({
|
|
869
|
+
index,
|
|
870
|
+
status: "failed",
|
|
871
|
+
fields: [],
|
|
872
|
+
changedWidgets: [],
|
|
873
|
+
error: {
|
|
874
|
+
code: "NotSupported",
|
|
875
|
+
message: "no form-effects batch door"
|
|
876
|
+
}
|
|
877
|
+
})),
|
|
878
|
+
changedWidgets: [],
|
|
879
|
+
meta: null
|
|
880
|
+
};
|
|
881
|
+
let result;
|
|
882
|
+
try {
|
|
883
|
+
result = await doc.forms.applyEffects(effects);
|
|
884
|
+
} catch (error) {
|
|
885
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
886
|
+
return {
|
|
887
|
+
results: effects.map((_, index) => ({
|
|
888
|
+
index,
|
|
889
|
+
status: "failed",
|
|
890
|
+
fields: [],
|
|
891
|
+
changedWidgets: [],
|
|
892
|
+
error: {
|
|
893
|
+
code: "Refused",
|
|
894
|
+
message
|
|
895
|
+
}
|
|
896
|
+
})),
|
|
897
|
+
changedWidgets: [],
|
|
898
|
+
meta: null
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
await refresh(true);
|
|
902
|
+
if (annotationHost) {
|
|
903
|
+
const pons = new Set(result.changedWidgets.map((widget) => widget.pageObjectNumber).filter((pon) => pon > 0));
|
|
904
|
+
for (const pon of pons) await annotationHost.reloadPage(pon);
|
|
905
|
+
}
|
|
906
|
+
return result;
|
|
1110
907
|
},
|
|
1111
908
|
setValue: (ref, value) => enqueueMutation(async () => {
|
|
1112
909
|
const doc = ctx.doc;
|
|
@@ -1365,9 +1162,6 @@ const initialFormState = () => ({ model: initialModel() });
|
|
|
1365
1162
|
/** Dumb store: the pure core computes the next model; the shell dispatches it. */
|
|
1366
1163
|
const formReducer = (state, action) => action.type === "SET_MODEL" ? { model: action.model } : state;
|
|
1367
1164
|
//#endregion
|
|
1368
|
-
//#region src/types.ts
|
|
1369
|
-
const FormToken = createCapabilityToken("form");
|
|
1370
|
-
//#endregion
|
|
1371
1165
|
//#region src/form.plugin.ts
|
|
1372
1166
|
/**
|
|
1373
1167
|
* The form plugin: the FIELD plane. Document-scoped; requires the
|
|
@@ -1382,17 +1176,17 @@ const FormToken = createCapabilityToken("form");
|
|
|
1382
1176
|
*/
|
|
1383
1177
|
const formPlugin = (options = {}) => definePlugin({
|
|
1384
1178
|
id: "form",
|
|
1385
|
-
token: FormToken,
|
|
1179
|
+
token: FormToken$1,
|
|
1386
1180
|
scope: "document",
|
|
1387
1181
|
requires: [InteractionToken],
|
|
1388
|
-
optional: [AnnotationToken],
|
|
1182
|
+
optional: [AnnotationToken, ActionsToken],
|
|
1389
1183
|
initialState: initialFormState,
|
|
1390
1184
|
reduce: formReducer,
|
|
1391
1185
|
capability: (ctx) => createFormCapability(ctx, options),
|
|
1392
1186
|
effects: registerFormEffects,
|
|
1393
1187
|
init: (ctx) => {
|
|
1394
1188
|
const interaction = ctx.get(InteractionToken);
|
|
1395
|
-
const form = ctx.get(FormToken);
|
|
1189
|
+
const form = ctx.get(FormToken$1);
|
|
1396
1190
|
interaction.registerTool({
|
|
1397
1191
|
id: "form-fill",
|
|
1398
1192
|
cursor: "default",
|
|
@@ -1423,6 +1217,49 @@ const formPlugin = (options = {}) => definePlugin({
|
|
|
1423
1217
|
matches: (a) => a.subtype.startsWith("widget"),
|
|
1424
1218
|
engaged: () => interaction.activeTool()?.enables.has("form-fill") ?? false
|
|
1425
1219
|
});
|
|
1220
|
+
const actions = ctx.tryGet(ActionsToken$1);
|
|
1221
|
+
if (actions) {
|
|
1222
|
+
ctx.cleanup(actions.registerFormCommitSink((effects) => {
|
|
1223
|
+
const formHost = ctx.tryGet(FormToken$1);
|
|
1224
|
+
if (!formHost) return Promise.resolve({
|
|
1225
|
+
results: effects.map((_, index) => ({
|
|
1226
|
+
index,
|
|
1227
|
+
status: "failed",
|
|
1228
|
+
fields: [],
|
|
1229
|
+
changedWidgets: []
|
|
1230
|
+
})),
|
|
1231
|
+
changedWidgets: [],
|
|
1232
|
+
meta: null
|
|
1233
|
+
});
|
|
1234
|
+
return formHost.commitScriptFormEffects(effects);
|
|
1235
|
+
}));
|
|
1236
|
+
ctx.cleanup(actions.registerSubmitResolver((intent, actionCtx, diagnose) => {
|
|
1237
|
+
const formHost = ctx.tryGet(FormToken$1);
|
|
1238
|
+
if (!formHost) throw new Error("form plugin unavailable");
|
|
1239
|
+
return formHost.resolveSubmitDataset(intent, actionCtx, diagnose);
|
|
1240
|
+
}));
|
|
1241
|
+
ctx.cleanup(actions.registerExecutor("reset-form", async (node, actionCtx) => {
|
|
1242
|
+
if (node.type !== "reset-form") return {
|
|
1243
|
+
status: "inert",
|
|
1244
|
+
reason: "not a reset-form node"
|
|
1245
|
+
};
|
|
1246
|
+
const formHost = ctx.tryGet(FormToken$1);
|
|
1247
|
+
if (!formHost) return {
|
|
1248
|
+
status: "inert",
|
|
1249
|
+
reason: "form plugin unavailable"
|
|
1250
|
+
};
|
|
1251
|
+
const result = await formHost.resetFormAction(node.fields, node.exclude, actionCtx.origin);
|
|
1252
|
+
if (result.status === "failed") return {
|
|
1253
|
+
status: "failed",
|
|
1254
|
+
error: result.error?.message ?? "reset failed"
|
|
1255
|
+
};
|
|
1256
|
+
if (result.effectsResult === null) return {
|
|
1257
|
+
status: "inert",
|
|
1258
|
+
reason: result.diagnostics[0]?.message ?? "reset-form resolved no fields"
|
|
1259
|
+
};
|
|
1260
|
+
return { status: "executed" };
|
|
1261
|
+
}));
|
|
1262
|
+
}
|
|
1426
1263
|
}
|
|
1427
1264
|
});
|
|
1428
1265
|
//#endregion
|