documents.js 2.3.3 → 3.0.1

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 (106) hide show
  1. package/README.md +27 -14
  2. package/dist/codecs/registry.d.cts +8 -3
  3. package/dist/codecs/registry.d.ts +8 -3
  4. package/dist/convert/canonicalise.cjs +20 -0
  5. package/dist/convert/canonicalise.d.cts +5 -0
  6. package/dist/convert/canonicalise.d.ts +5 -0
  7. package/dist/convert/canonicalise.js +18 -0
  8. package/dist/convert/composition.cjs +29 -28
  9. package/dist/convert/composition.d.cts +1 -1
  10. package/dist/convert/composition.d.ts +1 -1
  11. package/dist/convert/composition.js +29 -28
  12. package/dist/convert/convert.cjs +14 -26
  13. package/dist/convert/convert.d.cts +1 -1
  14. package/dist/convert/convert.d.ts +1 -1
  15. package/dist/convert/convert.js +14 -26
  16. package/dist/convert/decompose.cjs +134 -0
  17. package/dist/convert/decompose.d.cts +13 -0
  18. package/dist/convert/decompose.d.ts +13 -0
  19. package/dist/convert/decompose.js +126 -0
  20. package/dist/convert/factor-styles.cjs +409 -0
  21. package/dist/convert/factor-styles.d.cts +6 -0
  22. package/dist/convert/factor-styles.d.ts +6 -0
  23. package/dist/convert/factor-styles.js +407 -0
  24. package/dist/convert/flatten.cjs +138 -0
  25. package/dist/convert/flatten.d.cts +5 -0
  26. package/dist/convert/flatten.d.ts +5 -0
  27. package/dist/convert/flatten.js +137 -0
  28. package/dist/convert/from-package.cjs +4 -3
  29. package/dist/convert/from-package.d.cts +2 -1
  30. package/dist/convert/from-package.d.ts +2 -1
  31. package/dist/convert/from-package.js +5 -4
  32. package/dist/convert/local.cjs +1 -1
  33. package/dist/convert/local.js +1 -1
  34. package/dist/convert/variant-bridges.cjs +0 -4
  35. package/dist/convert/variant-bridges.js +1 -5
  36. package/dist/csv/read.cjs +0 -1
  37. package/dist/csv/read.js +1 -2
  38. package/dist/edit/docx/numbering.d.cts +1 -1
  39. package/dist/edit/docx/numbering.d.ts +1 -1
  40. package/dist/edit/docx/paragraph.cjs +1 -0
  41. package/dist/edit/docx/paragraph.js +1 -0
  42. package/dist/edit/pdf/editor.cjs +1 -2
  43. package/dist/edit/pdf/editor.d.cts +2 -2
  44. package/dist/edit/pdf/editor.d.ts +2 -2
  45. package/dist/edit/pdf/editor.js +1 -2
  46. package/dist/edit/pdf/item.d.cts +1 -1
  47. package/dist/edit/pdf/item.d.ts +1 -1
  48. package/dist/edit/pdf/page.d.cts +2 -2
  49. package/dist/edit/pdf/page.d.ts +2 -2
  50. package/dist/edit/pdf/util.d.cts +1 -1
  51. package/dist/edit/pdf/util.d.ts +1 -1
  52. package/dist/fonts/registry.d.cts +1 -1
  53. package/dist/fonts/registry.d.ts +1 -1
  54. package/dist/index.cjs +10 -15
  55. package/dist/index.d.cts +7 -4
  56. package/dist/index.d.ts +7 -4
  57. package/dist/index.js +6 -3
  58. package/dist/{item-Dj6f1VTE.d.cts → item-D9vQKT0T.d.cts} +2 -1
  59. package/dist/{item-Dj6f1VTE.d.ts → item-D9vQKT0T.d.ts} +2 -1
  60. package/dist/latex/lint.cjs +3 -2
  61. package/dist/latex/lint.js +3 -2
  62. package/dist/layout/drawing.d.cts +2 -1
  63. package/dist/layout/drawing.d.ts +2 -1
  64. package/dist/layout/engine.cjs +8 -5
  65. package/dist/layout/engine.d.cts +2 -1
  66. package/dist/layout/engine.d.ts +2 -1
  67. package/dist/layout/engine.js +8 -5
  68. package/dist/layout/lattice.d.cts +1 -1
  69. package/dist/layout/lattice.d.ts +1 -1
  70. package/dist/layout/reconstruct.cjs +0 -5
  71. package/dist/layout/reconstruct.d.cts +2 -1
  72. package/dist/layout/reconstruct.d.ts +2 -1
  73. package/dist/layout/reconstruct.js +0 -5
  74. package/dist/layout/shared.cjs +2 -1
  75. package/dist/layout/shared.d.cts +2 -1
  76. package/dist/layout/shared.d.ts +2 -1
  77. package/dist/layout/shared.js +2 -1
  78. package/dist/layout/sheets.d.cts +2 -1
  79. package/dist/layout/sheets.d.ts +2 -1
  80. package/dist/layout/slides.d.cts +2 -1
  81. package/dist/layout/slides.d.ts +2 -1
  82. package/dist/markdown/render.cjs +0 -1
  83. package/dist/markdown/render.js +1 -2
  84. package/dist/model/embedded-drawing.cjs +5 -8
  85. package/dist/model/embedded-drawing.js +5 -8
  86. package/dist/model/formula.cjs +0 -2
  87. package/dist/model/formula.js +0 -2
  88. package/dist/odb/report/render.cjs +0 -1
  89. package/dist/odb/report/render.js +1 -2
  90. package/dist/odb/spreadsheet.cjs +0 -1
  91. package/dist/odb/spreadsheet.js +1 -2
  92. package/dist/odf/odg/read.cjs +0 -2
  93. package/dist/odf/odg/read.js +0 -2
  94. package/dist/odf/odp/read.cjs +0 -2
  95. package/dist/odf/odp/read.js +0 -2
  96. package/dist/odf/ods/read.cjs +0 -2
  97. package/dist/odf/ods/read.js +0 -2
  98. package/dist/odf/odt/read.cjs +0 -2
  99. package/dist/odf/odt/read.js +0 -2
  100. package/dist/ooxml/docx/read.cjs +0 -2
  101. package/dist/ooxml/docx/read.js +0 -2
  102. package/dist/ooxml/pptx/read.cjs +0 -2
  103. package/dist/ooxml/pptx/read.js +0 -2
  104. package/dist/svg/read.cjs +8 -11
  105. package/dist/svg/read.js +8 -11
  106. package/package.json +5 -5
