documents.js 3.1.4 → 4.0.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.
@@ -1,152 +0,0 @@
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 (isConstructGroup(child)) {
109
- const own = chainWithRef(chain, child);
110
- blocks.push({
111
- kind: "constructStart",
112
- descriptor: child.node
113
- }, ...flattenSectionChildren(styles, own, child.children), { kind: "constructEnd" });
114
- } else if (child.kind === "paragraph") {
115
- const entry = entryOf(styles, chain);
116
- blocks.push(entry === void 0 ? child : applyEntry(entry, child));
117
- } else blocks.push(child);
118
- return blocks;
119
- }
120
- function flattenListChildren(styles, chain, children) {
121
- const blocks = [];
122
- for (const child of children) if (isListGroup(child)) {
123
- const own = chainWithRef(chain, child);
124
- blocks.push(resolveAnchor(styles, own, child.node), ...flattenListChildren(styles, own, child.children));
125
- } else if (isConstructGroup(child)) {
126
- const own = chainWithRef(chain, child);
127
- blocks.push({
128
- kind: "constructStart",
129
- descriptor: child.node
130
- }, ...flattenListChildren(styles, own, child.children), { kind: "constructEnd" });
131
- } else if (child.kind === "paragraph") {
132
- const entry = entryOf(styles, chain);
133
- blocks.push(entry === void 0 ? child : applyEntry(entry, child));
134
- } else blocks.push(child);
135
- return blocks;
136
- }
137
- function isHeadingGroup(child) {
138
- return "node" in child && "children" in child && child.node.kind === "paragraph" && child.node.headingLevel !== void 0;
139
- }
140
- function isListGroup(child) {
141
- return "node" in child && "children" in child && child.node.kind === "paragraph" && child.node.list !== void 0;
142
- }
143
- function isConstructGroup(child) {
144
- return "node" in child && "children" in child && child.node.kind !== "paragraph";
145
- }
146
- function resolveAnchor(styles, chain, anchor) {
147
- const entry = entryOf(styles, chain);
148
- if (entry === void 0) return anchor;
149
- return applyEntry(entry, anchor);
150
- }
151
- //#endregion
152
- export { flattenPackage };