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