@embedpdf/engine-core 3.0.0-next.14 → 3.0.0-next.15

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.
Files changed (134) hide show
  1. package/dist/AnnotationList-CkijRHet.d.cts +27163 -0
  2. package/dist/AnnotationList-CkijRHet.d.ts +27163 -0
  3. package/dist/Change-CKoxfTNY.js +889 -0
  4. package/dist/Change-CKoxfTNY.js.map +1 -0
  5. package/dist/Change-CjImsrN0.cjs +1020 -0
  6. package/dist/Change-CjImsrN0.cjs.map +1 -0
  7. package/dist/DocumentRenderService-BfFdFSwI.cjs +120 -0
  8. package/dist/DocumentRenderService-BfFdFSwI.cjs.map +1 -0
  9. package/dist/DocumentRenderService-DV3J9lMz.js +85 -0
  10. package/dist/DocumentRenderService-DV3J9lMz.js.map +1 -0
  11. package/dist/{DocumentSecurityService-Ca6EK3-3.d.ts → DocumentSecurityService-CU1M6Q7m.d.ts} +100 -112
  12. package/dist/{DocumentSecurityService-CIx1MT0T.d.cts → DocumentSecurityService-e6vp2de8.d.cts} +100 -112
  13. package/dist/LocalDocumentHandle-D02FMm-Z.cjs +53 -0
  14. package/dist/LocalDocumentHandle-D02FMm-Z.cjs.map +1 -0
  15. package/dist/LocalDocumentHandle-DAPdaOt_.js +30 -0
  16. package/dist/LocalDocumentHandle-DAPdaOt_.js.map +1 -0
  17. package/dist/LocalEngine-DFoBm4B3.d.cts +1565 -0
  18. package/dist/LocalEngine-DPriFujS.d.ts +1565 -0
  19. package/dist/PageInsertBlankInput-CXHAM7l6.cjs +17 -0
  20. package/dist/PageInsertBlankInput-CXHAM7l6.cjs.map +1 -0
  21. package/dist/PageInsertBlankInput-DhVYVJ_S.js +12 -0
  22. package/dist/PageInsertBlankInput-DhVYVJ_S.js.map +1 -0
  23. package/dist/authority-B3BklsB4.js +2239 -0
  24. package/dist/authority-B3BklsB4.js.map +1 -0
  25. package/dist/{viewport-BWeNHZmb.cjs → authority-DLMNR7ff.cjs} +820 -1647
  26. package/dist/authority-DLMNR7ff.cjs.map +1 -0
  27. package/dist/builders-CNj0POeY.js +161 -0
  28. package/dist/builders-CNj0POeY.js.map +1 -0
  29. package/dist/builders-Cgdp_YCn.cjs +184 -0
  30. package/dist/builders-Cgdp_YCn.cjs.map +1 -0
  31. package/dist/calibrate-7T3fnP8-.js +2404 -0
  32. package/dist/calibrate-7T3fnP8-.js.map +1 -0
  33. package/dist/calibrate-DJBXnhLE.cjs +2919 -0
  34. package/dist/calibrate-DJBXnhLE.cjs.map +1 -0
  35. package/dist/charmap-Cprti1ta.js +239 -0
  36. package/dist/charmap-Cprti1ta.js.map +1 -0
  37. package/dist/charmap-aYlefQP9.cjs +304 -0
  38. package/dist/charmap-aYlefQP9.cjs.map +1 -0
  39. package/dist/conformance.cjs +16466 -5247
  40. package/dist/conformance.cjs.map +1 -1
  41. package/dist/conformance.d.cts +924 -165
  42. package/dist/conformance.d.ts +924 -165
  43. package/dist/conformance.js +16429 -5252
  44. package/dist/conformance.js.map +1 -1
  45. package/dist/format-Bp3QFSH1.cjs +574 -0
  46. package/dist/format-Bp3QFSH1.cjs.map +1 -0
  47. package/dist/format-Dgg428Kb.js +461 -0
  48. package/dist/format-Dgg428Kb.js.map +1 -0
  49. package/dist/index.cjs +323 -116
  50. package/dist/index.d.cts +2 -2
  51. package/dist/index.d.ts +2 -2
  52. package/dist/index.js +14 -5
  53. package/dist/pageRefs-B3ur7Ckm.cjs +51 -0
  54. package/dist/pageRefs-B3ur7Ckm.cjs.map +1 -0
  55. package/dist/pageRefs-DkbuxSCV.js +40 -0
  56. package/dist/pageRefs-DkbuxSCV.js.map +1 -0
  57. package/dist/public.cjs +57 -0
  58. package/dist/public.d.cts +5 -0
  59. package/dist/public.d.ts +5 -0
  60. package/dist/public.js +9 -0
  61. package/dist/resolveAnnotationDraft-7Ke4nV7F.cjs +3646 -0
  62. package/dist/resolveAnnotationDraft-7Ke4nV7F.cjs.map +1 -0
  63. package/dist/resolveAnnotationDraft-CUpf0SMh.js +3167 -0
  64. package/dist/resolveAnnotationDraft-CUpf0SMh.js.map +1 -0
  65. package/dist/{resolver-inAVzsWt.js → resolver-CzxmD9Ck.js} +294 -144
  66. package/dist/resolver-CzxmD9Ck.js.map +1 -0
  67. package/dist/{resolver-CYVNV4hn.cjs → resolver-D2OCtE8F.cjs} +347 -143
  68. package/dist/resolver-D2OCtE8F.cjs.map +1 -0
  69. package/dist/runtime-Cl0v56mm.d.ts +2032 -0
  70. package/dist/runtime-p1WDzDo4.d.cts +2032 -0
  71. package/dist/runtime.cjs +726 -297
  72. package/dist/runtime.cjs.map +1 -1
  73. package/dist/runtime.d.cts +5 -1372
  74. package/dist/runtime.d.ts +5 -1372
  75. package/dist/runtime.js +396 -179
  76. package/dist/runtime.js.map +1 -1
  77. package/dist/schema-C-gU4vj7.cjs +3034 -0
  78. package/dist/schema-C-gU4vj7.cjs.map +1 -0
  79. package/dist/schema-DzlUNk38.js +2081 -0
  80. package/dist/schema-DzlUNk38.js.map +1 -0
  81. package/dist/schemas-Berf1swF.cjs +2481 -0
  82. package/dist/schemas-Berf1swF.cjs.map +1 -0
  83. package/dist/schemas-D6xOGjqg.js +1654 -0
  84. package/dist/schemas-D6xOGjqg.js.map +1 -0
  85. package/dist/subtype-CG4WpIRj.js +129 -0
  86. package/dist/subtype-CG4WpIRj.js.map +1 -0
  87. package/dist/subtype-aDt5TFmK.cjs +158 -0
  88. package/dist/subtype-aDt5TFmK.cjs.map +1 -0
  89. package/dist/validation.cjs +578 -0
  90. package/dist/validation.cjs.map +1 -0
  91. package/dist/validation.d.cts +174 -0
  92. package/dist/validation.d.ts +174 -0
  93. package/dist/validation.js +571 -0
  94. package/dist/validation.js.map +1 -0
  95. package/dist/value-BFS1R8ek.cjs +435 -0
  96. package/dist/value-BFS1R8ek.cjs.map +1 -0
  97. package/dist/value-Dhc8eYcx.js +340 -0
  98. package/dist/value-Dhc8eYcx.js.map +1 -0
  99. package/dist/viewport-BJW13EGZ.js +540 -0
  100. package/dist/viewport-BJW13EGZ.js.map +1 -0
  101. package/dist/viewport-DlL5Bxbg.cjs +671 -0
  102. package/dist/viewport-DlL5Bxbg.cjs.map +1 -0
  103. package/dist/wire.cjs +1051 -397
  104. package/dist/wire.cjs.map +1 -1
  105. package/dist/wire.d.cts +12403 -790
  106. package/dist/wire.d.ts +12403 -790
  107. package/dist/wire.js +821 -242
  108. package/dist/wire.js.map +1 -1
  109. package/package.json +10 -1
  110. package/dist/AbortError-DwuHPK_I.js +0 -27
  111. package/dist/AbortError-DwuHPK_I.js.map +0 -1
  112. package/dist/AbortError-P0RU0Mgg.cjs +0 -38
  113. package/dist/AbortError-P0RU0Mgg.cjs.map +0 -1
  114. package/dist/AnnotationListSnapshot-D9R5n2Kf.d.cts +0 -7101
  115. package/dist/AnnotationListSnapshot-D9R5n2Kf.d.ts +0 -7101
  116. package/dist/Engine-EWWHhEIi.d.ts +0 -1277
  117. package/dist/Engine-FNaAEjfb.d.cts +0 -1277
  118. package/dist/derive-BbsDk_Kz.cjs +0 -880
  119. package/dist/derive-BbsDk_Kz.cjs.map +0 -1
  120. package/dist/derive-DAFJolWu.js +0 -647
  121. package/dist/derive-DAFJolWu.js.map +0 -1
  122. package/dist/format-Bbphf4zR.js +0 -435
  123. package/dist/format-Bbphf4zR.js.map +0 -1
  124. package/dist/format-D_epWlob.cjs +0 -518
  125. package/dist/format-D_epWlob.cjs.map +0 -1
  126. package/dist/resolver-CYVNV4hn.cjs.map +0 -1
  127. package/dist/resolver-inAVzsWt.js.map +0 -1
  128. package/dist/schemas-CFQGvWiu.js +0 -3243
  129. package/dist/schemas-CFQGvWiu.js.map +0 -1
  130. package/dist/schemas-D0g6tKa9.cjs +0 -4694
  131. package/dist/schemas-D0g6tKa9.cjs.map +0 -1
  132. package/dist/viewport-BWeNHZmb.cjs.map +0 -1
  133. package/dist/viewport-CvMcadfQ.js +0 -3072
  134. package/dist/viewport-CvMcadfQ.js.map +0 -1
