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