@@ -0,0 +1,407 @@
1
+ import { flattenPackage } from "./flatten.js";
2
+ import { canonicalKey } from "./canonicalise.js";
3
+ import { decomposeDrawPage, decomposeSection, decomposeSheet, decomposeSlide } from "./decompose.js";
4
+ //#region src/convert/factor-styles.ts
5
+ const PARAGRAPH_STYLE_KEYS = [
6
+ "alignment",
7
+ "spacingBeforePt",
8
+ "spacingAfterPt",
9
+ "lineSpacing",
10
+ "indentLeftPt",
11
+ "indentFirstLinePt"
12
+ ];
13
+ const RUN_STYLE_KEYS = [
14
+ "bold",
15
+ "italic",
16
+ "underline",
17
+ "strike",
18
+ "fontFamily",
19
+ "sizePt",
20
+ "color"
21
+ ];
22
+ function isShapeGroupWrapper(wrapper) {
23
+ return !("kind" in wrapper.node);
24
+ }
25
+ function isAnchorGroupWrapper(wrapper) {
26
+ return "kind" in wrapper.node && wrapper.node.kind === "paragraph";
27
+ }
28
+ function isSlideGroupWrapper(wrapper) {
29
+ return "kind" in wrapper.node && wrapper.node.kind === "slide";
30
+ }
31
+ function isDrawPageGroupWrapper(wrapper) {
32
+ return "kind" in wrapper.node && wrapper.node.kind === "drawPage";
33
+ }
34
+ function isHeadingGroup(group) {
35
+ return group.node.headingLevel !== void 0;
36
+ }
37
+ function assemblePackage(content, pages) {
38
+ const envelope = {
39
+ metadata: content.metadata,
40
+ ...content.symbolTable !== void 0 ? { symbolTable: content.symbolTable } : {},
41
+ ...pages !== void 0 ? { pages: [...pages] } : {}
42
+ };
43
+ switch (content.kind) {
44
+ case "wordprocessing": return mint({
45
+ kind: "wordprocessing",
46
+ ...envelope,
47
+ children: content.sections.map(decomposeSection)
48
+ });
49
+ case "presentation": return mint({
50
+ kind: "presentation",
51
+ ...envelope,
52
+ children: content.slides.map(decomposeSlide)
53
+ });
54
+ case "spreadsheet": return mint({
55
+ kind: "spreadsheet",
56
+ ...envelope,
57
+ children: content.sheets.map(decomposeSheet)
58
+ });
59
+ case "drawing": return mint({
60
+ kind: "drawing",
61
+ ...envelope,
62
+ children: content.pages.map(decomposeDrawPage)
63
+ });
64
+ case "formula": return mint({
65
+ kind: "formula",
66
+ ...envelope,
67
+ children: [content.formula]
68
+ });
69
+ }
70
+ }
71
+ function factorStyles(pkg) {
72
+ const reassembled = assemblePackage(flattenPackage(pkg), pkg.pages);
73
+ if (pkg.definitions === void 0) return reassembled;
74
+ return {
75
+ ...reassembled,
76
+ definitions: pkg.definitions
77
+ };
78
+ }
79
+ function extentOf(wrapper) {
80
+ if (isShapeGroupWrapper(wrapper)) return flowExtent(wrapper.children);
81
+ if (isAnchorGroupWrapper(wrapper)) return [wrapper.node, ...flowExtent(wrapper.children)];
82
+ if (isSlideGroupWrapper(wrapper)) return wrapper.children.flatMap(extentOf);
83
+ if (isDrawPageGroupWrapper(wrapper)) {
84
+ const paragraphs = [];
85
+ for (const child of wrapper.children) if ("node" in child) paragraphs.push(...extentOf(child));
86
+ return paragraphs;
87
+ }
88
+ return flowExtent(wrapper.children);
89
+ }
90
+ function flowExtent(children) {
91
+ const paragraphs = [];
92
+ for (const child of children) if ("node" in child && "children" in child) paragraphs.push(...extentOf(child));
93
+ else if (child.kind === "paragraph") paragraphs.push(child);
94
+ return paragraphs;
95
+ }
96
+ function paragraphTuple(paragraph, keys) {
97
+ const tuple = {};
98
+ for (const key of keys) if (paragraph[key] !== void 0) tuple[key] = paragraph[key];
99
+ return tuple;
100
+ }
101
+ function runTuple(run, keys) {
102
+ const tuple = {};
103
+ for (const key of keys) if (run[key] !== void 0) tuple[key] = run[key];
104
+ return tuple;
105
+ }
106
+ function commonParagraphKeys(extent, frozen) {
107
+ return PARAGRAPH_STYLE_KEYS.filter((key) => !frozen.has(key) && extent.every((paragraph) => paragraph[key] !== void 0));
108
+ }
109
+ function commonRunKeys(extent, frozen) {
110
+ const runs = extent.flatMap((paragraph) => paragraph.runs);
111
+ if (runs.length === 0) return [];
112
+ return RUN_STYLE_KEYS.filter((key) => !frozen.has(key) && runs.every((run) => run[key] !== void 0));
113
+ }
114
+ function bestParagraphCandidate(extent, keys, factored) {
115
+ const groups = /* @__PURE__ */ new Map();
116
+ for (const paragraph of extent) {
117
+ if (factored.has(paragraph)) continue;
118
+ const tuple = paragraphTuple(paragraph, keys);
119
+ if (Object.keys(tuple).length === 0) continue;
120
+ const key = canonicalKey(tuple);
121
+ const existing = groups.get(key);
122
+ if (existing === void 0) groups.set(key, {
123
+ tuple,
124
+ keys,
125
+ positions: [paragraph]
126
+ });
127
+ else existing.positions.push(paragraph);
128
+ }
129
+ return bestGroup(groups);
130
+ }
131
+ function bestRunCandidate(extent, keys, factored) {
132
+ const groups = /* @__PURE__ */ new Map();
133
+ for (const paragraph of extent) for (const run of paragraph.runs) {
134
+ if (factored.has(run)) continue;
135
+ const tuple = runTuple(run, keys);
136
+ if (Object.keys(tuple).length === 0) continue;
137
+ const key = canonicalKey(tuple);
138
+ const existing = groups.get(key);
139
+ if (existing === void 0) groups.set(key, {
140
+ tuple,
141
+ keys,
142
+ positions: [run]
143
+ });
144
+ else existing.positions.push(run);
145
+ }
146
+ return bestGroup(groups);
147
+ }
148
+ function bestGroup(groups) {
149
+ let best;
150
+ for (const group of groups.values()) {
151
+ if (group.positions.length < 2) continue;
152
+ if (best === void 0 || group.positions.length > best.positions.length) best = group;
153
+ }
154
+ return best;
155
+ }
156
+ function plan(wrapper, visit, branch, state, entries) {
157
+ const extent = extentOf(wrapper);
158
+ const paragraphCandidate = extent.length > 0 ? bestParagraphCandidate(extent, commonParagraphKeys(extent, branch.frozenParagraphs), branch.factoredParagraphs) : void 0;
159
+ const runCandidate = extent.length > 0 ? bestRunCandidate(extent, commonRunKeys(extent, branch.frozenRuns), branch.factoredRuns) : void 0;
160
+ const nextFrozenParagraphs = new Set(branch.frozenParagraphs);
161
+ const nextFrozenRuns = new Set(branch.frozenRuns);
162
+ const nextFactoredParagraphs = new Set(branch.factoredParagraphs);
163
+ const nextFactoredRuns = new Set(branch.factoredRuns);
164
+ if (paragraphCandidate !== void 0 || runCandidate !== void 0) {
165
+ const content = {
166
+ ...paragraphCandidate !== void 0 ? { paragraph: paragraphCandidate.tuple } : {},
167
+ ...runCandidate !== void 0 ? { run: runCandidate.tuple } : {}
168
+ };
169
+ const contentKey = canonicalKey(content);
170
+ const existing = entries.get(contentKey);
171
+ if (existing === void 0) entries.set(contentKey, {
172
+ content,
173
+ wrappers: [wrapper],
174
+ frequency: (paragraphCandidate?.positions.length ?? 0) + (runCandidate?.positions.length ?? 0),
175
+ firstVisit: visit.index
176
+ });
177
+ else {
178
+ existing.wrappers.push(wrapper);
179
+ existing.frequency += (paragraphCandidate?.positions.length ?? 0) + (runCandidate?.positions.length ?? 0);
180
+ }
181
+ const strips = {
182
+ paragraphs: /* @__PURE__ */ new Map(),
183
+ runs: /* @__PURE__ */ new Map()
184
+ };
185
+ if (paragraphCandidate !== void 0) {
186
+ for (const paragraph of paragraphCandidate.positions) {
187
+ nextFactoredParagraphs.add(paragraph);
188
+ strips.paragraphs.set(paragraph, paragraphCandidate.keys);
189
+ }
190
+ for (const key of paragraphCandidate.keys) nextFrozenParagraphs.add(key);
191
+ }
192
+ if (runCandidate !== void 0) {
193
+ for (const run of runCandidate.positions) {
194
+ nextFactoredRuns.add(run);
195
+ strips.runs.set(run, runCandidate.keys);
196
+ }
197
+ for (const key of runCandidate.keys) nextFrozenRuns.add(key);
198
+ }
199
+ state.wrapperStrips.set(wrapper, strips);
200
+ }
201
+ visit.index += 1;
202
+ const next = {
203
+ frozenParagraphs: nextFrozenParagraphs,
204
+ frozenRuns: nextFrozenRuns,
205
+ factoredParagraphs: nextFactoredParagraphs,
206
+ factoredRuns: nextFactoredRuns
207
+ };
208
+ for (const child of childWrappers(wrapper)) plan(child, visit, next, state, entries);
209
+ }
210
+ function childWrappers(wrapper) {
211
+ if (isShapeGroupWrapper(wrapper) || isAnchorGroupWrapper(wrapper)) {
212
+ const wrappers = [];
213
+ for (const child of wrapper.children) if ("node" in child && "children" in child) wrappers.push(child);
214
+ return wrappers;
215
+ }
216
+ if (isSlideGroupWrapper(wrapper)) return [...wrapper.children];
217
+ if (isDrawPageGroupWrapper(wrapper)) {
218
+ const shapes = [];
219
+ for (const child of wrapper.children) if ("node" in child) shapes.push(child);
220
+ return shapes;
221
+ }
222
+ const wrappers = [];
223
+ for (const child of wrapper.children) if ("node" in child && "children" in child) wrappers.push(child);
224
+ return wrappers;
225
+ }
226
+ function mint(pkg) {
227
+ const state = {
228
+ wrapperRefs: /* @__PURE__ */ new Map(),
229
+ wrapperStrips: /* @__PURE__ */ new Map()
230
+ };
231
+ const entries = /* @__PURE__ */ new Map();
232
+ const visit = { index: 0 };
233
+ const rootBranch = {
234
+ frozenParagraphs: /* @__PURE__ */ new Set(),
235
+ frozenRuns: /* @__PURE__ */ new Set(),
236
+ factoredParagraphs: /* @__PURE__ */ new Set(),
237
+ factoredRuns: /* @__PURE__ */ new Set()
238
+ };
239
+ switch (pkg.kind) {
240
+ case "wordprocessing":
241
+ for (const root of pkg.children) plan(root, visit, rootBranch, state, entries);
242
+ break;
243
+ case "presentation":
244
+ for (const root of pkg.children) plan(root, visit, rootBranch, state, entries);
245
+ break;
246
+ case "drawing": for (const root of pkg.children) plan(root, visit, rootBranch, state, entries);
247
+ }
248
+ if (entries.size === 0) return pkg;
249
+ const ordered = [...entries.values()].sort((a, b) => b.frequency - a.frequency || a.firstVisit - b.firstVisit);
250
+ const styles = {};
251
+ ordered.forEach((entry, index) => {
252
+ const id = `s${index + 1}`;
253
+ styles[id] = entry.content;
254
+ for (const wrapper of entry.wrappers) state.wrapperRefs.set(wrapper, id);
255
+ });
256
+ switch (pkg.kind) {
257
+ case "wordprocessing": return {
258
+ ...pkg,
259
+ styles,
260
+ children: pkg.children.map((group) => rebuildSectionGroup(group, [], state))
261
+ };
262
+ case "presentation": return {
263
+ ...pkg,
264
+ styles,
265
+ children: pkg.children.map((group) => rebuildSlideGroup(group, [], state))
266
+ };
267
+ case "drawing": return {
268
+ ...pkg,
269
+ styles,
270
+ children: pkg.children.map((group) => rebuildDrawPageGroup(group, [], state))
271
+ };
272
+ case "spreadsheet":
273
+ case "formula": return {
274
+ ...pkg,
275
+ styles
276
+ };
277
+ }
278
+ }
279
+ function innerChain(group, chain, state) {
280
+ const own = state.wrapperStrips.get(group);
281
+ return own === void 0 ? chain : [...chain, own];
282
+ }
283
+ function paragraphStripsOf(chain, paragraph) {
284
+ let result;
285
+ for (const strips of chain) {
286
+ const found = strips.paragraphs.get(paragraph);
287
+ if (found !== void 0) result = found;
288
+ }
289
+ return result;
290
+ }
291
+ function runStripsOf(chain, run) {
292
+ let result;
293
+ for (const strips of chain) {
294
+ const found = strips.runs.get(run);
295
+ if (found !== void 0) result = found;
296
+ }
297
+ return result;
298
+ }
299
+ function rebuildSlideGroup(group, chain, state) {
300
+ const inner = innerChain(group, chain, state);
301
+ const children = group.children.map((shape) => rebuildShapeGroup(shape, inner, state));
302
+ const ref = state.wrapperRefs.get(group);
303
+ return ref === void 0 && children.every((child, index) => child === group.children[index]) ? group : {
304
+ node: group.node,
305
+ ...ref !== void 0 ? { style: ref } : {},
306
+ children
307
+ };
308
+ }
309
+ function rebuildDrawPageGroup(group, chain, state) {
310
+ const inner = innerChain(group, chain, state);
311
+ const children = group.children.map((child) => "node" in child ? rebuildShapeGroup(child, inner, state) : child);
312
+ const ref = state.wrapperRefs.get(group);
313
+ return ref === void 0 && children.every((child, index) => child === group.children[index]) ? group : {
314
+ node: group.node,
315
+ ...ref !== void 0 ? { style: ref } : {},
316
+ children
317
+ };
318
+ }
319
+ function rebuildSectionGroup(group, chain, state) {
320
+ const inner = innerChain(group, chain, state);
321
+ const children = group.children.map((child) => rebuildSectionChild(child, inner, state));
322
+ const ref = state.wrapperRefs.get(group);
323
+ return ref === void 0 && children.every((child, index) => child === group.children[index]) ? group : {
324
+ node: group.node,
325
+ ...ref !== void 0 ? { style: ref } : {},
326
+ children
327
+ };
328
+ }
329
+ function rebuildSectionChild(child, chain, state) {
330
+ if ("node" in child && "children" in child) return isHeadingGroup(child) ? rebuildHeadingGroup(child, chain, state, rebuildSectionChild) : rebuildListGroup(child, chain, state, rebuildListChild);
331
+ if (child.kind === "paragraph") return rebuildParagraph(child, chain);
332
+ return child;
333
+ }
334
+ function rebuildListChild(child, chain, state) {
335
+ if ("node" in child && "children" in child) return rebuildListGroup(child, chain, state, rebuildListChild);
336
+ if (child.kind === "paragraph") return rebuildParagraph(child, chain);
337
+ return child;
338
+ }
339
+ function rebuildShapeGroup(group, chain, state) {
340
+ const inner = innerChain(group, chain, state);
341
+ const children = group.children.map((child) => rebuildListChild(child, inner, state));
342
+ const ref = state.wrapperRefs.get(group);
343
+ return ref === void 0 && children.every((child, index) => child === group.children[index]) ? group : {
344
+ node: group.node,
345
+ ...ref !== void 0 ? { style: ref } : {},
346
+ children
347
+ };
348
+ }
349
+ function rebuildHeadingGroup(group, chain, state, rebuildChild) {
350
+ const inner = innerChain(group, chain, state);
351
+ const anchor = rebuildParagraph(group.node, inner);
352
+ assertHeadingAnchor(anchor);
353
+ const children = group.children.map((child) => rebuildChild(child, inner, state));
354
+ const ref = state.wrapperRefs.get(group);
355
+ return ref === void 0 && anchor === group.node && children.every((child, index) => child === group.children[index]) ? group : {
356
+ node: anchor,
357
+ ...ref !== void 0 ? { style: ref } : {},
358
+ children
359
+ };
360
+ }
361
+ function rebuildListGroup(group, chain, state, rebuildChild) {
362
+ const inner = innerChain(group, chain, state);
363
+ const anchor = rebuildParagraph(group.node, inner);
364
+ assertListAnchor(anchor);
365
+ const children = group.children.map((child) => rebuildChild(child, inner, state));
366
+ const ref = state.wrapperRefs.get(group);
367
+ return ref === void 0 && anchor === group.node && children.every((child, index) => child === group.children[index]) ? group : {
368
+ node: anchor,
369
+ ...ref !== void 0 ? { style: ref } : {},
370
+ children
371
+ };
372
+ }
373
+ function assertHeadingAnchor(paragraph) {
374
+ if (paragraph.headingLevel === void 0) throw new Error("factorStyles: stripping dropped a heading anchor's headingLevel");
375
+ }
376
+ function assertListAnchor(paragraph) {
377
+ if (paragraph.list === void 0) throw new Error("factorStyles: stripping dropped a list anchor's list membership");
378
+ }
379
+ function rebuildParagraph(paragraph, chain) {
380
+ const strips = paragraphStripsOf(chain, paragraph);
381
+ const base = strips === void 0 ? paragraph : stripParagraphKeys(paragraph, strips);
382
+ let changed = base !== paragraph;
383
+ const runs = [];
384
+ for (const run of base.runs) {
385
+ const runStrips = runStripsOf(chain, run);
386
+ const rebuilt = runStrips === void 0 ? run : stripRunKeys(run, runStrips);
387
+ changed ||= rebuilt !== run;
388
+ runs.push(rebuilt);
389
+ }
390
+ if (!changed) return paragraph;
391
+ return {
392
+ ...base,
393
+ runs
394
+ };
395
+ }
396
+ function stripParagraphKeys(paragraph, keys) {
397
+ const copy = { ...paragraph };
398
+ for (const key of keys) delete copy[key];
399
+ return copy;
400
+ }
401
+ function stripRunKeys(run, keys) {
402
+ const copy = { ...run };
403
+ for (const key of keys) delete copy[key];
404
+ return copy;
405
+ }
406
+ //#endregion
407
+ export { assemblePackage, factorStyles };
@@ -0,0 +1,138 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let document_schema_js = require("document-schema.js");
3
+ //#region src/convert/flatten.ts
4
+ function chainWithRef(chain, group) {
5
+ return group.style === void 0 ? chain : [...chain, group.style];
6
+ }
7
+ function entryOf(styles, chain) {
8
+ if (chain.length === 0) return void 0;
9
+ if (styles === void 0) throw new Error("flattenPackage: a group carries a style ref but the package has no styles table");
10
+ return (0, document_schema_js.resolveStyleChain)(styles, chain);
11
+ }
12
+ function applyEntry(entry, paragraph) {
13
+ const withParagraph = (0, document_schema_js.applyParagraphStyleProperties)(entry.paragraph, paragraph);
14
+ const runProperties = entry.run;
15
+ if (runProperties === void 0) return withParagraph;
16
+ return {
17
+ ...withParagraph,
18
+ runs: withParagraph.runs.map((run) => (0, document_schema_js.applyRunStyleProperties)(runProperties, run))
19
+ };
20
+ }
21
+ function flattenPackage(pkg) {
22
+ const styles = pkg.styles;
23
+ const envelope = {
24
+ metadata: pkg.metadata,
25
+ ...pkg.symbolTable !== void 0 ? { symbolTable: pkg.symbolTable } : {}
26
+ };
27
+ switch (pkg.kind) {
28
+ case "wordprocessing": return {
29
+ kind: "wordprocessing",
30
+ ...envelope,
31
+ sections: pkg.children.map((group) => ({
32
+ ...untag(group.node),
33
+ blocks: flattenSectionChildren(styles, chainWithRef([], group), group.children)
34
+ }))
35
+ };
36
+ case "presentation": return {
37
+ kind: "presentation",
38
+ ...envelope,
39
+ slides: pkg.children.map((group) => {
40
+ const chain = chainWithRef([], group);
41
+ return {
42
+ ...untag(group.node),
43
+ shapes: group.children.map((shape) => flattenShape(styles, chain, shape))
44
+ };
45
+ })
46
+ };
47
+ case "spreadsheet": return {
48
+ kind: "spreadsheet",
49
+ ...envelope,
50
+ sheets: pkg.children.map((group) => {
51
+ if (group.style !== void 0) throw new Error("flattenPackage: a sheet group carries a style ref but a sheet holds no block flow to resolve it onto");
52
+ const images = [];
53
+ const embedded = [];
54
+ for (const child of group.children) if ("kind" in child) images.push(child);
55
+ else embedded.push(child);
56
+ return {
57
+ ...untag(group.node),
58
+ images,
59
+ ...embedded.length > 0 ? { embeddedObjects: embedded } : {}
60
+ };
61
+ })
62
+ };
63
+ case "drawing": return {
64
+ kind: "drawing",
65
+ ...envelope,
66
+ pages: pkg.children.map((group) => {
67
+ const chain = chainWithRef([], group);
68
+ const shapes = [];
69
+ const vectors = [];
70
+ for (const child of group.children) if ("node" in child) shapes.push(flattenShape(styles, chain, child));
71
+ else vectors.push(child);
72
+ return {
73
+ ...untag(group.node),
74
+ shapes,
75
+ vectors
76
+ };
77
+ })
78
+ };
79
+ case "formula": {
80
+ const first = pkg.children[0];
81
+ if (pkg.children.length !== 1 || first === void 0 || !document_schema_js.ContentFormulaSchema.safeParse(first).success) throw new Error("flattenPackage: a formula package takes exactly one ContentFormula node");
82
+ return {
83
+ kind: "formula",
84
+ ...envelope,
85
+ formula: first
86
+ };
87
+ }
88
+ }
89
+ }
90
+ function untag(descriptor) {
91
+ const copy = { ...descriptor };
92
+ delete copy.kind;
93
+ return copy;
94
+ }
95
+ function flattenShape(styles, chain, group) {
96
+ return {
97
+ ...group.node,
98
+ blocks: flattenListChildren(styles, chainWithRef(chain, group), group.children)
99
+ };
100
+ }
101
+ function flattenSectionChildren(styles, chain, children) {
102
+ const blocks = [];
103
+ for (const child of children) if (isHeadingGroup(child)) {
104
+ const own = chainWithRef(chain, child);
105
+ blocks.push(resolveAnchor(styles, own, child.node), ...flattenSectionChildren(styles, own, child.children));
106
+ } else if (isListGroup(child)) {
107
+ const own = chainWithRef(chain, child);
108
+ blocks.push(resolveAnchor(styles, own, child.node), ...flattenListChildren(styles, own, child.children));
109
+ } else if (child.kind === "paragraph") {
110
+ const entry = entryOf(styles, chain);
111
+ blocks.push(entry === void 0 ? child : applyEntry(entry, child));
112
+ } else blocks.push(child);
113
+ return blocks;
114
+ }
115
+ function flattenListChildren(styles, chain, children) {
116
+ const blocks = [];
117
+ for (const child of children) if (isListGroup(child)) {
118
+ const own = chainWithRef(chain, child);
119
+ blocks.push(resolveAnchor(styles, own, child.node), ...flattenListChildren(styles, own, child.children));
120
+ } else if (child.kind === "paragraph") {
121
+ const entry = entryOf(styles, chain);
122
+ blocks.push(entry === void 0 ? child : applyEntry(entry, child));
123
+ } else blocks.push(child);
124
+ return blocks;
125
+ }
126
+ function isHeadingGroup(child) {
127
+ return "node" in child && "children" in child && child.node.kind === "paragraph" && child.node.headingLevel !== void 0;
128
+ }
129
+ function isListGroup(child) {
130
+ return "node" in child && "children" in child && child.node.kind === "paragraph" && child.node.list !== void 0;
131
+ }
132
+ function resolveAnchor(styles, chain, anchor) {
133
+ const entry = entryOf(styles, chain);
134
+ if (entry === void 0) return anchor;
135
+ return applyEntry(entry, anchor);
136
+ }
137
+ //#endregion
138
+ exports.flattenPackage = flattenPackage;
@@ -0,0 +1,5 @@
1
+ import { ContentDocument, DocumentPackage } from "document-schema.js";
2
+ //#region src/convert/flatten.d.ts
3
+ declare function flattenPackage(pkg: DocumentPackage): ContentDocument;
4
+ //#endregion
5
+ export { flattenPackage };
@@ -0,0 +1,5 @@
1
+ import { ContentDocument, DocumentPackage } from "document-schema.js";
2
+ //#region src/convert/flatten.d.ts
3
+ declare function flattenPackage(pkg: DocumentPackage): ContentDocument;
4
+ //#endregion
5
+ export { flattenPackage };
@@ -0,0 +1,137 @@
1
+ import { ContentFormulaSchema, applyParagraphStyleProperties, applyRunStyleProperties, resolveStyleChain } from "document-schema.js";
2
+ //#region src/convert/flatten.ts
3
+ function chainWithRef(chain, group) {
4
+ return group.style === void 0 ? chain : [...chain, group.style];
5
+ }
6
+ function entryOf(styles, chain) {
7
+ if (chain.length === 0) return void 0;
8
+ if (styles === void 0) throw new Error("flattenPackage: a group carries a style ref but the package has no styles table");
9
+ return resolveStyleChain(styles, chain);
10
+ }
11
+ function applyEntry(entry, paragraph) {
12
+ const withParagraph = applyParagraphStyleProperties(entry.paragraph, paragraph);
13
+ const runProperties = entry.run;
14
+ if (runProperties === void 0) return withParagraph;
15
+ return {
16
+ ...withParagraph,
17
+ runs: withParagraph.runs.map((run) => applyRunStyleProperties(runProperties, run))
18
+ };
19
+ }
20
+ function flattenPackage(pkg) {
21
+ const styles = pkg.styles;
22
+ const envelope = {
23
+ metadata: pkg.metadata,
24
+ ...pkg.symbolTable !== void 0 ? { symbolTable: pkg.symbolTable } : {}
25
+ };
26
+ switch (pkg.kind) {
27
+ case "wordprocessing": return {
28
+ kind: "wordprocessing",
29
+ ...envelope,
30
+ sections: pkg.children.map((group) => ({
31
+ ...untag(group.node),
32
+ blocks: flattenSectionChildren(styles, chainWithRef([], group), group.children)
33
+ }))
34
+ };
35
+ case "presentation": return {
36
+ kind: "presentation",
37
+ ...envelope,
38
+ slides: pkg.children.map((group) => {
39
+ const chain = chainWithRef([], group);
40
+ return {
41
+ ...untag(group.node),
42
+ shapes: group.children.map((shape) => flattenShape(styles, chain, shape))
43
+ };
44
+ })
45
+ };
46
+ case "spreadsheet": return {
47
+ kind: "spreadsheet",
48
+ ...envelope,
49
+ sheets: pkg.children.map((group) => {
50
+ if (group.style !== void 0) throw new Error("flattenPackage: a sheet group carries a style ref but a sheet holds no block flow to resolve it onto");
51
+ const images = [];
52
+ const embedded = [];
53
+ for (const child of group.children) if ("kind" in child) images.push(child);
54
+ else embedded.push(child);
55
+ return {
56
+ ...untag(group.node),
57
+ images,
58
+ ...embedded.length > 0 ? { embeddedObjects: embedded } : {}
59
+ };
60
+ })
61
+ };
62
+ case "drawing": return {
63
+ kind: "drawing",
64
+ ...envelope,
65
+ pages: pkg.children.map((group) => {
66
+ const chain = chainWithRef([], group);
67
+ const shapes = [];
68
+ const vectors = [];
69
+ for (const child of group.children) if ("node" in child) shapes.push(flattenShape(styles, chain, child));
70
+ else vectors.push(child);
71
+ return {
72
+ ...untag(group.node),
73
+ shapes,
74
+ vectors
75
+ };
76
+ })
77
+ };
78
+ case "formula": {
79
+ const first = pkg.children[0];
80
+ if (pkg.children.length !== 1 || first === void 0 || !ContentFormulaSchema.safeParse(first).success) throw new Error("flattenPackage: a formula package takes exactly one ContentFormula node");
81
+ return {
82
+ kind: "formula",
83
+ ...envelope,
84
+ formula: first
85
+ };
86
+ }
87
+ }
88
+ }
89
+ function untag(descriptor) {
90
+ const copy = { ...descriptor };
91
+ delete copy.kind;
92
+ return copy;
93
+ }
94
+ function flattenShape(styles, chain, group) {
95
+ return {
96
+ ...group.node,
97
+ blocks: flattenListChildren(styles, chainWithRef(chain, group), group.children)
98
+ };
99
+ }
100
+ function flattenSectionChildren(styles, chain, children) {
101
+ const blocks = [];
102
+ for (const child of children) if (isHeadingGroup(child)) {
103
+ const own = chainWithRef(chain, child);
104
+ blocks.push(resolveAnchor(styles, own, child.node), ...flattenSectionChildren(styles, own, child.children));
105
+ } else if (isListGroup(child)) {
106
+ const own = chainWithRef(chain, child);
107
+ blocks.push(resolveAnchor(styles, own, child.node), ...flattenListChildren(styles, own, child.children));
108
+ } else if (child.kind === "paragraph") {
109
+ const entry = entryOf(styles, chain);
110
+ blocks.push(entry === void 0 ? child : applyEntry(entry, child));
111
+ } else blocks.push(child);
112
+ return blocks;
113
+ }
114
+ function flattenListChildren(styles, chain, children) {
115
+ const blocks = [];
116
+ for (const child of children) if (isListGroup(child)) {
117
+ const own = chainWithRef(chain, child);
118
+ blocks.push(resolveAnchor(styles, own, child.node), ...flattenListChildren(styles, own, child.children));
119
+ } else if (child.kind === "paragraph") {
120
+ const entry = entryOf(styles, chain);
121
+ blocks.push(entry === void 0 ? child : applyEntry(entry, child));
122
+ } else blocks.push(child);
123
+ return blocks;
124
+ }
125
+ function isHeadingGroup(child) {
126
+ return "node" in child && "children" in child && child.node.kind === "paragraph" && child.node.headingLevel !== void 0;
127
+ }
128
+ function isListGroup(child) {
129
+ return "node" in child && "children" in child && child.node.kind === "paragraph" && child.node.list !== void 0;
130
+ }
131
+ function resolveAnchor(styles, chain, anchor) {
132
+ const entry = entryOf(styles, chain);
133
+ if (entry === void 0) return anchor;
134
+ return applyEntry(entry, anchor);
135
+ }
136
+ //#endregion
137
+ export { flattenPackage };