@embedpdf/plugin-form 3.1.0 → 3.2.0
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/README.md +61 -0
- package/dist/contract/host.cjs +1 -1
- package/dist/contract/host.d.cts +3 -3
- package/dist/contract/host.d.ts +3 -3
- package/dist/contract/host.js +1 -1
- package/dist/{contract-Djti7ew8.d.cts → contract-Bq_ewUE5.d.cts} +57 -12
- package/dist/{contract-BdEaLXUK.js → contract-Bu3RA2jA.js} +3 -1
- package/dist/contract-Bu3RA2jA.js.map +1 -0
- package/dist/{contract-6YEsObRl.d.ts → contract-DJLVUeAe.d.ts} +57 -12
- package/dist/{contract-CU0jrI_D.cjs → contract-DO7k0ua0.cjs} +3 -1
- package/dist/contract-DO7k0ua0.cjs.map +1 -0
- package/dist/contract.cjs +1 -1
- package/dist/contract.d.cts +2 -2
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +1 -1
- package/dist/{host-contract-XqkfFuoy.d.ts → host-contract-D9NExKAu.d.ts} +2 -2
- package/dist/{host-contract-DO9xsBoD.d.cts → host-contract-DI1Gf4fB.d.cts} +2 -2
- package/dist/index.cjs +516 -87
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +517 -88
- package/dist/index.js.map +1 -1
- package/dist/scripting.d.cts +1 -1
- package/dist/scripting.d.ts +1 -1
- package/package.json +18 -18
- package/dist/contract-BdEaLXUK.js.map +0 -1
- package/dist/contract-CU0jrI_D.cjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_contract = require("./contract-
|
|
2
|
+
const require_contract = require("./contract-DO7k0ua0.cjs");
|
|
3
3
|
const require_controller = require("./controller-BwbadmLW.cjs");
|
|
4
4
|
const require_contract_host = require("./contract/host.cjs");
|
|
5
5
|
let _embedpdf_core = require("@embedpdf/core");
|
|
@@ -761,9 +761,14 @@ function createFieldReads(ctx, { fields, rights, siblings }) {
|
|
|
761
761
|
};
|
|
762
762
|
});
|
|
763
763
|
const getSelectedField = (0, _embedpdf_core.memo)(() => [fields.view(), siblings.annotation()?.selection.getSelected() ?? NOTHING_SELECTED], (index, selected) => {
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
764
|
+
let found = null;
|
|
765
|
+
for (const annotation of selected) {
|
|
766
|
+
if (!annotation.subtype.startsWith("widget")) return null;
|
|
767
|
+
const field = fieldForWidget(index, widgetObjectOf(annotation.ref));
|
|
768
|
+
if (!field || found && fieldKeyOf(found) !== fieldKeyOf(field)) return null;
|
|
769
|
+
found = field;
|
|
770
|
+
}
|
|
771
|
+
return found;
|
|
767
772
|
});
|
|
768
773
|
return { api: {
|
|
769
774
|
getSnapshot: () => fields.view().snapshot,
|
|
@@ -1121,8 +1126,8 @@ const RESET_FAMILIES = /* @__PURE__ */ new Set([
|
|
|
1121
1126
|
* `widgetAfterPatch`): what the fields and their widgets hold after it. An op
|
|
1122
1127
|
* those rules refuse shows nothing; one whose values only the engine's schemas
|
|
1123
1128
|
* refuse shows until the engine's refusal takes the change away.
|
|
1124
|
-
* A create
|
|
1125
|
-
* shows once the engine answers.
|
|
1129
|
+
* A create, a copy and a new widget show once they name their numbers; taking
|
|
1130
|
+
* a widget out of its field shows once the engine answers.
|
|
1126
1131
|
*
|
|
1127
1132
|
* The view replays every pending change each time any change is staged; the
|
|
1128
1133
|
* same op over the same form gives the same form, so a field keeps its object
|
|
@@ -1157,6 +1162,8 @@ function predictOnce(index, op) {
|
|
|
1157
1162
|
return op.field ? upsertFields(restored, [op.field]) : restored;
|
|
1158
1163
|
}
|
|
1159
1164
|
case "forms.create": return createdField(index, op);
|
|
1165
|
+
case "forms.copy": return copiedFields(index, op);
|
|
1166
|
+
case "forms.addWidget": return addedWidget(index, op);
|
|
1160
1167
|
case "forms.update": {
|
|
1161
1168
|
const field = fieldByRef(index, op.field);
|
|
1162
1169
|
return field ? withFields(index, [(0, _embedpdf_engine_core_runtime.formFieldAfter)(withRows(index, field), op.patch)]) : index;
|
|
@@ -1231,6 +1238,106 @@ function createdField(index, op) {
|
|
|
1231
1238
|
}, index.pictureVersions) : index, [created]);
|
|
1232
1239
|
return op.draft.actions?.calculate ? withCalculationOrder(next, [...snapshot.calculationOrder, created.field.ref]) : next;
|
|
1233
1240
|
}
|
|
1241
|
+
/**
|
|
1242
|
+
* The fields a `forms.copy` makes, by the engine's own rule
|
|
1243
|
+
* (`planFormCopies`), each with the object numbers the op names: its own,
|
|
1244
|
+
* then its widgets'. None for a copy that doesn't name them all, which
|
|
1245
|
+
* shows when the engine answers, and none for one the engine would refuse.
|
|
1246
|
+
* What a script right leaves out is the engine's to say: nothing of it shows.
|
|
1247
|
+
*/
|
|
1248
|
+
function copiesOf(index, op) {
|
|
1249
|
+
const { snapshot } = index;
|
|
1250
|
+
if (!snapshot || !op.objectNumbers) return [];
|
|
1251
|
+
let creates;
|
|
1252
|
+
try {
|
|
1253
|
+
creates = (0, _embedpdf_engine_core_runtime.planFormCopies)({
|
|
1254
|
+
form: snapshot,
|
|
1255
|
+
fields: op.fields,
|
|
1256
|
+
...op.widgets ? { widgets: op.widgets } : {},
|
|
1257
|
+
placement: {
|
|
1258
|
+
...op.page ? { page: op.page } : {},
|
|
1259
|
+
...op.offset ? { offset: op.offset } : {}
|
|
1260
|
+
},
|
|
1261
|
+
values: op.values ?? true,
|
|
1262
|
+
mayScript: true
|
|
1263
|
+
}).creates;
|
|
1264
|
+
} catch {
|
|
1265
|
+
return [];
|
|
1266
|
+
}
|
|
1267
|
+
const needed = creates.reduce((count, create) => count + 1 + create.widgets.length, 0);
|
|
1268
|
+
if (op.objectNumbers.length !== needed) return [];
|
|
1269
|
+
let at = 0;
|
|
1270
|
+
return creates.map((create) => {
|
|
1271
|
+
const [objectNumber, ...widgetObjectNumbers] = op.objectNumbers.slice(at, at + 1 + create.widgets.length);
|
|
1272
|
+
at += 1 + create.widgets.length;
|
|
1273
|
+
return {
|
|
1274
|
+
...create,
|
|
1275
|
+
objectNumber,
|
|
1276
|
+
widgetObjectNumbers
|
|
1277
|
+
};
|
|
1278
|
+
});
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* `forms.copy`: each copy as a create of the same field shows (`createdField`),
|
|
1282
|
+
* then its widgets' flags, which a create writes none of, and its value.
|
|
1283
|
+
*/
|
|
1284
|
+
function copiedFields(index, op) {
|
|
1285
|
+
let next = index;
|
|
1286
|
+
for (const copy of copiesOf(index, op)) {
|
|
1287
|
+
const ref = {
|
|
1288
|
+
kind: "objectNumber",
|
|
1289
|
+
objectNumber: copy.objectNumber
|
|
1290
|
+
};
|
|
1291
|
+
if (fieldByRef(next, ref)) continue;
|
|
1292
|
+
next = createdField(next, {
|
|
1293
|
+
type: "forms.create",
|
|
1294
|
+
draft: copy.draft,
|
|
1295
|
+
objectNumber: copy.objectNumber,
|
|
1296
|
+
widgetObjectNumbers: copy.widgetObjectNumbers
|
|
1297
|
+
});
|
|
1298
|
+
const made = fieldByRef(next, ref);
|
|
1299
|
+
if (!made) continue;
|
|
1300
|
+
next = upsertWidgets(next, rowsOfField(next, made).map((row, at) => (0, _embedpdf_engine_core_runtime.widgetAfterPatch)(row, copy.flags[at] ?? {})));
|
|
1301
|
+
if (copy.value) next = withFields(next, [(0, _embedpdf_engine_core_runtime.fieldAfterValue)(withRows(next, made), copy.value)]);
|
|
1302
|
+
}
|
|
1303
|
+
return next;
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* `forms.addWidget`, once it names its widget's number: the widget as a
|
|
1307
|
+
* create of a field of that kind places it, last among the field's. One that
|
|
1308
|
+
* splits a merged field (the field's own widget moves to a new number) shows
|
|
1309
|
+
* when the engine answers.
|
|
1310
|
+
*/
|
|
1311
|
+
function addedWidget(index, op) {
|
|
1312
|
+
if (op.objectNumber === void 0 || op.splitObjectNumber !== void 0) return index;
|
|
1313
|
+
const field = fieldByRef(index, op.field);
|
|
1314
|
+
if (!field || field.kind === "unknown" || field.ref.kind !== "objectNumber") return index;
|
|
1315
|
+
if (widgetRowOf(index, op.objectNumber)) return index;
|
|
1316
|
+
const options = "options" in field ? { options: field.options.map(({ label, value }) => ({
|
|
1317
|
+
label,
|
|
1318
|
+
value
|
|
1319
|
+
})) } : {};
|
|
1320
|
+
const made = (0, _embedpdf_engine_core_runtime.formFieldOfDraft)({
|
|
1321
|
+
kind: field.kind,
|
|
1322
|
+
name: field.name,
|
|
1323
|
+
...options,
|
|
1324
|
+
widgets: [op.placement]
|
|
1325
|
+
}, {
|
|
1326
|
+
objectNumber: field.ref.objectNumber,
|
|
1327
|
+
widgets: [{
|
|
1328
|
+
kind: "objectNumber",
|
|
1329
|
+
page: op.placement.page,
|
|
1330
|
+
objectNumber: op.objectNumber
|
|
1331
|
+
}]
|
|
1332
|
+
});
|
|
1333
|
+
const [widget] = made.field.widgets;
|
|
1334
|
+
const [row] = made.widgets;
|
|
1335
|
+
if (!widget || !row) return index;
|
|
1336
|
+
return upsertWidgets(upsertFields(index, [{
|
|
1337
|
+
...field,
|
|
1338
|
+
widgets: [...field.widgets, widget]
|
|
1339
|
+
}]), [row]);
|
|
1340
|
+
}
|
|
1234
1341
|
/** The fields a change's ops write a value or a look to, each once, in op order. */
|
|
1235
1342
|
function fieldsNamedBy(index, ops) {
|
|
1236
1343
|
const named = /* @__PURE__ */ new Map();
|
|
@@ -1279,6 +1386,21 @@ function undoOfOp(index, op) {
|
|
|
1279
1386
|
objectNumber: op.objectNumber
|
|
1280
1387
|
}
|
|
1281
1388
|
}];
|
|
1389
|
+
case "forms.copy": return copiesOf(index, op).map((copy) => ({
|
|
1390
|
+
type: "forms.delete",
|
|
1391
|
+
field: {
|
|
1392
|
+
kind: "objectNumber",
|
|
1393
|
+
objectNumber: copy.objectNumber
|
|
1394
|
+
}
|
|
1395
|
+
})).reverse();
|
|
1396
|
+
case "forms.addWidget": return op.objectNumber === void 0 ? [] : [{
|
|
1397
|
+
type: "forms.deleteWidget",
|
|
1398
|
+
widget: {
|
|
1399
|
+
kind: "objectNumber",
|
|
1400
|
+
page: op.placement.page,
|
|
1401
|
+
objectNumber: op.objectNumber
|
|
1402
|
+
}
|
|
1403
|
+
}];
|
|
1282
1404
|
case "forms.update":
|
|
1283
1405
|
case "forms.delete": {
|
|
1284
1406
|
const field = fieldByRef(index, op.field);
|
|
@@ -1564,7 +1686,7 @@ function createFormattedTexts(ctx, fields) {
|
|
|
1564
1686
|
* while the picture it holds is older than the widget's look.
|
|
1565
1687
|
*/
|
|
1566
1688
|
const NOTHING_SHOWN = Object.freeze([]);
|
|
1567
|
-
const numberOf = (ref) => ref.kind === "objectNumber" ? ref.objectNumber : -1;
|
|
1689
|
+
const numberOf$1 = (ref) => ref.kind === "objectNumber" ? ref.objectNumber : -1;
|
|
1568
1690
|
function createWidgetLooks({ fields, formatted, design, fonts }) {
|
|
1569
1691
|
/** Moves when the fonts plugin may draw a widget's text differently. */
|
|
1570
1692
|
const fontsEpoch = () => fonts()?.getPaintFontsEpoch() ?? 0;
|
|
@@ -1619,7 +1741,7 @@ function createWidgetLooks({ fields, formatted, design, fonts }) {
|
|
|
1619
1741
|
design.lending(),
|
|
1620
1742
|
fontsEpoch()
|
|
1621
1743
|
], (pageObjectNumber, view, truth, _formatted, lent) => lent ? NOTHING_SHOWN : shownWidgetsOf(view, pageObjectNumber, (row) => {
|
|
1622
|
-
const number = numberOf(row.ref);
|
|
1744
|
+
const number = numberOf$1(row.ref);
|
|
1623
1745
|
return {
|
|
1624
1746
|
pending: lookOf(view, number) !== lookOf(truth, number),
|
|
1625
1747
|
pictureVersion: pictureVersionOf(truth, number)
|
|
@@ -1631,7 +1753,7 @@ function createWidgetLooks({ fields, formatted, design, fonts }) {
|
|
|
1631
1753
|
* unless `now`, then in the widget's standard font.
|
|
1632
1754
|
*/
|
|
1633
1755
|
const paint = (row, box, rotation, now = false) => {
|
|
1634
|
-
const field = fieldForWidget(fields.view(), numberOf(row.ref));
|
|
1756
|
+
const field = fieldForWidget(fields.view(), numberOf$1(row.ref));
|
|
1635
1757
|
if (!field) return null;
|
|
1636
1758
|
const text = formatted.textOf(field);
|
|
1637
1759
|
const shown = text === null ? {} : { text };
|
|
@@ -1686,10 +1808,10 @@ function createWidgetLooks({ fields, formatted, design, fonts }) {
|
|
|
1686
1808
|
};
|
|
1687
1809
|
return {
|
|
1688
1810
|
listShownWidgets: (pageObjectNumber) => shownOn(pageObjectNumber),
|
|
1689
|
-
getWidgetPainting: (ref) => sceneOf(numberOf(ref)),
|
|
1811
|
+
getWidgetPainting: (ref) => sceneOf(numberOf$1(ref)),
|
|
1690
1812
|
/** The widget's look in the view isn't its confirmed look yet: it draws live until its picture is. */
|
|
1691
1813
|
isPending: (ref) => {
|
|
1692
|
-
const number = numberOf(ref);
|
|
1814
|
+
const number = numberOf$1(ref);
|
|
1693
1815
|
return lookOf(fields.view(), number) !== lookOf(fields.get(), number);
|
|
1694
1816
|
},
|
|
1695
1817
|
/**
|
|
@@ -1697,12 +1819,12 @@ function createWidgetLooks({ fields, formatted, design, fonts }) {
|
|
|
1697
1819
|
* for one whose field it doesn't draw, or with a mark in it, which only its picture shows.
|
|
1698
1820
|
*/
|
|
1699
1821
|
painterOf: (row) => {
|
|
1700
|
-
const field = fieldForWidget(fields.view(), numberOf(row.ref));
|
|
1822
|
+
const field = fieldForWidget(fields.view(), numberOf$1(row.ref));
|
|
1701
1823
|
return field && (0, _embedpdf_core_form.paints)(field) && !row.hasMark ? (box) => paint(row, box, 0, true) ?? [] : null;
|
|
1702
1824
|
},
|
|
1703
1825
|
getTextEditorLayout: (ref, text) => {
|
|
1704
1826
|
const view = fields.view();
|
|
1705
|
-
const number = numberOf(ref);
|
|
1827
|
+
const number = numberOf$1(ref);
|
|
1706
1828
|
const row = widgetRowOf(view, number);
|
|
1707
1829
|
const field = row ? fieldForWidget(view, number) : null;
|
|
1708
1830
|
if (!row || !field) return null;
|
|
@@ -1711,7 +1833,7 @@ function createWidgetLooks({ fields, formatted, design, fonts }) {
|
|
|
1711
1833
|
return layout && (painted ? layout : inItsFont(layout, row.fontFamily ?? null));
|
|
1712
1834
|
},
|
|
1713
1835
|
getWidgetFontCss: (ref) => {
|
|
1714
|
-
const row = widgetRowOf(fields.view(), numberOf(ref));
|
|
1836
|
+
const row = widgetRowOf(fields.view(), numberOf$1(ref));
|
|
1715
1837
|
return row ? fontCssOf(row.fontFamily ?? "helvetica") : null;
|
|
1716
1838
|
}
|
|
1717
1839
|
};
|
|
@@ -1993,6 +2115,182 @@ function createObjectNumbers(ctx) {
|
|
|
1993
2115
|
};
|
|
1994
2116
|
}
|
|
1995
2117
|
//#endregion
|
|
2118
|
+
//#region src/write/new-widget.ts
|
|
2119
|
+
/**
|
|
2120
|
+
* The value a radio group's new button gets: the next free `Choice<n>`,
|
|
2121
|
+
* counting on from its buttons (the radio tool names its first `Choice1`).
|
|
2122
|
+
*/
|
|
2123
|
+
function nextChoiceOf(exportValues) {
|
|
2124
|
+
const taken = new Set(exportValues);
|
|
2125
|
+
let n = exportValues.length + 1;
|
|
2126
|
+
while (taken.has(`Choice${n}`)) n += 1;
|
|
2127
|
+
return `Choice${n}`;
|
|
2128
|
+
}
|
|
2129
|
+
/** Whether the field's dictionary is its only widget's too, which adding a widget splits. */
|
|
2130
|
+
const isMerged = (field) => field.ref.kind === "objectNumber" && field.widgets.some((widget) => widget.objectNumber === field.ref.objectNumber);
|
|
2131
|
+
//#endregion
|
|
2132
|
+
//#region src/write/duplicate.ts
|
|
2133
|
+
/**
|
|
2134
|
+
* Duplicate, for a form's widgets. What a copy becomes follows from what its
|
|
2135
|
+
* widget is. A radio button is one answer of its group, so its copy is
|
|
2136
|
+
* another button of that group (`forms.addWidget`), with the next free value.
|
|
2137
|
+
* Any other widget is a whole question, so its copy is a new field
|
|
2138
|
+
* (`forms.copy`), and widgets of one field copied together become one new
|
|
2139
|
+
* field. Pure: the ops, given the object numbers they take.
|
|
2140
|
+
*/
|
|
2141
|
+
/** The flags a widget has when a create or `addWidget` made it. */
|
|
2142
|
+
const NEW_WIDGET_FLAGS = {
|
|
2143
|
+
invisible: false,
|
|
2144
|
+
hidden: false,
|
|
2145
|
+
print: true,
|
|
2146
|
+
noZoom: false,
|
|
2147
|
+
noRotate: false,
|
|
2148
|
+
noView: false,
|
|
2149
|
+
readOnly: false,
|
|
2150
|
+
locked: false,
|
|
2151
|
+
toggleNoView: false,
|
|
2152
|
+
lockedContents: false
|
|
2153
|
+
};
|
|
2154
|
+
/** The flags of `row` a new widget doesn't have, as a widget patch. */
|
|
2155
|
+
function flagsToSet(row) {
|
|
2156
|
+
const flags = (0, _embedpdf_engine_core_runtime.widgetFlagsOf)(row);
|
|
2157
|
+
const fresh = NEW_WIDGET_FLAGS;
|
|
2158
|
+
return Object.fromEntries(Object.entries(flags).filter(([name, value]) => value !== fresh[name]));
|
|
2159
|
+
}
|
|
2160
|
+
const numberOf = (ref) => ref.kind === "objectNumber" ? ref.objectNumber : -1;
|
|
2161
|
+
/**
|
|
2162
|
+
* The ops that copy `widgets`, which the form shows, by the rule above.
|
|
2163
|
+
* `mayScript` is whether the session may write scripts, submits and links: a
|
|
2164
|
+
* copied radio button is a write of this plugin's, so it leaves them out
|
|
2165
|
+
* itself; a copied field is the engine's write, which leaves them out there.
|
|
2166
|
+
* Throws `not-found` for a widget the form doesn't show.
|
|
2167
|
+
*/
|
|
2168
|
+
function widgetDuplicateOf(index, widgets, placement, options) {
|
|
2169
|
+
const { snapshot } = index;
|
|
2170
|
+
const offset = placement.offset ?? {
|
|
2171
|
+
x: 0,
|
|
2172
|
+
y: 0
|
|
2173
|
+
};
|
|
2174
|
+
const dropped = [];
|
|
2175
|
+
const rows = widgets.map((ref) => {
|
|
2176
|
+
const row = widgetRowOf(index, numberOf(ref));
|
|
2177
|
+
if (!row || !snapshot) throw new _embedpdf_core.PluginError("not-found", "form", `no widget ${(0, _embedpdf_engine_core_runtime.annotationKey)(ref)}`);
|
|
2178
|
+
return row;
|
|
2179
|
+
});
|
|
2180
|
+
/** The fields copied whole or in part, in the order their first widget was given. */
|
|
2181
|
+
const fields = [];
|
|
2182
|
+
const only = [];
|
|
2183
|
+
/** Each radio button copied, with the new button's place, look and value. */
|
|
2184
|
+
const buttons = [];
|
|
2185
|
+
const values = /* @__PURE__ */ new Map();
|
|
2186
|
+
const split = /* @__PURE__ */ new Set();
|
|
2187
|
+
for (const row of rows) {
|
|
2188
|
+
const field = fieldForWidget(index, numberOf(row.ref));
|
|
2189
|
+
if (!field) {
|
|
2190
|
+
dropped.push({
|
|
2191
|
+
ref: null,
|
|
2192
|
+
widget: row.ref,
|
|
2193
|
+
reason: "no-field"
|
|
2194
|
+
});
|
|
2195
|
+
continue;
|
|
2196
|
+
}
|
|
2197
|
+
const key = fieldKeyOf(field);
|
|
2198
|
+
if (field.kind !== "radio") {
|
|
2199
|
+
if (!fields.some((known) => fieldKeyOf(known) === key)) fields.push(field);
|
|
2200
|
+
only.push(row.ref);
|
|
2201
|
+
continue;
|
|
2202
|
+
}
|
|
2203
|
+
const taken = values.get(key) ?? field.widgets.map((widget) => widget.exportValue);
|
|
2204
|
+
const exportValue = nextChoiceOf(taken);
|
|
2205
|
+
values.set(key, [...taken, exportValue]);
|
|
2206
|
+
const look = (0, _embedpdf_engine_core_runtime.widgetPlacementOf)(field, row, (same) => same, options.mayScript, dropped);
|
|
2207
|
+
buttons.push({
|
|
2208
|
+
row,
|
|
2209
|
+
field,
|
|
2210
|
+
placement: {
|
|
2211
|
+
...look,
|
|
2212
|
+
...placement.page ? { page: placement.page } : {},
|
|
2213
|
+
...look.rect ? { rect: {
|
|
2214
|
+
...look.rect,
|
|
2215
|
+
x: look.rect.x + offset.x,
|
|
2216
|
+
y: look.rect.y + offset.y
|
|
2217
|
+
} } : {},
|
|
2218
|
+
exportValue
|
|
2219
|
+
},
|
|
2220
|
+
split: isMerged(field) && !split.has(key)
|
|
2221
|
+
});
|
|
2222
|
+
split.add(key);
|
|
2223
|
+
}
|
|
2224
|
+
let plan = null;
|
|
2225
|
+
if (fields.length > 0 && snapshot) plan = (0, _embedpdf_engine_core_runtime.planFormCopies)({
|
|
2226
|
+
form: snapshot,
|
|
2227
|
+
fields: fields.map((field) => field.ref),
|
|
2228
|
+
widgets: only,
|
|
2229
|
+
placement,
|
|
2230
|
+
values: options.values,
|
|
2231
|
+
mayScript: true
|
|
2232
|
+
});
|
|
2233
|
+
const forFields = plan ? plan.creates.reduce((count, create) => count + 1 + create.widgets.length, 0) : 0;
|
|
2234
|
+
return {
|
|
2235
|
+
needs: forFields + buttons.reduce((count, button) => count + (button.split ? 2 : 1), 0),
|
|
2236
|
+
dropped,
|
|
2237
|
+
build(numbers) {
|
|
2238
|
+
const ops = [];
|
|
2239
|
+
const copyOf = /* @__PURE__ */ new Map();
|
|
2240
|
+
let at = 0;
|
|
2241
|
+
if (plan) {
|
|
2242
|
+
ops.push({
|
|
2243
|
+
type: "forms.copy",
|
|
2244
|
+
fields: fields.map((field) => field.ref),
|
|
2245
|
+
widgets: only,
|
|
2246
|
+
...placement.page ? { page: placement.page } : {},
|
|
2247
|
+
...placement.offset ? { offset: placement.offset } : {},
|
|
2248
|
+
values: options.values,
|
|
2249
|
+
objectNumbers: numbers.slice(0, forFields)
|
|
2250
|
+
});
|
|
2251
|
+
for (const create of plan.creates) {
|
|
2252
|
+
at += 1;
|
|
2253
|
+
create.widgets.forEach((source, widget) => {
|
|
2254
|
+
copyOf.set((0, _embedpdf_engine_core_runtime.annotationKey)(source), {
|
|
2255
|
+
kind: "objectNumber",
|
|
2256
|
+
page: create.draft.widgets[widget].page,
|
|
2257
|
+
objectNumber: numbers[at + widget]
|
|
2258
|
+
});
|
|
2259
|
+
});
|
|
2260
|
+
at += create.widgets.length;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
for (const button of buttons) {
|
|
2264
|
+
const objectNumber = numbers[at];
|
|
2265
|
+
const copy = {
|
|
2266
|
+
kind: "objectNumber",
|
|
2267
|
+
page: button.placement.page,
|
|
2268
|
+
objectNumber
|
|
2269
|
+
};
|
|
2270
|
+
ops.push({
|
|
2271
|
+
type: "forms.addWidget",
|
|
2272
|
+
field: button.field.ref,
|
|
2273
|
+
placement: button.placement,
|
|
2274
|
+
objectNumber,
|
|
2275
|
+
...button.split ? { splitObjectNumber: numbers[at + 1] } : {}
|
|
2276
|
+
});
|
|
2277
|
+
at += button.split ? 2 : 1;
|
|
2278
|
+
const flags = flagsToSet(button.row);
|
|
2279
|
+
if (Object.keys(flags).length > 0) ops.push({
|
|
2280
|
+
type: "forms.updateWidget",
|
|
2281
|
+
widget: copy,
|
|
2282
|
+
patch: flags
|
|
2283
|
+
});
|
|
2284
|
+
copyOf.set((0, _embedpdf_engine_core_runtime.annotationKey)(button.row.ref), copy);
|
|
2285
|
+
}
|
|
2286
|
+
return {
|
|
2287
|
+
ops,
|
|
2288
|
+
copies: rows.flatMap((row) => copyOf.get((0, _embedpdf_engine_core_runtime.annotationKey)(row.ref)) ?? [])
|
|
2289
|
+
};
|
|
2290
|
+
}
|
|
2291
|
+
};
|
|
2292
|
+
}
|
|
2293
|
+
//#endregion
|
|
1996
2294
|
//#region src/write/next-in-line.ts
|
|
1997
2295
|
const fits = (box, page) => box.x >= 0 && box.y >= 0 && box.x + box.width <= page.width && box.y + box.height <= page.height;
|
|
1998
2296
|
/** The box after `boxes`, the field's widgets on one page in the field's order. */
|
|
@@ -2087,13 +2385,8 @@ const lookOf = (widget) => {
|
|
|
2087
2385
|
function exportValueFor(field) {
|
|
2088
2386
|
if (field.kind === "checkbox") return field.exportValue;
|
|
2089
2387
|
if (field.kind !== "radio") return void 0;
|
|
2090
|
-
|
|
2091
|
-
let n = field.widgets.length + 1;
|
|
2092
|
-
while (taken.has(`Choice${n}`)) n += 1;
|
|
2093
|
-
return `Choice${n}`;
|
|
2388
|
+
return nextChoiceOf(field.widgets.map((widget) => widget.exportValue));
|
|
2094
2389
|
}
|
|
2095
|
-
/** Whether the field's dictionary is its only widget's too, which adding a widget splits. */
|
|
2096
|
-
const isMerged = (field) => field.ref.kind === "objectNumber" && field.widgets.some((widget) => widget.objectNumber === field.ref.objectNumber);
|
|
2097
2390
|
const MODIFY = "doc.forms.modify";
|
|
2098
2391
|
const SCRIPT = "doc.forms.script";
|
|
2099
2392
|
function createFieldWrites(ctx, { fields, enqueue }) {
|
|
@@ -2109,6 +2402,12 @@ function createFieldWrites(ctx, { fields, enqueue }) {
|
|
|
2109
2402
|
ops: [op],
|
|
2110
2403
|
undo: undoLookOf(fields.view(), [op])
|
|
2111
2404
|
});
|
|
2405
|
+
/** Stage the ops of one design verb as one change, one step of the history. */
|
|
2406
|
+
const stageAll = (verb, ops) => ctx.changes.stage({
|
|
2407
|
+
label: { key: `form.${verb}` },
|
|
2408
|
+
ops: [...ops],
|
|
2409
|
+
undo: undoLookOf(fields.view(), ops)
|
|
2410
|
+
});
|
|
2112
2411
|
/** The engine's answer to a staged design change: its one item. */
|
|
2113
2412
|
const answer = async (change, options) => {
|
|
2114
2413
|
try {
|
|
@@ -2165,67 +2464,180 @@ function createFieldWrites(ctx, { fields, enqueue }) {
|
|
|
2165
2464
|
widget
|
|
2166
2465
|
};
|
|
2167
2466
|
};
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
fields,
|
|
2185
|
-
position
|
|
2186
|
-
});
|
|
2187
|
-
return { calculationOrder: (await answer(change, options)).calculationOrder };
|
|
2188
|
-
},
|
|
2189
|
-
delete: async (ref, options) => {
|
|
2190
|
-
allow("form.delete");
|
|
2191
|
-
await answer(stage("delete", {
|
|
2192
|
-
type: "forms.delete",
|
|
2193
|
-
field: ref
|
|
2194
|
-
}), options);
|
|
2195
|
-
},
|
|
2196
|
-
removeWidget: async (ref, widget, options) => {
|
|
2197
|
-
allow("form.removeWidget");
|
|
2198
|
-
const change = stage("removeWidget", {
|
|
2199
|
-
type: "forms.removeWidget",
|
|
2200
|
-
field: ref,
|
|
2201
|
-
widget
|
|
2202
|
-
});
|
|
2203
|
-
return { field: (await answer(change, options)).field };
|
|
2204
|
-
},
|
|
2205
|
-
updateWidget: async (widget, patch, options) => {
|
|
2206
|
-
allow("form.updateWidget");
|
|
2207
|
-
const change = stage("updateWidget", {
|
|
2208
|
-
type: "forms.updateWidget",
|
|
2209
|
-
widget,
|
|
2210
|
-
patch
|
|
2211
|
-
});
|
|
2212
|
-
return { widget: (await answer(change, options)).widget };
|
|
2213
|
-
},
|
|
2214
|
-
deleteWidget: async (widget, options) => {
|
|
2215
|
-
allow("form.deleteWidget");
|
|
2216
|
-
const change = stage("deleteWidget", {
|
|
2217
|
-
type: "forms.deleteWidget",
|
|
2218
|
-
widget
|
|
2467
|
+
/** Where a duplicate's copies go, with a page index as that page's ref. */
|
|
2468
|
+
const placementOf = (options) => ({
|
|
2469
|
+
...options.page === void 0 ? {} : { page: ctx.pageOf(options.page).ref },
|
|
2470
|
+
...options.offset ? { offset: options.offset } : {}
|
|
2471
|
+
});
|
|
2472
|
+
/**
|
|
2473
|
+
* The ops that copy these widgets by the designer's rule (write/duplicate.ts),
|
|
2474
|
+
* under the object numbers they take, with each copy's ref and what was
|
|
2475
|
+
* left out before the engine is asked.
|
|
2476
|
+
*/
|
|
2477
|
+
const copyOps = async (widgets, placement, values) => {
|
|
2478
|
+
let plan;
|
|
2479
|
+
try {
|
|
2480
|
+
plan = widgetDuplicateOf(fields.view(), widgets, placement, {
|
|
2481
|
+
values,
|
|
2482
|
+
mayScript: ctx.allows(SCRIPT)
|
|
2219
2483
|
});
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
repair: async (options) => {
|
|
2223
|
-
allow("form.repair");
|
|
2224
|
-
const { signal, ...repair } = options ?? {};
|
|
2225
|
-
const run = () => options ? ctx.doc.forms.repair(repair) : ctx.doc.forms.repair();
|
|
2226
|
-
return enqueue(() => ctx.cancellable(signal, run()), options);
|
|
2484
|
+
} catch (error) {
|
|
2485
|
+
throw (0, _embedpdf_core.toPluginError)("form", error);
|
|
2227
2486
|
}
|
|
2228
|
-
|
|
2487
|
+
const taken = plan.needs ? numbers.takeNow(plan.needs) ?? await numbers.take(plan.needs) : [];
|
|
2488
|
+
return {
|
|
2489
|
+
...plan.build(taken),
|
|
2490
|
+
dropped: plan.dropped
|
|
2491
|
+
};
|
|
2492
|
+
};
|
|
2493
|
+
const duplicate = async (refs, options = {}) => {
|
|
2494
|
+
allow("form.duplicate");
|
|
2495
|
+
if (refs.length === 0) return {
|
|
2496
|
+
fields: [],
|
|
2497
|
+
widgets: [],
|
|
2498
|
+
dropped: []
|
|
2499
|
+
};
|
|
2500
|
+
const placement = placementOf(options);
|
|
2501
|
+
const values = options.values ?? false;
|
|
2502
|
+
const { snapshot } = fields.view();
|
|
2503
|
+
if (!snapshot) throw new _embedpdf_core.PluginError("not-ready", "form", "the form is still loading");
|
|
2504
|
+
let count;
|
|
2505
|
+
try {
|
|
2506
|
+
count = (0, _embedpdf_engine_core_runtime.planFormCopies)({
|
|
2507
|
+
form: snapshot,
|
|
2508
|
+
fields: refs,
|
|
2509
|
+
placement,
|
|
2510
|
+
values,
|
|
2511
|
+
mayScript: true
|
|
2512
|
+
}).creates.reduce((sum, create) => sum + 1 + create.widgets.length, 0);
|
|
2513
|
+
} catch (error) {
|
|
2514
|
+
throw (0, _embedpdf_core.toPluginError)("form", error);
|
|
2515
|
+
}
|
|
2516
|
+
const objectNumbers = count ? numbers.takeNow(count) ?? await numbers.take(count) : [];
|
|
2517
|
+
const change = stageAll("duplicate", [{
|
|
2518
|
+
type: "forms.copy",
|
|
2519
|
+
fields: refs,
|
|
2520
|
+
...placement,
|
|
2521
|
+
values,
|
|
2522
|
+
objectNumbers
|
|
2523
|
+
}]);
|
|
2524
|
+
const item = await answer(change, options);
|
|
2525
|
+
return {
|
|
2526
|
+
fields: item.fields,
|
|
2527
|
+
widgets: item.widgets,
|
|
2528
|
+
dropped: item.dropped
|
|
2529
|
+
};
|
|
2530
|
+
};
|
|
2531
|
+
const duplicateWidgets = async (refs, options = {}) => {
|
|
2532
|
+
allow("form.duplicate");
|
|
2533
|
+
const copy = await copyOps(refs, placementOf(options), options.values ?? false);
|
|
2534
|
+
if (copy.ops.length === 0) return {
|
|
2535
|
+
fields: [],
|
|
2536
|
+
widgets: [],
|
|
2537
|
+
dropped: [...copy.dropped]
|
|
2538
|
+
};
|
|
2539
|
+
const change = stageAll("duplicate", copy.ops);
|
|
2540
|
+
let items;
|
|
2541
|
+
try {
|
|
2542
|
+
items = (await ctx.cancellable(options.signal, change.result)).items;
|
|
2543
|
+
} catch (error) {
|
|
2544
|
+
throw (0, _embedpdf_core.toPluginError)("form", error);
|
|
2545
|
+
}
|
|
2546
|
+
return duplicateResultOf(items, copy.copies, copy.dropped);
|
|
2547
|
+
};
|
|
2548
|
+
return {
|
|
2549
|
+
copyOps,
|
|
2550
|
+
api: {
|
|
2551
|
+
create,
|
|
2552
|
+
addWidget,
|
|
2553
|
+
duplicate,
|
|
2554
|
+
duplicateWidgets,
|
|
2555
|
+
update: async (ref, patch, options) => {
|
|
2556
|
+
allow("form.update", (0, _embedpdf_engine_core_runtime.writesScripts)(patch.actions));
|
|
2557
|
+
const change = stage("update", {
|
|
2558
|
+
type: "forms.update",
|
|
2559
|
+
field: ref,
|
|
2560
|
+
patch
|
|
2561
|
+
});
|
|
2562
|
+
return { field: (await answer(change, options)).field };
|
|
2563
|
+
},
|
|
2564
|
+
reorderCalculations: async (fields, position, options) => {
|
|
2565
|
+
allow("form.reorderCalculations");
|
|
2566
|
+
const change = stage("reorderCalculations", {
|
|
2567
|
+
type: "forms.reorderCalculations",
|
|
2568
|
+
fields,
|
|
2569
|
+
position
|
|
2570
|
+
});
|
|
2571
|
+
return { calculationOrder: (await answer(change, options)).calculationOrder };
|
|
2572
|
+
},
|
|
2573
|
+
delete: async (ref, options) => {
|
|
2574
|
+
allow("form.delete");
|
|
2575
|
+
await answer(stage("delete", {
|
|
2576
|
+
type: "forms.delete",
|
|
2577
|
+
field: ref
|
|
2578
|
+
}), options);
|
|
2579
|
+
},
|
|
2580
|
+
removeWidget: async (ref, widget, options) => {
|
|
2581
|
+
allow("form.removeWidget");
|
|
2582
|
+
const change = stage("removeWidget", {
|
|
2583
|
+
type: "forms.removeWidget",
|
|
2584
|
+
field: ref,
|
|
2585
|
+
widget
|
|
2586
|
+
});
|
|
2587
|
+
return { field: (await answer(change, options)).field };
|
|
2588
|
+
},
|
|
2589
|
+
updateWidget: async (widget, patch, options) => {
|
|
2590
|
+
allow("form.updateWidget");
|
|
2591
|
+
const change = stage("updateWidget", {
|
|
2592
|
+
type: "forms.updateWidget",
|
|
2593
|
+
widget,
|
|
2594
|
+
patch
|
|
2595
|
+
});
|
|
2596
|
+
return { widget: (await answer(change, options)).widget };
|
|
2597
|
+
},
|
|
2598
|
+
deleteWidget: async (widget, options) => {
|
|
2599
|
+
allow("form.deleteWidget");
|
|
2600
|
+
const change = stage("deleteWidget", {
|
|
2601
|
+
type: "forms.deleteWidget",
|
|
2602
|
+
widget
|
|
2603
|
+
});
|
|
2604
|
+
return { field: (await answer(change, options)).field };
|
|
2605
|
+
},
|
|
2606
|
+
repair: async (options) => {
|
|
2607
|
+
allow("form.repair");
|
|
2608
|
+
const { signal, ...repair } = options ?? {};
|
|
2609
|
+
const run = () => options ? ctx.doc.forms.repair(repair) : ctx.doc.forms.repair();
|
|
2610
|
+
return enqueue(() => ctx.cancellable(signal, run()), options);
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
};
|
|
2614
|
+
}
|
|
2615
|
+
/**
|
|
2616
|
+
* What a duplicate of widgets resolves, read off the engine's answer: the
|
|
2617
|
+
* fields it made and each field that got a new button, as they are after the
|
|
2618
|
+
* last op that named them; the copies' rows, in the order their originals
|
|
2619
|
+
* were given; and what was left out, here and by the engine.
|
|
2620
|
+
*/
|
|
2621
|
+
function duplicateResultOf(items, copies, dropped) {
|
|
2622
|
+
const fields = /* @__PURE__ */ new Map();
|
|
2623
|
+
const rows = /* @__PURE__ */ new Map();
|
|
2624
|
+
const left = [...dropped];
|
|
2625
|
+
const hold = (row) => {
|
|
2626
|
+
if (row.ref.kind === "objectNumber") rows.set(row.ref.objectNumber, row);
|
|
2627
|
+
};
|
|
2628
|
+
for (const item of items) if (item.type === "forms.copy") {
|
|
2629
|
+
for (const field of item.fields) fields.set(fieldKeyOf(field), field);
|
|
2630
|
+
item.widgets.forEach(hold);
|
|
2631
|
+
left.push(...item.dropped);
|
|
2632
|
+
} else if (item.type === "forms.addWidget") {
|
|
2633
|
+
fields.set(fieldKeyOf(item.field), item.field);
|
|
2634
|
+
item.widgets.forEach(hold);
|
|
2635
|
+
} else if (item.type === "forms.updateWidget") hold(item.widget);
|
|
2636
|
+
return {
|
|
2637
|
+
fields: [...fields.values()],
|
|
2638
|
+
widgets: copies.flatMap((copy) => copy.kind === "objectNumber" ? rows.get(copy.objectNumber) ?? [] : []),
|
|
2639
|
+
dropped: left
|
|
2640
|
+
};
|
|
2229
2641
|
}
|
|
2230
2642
|
//#endregion
|
|
2231
2643
|
//#region src/write/fill.ts
|
|
@@ -2630,17 +3042,32 @@ function createInterchange(ctx, services) {
|
|
|
2630
3042
|
})), options);
|
|
2631
3043
|
};
|
|
2632
3044
|
const importBundle = async (bundle, options = {}) => {
|
|
2633
|
-
const { signal, ...importOptions } = options;
|
|
3045
|
+
const { signal, history, ...importOptions } = options;
|
|
2634
3046
|
ctx.assertAllowed("doc.forms.modify", "form.import");
|
|
2635
3047
|
if ((importOptions.attribution ?? "restore") === "restore") ctx.assertAllowed("doc.forms.import", "form.import");
|
|
2636
3048
|
return enqueue(async () => {
|
|
2637
|
-
const
|
|
3049
|
+
const { items } = await ctx.changes.stage({
|
|
3050
|
+
label: { key: "form.import" },
|
|
3051
|
+
opId: importOptions.opId,
|
|
3052
|
+
history,
|
|
3053
|
+
send: (doc, { opId }) => ctx.cancellable(signal, doc.forms.import(bundle, {
|
|
3054
|
+
...importOptions,
|
|
3055
|
+
opId
|
|
3056
|
+
})).then((result) => ({
|
|
3057
|
+
items: [{
|
|
3058
|
+
type: "forms.import",
|
|
3059
|
+
...result
|
|
3060
|
+
}],
|
|
3061
|
+
meta: result.meta
|
|
3062
|
+
}))
|
|
3063
|
+
}).result;
|
|
3064
|
+
const imported = items[0];
|
|
2638
3065
|
const annotationHost = siblings.annotation();
|
|
2639
3066
|
if (annotationHost) await annotationHost.whenSynced();
|
|
2640
3067
|
return {
|
|
2641
|
-
fields:
|
|
2642
|
-
refMap:
|
|
2643
|
-
dropped:
|
|
3068
|
+
fields: imported.fields,
|
|
3069
|
+
refMap: imported.refMap,
|
|
3070
|
+
dropped: imported.dropped
|
|
2644
3071
|
};
|
|
2645
3072
|
}, options);
|
|
2646
3073
|
};
|
|
@@ -2661,7 +3088,7 @@ function createInterchange(ctx, services) {
|
|
|
2661
3088
|
* form layer draws and fills every widget.
|
|
2662
3089
|
*/
|
|
2663
3090
|
const objectNumberOf = (ref) => ref.kind === "objectNumber" ? ref.objectNumber : -1;
|
|
2664
|
-
function createWidgetLender(ctx, { fields, design, pictures, looks }) {
|
|
3091
|
+
function createWidgetLender(ctx, { fields, design, pictures, looks }, copyOps) {
|
|
2665
3092
|
const records = (0, _embedpdf_core.memo)(() => [
|
|
2666
3093
|
design.lending(),
|
|
2667
3094
|
fields.get().snapshot,
|
|
@@ -2736,11 +3163,13 @@ function createWidgetLender(ctx, { fields, design, pictures, looks }) {
|
|
|
2736
3163
|
widgets: [...refs],
|
|
2737
3164
|
position
|
|
2738
3165
|
}],
|
|
3166
|
+
copyOps: (refs, placement) => copyOps(refs, placement, false),
|
|
2739
3167
|
getAuthority: () => {
|
|
2740
3168
|
const may = ctx.allows("doc.forms.modify");
|
|
2741
3169
|
return {
|
|
2742
3170
|
update: may,
|
|
2743
|
-
delete: may
|
|
3171
|
+
delete: may,
|
|
3172
|
+
copy: may
|
|
2744
3173
|
};
|
|
2745
3174
|
},
|
|
2746
3175
|
getPictureEpoch: pictures.epoch,
|
|
@@ -3223,7 +3652,7 @@ function createFormController(ctx) {
|
|
|
3223
3652
|
]);
|
|
3224
3653
|
return {
|
|
3225
3654
|
api,
|
|
3226
|
-
connect: () => connectForm(ctx, api, createWidgetLender(ctx, services))
|
|
3655
|
+
connect: () => connectForm(ctx, api, createWidgetLender(ctx, services, design.copyOps))
|
|
3227
3656
|
};
|
|
3228
3657
|
}
|
|
3229
3658
|
//#endregion
|