@@ -0,0 +1,578 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_value = require("./value-BFS1R8ek.cjs");
3
+ const require_resolveAnnotationDraft = require("./resolveAnnotationDraft-7Ke4nV7F.cjs");
4
+ const require_format = require("./format-Bp3QFSH1.cjs");
5
+ const require_pageRefs = require("./pageRefs-B3ur7Ckm.cjs");
6
+ const require_schema = require("./schema-C-gU4vj7.cjs");
7
+ //#region src/annotation/checkWrite.ts
8
+ /**
9
+ * A prepared create's data checked against its kind's schema. The caller
10
+ * writes its own data, not the schema's output, so nothing is reshaped on its
11
+ * way in.
12
+ */
13
+ function assertAnnotationDraft(draft, options = {}) {
14
+ const declaration = require_schema.declarationOf(String(draft.subtype));
15
+ const linked = options.linked ?? [];
16
+ const checked = (declaration && linked.length > 0 ? declaration.fileSchemas.create.omit(Object.fromEntries(linked.map((name) => [name, true]))) : require_schema.FileAnnotationDraftSchema).safeParse(draft);
17
+ if (!checked.success) throw invalidWrite(checked.error, `${String(draft.subtype)} create`);
18
+ }
19
+ /**
20
+ * A prepared update's data (`pdfPrepareAnnotationPatch`) checked against the
21
+ * kind of `current`. A `readBack()` value still in it is a change, as
22
+ * preparation drops one sent back as it reads, so one a write can't make is
23
+ * refused; the rest is checked against the kind's update schema.
24
+ */
25
+ function assertAnnotationPatch(current, prepared) {
26
+ if (current.subtype === "unsupported") return;
27
+ const values = prepared;
28
+ for (const [name, write] of Object.entries(require_schema.KIND_BY_SUBTYPE[current.subtype].readBackWrites)) {
29
+ const value = values[name];
30
+ if (value === void 0 || write?.safeParse(value).success) continue;
31
+ throw new require_format.EngineError(require_format.EngineErrorCode.InvalidArg, write ? `${current.subtype} update field '${name}': this value can be read but not written; send it back unchanged or replace it` : `${current.subtype} update field '${name}': the engine works this out; send it back unchanged or leave it out`, { details: { field: name } });
32
+ }
33
+ const parsed = require_schema.fileAnnotationPatchSchemaOf(current.subtype).safeParse(prepared);
34
+ if (!parsed.success) throw invalidWrite(parsed.error, `${current.subtype} update`);
35
+ }
36
+ /**
37
+ * The draft the engine writes for `draft`, in the file's coordinates:
38
+ * prepared (`pdfPrepareAnnotationDraft`), checked against its kind, and
39
+ * every field that follows from it stated (`pdfDeriveAnnotationDraft`). A
40
+ * viewer predicts the same without schema validation
41
+ * (`pdfPredictAnnotationDraft`).
42
+ *
43
+ * Throws `InvalidArg` for a draft the engine refuses. Pure: it reads nothing
44
+ * but its arguments.
45
+ */
46
+ function pdfResolveAnnotationDraft(draft, options = {}) {
47
+ const prepared = require_resolveAnnotationDraft.pdfPrepareAnnotationDraft(draft);
48
+ assertAnnotationDraft(prepared, { linked: options.linked ?? [] });
49
+ return require_resolveAnnotationDraft.pdfDeriveAnnotationDraft(prepared, options);
50
+ }
51
+ /**
52
+ * The patch the engine writes for `patch` on `current`, in the file's
53
+ * coordinates: prepared (`pdfPrepareAnnotationPatch`), checked against its
54
+ * kind (`assertAnnotationPatch`), and every field that follows from it
55
+ * stated (`pdfDeriveAnnotationPatch`). A viewer predicts the same without
56
+ * schema validation (`pdfPredictAnnotationPatch`).
57
+ *
58
+ * Throws `InvalidArg` for a patch the engine refuses. Pure: it reads nothing
59
+ * but its arguments.
60
+ */
61
+ function pdfResolveAnnotationPatch(current, patch, options = {}) {
62
+ const prepared = require_resolveAnnotationDraft.pdfPrepareAnnotationPatch(current, patch);
63
+ assertAnnotationPatch(current, prepared);
64
+ return require_resolveAnnotationDraft.pdfDeriveAnnotationPatch(current, prepared, options);
65
+ }
66
+ function invalidWrite(error, where) {
67
+ const issue = error.issues[0];
68
+ const field = issue.path.join(".");
69
+ return new require_format.EngineError(require_format.EngineErrorCode.InvalidArg, `${where}${field ? ` field '${field}'` : ""}: ${issue.message}`, { details: field ? { field } : {} });
70
+ }
71
+ //#endregion
72
+ //#region src/transfer/planAnnotationImport.ts
73
+ /**
74
+ * What an import of `bundle` into the `target` pages creates, and what it
75
+ * leaves out. Nothing here touches a document, so an import refuses before
76
+ * its first write:
77
+ *
78
+ * - `InvalidArg` when an item's data isn't valid for its kind, or when a
79
+ * page an item is on or points at maps to no target page (naming them);
80
+ * - `NotFound` when a page list maps to a page the target doesn't have.
81
+ *
82
+ * Unsupported items and unresolved relationships are left out in this order:
83
+ *
84
+ * 1. an item of an unsupported kind, and a form field's widget; a field
85
+ * marker leaves out only that field;
86
+ * 2. an item whose `reply.to` or `parent` is no item of the bundle on the
87
+ * same target page (`parent-missing`);
88
+ * 3. an item whose name is used on its target page, or by an earlier item
89
+ * going to the same page (`name-conflict`);
90
+ * 4. then, until nothing changes, an item whose `reply.to` or `parent` was
91
+ * left out (`parent-dropped`), so a whole branch goes together.
92
+ *
93
+ * `nameTaken(page, nm)` answers for the target page as it is before the import.
94
+ */
95
+ function planAnnotationImport(input) {
96
+ const { bundle, target, nameTaken } = input;
97
+ const items = bundle.items.map((item) => item.data);
98
+ const mapPage = require_resolveAnnotationDraft.bundlePageMapping({
99
+ bundlePages: bundle.pages,
100
+ pages: input.pages ?? "same",
101
+ target,
102
+ named: items.flatMap(pagesNamedBy)
103
+ });
104
+ const drops = /* @__PURE__ */ new Map();
105
+ const fieldDrops = /* @__PURE__ */ new Map();
106
+ const drafts = /* @__PURE__ */ new Map();
107
+ items.forEach((data, index) => {
108
+ if (data.subtype === "unsupported") {
109
+ drops.set(index, "unsupported-kind");
110
+ return;
111
+ }
112
+ if (data.subtype === "widget") {
113
+ drops.set(index, "widget");
114
+ return;
115
+ }
116
+ if (data.subtype === "popup" && data.parent === null) {
117
+ drops.set(index, "parent-missing");
118
+ return;
119
+ }
120
+ const unwritable = unwritableValuesOf(data);
121
+ if (unwritable === "item") {
122
+ drops.set(index, "unsupported-value");
123
+ return;
124
+ }
125
+ const markers = [...fieldMarkersOf(data), ...unwritable];
126
+ if (markers.length > 0) fieldDrops.set(index, markers);
127
+ drafts.set(index, draftOf$1(data, markers, mapPage, index));
128
+ });
129
+ const byKey = /* @__PURE__ */ new Map();
130
+ items.forEach((data, index) => {
131
+ const key = require_format.annotationKey(data.ref);
132
+ if (!byKey.has(key)) byKey.set(key, index);
133
+ });
134
+ const pageOf = (index) => require_format.encodePageKey(mapPage(items[index].ref.page));
135
+ const resolve = (ref, from) => {
136
+ const found = byKey.get(require_format.annotationKey(ref));
137
+ return found !== void 0 && pageOf(found) === pageOf(from) ? found : void 0;
138
+ };
139
+ const linksOf = (data) => [data.reply?.to, data.subtype === "popup" ? data.parent : null].filter((ref) => ref != null);
140
+ items.forEach((data, index) => {
141
+ if (drops.has(index)) return;
142
+ if (linksOf(data).some((ref) => resolve(ref, index) === void 0)) drops.set(index, "parent-missing");
143
+ });
144
+ const popupOf = /* @__PURE__ */ new Map();
145
+ items.forEach((data, index) => {
146
+ if (drops.has(index) || data.subtype !== "popup" || !data.parent) return;
147
+ const parent = resolve(data.parent, index);
148
+ if (popupOf.has(parent)) drops.set(index, "popup-taken");
149
+ else popupOf.set(parent, index);
150
+ });
151
+ const claimed = /* @__PURE__ */ new Set();
152
+ items.forEach((data, index) => {
153
+ if (drops.has(index) || !data.nm) return;
154
+ const page = mapPage(data.ref.page);
155
+ const claim = `${require_format.encodePageKey(page)}\u0000${data.nm}`;
156
+ if (claimed.has(claim) || nameTaken(page, data.nm)) drops.set(index, "name-conflict");
157
+ else claimed.add(claim);
158
+ });
159
+ for (let changed = true; changed;) {
160
+ changed = false;
161
+ items.forEach((data, index) => {
162
+ if (drops.has(index)) return;
163
+ if (linksOf(data).some((ref) => drops.has(resolve(ref, index)))) {
164
+ drops.set(index, "parent-dropped");
165
+ changed = true;
166
+ }
167
+ });
168
+ }
169
+ const planned = /* @__PURE__ */ new Map();
170
+ items.forEach((_data, index) => {
171
+ if (!drops.has(index)) planned.set(index, planned.size);
172
+ });
173
+ const creates = [...planned.keys()].map((index) => {
174
+ const data = items[index];
175
+ const replyTo = data.reply ? planned.get(resolve(data.reply.to, index)) : void 0;
176
+ const parent = data.subtype === "popup" && data.parent ? planned.get(resolve(data.parent, index)) : void 0;
177
+ return {
178
+ item: index,
179
+ page: mapPage(data.ref.page),
180
+ draft: drafts.get(index),
181
+ ...replyTo !== void 0 ? { replyTo: {
182
+ planned: replyTo,
183
+ type: data.reply.type
184
+ } } : {},
185
+ ...parent !== void 0 ? { parent } : {}
186
+ };
187
+ });
188
+ const dropped = [];
189
+ items.forEach((data, index) => {
190
+ const reason = drops.get(index);
191
+ if (reason) dropped.push({
192
+ ref: data.ref,
193
+ reason
194
+ });
195
+ else for (const drop of fieldDrops.get(index) ?? []) dropped.push({
196
+ ref: data.ref,
197
+ ...drop
198
+ });
199
+ });
200
+ return {
201
+ creates,
202
+ dropped
203
+ };
204
+ }
205
+ /**
206
+ * The pages an item is on or points at, apart from its links to other
207
+ * annotations (`reply.to`, `parent`, `popup`), which resolve in the bundle.
208
+ */
209
+ function pagesNamedBy(data) {
210
+ const { ref, reply: _reply, popup: _popup, ...rest } = data;
211
+ const fields = { ...rest };
212
+ if (data.subtype === "popup") delete fields.parent;
213
+ return [ref.page, ...require_pageRefs.pageRefsIn(fields)];
214
+ }
215
+ /** The fields of `data` that are markers for something the engine can't write. */
216
+ function fieldMarkersOf(data) {
217
+ const markers = [];
218
+ const measure = data.measure;
219
+ if (measure?.subtype === "geospatial") markers.push({
220
+ field: "measure",
221
+ reason: "geospatial"
222
+ });
223
+ if (measure?.subtype === "unknown") markers.push({
224
+ field: "measure",
225
+ reason: "unknown-measure"
226
+ });
227
+ const target = data.subtype === "link" ? data.target : null;
228
+ const carriedTarget = !!target && isWritableLinkTarget(target);
229
+ if (target && !carriedTarget) markers.push({
230
+ field: "target",
231
+ reason: "unsupported-action"
232
+ });
233
+ const { actions } = data;
234
+ if (Object.entries(actions ?? {}).some(([trigger, tree]) => data.subtype !== "link" || trigger !== "activate" ? true : carriedTarget && !writtenAsIs(tree))) markers.push({
235
+ field: "actions",
236
+ reason: "unsupported-action"
237
+ });
238
+ return markers;
239
+ }
240
+ /**
241
+ * The values `data` reads that a write can't make (a `readBack()` field's
242
+ * read-only values), each left out; `'item'` when one is a field every
243
+ * create needs. A link's target is reported by {@link fieldMarkersOf}.
244
+ */
245
+ function unwritableValuesOf(data) {
246
+ const unwritable = [];
247
+ const declaration = require_schema.declarationOf(data.subtype);
248
+ for (const [name, spec] of Object.entries(declaration?.fields ?? {})) {
249
+ const { owner, readBack, required } = spec.traits;
250
+ if (owner !== "data" || !readBack || name === "target") continue;
251
+ const value = data[name];
252
+ if (value == null || spec.write.safeParse(value).success) continue;
253
+ if (required) return "item";
254
+ unwritable.push({
255
+ field: name,
256
+ reason: "unsupported-value"
257
+ });
258
+ }
259
+ return unwritable;
260
+ }
261
+ /** Whether a link's `/A` is what writing its target writes again. */
262
+ function writtenAsIs(tree) {
263
+ const root = tree?.root;
264
+ if (!tree || tree.incomplete || !root || root.next.length > 0) return false;
265
+ return !(root.type === "uri" && root.isMap);
266
+ }
267
+ /** Whether a write can make a link's target; the others read as markers. */
268
+ function isWritableLinkTarget(target) {
269
+ return target.kind === "named" ? STANDARD_NAMED_ACTIONS.has(target.name ?? "") : target.kind === "goto" || target.kind === "uri";
270
+ }
271
+ const STANDARD_NAMED_ACTIONS = /* @__PURE__ */ new Set([
272
+ "NextPage",
273
+ "PrevPage",
274
+ "FirstPage",
275
+ "LastPage"
276
+ ]);
277
+ /** The create an item becomes, checked against its kind's create schema. */
278
+ function draftOf$1(data, markers, mapPage, index) {
279
+ const fields = { ...data };
280
+ delete fields.reply;
281
+ const declared = require_schema.declarationOf(data.subtype)?.fields ?? {};
282
+ for (const { field } of markers) if (declared[field]?.traits.writeNullable) fields[field] = null;
283
+ else delete fields[field];
284
+ const checked = require_schema.AnnotationDraftSchema.safeParse(fields);
285
+ if (data.subtype === "popup") delete fields.parent;
286
+ if (!checked.success) {
287
+ const issue = checked.error.issues[0];
288
+ const path = issue.path.join(".");
289
+ throw new require_format.EngineError(require_format.EngineErrorCode.InvalidArg, `import: item ${index}${path ? ` field '${path}'` : ""}: ${issue.message}`, { details: {
290
+ item: index,
291
+ ...path ? { field: path } : {}
292
+ } });
293
+ }
294
+ return require_pageRefs.mapPageRefs(fields, mapPage);
295
+ }
296
+ //#endregion
297
+ //#region src/transfer/planFormImport.ts
298
+ /**
299
+ * What a form import writes and leaves out, decided before its first write: a design import's
300
+ * fields checked against the create schema, a values import's values matched to the form's
301
+ * fields. Built with zod, so it runs where the engine does, from
302
+ * `@embedpdf/engine-core/validation`; the facts an import commits are in `formImport.ts`.
303
+ */
304
+ /**
305
+ * What a design import of `bundle` into the `target` pages creates, and what
306
+ * it leaves out. Nothing here touches a document, so an import refuses
307
+ * before its first write:
308
+ *
309
+ * - `InvalidArg` when a field's data isn't valid for its family, or when a
310
+ * page a widget is on or points at maps to no target page;
311
+ * - `NotFound` when a page list maps to a page the target doesn't have.
312
+ *
313
+ * Left out, in bundle order: a field of an unknown family, a field whose
314
+ * name is taken (`nameTaken`, for the document as it is before the
315
+ * import), a widget whose field isn't in the bundle, and each action that
316
+ * is a script, a submit or a link when `mayScript` is false
317
+ * (`doc.forms.script`), or that a write can't make. Each field keeps every
318
+ * widget it has. With `values`, each field gets the value the bundle holds.
319
+ */
320
+ function planFormImport(input) {
321
+ const { bundle, mayScript } = input;
322
+ const mapPage = require_resolveAnnotationDraft.bundlePageMapping({
323
+ bundlePages: bundle.pages,
324
+ pages: input.pages ?? "same",
325
+ target: input.target,
326
+ named: bundle.widgets.flatMap((row) => require_pageRefs.pageRefsIn(row.data))
327
+ });
328
+ const rowByKey = new Map(bundle.widgets.map((row, index) => [require_format.annotationKey(row.data.ref), index]));
329
+ const owned = /* @__PURE__ */ new Set();
330
+ const dropped = [];
331
+ const claimed = /* @__PURE__ */ new Set();
332
+ const creates = [];
333
+ bundle.fields.forEach(({ data: field }, index) => {
334
+ const rows = field.widgets.flatMap((widget) => {
335
+ const row = widget.ref ? rowByKey.get(require_format.annotationKey(widget.ref)) : void 0;
336
+ return row === void 0 ? [] : [row];
337
+ });
338
+ for (const row of rows) owned.add(row);
339
+ if (field.family === "unknown") {
340
+ dropped.push({
341
+ ref: field.ref,
342
+ reason: "unsupported-family"
343
+ });
344
+ return;
345
+ }
346
+ if (claimed.has(field.name) || input.nameTaken(field.name)) {
347
+ dropped.push({
348
+ ref: field.ref,
349
+ reason: "name-conflict"
350
+ });
351
+ return;
352
+ }
353
+ claimed.add(field.name);
354
+ const draft = draftOf(field, fieldActionsOf(field, mayScript, dropped), rows.map((row) => placementOf(field, bundle.widgets[row].data, mapPage, mayScript, dropped)), index);
355
+ const value = input.values && holdsValue(field) ? require_value.fieldValueOf(field) : null;
356
+ creates.push({
357
+ field: index,
358
+ draft,
359
+ widgets: rows,
360
+ flags: rows.map((row) => flagsOf(bundle.widgets[row].data)),
361
+ ...value ? { value } : {}
362
+ });
363
+ });
364
+ bundle.widgets.forEach((row, index) => {
365
+ if (!owned.has(index)) dropped.push({
366
+ ref: row.data.field,
367
+ widget: row.data.ref,
368
+ reason: "no-field"
369
+ });
370
+ });
371
+ return {
372
+ creates,
373
+ calculationOrder: bundle.calculationOrder.flatMap((ref) => {
374
+ const at = creates.findIndex((create) => require_format.encodeFieldRefKey(bundle.fields[create.field].data.ref) === require_format.encodeFieldRefKey(ref) && create.draft.actions?.calculate);
375
+ return at < 0 ? [] : [at];
376
+ }),
377
+ dropped
378
+ };
379
+ }
380
+ /**
381
+ * What a values import of `bundle` writes into a form whose fields are
382
+ * `target`: each field the bundle holds a value for gets it, matched by full
383
+ * name. Left out, in bundle order: a field the document doesn't have
384
+ * (`no-field`), one of another family (`wrong-family`), and one `refusal`
385
+ * names a reason for: a value the field can't take, a field a signature
386
+ * locked, a field the session may not fill in. So everything is decided
387
+ * before the first write.
388
+ */
389
+ function planFormValuesImport(input) {
390
+ const writes = [];
391
+ const dropped = [];
392
+ input.bundle.fields.forEach(({ data: field }, index) => {
393
+ const value = holdsValue(field) ? require_value.fieldValueOf(field) : null;
394
+ if (!value) return;
395
+ const target = input.target.find((candidate) => require_value.fieldMatches(candidate, {
396
+ kind: "fqn",
397
+ name: field.name
398
+ }));
399
+ if (!target) dropped.push({
400
+ ref: field.ref,
401
+ reason: "no-field"
402
+ });
403
+ else if (target.family !== field.family) dropped.push({
404
+ ref: field.ref,
405
+ reason: "wrong-family"
406
+ });
407
+ else {
408
+ const refused = input.refusal?.(target, value);
409
+ if (refused) dropped.push({
410
+ ref: field.ref,
411
+ reason: refused
412
+ });
413
+ else writes.push({
414
+ field: index,
415
+ target,
416
+ value
417
+ });
418
+ }
419
+ });
420
+ return {
421
+ writes,
422
+ dropped
423
+ };
424
+ }
425
+ /** Whether a field read holds a value a write can set. */
426
+ function holdsValue(field) {
427
+ return field.valueEntry.kind === "scalar" || field.valueEntry.kind === "array";
428
+ }
429
+ /** A field's scripts as a create writes them; each left out is reported. */
430
+ function fieldActionsOf(field, mayScript, dropped) {
431
+ const actions = {};
432
+ for (const [event, tree] of Object.entries(field.actions ?? {})) {
433
+ if (!tree) continue;
434
+ const script = require_value.fieldScriptOf(tree);
435
+ const member = `actions.${event}`;
436
+ if (!script) dropped.push({
437
+ ref: field.ref,
438
+ field: member,
439
+ reason: "unsupported-action"
440
+ });
441
+ else if (!mayScript) dropped.push({
442
+ ref: field.ref,
443
+ field: member,
444
+ reason: "script-not-allowed"
445
+ });
446
+ else actions[event] = script;
447
+ }
448
+ return actions;
449
+ }
450
+ /** A widget row as a create places it: on its mapped page, with its look and actions. */
451
+ function placementOf(field, row, mapPage, mayScript, dropped) {
452
+ const actions = {};
453
+ for (const event of require_value.WIDGET_ACTION_EVENTS) {
454
+ const tree = row.actions?.[event];
455
+ if (!tree) continue;
456
+ const write = require_value.actionWriteOf(tree);
457
+ const drop = (reason) => dropped.push({
458
+ ref: field.ref,
459
+ widget: row.ref,
460
+ field: `actions.${event}`,
461
+ reason
462
+ });
463
+ if (!write) drop("unsupported-action");
464
+ else if (!mayScript && require_value.needsScriptRight(write)) drop("script-not-allowed");
465
+ else actions[event] = require_pageRefs.mapPageRefs(write, mapPage);
466
+ }
467
+ const toggle = field.family === "checkbox" || field.family === "radio" ? field.widgets.find((widget) => widget.ref && require_format.annotationKey(widget.ref) === require_format.annotationKey(row.ref)) : void 0;
468
+ const look = {
469
+ color: row.color,
470
+ interiorColor: row.interiorColor,
471
+ fontFamily: row.fontFamily,
472
+ fontSize: row.fontSize,
473
+ fontColor: row.fontColor,
474
+ caption: field.family === "pushbutton" ? row.caption : null
475
+ };
476
+ return {
477
+ page: mapPage(row.page),
478
+ rect: row.rect,
479
+ ...row.rotation ? { rotation: row.rotation } : {},
480
+ strokeWidth: row.strokeWidth,
481
+ borderStyle: row.borderStyle,
482
+ textAlign: row.textAlign,
483
+ ...Object.fromEntries(Object.entries(look).filter(([, value]) => value != null)),
484
+ ...toggle && "exportValue" in toggle ? { exportValue: toggle.exportValue } : {},
485
+ ...Object.keys(actions).length > 0 ? { actions } : {}
486
+ };
487
+ }
488
+ function flagsOf(row) {
489
+ return Object.fromEntries(require_value.WIDGET_FLAG_NAMES.map((name) => [name, row[name]]));
490
+ }
491
+ /** The create a field row becomes, checked against the create schema. */
492
+ function draftOf(field, actions, widgets, index) {
493
+ const base = {
494
+ name: field.name,
495
+ readOnly: field.readOnly,
496
+ required: field.required,
497
+ noExport: field.noExport,
498
+ ...field.alternateName !== null ? { alternateName: field.alternateName } : {},
499
+ ...field.mappingName !== null ? { mappingName: field.mappingName } : {},
500
+ ...field.groupId !== null ? { groupId: field.groupId } : {},
501
+ ...Object.keys(actions).length > 0 ? { actions } : {},
502
+ widgets
503
+ };
504
+ const hasDefault = field.defaultValueEntry.kind !== "none";
505
+ const options = (list) => list.map(({ label, value }) => ({
506
+ label,
507
+ value
508
+ }));
509
+ let draft;
510
+ switch (field.family) {
511
+ case "text":
512
+ draft = {
513
+ ...base,
514
+ family: "text",
515
+ ...hasDefault ? { defaultValue: field.defaultValue } : {},
516
+ ...field.maxLength !== null ? { maxLength: field.maxLength } : {},
517
+ multiline: field.multiline,
518
+ password: field.password,
519
+ comb: field.comb
520
+ };
521
+ break;
522
+ case "radio":
523
+ draft = {
524
+ ...base,
525
+ family: "radio",
526
+ radiosInUnison: field.radiosInUnison,
527
+ noToggleToOff: field.noToggleToOff
528
+ };
529
+ break;
530
+ case "combobox":
531
+ draft = {
532
+ ...base,
533
+ family: "combobox",
534
+ edit: field.edit,
535
+ options: options(field.options),
536
+ ...hasDefault ? { defaultValue: field.defaultValue } : {}
537
+ };
538
+ break;
539
+ case "listbox":
540
+ draft = {
541
+ ...base,
542
+ family: "listbox",
543
+ multiSelect: field.multiSelect,
544
+ options: options(field.options),
545
+ ...hasDefault ? { defaultValue: field.defaultValue } : {}
546
+ };
547
+ break;
548
+ case "checkbox":
549
+ case "pushbutton":
550
+ case "signature":
551
+ draft = {
552
+ ...base,
553
+ family: field.family
554
+ };
555
+ break;
556
+ default: throw new require_format.EngineError(require_format.EngineErrorCode.InvalidArg, `import: field ${index} has no family`);
557
+ }
558
+ const checked = require_schema.FormFieldDraftSchema.safeParse(draft);
559
+ if (!checked.success) {
560
+ const issue = checked.error.issues[0];
561
+ const path = issue.path.join(".");
562
+ throw new require_format.EngineError(require_format.EngineErrorCode.InvalidArg, `import: field ${index}${path ? ` member '${path}'` : ""}: ${issue.message}`, { details: {
563
+ item: index,
564
+ ...path ? { field: path } : {}
565
+ } });
566
+ }
567
+ return draft;
568
+ }
569
+ //#endregion
570
+ exports.assertAnnotationDraft = assertAnnotationDraft;
571
+ exports.assertAnnotationPatch = assertAnnotationPatch;
572
+ exports.pdfResolveAnnotationDraft = pdfResolveAnnotationDraft;
573
+ exports.pdfResolveAnnotationPatch = pdfResolveAnnotationPatch;
574
+ exports.planAnnotationImport = planAnnotationImport;
575
+ exports.planFormImport = planFormImport;
576
+ exports.planFormValuesImport = planFormValuesImport;
577
+
578
+ //# sourceMappingURL=validation.cjs.map