@embedpdf/plugin-form 3.0.0-next.14 → 3.0.0-next.16
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 +29 -2
- package/dist/contract/host.cjs.map +1 -0
- package/dist/contract/host.d.cts +3 -2
- package/dist/contract/host.d.ts +3 -2
- package/dist/contract/host.js +14 -2
- package/dist/contract/host.js.map +1 -0
- package/dist/contract-BwcgtCOD.d.ts +566 -0
- package/dist/contract-CU0jrI_D.cjs +56 -0
- package/dist/contract-CU0jrI_D.cjs.map +1 -0
- package/dist/contract-D-yQxw3H.d.cts +566 -0
- package/dist/contract-DcncvPcF.js +45 -0
- package/dist/contract-DcncvPcF.js.map +1 -0
- package/dist/contract.cjs +17 -6
- package/dist/contract.d.cts +2 -8
- package/dist/contract.d.ts +2 -8
- package/dist/contract.js +2 -7
- package/dist/controller-CS6tV2m1.cjs +514 -0
- package/dist/controller-CS6tV2m1.cjs.map +1 -0
- package/dist/controller-Sr5M_8G8.js +497 -0
- package/dist/controller-Sr5M_8G8.js.map +1 -0
- package/dist/host-contract-1epLfK1S.d.cts +87 -0
- package/dist/host-contract-BRYUbpT_.d.ts +87 -0
- package/dist/index.cjs +2917 -1205
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -17
- package/dist/index.d.ts +46 -17
- package/dist/index.js +2896 -1206
- package/dist/index.js.map +1 -1
- package/dist/scripting.cjs +3 -484
- package/dist/scripting.d.cts +58 -22
- package/dist/scripting.d.ts +58 -22
- package/dist/scripting.js +1 -482
- package/package.json +15 -14
- package/dist/contract.cjs.map +0 -1
- package/dist/contract.js.map +0 -1
- package/dist/internal.cjs +0 -4
- package/dist/internal.d.cts +0 -3
- package/dist/internal.d.ts +0 -3
- package/dist/internal.js +0 -3
- package/dist/scripting.cjs.map +0 -1
- package/dist/scripting.js.map +0 -1
- package/dist/types-Bpc32Qmx.d.cts +0 -309
- package/dist/types-C1uIIYfn.cjs +0 -16
- package/dist/types-C1uIIYfn.cjs.map +0 -1
- package/dist/types-DRirhZKr.d.ts +0 -309
- package/dist/types-DkQmmcTG.js +0 -12
- package/dist/types-DkQmmcTG.js.map +0 -1
package/dist/scripting.cjs
CHANGED
|
@@ -1,485 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const sameRef = (left, right) => refKey(left) === refKey(right);
|
|
6
|
-
const cloneValue = (value) => Array.isArray(value) ? [...value] : value;
|
|
7
|
-
const sameValue = (left, right) => Array.isArray(left) && Array.isArray(right) ? left.length === right.length && left.every((value, index) => value === right[index]) : left === right;
|
|
8
|
-
function cloneFields(fields) {
|
|
9
|
-
return fields.map((field) => ({
|
|
10
|
-
...field,
|
|
11
|
-
ref: { ...field.ref },
|
|
12
|
-
value: cloneValue(field.value),
|
|
13
|
-
defaultValue: cloneValue(field.defaultValue),
|
|
14
|
-
...field.options ? { options: field.options.map((option) => ({ ...option })) } : {}
|
|
15
|
-
}));
|
|
16
|
-
}
|
|
17
|
-
function fieldByRef(fields, ref) {
|
|
18
|
-
return fields.find((field) => sameRef(field.ref, ref));
|
|
19
|
-
}
|
|
20
|
-
function snapshotField(snapshot, ref) {
|
|
21
|
-
return snapshot.fields.find((field) => sameRef(field.ref, ref));
|
|
22
|
-
}
|
|
23
|
-
function scriptValueFromFormValue(field, value) {
|
|
24
|
-
if (field.family === "text" && value.type === "text") return value.value;
|
|
25
|
-
if ((field.family === "checkbox" || field.family === "radio") && value.type === "toggle") return value.state ?? "Off";
|
|
26
|
-
if (field.family === "combobox" && value.type === "choice") return value.values[0] ?? "";
|
|
27
|
-
if (field.family === "listbox" && value.type === "choice") return [...value.values];
|
|
28
|
-
throw new Error(`Form value type '${value.type}' does not match field family '${field.family}'`);
|
|
29
|
-
}
|
|
30
|
-
function formValueFromScriptValue(field) {
|
|
31
|
-
switch (field.family) {
|
|
32
|
-
case "text": return {
|
|
33
|
-
type: "text",
|
|
34
|
-
value: String(field.value ?? "")
|
|
35
|
-
};
|
|
36
|
-
case "checkbox":
|
|
37
|
-
case "radio": return {
|
|
38
|
-
type: "toggle",
|
|
39
|
-
state: field.value === null || field.value === void 0 || String(field.value) === "Off" ? null : String(field.value)
|
|
40
|
-
};
|
|
41
|
-
case "combobox": return {
|
|
42
|
-
type: "choice",
|
|
43
|
-
values: Array.isArray(field.value) ? field.value.slice(0, 1) : [String(field.value ?? "")]
|
|
44
|
-
};
|
|
45
|
-
case "listbox": return {
|
|
46
|
-
type: "choice",
|
|
47
|
-
values: Array.isArray(field.value) ? [...field.value] : [String(field.value ?? "")]
|
|
48
|
-
};
|
|
49
|
-
default: return null;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function applyEffects(overlay, effects) {
|
|
53
|
-
for (const effect of effects) {
|
|
54
|
-
if (effect.kind === "reset") {
|
|
55
|
-
for (const ref of effect.refs) {
|
|
56
|
-
const field = fieldByRef(overlay.fields, ref);
|
|
57
|
-
if (!field) continue;
|
|
58
|
-
field.value = cloneValue(field.defaultValue);
|
|
59
|
-
overlay.resetKeys.add(refKey(ref));
|
|
60
|
-
}
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
const field = fieldByRef(overlay.fields, effect.ref);
|
|
64
|
-
if (!field) continue;
|
|
65
|
-
const key = refKey(effect.ref);
|
|
66
|
-
if (effect.kind === "setValue") {
|
|
67
|
-
field.value = scriptValueFromFormValue(field, effect.value);
|
|
68
|
-
overlay.resetKeys.delete(key);
|
|
69
|
-
} else if (effect.kind === "setDisplay") field.display = effect.display;
|
|
70
|
-
else overlay.appearances.set(key, {
|
|
71
|
-
ref: effect.ref,
|
|
72
|
-
text: effect.text
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function canonicalEffects(overlay) {
|
|
77
|
-
const effects = [];
|
|
78
|
-
const resets = [];
|
|
79
|
-
for (const field of overlay.fields) {
|
|
80
|
-
const original = fieldByRef(overlay.original, field.ref);
|
|
81
|
-
if (!original) continue;
|
|
82
|
-
if (!sameValue(original.value, field.value)) {
|
|
83
|
-
const key = refKey(field.ref);
|
|
84
|
-
if (overlay.resetKeys.has(key) && sameValue(field.value, field.defaultValue)) resets.push(field.ref);
|
|
85
|
-
else {
|
|
86
|
-
const value = formValueFromScriptValue(field);
|
|
87
|
-
if (value) effects.push({
|
|
88
|
-
kind: "setValue",
|
|
89
|
-
ref: field.ref,
|
|
90
|
-
value
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (original.display !== field.display) effects.push({
|
|
95
|
-
kind: "setDisplay",
|
|
96
|
-
ref: field.ref,
|
|
97
|
-
display: field.display
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
if (resets.length > 0) effects.unshift({
|
|
101
|
-
kind: "reset",
|
|
102
|
-
refs: resets
|
|
103
|
-
});
|
|
104
|
-
effects.push(...Array.from(overlay.appearances.values(), ({ ref, text }) => ({
|
|
105
|
-
kind: "setAppearanceText",
|
|
106
|
-
ref,
|
|
107
|
-
text
|
|
108
|
-
})));
|
|
109
|
-
return effects;
|
|
110
|
-
}
|
|
111
|
-
function scriptError(message) {
|
|
112
|
-
return {
|
|
113
|
-
kind: "invalid-output",
|
|
114
|
-
message
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
function utf8Length(value) {
|
|
118
|
-
let length = 0;
|
|
119
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
120
|
-
const code = value.charCodeAt(index);
|
|
121
|
-
if (code < 128) length += 1;
|
|
122
|
-
else if (code < 2048) length += 2;
|
|
123
|
-
else if (code >= 55296 && code <= 56319 && index + 1 < value.length) {
|
|
124
|
-
length += 4;
|
|
125
|
-
index += 1;
|
|
126
|
-
} else length += 3;
|
|
127
|
-
}
|
|
128
|
-
return length;
|
|
129
|
-
}
|
|
130
|
-
function pageNumberFor(meta, field) {
|
|
131
|
-
const pon = field.widgets.find((widget) => widget.pageObjectNumber > 0)?.pageObjectNumber;
|
|
132
|
-
if (!meta || pon === void 0) return 0;
|
|
133
|
-
const index = meta.pages.findIndex((page) => page.pageObjectNumber === pon);
|
|
134
|
-
return Math.max(0, index);
|
|
135
|
-
}
|
|
136
|
-
function statusFromEffects(result) {
|
|
137
|
-
if (result.results.some(({ status }) => status === "failed" || status === "skipped")) return "failed";
|
|
138
|
-
if (result.results.some(({ status }) => status === "rejected")) return "failed";
|
|
139
|
-
return result.results.some(({ status }) => status === "applied") ? "applied" : "unchanged";
|
|
140
|
-
}
|
|
141
|
-
var FormScriptingController = class {
|
|
142
|
-
constructor(options) {
|
|
143
|
-
this.options = options;
|
|
144
|
-
this.disposed = false;
|
|
145
|
-
this.transaction = options.transaction.bind(options);
|
|
146
|
-
this.transactionBudget = options.budget;
|
|
147
|
-
}
|
|
148
|
-
/** The realm belongs to its OWNER (the actions plugin, or the caller that
|
|
149
|
-
* minted a detached one) — this only fences further transactions. */
|
|
150
|
-
dispose() {
|
|
151
|
-
this.disposed = true;
|
|
152
|
-
}
|
|
153
|
-
async commit(ref, proposed) {
|
|
154
|
-
return this.transact(ref, proposed);
|
|
155
|
-
}
|
|
156
|
-
/** Execute one originating widget's activation action (`/A`, including `/Next`). */
|
|
157
|
-
async activate(ref, action) {
|
|
158
|
-
return this.transact(ref, void 0, action);
|
|
159
|
-
}
|
|
160
|
-
/** Run lazy document boot plus the `/CO` chain without a user field change.
|
|
161
|
-
* The anchor field resolves INSIDE the transaction (undefined ref). */
|
|
162
|
-
async recalculate() {
|
|
163
|
-
return this.transact(void 0);
|
|
164
|
-
}
|
|
165
|
-
async transact(refInput, proposed, activation) {
|
|
166
|
-
if (this.disposed) throw new Error("Form scripting controller is disposed");
|
|
167
|
-
return this.transaction((txn) => this.transactBody(txn, refInput, proposed, activation));
|
|
168
|
-
}
|
|
169
|
-
async transactBody(txn, refInput, proposed, activation) {
|
|
170
|
-
const snapshot = await this.options.doc.forms.list();
|
|
171
|
-
const ref = refInput ?? snapshot.calculationOrder.find((candidate) => candidate !== null && snapshotField(snapshot, candidate) !== void 0) ?? snapshot.fields[0]?.ref;
|
|
172
|
-
if (!ref) return {
|
|
173
|
-
status: "unchanged",
|
|
174
|
-
scripted: true,
|
|
175
|
-
effectsResult: null,
|
|
176
|
-
uiEffects: [],
|
|
177
|
-
diagnostics: []
|
|
178
|
-
};
|
|
179
|
-
const target = snapshotField(snapshot, ref);
|
|
180
|
-
if (!target) return this.failed([], [], scriptError(`Form field '${refKey(ref)}' no longer exists`));
|
|
181
|
-
const original = (0, _embedpdf_core_acrojs.scriptFieldsFromSnapshot)(snapshot);
|
|
182
|
-
const overlay = {
|
|
183
|
-
original,
|
|
184
|
-
fields: cloneFields(original),
|
|
185
|
-
resetKeys: /* @__PURE__ */ new Set(),
|
|
186
|
-
appearances: /* @__PURE__ */ new Map()
|
|
187
|
-
};
|
|
188
|
-
const targetInput = fieldByRef(overlay.fields, ref);
|
|
189
|
-
if (!targetInput) return this.failed([], [], scriptError(`Form field '${refKey(ref)}' has no script view`));
|
|
190
|
-
const hasProposedValue = proposed !== void 0;
|
|
191
|
-
let proposedValue = cloneValue(targetInput.value);
|
|
192
|
-
if (proposed) try {
|
|
193
|
-
proposedValue = scriptValueFromFormValue(targetInput, proposed);
|
|
194
|
-
} catch (error) {
|
|
195
|
-
return this.failed([], [], scriptError(error instanceof Error ? error.message : String(error)));
|
|
196
|
-
}
|
|
197
|
-
const uiEffects = [];
|
|
198
|
-
const diagnostics = [];
|
|
199
|
-
const pageNumber = pageNumberFor(this.options.document(), target);
|
|
200
|
-
const budget = this.transactionBudget ?? _embedpdf_core_acrojs.DEFAULT_SCRIPT_BUDGET;
|
|
201
|
-
let executionMs = 0;
|
|
202
|
-
let aggregateOutputSize = 0;
|
|
203
|
-
const consume = (output, phase) => {
|
|
204
|
-
aggregateOutputSize += utf8Length(JSON.stringify(output));
|
|
205
|
-
uiEffects.push(...output.uiEffects.map((effect) => ({
|
|
206
|
-
...effect,
|
|
207
|
-
phase
|
|
208
|
-
})));
|
|
209
|
-
diagnostics.push(...output.diagnostics);
|
|
210
|
-
if (aggregateOutputSize > budget.maxOutputBytes) return {
|
|
211
|
-
kind: "budget",
|
|
212
|
-
message: `Form script transaction output exceeded ${budget.maxOutputBytes} bytes`
|
|
213
|
-
};
|
|
214
|
-
return output.error ?? null;
|
|
215
|
-
};
|
|
216
|
-
const remainingBudget = () => {
|
|
217
|
-
const remaining = budget.maxExecutionMs - executionMs;
|
|
218
|
-
return remaining <= 0 ? null : {
|
|
219
|
-
...budget,
|
|
220
|
-
maxExecutionMs: remaining
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
const world = (fields, event) => ({
|
|
224
|
-
fields,
|
|
225
|
-
pageNumber,
|
|
226
|
-
event
|
|
227
|
-
});
|
|
228
|
-
const run = async (source, input) => {
|
|
229
|
-
const remaining = remainingBudget();
|
|
230
|
-
if (!remaining) return { error: {
|
|
231
|
-
kind: "budget",
|
|
232
|
-
message: "Form script transaction timed out"
|
|
233
|
-
} };
|
|
234
|
-
let output;
|
|
235
|
-
try {
|
|
236
|
-
const startedAt = Date.now();
|
|
237
|
-
output = await txn.run(source, input, remaining);
|
|
238
|
-
executionMs += Date.now() - startedAt;
|
|
239
|
-
} catch (error) {
|
|
240
|
-
return { error: scriptError(error instanceof Error ? error.message : String(error)) };
|
|
241
|
-
}
|
|
242
|
-
const error = consume(output, "user");
|
|
243
|
-
return error ? { error } : { output };
|
|
244
|
-
};
|
|
245
|
-
{
|
|
246
|
-
let boot = null;
|
|
247
|
-
try {
|
|
248
|
-
boot = await txn.boot(world(overlay.fields, { kind: "name-tree-boot" }), budget);
|
|
249
|
-
} catch (error) {
|
|
250
|
-
diagnostics.push({
|
|
251
|
-
code: "script-error",
|
|
252
|
-
message: `Document boot script failed (continuing without it): ${error instanceof Error ? error.message : String(error)}`
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
if (boot) {
|
|
256
|
-
const bootError = consume(boot, "boot");
|
|
257
|
-
if (!bootError) applyEffects(overlay, boot.formEffects);
|
|
258
|
-
else diagnostics.push({
|
|
259
|
-
code: "script-error",
|
|
260
|
-
message: `Document boot script failed (continuing without it): ${bootError.message}`
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
const bootEffects = canonicalEffects(overlay);
|
|
265
|
-
if (activation) {
|
|
266
|
-
let program;
|
|
267
|
-
try {
|
|
268
|
-
program = (0, _embedpdf_core_acrojs.javaScriptProgramFromActionTree)(activation);
|
|
269
|
-
} catch (error) {
|
|
270
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
271
|
-
}
|
|
272
|
-
if (program) {
|
|
273
|
-
const activated = await run(program, world(overlay.fields, {
|
|
274
|
-
kind: "widget-activate",
|
|
275
|
-
target: ref,
|
|
276
|
-
source: ref,
|
|
277
|
-
value: targetInput.value
|
|
278
|
-
}));
|
|
279
|
-
if (activated.error) return this.failed(uiEffects, diagnostics, activated.error);
|
|
280
|
-
applyEffects(overlay, activated.output.formEffects);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
if (!activation && hasProposedValue && target.actions?.keystroke && target.family === "text") {
|
|
284
|
-
let program;
|
|
285
|
-
try {
|
|
286
|
-
program = (0, _embedpdf_core_acrojs.javaScriptProgramFromActionTree)(target.actions.keystroke);
|
|
287
|
-
} catch (error) {
|
|
288
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
289
|
-
}
|
|
290
|
-
if (program) {
|
|
291
|
-
const oldValue = String(targetInput.value ?? "");
|
|
292
|
-
const typing = await run(program, world(overlay.fields, {
|
|
293
|
-
kind: "field-keystroke",
|
|
294
|
-
target: ref,
|
|
295
|
-
source: ref,
|
|
296
|
-
value: oldValue,
|
|
297
|
-
change: String(proposedValue ?? ""),
|
|
298
|
-
selStart: 0,
|
|
299
|
-
selEnd: oldValue.length,
|
|
300
|
-
willCommit: false
|
|
301
|
-
}));
|
|
302
|
-
if (typing.error && typing.error.kind !== "exception") return this.failed(uiEffects, diagnostics, typing.error);
|
|
303
|
-
if (typing.error) diagnostics.push({
|
|
304
|
-
code: "script-error",
|
|
305
|
-
message: `Keystroke script failed (kept the typed value): ${typing.error.message}`
|
|
306
|
-
});
|
|
307
|
-
else {
|
|
308
|
-
if (!typing.output.event.rc) return this.finishRejected(bootEffects, uiEffects, diagnostics);
|
|
309
|
-
applyEffects(overlay, typing.output.formEffects);
|
|
310
|
-
const event = typing.output.event;
|
|
311
|
-
const base = String(event.value ?? "");
|
|
312
|
-
proposedValue = base.slice(0, event.selStart) + event.change + base.slice(Math.max(event.selStart, event.selEnd));
|
|
313
|
-
}
|
|
314
|
-
const commit = await run(program, world(overlay.fields, {
|
|
315
|
-
kind: "field-keystroke",
|
|
316
|
-
target: ref,
|
|
317
|
-
source: ref,
|
|
318
|
-
value: proposedValue,
|
|
319
|
-
change: "",
|
|
320
|
-
selStart: 0,
|
|
321
|
-
selEnd: 0,
|
|
322
|
-
willCommit: true
|
|
323
|
-
}));
|
|
324
|
-
if (commit.error && commit.error.kind !== "exception") return this.failed(uiEffects, diagnostics, commit.error);
|
|
325
|
-
if (commit.error) diagnostics.push({
|
|
326
|
-
code: "script-error",
|
|
327
|
-
message: `Keystroke commit script failed (kept the typed value): ${commit.error.message}`
|
|
328
|
-
});
|
|
329
|
-
else {
|
|
330
|
-
if (!commit.output.event.rc) return this.finishRejected(bootEffects, uiEffects, diagnostics);
|
|
331
|
-
applyEffects(overlay, commit.output.formEffects);
|
|
332
|
-
proposedValue = cloneValue(commit.output.event.value);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
if (!activation && hasProposedValue) {
|
|
337
|
-
targetInput.value = cloneValue(proposedValue);
|
|
338
|
-
overlay.resetKeys.delete(refKey(ref));
|
|
339
|
-
}
|
|
340
|
-
if (!activation && hasProposedValue && target.actions?.validate) {
|
|
341
|
-
let program;
|
|
342
|
-
try {
|
|
343
|
-
program = (0, _embedpdf_core_acrojs.javaScriptProgramFromActionTree)(target.actions.validate);
|
|
344
|
-
} catch (error) {
|
|
345
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
346
|
-
}
|
|
347
|
-
if (program) {
|
|
348
|
-
const validation = await run(program, world(overlay.fields, {
|
|
349
|
-
kind: "field-validate",
|
|
350
|
-
target: ref,
|
|
351
|
-
source: ref,
|
|
352
|
-
value: proposedValue,
|
|
353
|
-
willCommit: true
|
|
354
|
-
}));
|
|
355
|
-
if (validation.error && validation.error.kind !== "exception") return this.failed(uiEffects, diagnostics, validation.error);
|
|
356
|
-
if (validation.error) diagnostics.push({
|
|
357
|
-
code: "script-error",
|
|
358
|
-
message: `Validate script failed (accepted the value): ${validation.error.message}`
|
|
359
|
-
});
|
|
360
|
-
else {
|
|
361
|
-
if (!validation.output.event.rc) return this.finishRejected(bootEffects, uiEffects, diagnostics);
|
|
362
|
-
targetInput.value = cloneValue(validation.output.event.value);
|
|
363
|
-
applyEffects(overlay, validation.output.formEffects);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
const formatRefs = !activation && hasProposedValue ? [ref] : [];
|
|
368
|
-
for (const calculationRef of activation ? [] : snapshot.calculationOrder) {
|
|
369
|
-
if (!calculationRef) continue;
|
|
370
|
-
const calculated = snapshotField(snapshot, calculationRef);
|
|
371
|
-
const tree = calculated?.actions?.calculate;
|
|
372
|
-
if (!calculated || !tree) continue;
|
|
373
|
-
let program;
|
|
374
|
-
try {
|
|
375
|
-
program = (0, _embedpdf_core_acrojs.javaScriptProgramFromActionTree)(tree);
|
|
376
|
-
} catch (error) {
|
|
377
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
378
|
-
}
|
|
379
|
-
const calculatedInput = fieldByRef(overlay.fields, calculationRef);
|
|
380
|
-
if (program && calculatedInput) {
|
|
381
|
-
const calculation = await run(program, world(overlay.fields, {
|
|
382
|
-
kind: "field-calculate",
|
|
383
|
-
target: calculationRef,
|
|
384
|
-
...hasProposedValue ? { source: ref } : {},
|
|
385
|
-
value: calculatedInput.value
|
|
386
|
-
}));
|
|
387
|
-
if (calculation.error && calculation.error.kind !== "exception") return this.failed(uiEffects, diagnostics, calculation.error);
|
|
388
|
-
if (calculation.error) diagnostics.push({
|
|
389
|
-
code: "script-error",
|
|
390
|
-
message: `Calculate script failed (field left unchanged): ${calculation.error.message}`
|
|
391
|
-
});
|
|
392
|
-
else applyEffects(overlay, calculation.output.formEffects);
|
|
393
|
-
}
|
|
394
|
-
if (!formatRefs.some((candidate) => sameRef(candidate, calculationRef))) formatRefs.push(calculationRef);
|
|
395
|
-
}
|
|
396
|
-
for (const formatRef of formatRefs) {
|
|
397
|
-
const field = snapshotField(snapshot, formatRef);
|
|
398
|
-
const tree = field?.actions?.format;
|
|
399
|
-
const overlayField = fieldByRef(overlay.fields, formatRef);
|
|
400
|
-
if (!field || !tree || !overlayField) continue;
|
|
401
|
-
let program;
|
|
402
|
-
try {
|
|
403
|
-
program = (0, _embedpdf_core_acrojs.javaScriptProgramFromActionTree)(tree);
|
|
404
|
-
} catch (error) {
|
|
405
|
-
return this.failed(uiEffects, diagnostics, scriptError(error instanceof Error ? error.message : String(error)));
|
|
406
|
-
}
|
|
407
|
-
if (!program) continue;
|
|
408
|
-
const format = await run(program, world(overlay.fields, {
|
|
409
|
-
kind: "field-format",
|
|
410
|
-
target: formatRef,
|
|
411
|
-
...hasProposedValue ? { source: ref } : {},
|
|
412
|
-
value: overlayField.value
|
|
413
|
-
}));
|
|
414
|
-
if (format.error && format.error.kind !== "exception") return this.failed(uiEffects, diagnostics, format.error);
|
|
415
|
-
if (format.error) {
|
|
416
|
-
diagnostics.push({
|
|
417
|
-
code: "script-error",
|
|
418
|
-
message: `Format script failed (kept the raw value): ${format.error.message}`
|
|
419
|
-
});
|
|
420
|
-
continue;
|
|
421
|
-
}
|
|
422
|
-
applyEffects(overlay, format.output.formEffects);
|
|
423
|
-
}
|
|
424
|
-
const effects = canonicalEffects(overlay);
|
|
425
|
-
if (effects.length + uiEffects.length > budget.maxEffects) return this.failed(uiEffects, diagnostics, {
|
|
426
|
-
kind: "budget",
|
|
427
|
-
message: `Form script transaction exceeded ${budget.maxEffects} effects`
|
|
428
|
-
});
|
|
429
|
-
if (effects.length === 0) return {
|
|
430
|
-
status: "unchanged",
|
|
431
|
-
scripted: true,
|
|
432
|
-
effectsResult: null,
|
|
433
|
-
uiEffects,
|
|
434
|
-
diagnostics
|
|
435
|
-
};
|
|
436
|
-
if (!this.options.doc.forms.applyEffects) return this.failed(uiEffects, diagnostics, scriptError("This engine does not support batched form effects"));
|
|
437
|
-
const effectsResult = await this.options.doc.forms.applyEffects(effects);
|
|
438
|
-
const status = statusFromEffects(effectsResult);
|
|
439
|
-
return {
|
|
440
|
-
status,
|
|
441
|
-
scripted: true,
|
|
442
|
-
effectsResult,
|
|
443
|
-
uiEffects,
|
|
444
|
-
diagnostics,
|
|
445
|
-
...status === "failed" ? { error: scriptError("One or more native form effects failed") } : {}
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
failed(uiEffects, diagnostics, error) {
|
|
449
|
-
return {
|
|
450
|
-
status: "failed",
|
|
451
|
-
scripted: true,
|
|
452
|
-
effectsResult: null,
|
|
453
|
-
uiEffects,
|
|
454
|
-
diagnostics,
|
|
455
|
-
error
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
async finishRejected(bootEffects, uiEffects, diagnostics) {
|
|
459
|
-
if (bootEffects.length === 0) return {
|
|
460
|
-
status: "rejected",
|
|
461
|
-
scripted: true,
|
|
462
|
-
effectsResult: null,
|
|
463
|
-
uiEffects,
|
|
464
|
-
diagnostics
|
|
465
|
-
};
|
|
466
|
-
if (!this.options.doc.forms.applyEffects) return this.failed(uiEffects, diagnostics, scriptError("Batched form effects are unavailable"));
|
|
467
|
-
const effectsResult = await this.options.doc.forms.applyEffects(bootEffects);
|
|
468
|
-
if (statusFromEffects(effectsResult) === "failed") return this.failed(uiEffects, diagnostics, scriptError("A name-tree boot effect failed"));
|
|
469
|
-
return {
|
|
470
|
-
status: "rejected",
|
|
471
|
-
scripted: true,
|
|
472
|
-
effectsResult,
|
|
473
|
-
uiEffects,
|
|
474
|
-
diagnostics
|
|
475
|
-
};
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
function createFormScriptingController(options) {
|
|
479
|
-
return new FormScriptingController(options);
|
|
480
|
-
}
|
|
481
|
-
//#endregion
|
|
482
|
-
exports.FormScriptingController = FormScriptingController;
|
|
483
|
-
exports.createFormScriptingController = createFormScriptingController;
|
|
484
|
-
|
|
485
|
-
//# sourceMappingURL=scripting.cjs.map
|
|
2
|
+
const require_controller = require("./controller-CS6tV2m1.cjs");
|
|
3
|
+
exports.FormScriptingController = require_controller.FormScriptingController;
|
|
4
|
+
exports.createFormScriptingController = require_controller.createFormScriptingController;
|
package/dist/scripting.d.cts
CHANGED
|
@@ -1,47 +1,83 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { R as FormUiEffect } from "./contract-D-yQxw3H.cjs";
|
|
2
2
|
import { DocumentMeta } from "@embedpdf/core";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
//#region src/scripting.d.ts
|
|
3
|
+
import { ScriptBudget, ScriptDiagnostic, ScriptExecutionError, ScriptTransaction } from "@embedpdf/core-acrojs";
|
|
4
|
+
import { FormEffect, FormFieldRef, FormFieldValue, FormSnapshot, PdfActionTree } from "@embedpdf/engine-core/runtime";
|
|
5
|
+
//#region src/scripting/controller.d.ts
|
|
6
6
|
/**
|
|
7
|
-
* The controller never owns a realm. It rides a realm
|
|
7
|
+
* The controller never owns a realm. It rides a realm port — this document's
|
|
8
8
|
* own (the actions plugin's per-document host) or a detached one minted by
|
|
9
9
|
* the actions plugin for a document that is not displayed (stamp's
|
|
10
10
|
* asset documents). Either way the owner disposes the realm; `dispose()`
|
|
11
11
|
* here only fences further transactions.
|
|
12
12
|
*/
|
|
13
13
|
interface FormScriptingControllerOptions {
|
|
14
|
-
|
|
15
|
-
document(): DocumentMeta | null;
|
|
16
|
-
/** The realm's transaction port ({@link ScriptTransaction}).
|
|
17
|
-
* pipeline runs WHOLLY inside one transaction: snapshot fetch, every
|
|
18
|
-
* pass, and the engine commit — the commit-inside-the-boundary law. */
|
|
14
|
+
/** What the scripts read of the document: its name, page count and pages. */
|
|
15
|
+
document(): Pick<DocumentMeta, 'name' | 'pageCount' | 'pages'> | null;
|
|
16
|
+
/** The realm's transaction port ({@link ScriptTransaction}): the realm runs one at a time. */
|
|
19
17
|
transaction<T>(body: (txn: ScriptTransaction) => Promise<T>): Promise<T>;
|
|
20
18
|
/** The transaction aggregate (output bytes, wall-clock across passes);
|
|
21
19
|
* the realm owner hands over the same budget its runs enforce. */
|
|
22
20
|
budget?: ScriptBudget;
|
|
23
21
|
}
|
|
22
|
+
/** What the form's scripts made of one write, before anything is written. */
|
|
23
|
+
interface FormScriptResult {
|
|
24
|
+
/**
|
|
25
|
+
* `accepted`: write `effects`. `rejected`: a keystroke or validate script refused the value,
|
|
26
|
+
* so only `bootEffects` are written. `failed`: the transaction broke (a budget, a script that
|
|
27
|
+
* couldn't run), so nothing is.
|
|
28
|
+
*/
|
|
29
|
+
readonly status: 'accepted' | 'rejected' | 'failed';
|
|
30
|
+
/** What the scripts changed in the form, in the order to write it (`formEffectOps`). */
|
|
31
|
+
readonly effects: FormEffect[];
|
|
32
|
+
/** What the document's open scripts changed: written even when the value is refused. */
|
|
33
|
+
readonly bootEffects: FormEffect[];
|
|
34
|
+
readonly uiEffects: FormUiEffect[];
|
|
35
|
+
readonly diagnostics: ScriptDiagnostic[];
|
|
36
|
+
readonly error?: ScriptExecutionError;
|
|
37
|
+
}
|
|
38
|
+
/** The scripts one transaction can run, each against the form it is given. */
|
|
39
|
+
interface FormScripts {
|
|
40
|
+
/**
|
|
41
|
+
* A field's new value through its keystroke and validate scripts, then the calculations and
|
|
42
|
+
* the formats. `form` is the form without the value.
|
|
43
|
+
*/
|
|
44
|
+
commit(form: FormSnapshot, ref: FormFieldRef, value: FormFieldValue): Promise<FormScriptResult>;
|
|
45
|
+
/** One widget's activation action (`/A`, with `/Next`). Only its scripts run here. */
|
|
46
|
+
activate(form: FormSnapshot, ref: FormFieldRef, action: PdfActionTree<unknown>): Promise<FormScriptResult>;
|
|
47
|
+
/** The document's open scripts (once per realm) and the calculations, without a field change. */
|
|
48
|
+
recalculate(form: FormSnapshot): Promise<FormScriptResult>;
|
|
49
|
+
}
|
|
24
50
|
declare class FormScriptingController {
|
|
25
51
|
private readonly options;
|
|
26
52
|
private disposed;
|
|
27
|
-
private readonly transaction;
|
|
28
53
|
private readonly transactionBudget;
|
|
29
54
|
constructor(options: FormScriptingControllerOptions);
|
|
30
|
-
/** The realm belongs to its
|
|
55
|
+
/** The realm belongs to its owner (the actions plugin, or the caller that
|
|
31
56
|
* minted a detached one) — this only fences further transactions. */
|
|
32
57
|
dispose(): void;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
58
|
+
/**
|
|
59
|
+
* One realm transaction: `body` runs its scripts and writes what they changed before the
|
|
60
|
+
* next transaction starts, so that one reads the form with it.
|
|
61
|
+
*/
|
|
62
|
+
transaction<T>(body: (scripts: FormScripts) => Promise<T>): Promise<T>;
|
|
63
|
+
/**
|
|
64
|
+
* Load the realm before the first write needs it: the scripts' engine starts, and the
|
|
65
|
+
* document's open scripts run, their effects kept for the first write as without warming.
|
|
66
|
+
* Nothing is written.
|
|
67
|
+
*/
|
|
68
|
+
warm(form: FormSnapshot): Promise<void>;
|
|
69
|
+
/** {@link FormScripts.commit} in a transaction of its own. */
|
|
70
|
+
commit(form: FormSnapshot, ref: FormFieldRef, value: FormFieldValue): Promise<FormScriptResult>;
|
|
71
|
+
/** {@link FormScripts.activate} in a transaction of its own. */
|
|
72
|
+
activate(form: FormSnapshot, ref: FormFieldRef, action: PdfActionTree<unknown>): Promise<FormScriptResult>;
|
|
73
|
+
/** {@link FormScripts.recalculate} in a transaction of its own. */
|
|
74
|
+
recalculate(form: FormSnapshot): Promise<FormScriptResult>;
|
|
75
|
+
private run;
|
|
41
76
|
private failed;
|
|
42
|
-
|
|
77
|
+
/** The value is refused; the open scripts' effects are still written. */
|
|
78
|
+
private rejected;
|
|
43
79
|
}
|
|
44
80
|
declare function createFormScriptingController(options: FormScriptingControllerOptions): FormScriptingController;
|
|
45
81
|
//#endregion
|
|
46
|
-
export { FormScriptingController, FormScriptingControllerOptions, createFormScriptingController };
|
|
82
|
+
export { FormScriptResult, FormScriptingController, FormScriptingControllerOptions, FormScripts, createFormScriptingController };
|
|
47
83
|
//# sourceMappingURL=scripting.d.cts.map
|