odf.js 5.1.2 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/{codec-BxxFEqvT.d.ts → codec-BQB05qN1.d.ts} +1 -1
- package/dist/{codec-BAz7YIru.d.cts → codec-BoPvy83Y.d.cts} +1 -1
- package/dist/codec.d.cts +1 -1
- package/dist/codec.d.ts +1 -1
- package/dist/embedded-BBJoUl86.js +777 -0
- package/dist/{embedded-BPhuSEEI.d.ts → embedded-CLuwn306.d.ts} +6 -1
- package/dist/embedded-FMIPM1fw.cjs +818 -0
- package/dist/{embedded-J3M3kDvY.d.cts → embedded-ztNK8w6V.d.cts} +6 -1
- package/dist/index.cjs +7 -9
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -4
- package/dist/typed/draw/embedded.cjs +4 -85
- package/dist/typed/draw/embedded.d.cts +2 -2
- package/dist/typed/draw/embedded.d.ts +2 -2
- package/dist/typed/draw/embedded.js +2 -84
- package/dist/typed/odb/form.cjs +2 -2
- package/dist/typed/odb/form.js +1 -1
- package/dist/typed/ods/read.cjs +3 -446
- package/dist/typed/ods/read.js +1 -444
- package/dist/typed/odt/read.cjs +3 -310
- package/dist/typed/odt/read.js +1 -308
- package/package.json +1 -1
package/dist/typed/odt/read.cjs
CHANGED
|
@@ -1,311 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const require_typed_shared_metadata = require("../shared/metadata.cjs");
|
|
6
|
-
const require_typed_shared_list = require("../shared/list.cjs");
|
|
7
|
-
const require_typed_shared_constructs = require("../shared/constructs.cjs");
|
|
8
|
-
const require_typed_shared_paragraph = require("../shared/paragraph.cjs");
|
|
9
|
-
const require_typed_shared_table = require("../shared/table.cjs");
|
|
10
|
-
const require_typed_draw_shapes = require("../draw/shapes.cjs");
|
|
11
|
-
const require_typed_formula_read = require("../formula/read.cjs");
|
|
12
|
-
const require_typed_draw_embedded = require("../draw/embedded.cjs");
|
|
13
|
-
const require_typed_odp_read = require("../odp/read.cjs");
|
|
14
|
-
const require_typed_shared_forms = require("../shared/forms.cjs");
|
|
15
|
-
const require_typed_odg_read = require("../odg/read.cjs");
|
|
16
|
-
let document_schema_js = require("document-schema.js");
|
|
17
|
-
//#region src/typed/odt/read.ts
|
|
18
|
-
const CONTENT_PART = "content.xml";
|
|
19
|
-
const STYLES_PART = "styles.xml";
|
|
20
|
-
const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
|
|
21
|
-
function readOutlineLevel(headingElement) {
|
|
22
|
-
const raw = require_xml_query.attrValue(headingElement, "text:outline-level");
|
|
23
|
-
if (raw === void 0) return 1;
|
|
24
|
-
const parsed = Number.parseInt(raw, 10);
|
|
25
|
-
return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
|
|
26
|
-
}
|
|
27
|
-
function readParagraphOrHeading(element, paragraph) {
|
|
28
|
-
if (element.tag === "text:h") {
|
|
29
|
-
const outlineLevel = readOutlineLevel(element);
|
|
30
|
-
paragraph.styleId = `Heading${outlineLevel}`;
|
|
31
|
-
paragraph.headingLevel = outlineLevel;
|
|
32
|
-
}
|
|
33
|
-
return paragraph;
|
|
34
|
-
}
|
|
35
|
-
function readOdtEmbeddedDocument(reference, frame) {
|
|
36
|
-
switch (reference.objectKind) {
|
|
37
|
-
case "wordprocessing": {
|
|
38
|
-
const { metadata, sections } = readOdtContent(reference.package);
|
|
39
|
-
return {
|
|
40
|
-
kind: "wordprocessing",
|
|
41
|
-
metadata,
|
|
42
|
-
sections
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
case "presentation": {
|
|
46
|
-
const { metadata, slides } = require_typed_odp_read.readOdpContent(reference.package);
|
|
47
|
-
return {
|
|
48
|
-
kind: "presentation",
|
|
49
|
-
metadata,
|
|
50
|
-
slides
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
case "drawing": {
|
|
54
|
-
const { metadata, pages } = require_typed_odg_read.readOdgContent(reference.package);
|
|
55
|
-
return {
|
|
56
|
-
kind: "drawing",
|
|
57
|
-
metadata,
|
|
58
|
-
pages
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
case "formula": return require_typed_formula_read.readOdfFormulaContent(reference.package);
|
|
62
|
-
case "chart": return require_typed_draw_embedded.readOdfChartContent(reference.package, frame, "odt").document;
|
|
63
|
-
case "spreadsheet": return;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function readAnchoredFrameSources(paragraphElement, pkg, state) {
|
|
67
|
-
if (!state.liftFrames) return [];
|
|
68
|
-
const sources = [];
|
|
69
|
-
const readFrameInto = (blocks, frameElement) => {
|
|
70
|
-
const shape = require_typed_draw_shapes.readDrawFrame(frameElement, [], pkg, state.listIdState, true);
|
|
71
|
-
if (shape === void 0) return;
|
|
72
|
-
const reference = require_typed_draw_embedded.readDrawObjectReference(frameElement, pkg);
|
|
73
|
-
if (reference?.objectKind === "chart") {
|
|
74
|
-
const { document, residue } = require_typed_draw_embedded.readOdfChartContent(reference.package, shape.frame, "odt");
|
|
75
|
-
const embeddedBlock = {
|
|
76
|
-
kind: "embeddedObject",
|
|
77
|
-
objectKind: "chart",
|
|
78
|
-
document,
|
|
79
|
-
frame: shape.frame
|
|
80
|
-
};
|
|
81
|
-
if (residue !== void 0) embeddedBlock.source = residue;
|
|
82
|
-
blocks.push(embeddedBlock);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (reference !== void 0) {
|
|
86
|
-
const document = readOdtEmbeddedDocument(reference, shape.frame);
|
|
87
|
-
if (document !== void 0) {
|
|
88
|
-
blocks.push({
|
|
89
|
-
kind: "embeddedObject",
|
|
90
|
-
objectKind: reference.objectKind,
|
|
91
|
-
document,
|
|
92
|
-
frame: shape.frame
|
|
93
|
-
});
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
blocks.push(...shape.blocks);
|
|
98
|
-
};
|
|
99
|
-
for (const child of paragraphElement.children) {
|
|
100
|
-
if (child.type !== "element") continue;
|
|
101
|
-
if (child.tag === "draw:frame") {
|
|
102
|
-
const blocks = [];
|
|
103
|
-
readFrameInto(blocks, child);
|
|
104
|
-
if (blocks.length > 0) sources.push({
|
|
105
|
-
child,
|
|
106
|
-
blocks
|
|
107
|
-
});
|
|
108
|
-
} else if (child.tag === "draw:g") {
|
|
109
|
-
const blocks = [];
|
|
110
|
-
for (const grandChild of child.children) if (grandChild.type === "element" && grandChild.tag === "draw:frame") readFrameInto(blocks, grandChild);
|
|
111
|
-
if (blocks.length > 0) sources.push({
|
|
112
|
-
child,
|
|
113
|
-
blocks
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return sources;
|
|
118
|
-
}
|
|
119
|
-
function liftedBlocksBeforeHalf(read, half) {
|
|
120
|
-
const halfIndex = read.element.children.indexOf(half);
|
|
121
|
-
if (halfIndex === -1) return 0;
|
|
122
|
-
let count = 0;
|
|
123
|
-
for (const source of read.liftedSources) if (read.element.children.indexOf(source.child) < halfIndex) count += source.blocks.length;
|
|
124
|
-
return count;
|
|
125
|
-
}
|
|
126
|
-
function readBlocks(nodes, pkg, state, baseIndex = 0) {
|
|
127
|
-
const blocks = [];
|
|
128
|
-
const emitParagraphs = (reads) => {
|
|
129
|
-
let cursor = baseIndex + blocks.length;
|
|
130
|
-
for (const read of reads) {
|
|
131
|
-
const lifted = read.liftedSources.flatMap((source) => source.blocks);
|
|
132
|
-
for (const half of read.halves) {
|
|
133
|
-
const eventIndex = require_typed_shared_constructs.odfMarkerHalfEventIndex(half, read.element, cursor, liftedBlocksBeforeHalf(read, half.element));
|
|
134
|
-
if (eventIndex !== void 0) state.markerEvents.push({
|
|
135
|
-
kind: half.kind,
|
|
136
|
-
side: half.side,
|
|
137
|
-
key: half.key,
|
|
138
|
-
index: eventIndex,
|
|
139
|
-
qualified: true,
|
|
140
|
-
order: state.order++,
|
|
141
|
-
descriptor: half.descriptor,
|
|
142
|
-
element: half.element
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
blocks.push(read.paragraph);
|
|
146
|
-
for (const block of lifted) blocks.push(block);
|
|
147
|
-
cursor += 1 + lifted.length;
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
const readOneParagraph = (element) => {
|
|
151
|
-
const halves = [];
|
|
152
|
-
return {
|
|
153
|
-
element,
|
|
154
|
-
paragraph: readParagraphOrHeading(element, require_typed_shared_paragraph.readOdfParagraph(element, pkg, {
|
|
155
|
-
provenanceRegions: state.provenanceRegions,
|
|
156
|
-
markersOut: halves,
|
|
157
|
-
definitions: state.definitions,
|
|
158
|
-
listIdState: state.listIdState,
|
|
159
|
-
format: "odt"
|
|
160
|
-
})),
|
|
161
|
-
halves,
|
|
162
|
-
liftedSources: readAnchoredFrameSources(element, pkg, state)
|
|
163
|
-
};
|
|
164
|
-
};
|
|
165
|
-
for (const node of nodes) {
|
|
166
|
-
if (node.type !== "element") continue;
|
|
167
|
-
if (node.tag === "text:p" || node.tag === "text:h") emitParagraphs([readOneParagraph(node)]);
|
|
168
|
-
else if (node.tag === "text:list") {
|
|
169
|
-
const numId = require_typed_shared_list.mintOdfListNumId(pkg, node, state.listIdState);
|
|
170
|
-
const reads = [];
|
|
171
|
-
require_typed_shared_list.readOdfListParagraphs(node, {
|
|
172
|
-
numId,
|
|
173
|
-
level: 0
|
|
174
|
-
}, (element) => {
|
|
175
|
-
const read = readOneParagraph(element);
|
|
176
|
-
reads.push(read);
|
|
177
|
-
return read.paragraph;
|
|
178
|
-
});
|
|
179
|
-
emitParagraphs(reads);
|
|
180
|
-
} else if (node.tag === "table:table") blocks.push(require_typed_shared_table.readOdfTable(node, pkg));
|
|
181
|
-
else if (node.tag === "text:section") {
|
|
182
|
-
const startIndex = baseIndex + blocks.length;
|
|
183
|
-
const order = state.order++;
|
|
184
|
-
blocks.push(...readBlocks(node.children, pkg, state, startIndex));
|
|
185
|
-
state.wrapperExtents.push({
|
|
186
|
-
startIndex,
|
|
187
|
-
endIndex: baseIndex + blocks.length,
|
|
188
|
-
order,
|
|
189
|
-
descriptor: require_typed_shared_constructs.odfDivisionDescriptor(node, pkg)
|
|
190
|
-
});
|
|
191
|
-
} else if (require_typed_shared_constructs.isOdfIndexWrapper(node)) {
|
|
192
|
-
const startIndex = baseIndex + blocks.length;
|
|
193
|
-
const order = state.order++;
|
|
194
|
-
const body = node.children.find((child) => child.type === "element" && child.tag === "text:index-body");
|
|
195
|
-
blocks.push(...body === void 0 ? [] : readBlocks(body.children, pkg, state, startIndex));
|
|
196
|
-
state.wrapperExtents.push({
|
|
197
|
-
startIndex,
|
|
198
|
-
endIndex: baseIndex + blocks.length,
|
|
199
|
-
order,
|
|
200
|
-
descriptor: require_typed_shared_constructs.odfIndexControlDescriptor(node)
|
|
201
|
-
});
|
|
202
|
-
} else if (node.tag === "text:index-title") blocks.push(...readBlocks(node.children, pkg, state, baseIndex + blocks.length));
|
|
203
|
-
else if (node.tag === "office:forms") blocks.push(...require_typed_shared_forms.readOdfFormControlConstructs(node, "odt"));
|
|
204
|
-
}
|
|
205
|
-
return blocks;
|
|
206
|
-
}
|
|
207
|
-
function parseKnownOdfLength(value) {
|
|
208
|
-
const parsed = require_typed_shared_units.parseOdfLength(value);
|
|
209
|
-
if (parsed === void 0) throw new Error(`readOdtContent: internal error -- "${value}" is not a valid ODF length literal`);
|
|
210
|
-
return parsed;
|
|
211
|
-
}
|
|
212
|
-
const DEFAULT_MARGIN_PT = parseKnownOdfLength("2cm");
|
|
213
|
-
const DEFAULT_MARGINS = {
|
|
214
|
-
topPt: DEFAULT_MARGIN_PT,
|
|
215
|
-
rightPt: DEFAULT_MARGIN_PT,
|
|
216
|
-
bottomPt: DEFAULT_MARGIN_PT,
|
|
217
|
-
leftPt: DEFAULT_MARGIN_PT
|
|
218
|
-
};
|
|
219
|
-
function findPageLayoutElement(pkg, pageLayoutName) {
|
|
220
|
-
if (pageLayoutName === void 0) return;
|
|
221
|
-
for (const partPath of AUTOMATIC_STYLE_PARTS) {
|
|
222
|
-
const part = pkg.parts[partPath];
|
|
223
|
-
if (part?.kind !== "xml") continue;
|
|
224
|
-
const root = require_xml_query.rootElement(part.nodes);
|
|
225
|
-
const automaticStyles = root === void 0 ? void 0 : require_xml_query.findChildElement(root.children, "office:automatic-styles");
|
|
226
|
-
if (automaticStyles === void 0) continue;
|
|
227
|
-
const found = require_xml_query.childrenWithTag(automaticStyles, "style:page-layout").find((element) => require_xml_query.attrValue(element, "style:name") === pageLayoutName);
|
|
228
|
-
if (found !== void 0) return found;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
function readFirstMasterPageGeometry(pkg) {
|
|
232
|
-
const stylesPart = pkg.parts[STYLES_PART];
|
|
233
|
-
const stylesRoot = stylesPart?.kind === "xml" ? require_xml_query.rootElement(stylesPart.nodes) : void 0;
|
|
234
|
-
const masterStyles = stylesRoot === void 0 ? void 0 : require_xml_query.findChildElement(stylesRoot.children, "office:master-styles");
|
|
235
|
-
const masterPage = masterStyles === void 0 ? void 0 : require_xml_query.findChildElement(masterStyles.children, "style:master-page");
|
|
236
|
-
const layout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : require_xml_query.attrValue(masterPage, "style:page-layout-name"));
|
|
237
|
-
const properties = layout === void 0 ? void 0 : require_xml_query.findChildElement(layout.children, "style:page-layout-properties");
|
|
238
|
-
const pageSize = properties === void 0 ? void 0 : require_typed_shared_geometry.parsePageSize(properties);
|
|
239
|
-
const margins = properties === void 0 ? void 0 : require_typed_shared_geometry.parseMargins(properties);
|
|
240
|
-
return {
|
|
241
|
-
pageSize: pageSize ?? document_schema_js.PAGE_SIZE_A4,
|
|
242
|
-
margins: margins ?? DEFAULT_MARGINS
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
function readOdtContent(pkg, options = {}) {
|
|
246
|
-
const contentPart = pkg.parts[CONTENT_PART];
|
|
247
|
-
if (contentPart?.kind !== "xml") throw new Error(`readOdtContent: package has no ${CONTENT_PART} part`);
|
|
248
|
-
const contentRoot = require_xml_query.rootElement(contentPart.nodes);
|
|
249
|
-
const body = contentRoot === void 0 ? void 0 : require_xml_query.findChildElement(contentRoot.children, "office:body");
|
|
250
|
-
const textElement = body === void 0 ? void 0 : require_xml_query.findChildElement(body.children, "office:text");
|
|
251
|
-
if (textElement === void 0) throw new Error(`readOdtContent: ${CONTENT_PART} has no office:body/office:text element`);
|
|
252
|
-
const metadata = require_typed_shared_metadata.readOdfMetadata(pkg);
|
|
253
|
-
const { pageSize, margins } = readFirstMasterPageGeometry(pkg);
|
|
254
|
-
const provenanceRegions = /* @__PURE__ */ new Map();
|
|
255
|
-
require_typed_shared_constructs.collectOdfProvenanceRegions(textElement.children, provenanceRegions);
|
|
256
|
-
const definitions = {
|
|
257
|
-
entries: {},
|
|
258
|
-
nextNoteOrdinal: 1,
|
|
259
|
-
nextAnnotationOrdinal: 1
|
|
260
|
-
};
|
|
261
|
-
require_typed_shared_constructs.collectOdfFieldMasterDefinitions(textElement.children, definitions.entries);
|
|
262
|
-
for (const partPath of AUTOMATIC_STYLE_PARTS) {
|
|
263
|
-
const part = pkg.parts[partPath];
|
|
264
|
-
if (part?.kind !== "xml") continue;
|
|
265
|
-
require_typed_shared_constructs.collectOdfDataStyleDefinitions(part.nodes, definitions.entries);
|
|
266
|
-
require_typed_shared_constructs.collectOdfFontFaceDefinitions(part.nodes, definitions.entries);
|
|
267
|
-
}
|
|
268
|
-
const state = {
|
|
269
|
-
listIdState: { next: 1 },
|
|
270
|
-
provenanceRegions,
|
|
271
|
-
definitions,
|
|
272
|
-
wrapperExtents: [],
|
|
273
|
-
markerEvents: [],
|
|
274
|
-
liftFrames: options.frames !== "none",
|
|
275
|
-
order: 0
|
|
276
|
-
};
|
|
277
|
-
const walked = readBlocks(textElement.children, pkg, state);
|
|
278
|
-
const { extents: markerExtents, paired } = require_typed_shared_constructs.resolveOdfMarkerEvents(state.markerEvents);
|
|
279
|
-
const extents = [...state.wrapperExtents, ...markerExtents];
|
|
280
|
-
for (const event of state.markerEvents) if (event.kind === "annotation" && event.side === "start" && !paired.has(event.element)) {
|
|
281
|
-
const descriptor = event.descriptor();
|
|
282
|
-
if (descriptor !== void 0) extents.push({
|
|
283
|
-
startIndex: event.index,
|
|
284
|
-
endIndex: event.index,
|
|
285
|
-
order: event.order,
|
|
286
|
-
descriptor
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
return {
|
|
290
|
-
metadata,
|
|
291
|
-
sections: [{
|
|
292
|
-
pageSize,
|
|
293
|
-
margins,
|
|
294
|
-
blocks: require_typed_shared_constructs.insertOdfConstructMarkers(walked, extents)
|
|
295
|
-
}],
|
|
296
|
-
...Object.keys(definitions.entries).length > 0 ? { definitions: definitions.entries } : {}
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
function readOdt(pkg, options = {}) {
|
|
300
|
-
const { metadata, sections, definitions } = readOdtContent(pkg, options);
|
|
301
|
-
const assembled = (0, document_schema_js.assemblePackage)({
|
|
302
|
-
kind: "wordprocessing",
|
|
303
|
-
metadata,
|
|
304
|
-
sections
|
|
305
|
-
});
|
|
306
|
-
if (definitions !== void 0) assembled.definitions = definitions;
|
|
307
|
-
return assembled;
|
|
308
|
-
}
|
|
309
|
-
//#endregion
|
|
310
|
-
exports.readOdt = readOdt;
|
|
311
|
-
exports.readOdtContent = readOdtContent;
|
|
2
|
+
const require_embedded = require("../../embedded-FMIPM1fw.cjs");
|
|
3
|
+
exports.readOdt = require_embedded.readOdt;
|
|
4
|
+
exports.readOdtContent = require_embedded.readOdtContent;
|
package/dist/typed/odt/read.js
CHANGED
|
@@ -1,309 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { attrValue, childrenWithTag, findChildElement, rootElement } from "../../xml/query.js";
|
|
3
|
-
import { parseMargins, parsePageSize } from "../shared/geometry.js";
|
|
4
|
-
import { readOdfMetadata } from "../shared/metadata.js";
|
|
5
|
-
import { mintOdfListNumId, readOdfListParagraphs } from "../shared/list.js";
|
|
6
|
-
import { collectOdfDataStyleDefinitions, collectOdfFieldMasterDefinitions, collectOdfFontFaceDefinitions, collectOdfProvenanceRegions, insertOdfConstructMarkers, isOdfIndexWrapper, odfDivisionDescriptor, odfIndexControlDescriptor, odfMarkerHalfEventIndex, resolveOdfMarkerEvents } from "../shared/constructs.js";
|
|
7
|
-
import { readOdfParagraph } from "../shared/paragraph.js";
|
|
8
|
-
import { readOdfTable } from "../shared/table.js";
|
|
9
|
-
import { readDrawFrame } from "../draw/shapes.js";
|
|
10
|
-
import { readOdfFormulaContent } from "../formula/read.js";
|
|
11
|
-
import { readDrawObjectReference, readOdfChartContent } from "../draw/embedded.js";
|
|
12
|
-
import { readOdpContent } from "../odp/read.js";
|
|
13
|
-
import { readOdfFormControlConstructs } from "../shared/forms.js";
|
|
14
|
-
import { readOdgContent } from "../odg/read.js";
|
|
15
|
-
import { PAGE_SIZE_A4, assemblePackage } from "document-schema.js";
|
|
16
|
-
//#region src/typed/odt/read.ts
|
|
17
|
-
const CONTENT_PART = "content.xml";
|
|
18
|
-
const STYLES_PART = "styles.xml";
|
|
19
|
-
const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
|
|
20
|
-
function readOutlineLevel(headingElement) {
|
|
21
|
-
const raw = attrValue(headingElement, "text:outline-level");
|
|
22
|
-
if (raw === void 0) return 1;
|
|
23
|
-
const parsed = Number.parseInt(raw, 10);
|
|
24
|
-
return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
|
|
25
|
-
}
|
|
26
|
-
function readParagraphOrHeading(element, paragraph) {
|
|
27
|
-
if (element.tag === "text:h") {
|
|
28
|
-
const outlineLevel = readOutlineLevel(element);
|
|
29
|
-
paragraph.styleId = `Heading${outlineLevel}`;
|
|
30
|
-
paragraph.headingLevel = outlineLevel;
|
|
31
|
-
}
|
|
32
|
-
return paragraph;
|
|
33
|
-
}
|
|
34
|
-
function readOdtEmbeddedDocument(reference, frame) {
|
|
35
|
-
switch (reference.objectKind) {
|
|
36
|
-
case "wordprocessing": {
|
|
37
|
-
const { metadata, sections } = readOdtContent(reference.package);
|
|
38
|
-
return {
|
|
39
|
-
kind: "wordprocessing",
|
|
40
|
-
metadata,
|
|
41
|
-
sections
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
case "presentation": {
|
|
45
|
-
const { metadata, slides } = readOdpContent(reference.package);
|
|
46
|
-
return {
|
|
47
|
-
kind: "presentation",
|
|
48
|
-
metadata,
|
|
49
|
-
slides
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
case "drawing": {
|
|
53
|
-
const { metadata, pages } = readOdgContent(reference.package);
|
|
54
|
-
return {
|
|
55
|
-
kind: "drawing",
|
|
56
|
-
metadata,
|
|
57
|
-
pages
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
case "formula": return readOdfFormulaContent(reference.package);
|
|
61
|
-
case "chart": return readOdfChartContent(reference.package, frame, "odt").document;
|
|
62
|
-
case "spreadsheet": return;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function readAnchoredFrameSources(paragraphElement, pkg, state) {
|
|
66
|
-
if (!state.liftFrames) return [];
|
|
67
|
-
const sources = [];
|
|
68
|
-
const readFrameInto = (blocks, frameElement) => {
|
|
69
|
-
const shape = readDrawFrame(frameElement, [], pkg, state.listIdState, true);
|
|
70
|
-
if (shape === void 0) return;
|
|
71
|
-
const reference = readDrawObjectReference(frameElement, pkg);
|
|
72
|
-
if (reference?.objectKind === "chart") {
|
|
73
|
-
const { document, residue } = readOdfChartContent(reference.package, shape.frame, "odt");
|
|
74
|
-
const embeddedBlock = {
|
|
75
|
-
kind: "embeddedObject",
|
|
76
|
-
objectKind: "chart",
|
|
77
|
-
document,
|
|
78
|
-
frame: shape.frame
|
|
79
|
-
};
|
|
80
|
-
if (residue !== void 0) embeddedBlock.source = residue;
|
|
81
|
-
blocks.push(embeddedBlock);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
if (reference !== void 0) {
|
|
85
|
-
const document = readOdtEmbeddedDocument(reference, shape.frame);
|
|
86
|
-
if (document !== void 0) {
|
|
87
|
-
blocks.push({
|
|
88
|
-
kind: "embeddedObject",
|
|
89
|
-
objectKind: reference.objectKind,
|
|
90
|
-
document,
|
|
91
|
-
frame: shape.frame
|
|
92
|
-
});
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
blocks.push(...shape.blocks);
|
|
97
|
-
};
|
|
98
|
-
for (const child of paragraphElement.children) {
|
|
99
|
-
if (child.type !== "element") continue;
|
|
100
|
-
if (child.tag === "draw:frame") {
|
|
101
|
-
const blocks = [];
|
|
102
|
-
readFrameInto(blocks, child);
|
|
103
|
-
if (blocks.length > 0) sources.push({
|
|
104
|
-
child,
|
|
105
|
-
blocks
|
|
106
|
-
});
|
|
107
|
-
} else if (child.tag === "draw:g") {
|
|
108
|
-
const blocks = [];
|
|
109
|
-
for (const grandChild of child.children) if (grandChild.type === "element" && grandChild.tag === "draw:frame") readFrameInto(blocks, grandChild);
|
|
110
|
-
if (blocks.length > 0) sources.push({
|
|
111
|
-
child,
|
|
112
|
-
blocks
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return sources;
|
|
117
|
-
}
|
|
118
|
-
function liftedBlocksBeforeHalf(read, half) {
|
|
119
|
-
const halfIndex = read.element.children.indexOf(half);
|
|
120
|
-
if (halfIndex === -1) return 0;
|
|
121
|
-
let count = 0;
|
|
122
|
-
for (const source of read.liftedSources) if (read.element.children.indexOf(source.child) < halfIndex) count += source.blocks.length;
|
|
123
|
-
return count;
|
|
124
|
-
}
|
|
125
|
-
function readBlocks(nodes, pkg, state, baseIndex = 0) {
|
|
126
|
-
const blocks = [];
|
|
127
|
-
const emitParagraphs = (reads) => {
|
|
128
|
-
let cursor = baseIndex + blocks.length;
|
|
129
|
-
for (const read of reads) {
|
|
130
|
-
const lifted = read.liftedSources.flatMap((source) => source.blocks);
|
|
131
|
-
for (const half of read.halves) {
|
|
132
|
-
const eventIndex = odfMarkerHalfEventIndex(half, read.element, cursor, liftedBlocksBeforeHalf(read, half.element));
|
|
133
|
-
if (eventIndex !== void 0) state.markerEvents.push({
|
|
134
|
-
kind: half.kind,
|
|
135
|
-
side: half.side,
|
|
136
|
-
key: half.key,
|
|
137
|
-
index: eventIndex,
|
|
138
|
-
qualified: true,
|
|
139
|
-
order: state.order++,
|
|
140
|
-
descriptor: half.descriptor,
|
|
141
|
-
element: half.element
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
blocks.push(read.paragraph);
|
|
145
|
-
for (const block of lifted) blocks.push(block);
|
|
146
|
-
cursor += 1 + lifted.length;
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
const readOneParagraph = (element) => {
|
|
150
|
-
const halves = [];
|
|
151
|
-
return {
|
|
152
|
-
element,
|
|
153
|
-
paragraph: readParagraphOrHeading(element, readOdfParagraph(element, pkg, {
|
|
154
|
-
provenanceRegions: state.provenanceRegions,
|
|
155
|
-
markersOut: halves,
|
|
156
|
-
definitions: state.definitions,
|
|
157
|
-
listIdState: state.listIdState,
|
|
158
|
-
format: "odt"
|
|
159
|
-
})),
|
|
160
|
-
halves,
|
|
161
|
-
liftedSources: readAnchoredFrameSources(element, pkg, state)
|
|
162
|
-
};
|
|
163
|
-
};
|
|
164
|
-
for (const node of nodes) {
|
|
165
|
-
if (node.type !== "element") continue;
|
|
166
|
-
if (node.tag === "text:p" || node.tag === "text:h") emitParagraphs([readOneParagraph(node)]);
|
|
167
|
-
else if (node.tag === "text:list") {
|
|
168
|
-
const numId = mintOdfListNumId(pkg, node, state.listIdState);
|
|
169
|
-
const reads = [];
|
|
170
|
-
readOdfListParagraphs(node, {
|
|
171
|
-
numId,
|
|
172
|
-
level: 0
|
|
173
|
-
}, (element) => {
|
|
174
|
-
const read = readOneParagraph(element);
|
|
175
|
-
reads.push(read);
|
|
176
|
-
return read.paragraph;
|
|
177
|
-
});
|
|
178
|
-
emitParagraphs(reads);
|
|
179
|
-
} else if (node.tag === "table:table") blocks.push(readOdfTable(node, pkg));
|
|
180
|
-
else if (node.tag === "text:section") {
|
|
181
|
-
const startIndex = baseIndex + blocks.length;
|
|
182
|
-
const order = state.order++;
|
|
183
|
-
blocks.push(...readBlocks(node.children, pkg, state, startIndex));
|
|
184
|
-
state.wrapperExtents.push({
|
|
185
|
-
startIndex,
|
|
186
|
-
endIndex: baseIndex + blocks.length,
|
|
187
|
-
order,
|
|
188
|
-
descriptor: odfDivisionDescriptor(node, pkg)
|
|
189
|
-
});
|
|
190
|
-
} else if (isOdfIndexWrapper(node)) {
|
|
191
|
-
const startIndex = baseIndex + blocks.length;
|
|
192
|
-
const order = state.order++;
|
|
193
|
-
const body = node.children.find((child) => child.type === "element" && child.tag === "text:index-body");
|
|
194
|
-
blocks.push(...body === void 0 ? [] : readBlocks(body.children, pkg, state, startIndex));
|
|
195
|
-
state.wrapperExtents.push({
|
|
196
|
-
startIndex,
|
|
197
|
-
endIndex: baseIndex + blocks.length,
|
|
198
|
-
order,
|
|
199
|
-
descriptor: odfIndexControlDescriptor(node)
|
|
200
|
-
});
|
|
201
|
-
} else if (node.tag === "text:index-title") blocks.push(...readBlocks(node.children, pkg, state, baseIndex + blocks.length));
|
|
202
|
-
else if (node.tag === "office:forms") blocks.push(...readOdfFormControlConstructs(node, "odt"));
|
|
203
|
-
}
|
|
204
|
-
return blocks;
|
|
205
|
-
}
|
|
206
|
-
function parseKnownOdfLength(value) {
|
|
207
|
-
const parsed = parseOdfLength(value);
|
|
208
|
-
if (parsed === void 0) throw new Error(`readOdtContent: internal error -- "${value}" is not a valid ODF length literal`);
|
|
209
|
-
return parsed;
|
|
210
|
-
}
|
|
211
|
-
const DEFAULT_MARGIN_PT = parseKnownOdfLength("2cm");
|
|
212
|
-
const DEFAULT_MARGINS = {
|
|
213
|
-
topPt: DEFAULT_MARGIN_PT,
|
|
214
|
-
rightPt: DEFAULT_MARGIN_PT,
|
|
215
|
-
bottomPt: DEFAULT_MARGIN_PT,
|
|
216
|
-
leftPt: DEFAULT_MARGIN_PT
|
|
217
|
-
};
|
|
218
|
-
function findPageLayoutElement(pkg, pageLayoutName) {
|
|
219
|
-
if (pageLayoutName === void 0) return;
|
|
220
|
-
for (const partPath of AUTOMATIC_STYLE_PARTS) {
|
|
221
|
-
const part = pkg.parts[partPath];
|
|
222
|
-
if (part?.kind !== "xml") continue;
|
|
223
|
-
const root = rootElement(part.nodes);
|
|
224
|
-
const automaticStyles = root === void 0 ? void 0 : findChildElement(root.children, "office:automatic-styles");
|
|
225
|
-
if (automaticStyles === void 0) continue;
|
|
226
|
-
const found = childrenWithTag(automaticStyles, "style:page-layout").find((element) => attrValue(element, "style:name") === pageLayoutName);
|
|
227
|
-
if (found !== void 0) return found;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
function readFirstMasterPageGeometry(pkg) {
|
|
231
|
-
const stylesPart = pkg.parts[STYLES_PART];
|
|
232
|
-
const stylesRoot = stylesPart?.kind === "xml" ? rootElement(stylesPart.nodes) : void 0;
|
|
233
|
-
const masterStyles = stylesRoot === void 0 ? void 0 : findChildElement(stylesRoot.children, "office:master-styles");
|
|
234
|
-
const masterPage = masterStyles === void 0 ? void 0 : findChildElement(masterStyles.children, "style:master-page");
|
|
235
|
-
const layout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
|
|
236
|
-
const properties = layout === void 0 ? void 0 : findChildElement(layout.children, "style:page-layout-properties");
|
|
237
|
-
const pageSize = properties === void 0 ? void 0 : parsePageSize(properties);
|
|
238
|
-
const margins = properties === void 0 ? void 0 : parseMargins(properties);
|
|
239
|
-
return {
|
|
240
|
-
pageSize: pageSize ?? PAGE_SIZE_A4,
|
|
241
|
-
margins: margins ?? DEFAULT_MARGINS
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
function readOdtContent(pkg, options = {}) {
|
|
245
|
-
const contentPart = pkg.parts[CONTENT_PART];
|
|
246
|
-
if (contentPart?.kind !== "xml") throw new Error(`readOdtContent: package has no ${CONTENT_PART} part`);
|
|
247
|
-
const contentRoot = rootElement(contentPart.nodes);
|
|
248
|
-
const body = contentRoot === void 0 ? void 0 : findChildElement(contentRoot.children, "office:body");
|
|
249
|
-
const textElement = body === void 0 ? void 0 : findChildElement(body.children, "office:text");
|
|
250
|
-
if (textElement === void 0) throw new Error(`readOdtContent: ${CONTENT_PART} has no office:body/office:text element`);
|
|
251
|
-
const metadata = readOdfMetadata(pkg);
|
|
252
|
-
const { pageSize, margins } = readFirstMasterPageGeometry(pkg);
|
|
253
|
-
const provenanceRegions = /* @__PURE__ */ new Map();
|
|
254
|
-
collectOdfProvenanceRegions(textElement.children, provenanceRegions);
|
|
255
|
-
const definitions = {
|
|
256
|
-
entries: {},
|
|
257
|
-
nextNoteOrdinal: 1,
|
|
258
|
-
nextAnnotationOrdinal: 1
|
|
259
|
-
};
|
|
260
|
-
collectOdfFieldMasterDefinitions(textElement.children, definitions.entries);
|
|
261
|
-
for (const partPath of AUTOMATIC_STYLE_PARTS) {
|
|
262
|
-
const part = pkg.parts[partPath];
|
|
263
|
-
if (part?.kind !== "xml") continue;
|
|
264
|
-
collectOdfDataStyleDefinitions(part.nodes, definitions.entries);
|
|
265
|
-
collectOdfFontFaceDefinitions(part.nodes, definitions.entries);
|
|
266
|
-
}
|
|
267
|
-
const state = {
|
|
268
|
-
listIdState: { next: 1 },
|
|
269
|
-
provenanceRegions,
|
|
270
|
-
definitions,
|
|
271
|
-
wrapperExtents: [],
|
|
272
|
-
markerEvents: [],
|
|
273
|
-
liftFrames: options.frames !== "none",
|
|
274
|
-
order: 0
|
|
275
|
-
};
|
|
276
|
-
const walked = readBlocks(textElement.children, pkg, state);
|
|
277
|
-
const { extents: markerExtents, paired } = resolveOdfMarkerEvents(state.markerEvents);
|
|
278
|
-
const extents = [...state.wrapperExtents, ...markerExtents];
|
|
279
|
-
for (const event of state.markerEvents) if (event.kind === "annotation" && event.side === "start" && !paired.has(event.element)) {
|
|
280
|
-
const descriptor = event.descriptor();
|
|
281
|
-
if (descriptor !== void 0) extents.push({
|
|
282
|
-
startIndex: event.index,
|
|
283
|
-
endIndex: event.index,
|
|
284
|
-
order: event.order,
|
|
285
|
-
descriptor
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
return {
|
|
289
|
-
metadata,
|
|
290
|
-
sections: [{
|
|
291
|
-
pageSize,
|
|
292
|
-
margins,
|
|
293
|
-
blocks: insertOdfConstructMarkers(walked, extents)
|
|
294
|
-
}],
|
|
295
|
-
...Object.keys(definitions.entries).length > 0 ? { definitions: definitions.entries } : {}
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
function readOdt(pkg, options = {}) {
|
|
299
|
-
const { metadata, sections, definitions } = readOdtContent(pkg, options);
|
|
300
|
-
const assembled = assemblePackage({
|
|
301
|
-
kind: "wordprocessing",
|
|
302
|
-
metadata,
|
|
303
|
-
sections
|
|
304
|
-
});
|
|
305
|
-
if (definitions !== void 0) assembled.definitions = definitions;
|
|
306
|
-
return assembled;
|
|
307
|
-
}
|
|
308
|
-
//#endregion
|
|
1
|
+
import { a as readOdtContent, i as readOdt } from "../../embedded-BBJoUl86.js";
|
|
309
2
|
export { readOdt, readOdtContent };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "odf.js",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Type-safe, lossless round-trip conversion between OpenDocument Format packages (odt, ods, odp) and JSON, hand-written and dependency-minimal, built on Zod 4 codecs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|