ooxml.js 4.1.1 → 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 +6 -4
- package/dist/{codec-DxnbKt4O.d.ts → codec-BrTMlL0a.d.ts} +2 -2
- package/dist/{codec-DO0MNF45.d.cts → codec-DNjHjEDm.d.cts} +2 -2
- package/dist/codec.d.cts +1 -1
- package/dist/codec.d.ts +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/{read-Legu2Pt-.js → read-BQoFiqsb.js} +31 -6
- package/dist/{read-DY3Sm5T3.d.cts → read-BoV9SZy4.d.cts} +34 -0
- package/dist/{read-ClIHv1ef.d.cts → read-CDX1WbiF.d.cts} +17 -0
- package/dist/{read-gB0H2bff.d.ts → read-CxMli3Om.d.ts} +17 -0
- package/dist/{read-DPAy4uu8.cjs → read-Di5UfiSh.cjs} +29 -4
- package/dist/{read-DcJZITV9.d.ts → read-ZWJPDvG-.d.ts} +34 -0
- package/dist/typed/document-package.cjs +2 -2
- package/dist/typed/document-package.js +2 -2
- package/dist/typed/docx/constructs.cjs +59 -5
- package/dist/typed/docx/constructs.d.cts +10 -2
- package/dist/typed/docx/constructs.d.ts +10 -2
- package/dist/typed/docx/constructs.js +59 -6
- package/dist/typed/docx/read.cjs +1 -1
- package/dist/typed/docx/read.d.cts +1 -1
- package/dist/typed/docx/read.d.ts +1 -1
- package/dist/typed/docx/read.js +1 -1
- package/dist/typed/docx/write.cjs +139 -6
- package/dist/typed/docx/write.js +140 -7
- package/dist/typed/embedded.cjs +1 -1
- package/dist/typed/embedded.js +1 -1
- package/dist/typed/pptx/read.cjs +1 -1
- package/dist/typed/pptx/read.d.cts +1 -1
- package/dist/typed/pptx/read.d.ts +1 -1
- package/dist/typed/pptx/read.js +1 -1
- package/package.json +3 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_xml_build = require("../../xml/build.cjs");
|
|
2
3
|
const require_typed_util = require("../util.cjs");
|
|
3
4
|
//#region src/typed/docx/constructs.ts
|
|
4
5
|
const NON_CONTENT_PARAGRAPH_CHILDREN = /* @__PURE__ */ new Set([
|
|
@@ -85,6 +86,37 @@ function insertConstructMarkers(blocks, extents) {
|
|
|
85
86
|
}
|
|
86
87
|
return out;
|
|
87
88
|
}
|
|
89
|
+
function isBlockScopedHalf(half, index) {
|
|
90
|
+
const position = index.elements.indexOf(half.element);
|
|
91
|
+
if (position === -1) return false;
|
|
92
|
+
const leading = index.firstContentIndex === -1 || position < index.firstContentIndex;
|
|
93
|
+
const trailing = index.lastContentIndex === -1 || position > index.lastContentIndex;
|
|
94
|
+
return leading || trailing;
|
|
95
|
+
}
|
|
96
|
+
function runBookmarkExtents(halves, index) {
|
|
97
|
+
const byId = /* @__PURE__ */ new Map();
|
|
98
|
+
for (const half of halves) {
|
|
99
|
+
const existing = byId.get(half.id);
|
|
100
|
+
if (existing === void 0) byId.set(half.id, [half]);
|
|
101
|
+
else existing.push(half);
|
|
102
|
+
}
|
|
103
|
+
const extents = [];
|
|
104
|
+
for (const pair of byId.values()) {
|
|
105
|
+
const starts = pair.filter((half) => half.kind === "start");
|
|
106
|
+
const ends = pair.filter((half) => half.kind === "end");
|
|
107
|
+
const open = starts[0];
|
|
108
|
+
const close = ends[0];
|
|
109
|
+
if (starts.length !== 1 || ends.length !== 1 || open === void 0 || close === void 0) continue;
|
|
110
|
+
if (open.name === void 0 || close.runPosition < open.runPosition) continue;
|
|
111
|
+
if (isBlockScopedHalf(open, index) && isBlockScopedHalf(close, index)) continue;
|
|
112
|
+
extents.push({
|
|
113
|
+
descriptor: bookmarkAnchorDescriptor(open.name),
|
|
114
|
+
startRun: open.runPosition,
|
|
115
|
+
endRun: close.runPosition
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return extents;
|
|
119
|
+
}
|
|
88
120
|
const CONTROL_TYPE_BY_TAG = /* @__PURE__ */ new Map([
|
|
89
121
|
["w:richText", "richText"],
|
|
90
122
|
["w:text", "plainText"],
|
|
@@ -105,17 +137,36 @@ const LOCK_BY_VALUE = /* @__PURE__ */ new Map([
|
|
|
105
137
|
]);
|
|
106
138
|
const TABLE_OF_CONTENTS_GALLERY = "Table of Contents";
|
|
107
139
|
function readControlType(sdtPr) {
|
|
108
|
-
if (sdtPr === void 0) return
|
|
140
|
+
if (sdtPr === void 0) return {
|
|
141
|
+
controlType: "richText",
|
|
142
|
+
galleryResidue: void 0
|
|
143
|
+
};
|
|
109
144
|
for (const child of sdtPr.children) {
|
|
110
145
|
if (child.type !== "element") continue;
|
|
111
146
|
const mapped = CONTROL_TYPE_BY_TAG.get(child.tag);
|
|
112
|
-
if (mapped !== void 0) return
|
|
147
|
+
if (mapped !== void 0) return {
|
|
148
|
+
controlType: mapped,
|
|
149
|
+
galleryResidue: void 0
|
|
150
|
+
};
|
|
113
151
|
if (child.tag === "w:docPartObj" || child.tag === "w:docPartList") {
|
|
114
152
|
const gallery = require_typed_util.childrenWithTag(child, "w:docPartGallery")[0];
|
|
115
|
-
|
|
153
|
+
if ((gallery === void 0 ? void 0 : require_typed_util.attr(gallery, "w:val")) === "Table of Contents") return {
|
|
154
|
+
controlType: "index",
|
|
155
|
+
galleryResidue: void 0
|
|
156
|
+
};
|
|
157
|
+
return {
|
|
158
|
+
controlType: "richText",
|
|
159
|
+
galleryResidue: {
|
|
160
|
+
format: "docx",
|
|
161
|
+
xml: require_xml_build.buildXml([child])
|
|
162
|
+
}
|
|
163
|
+
};
|
|
116
164
|
}
|
|
117
165
|
}
|
|
118
|
-
return
|
|
166
|
+
return {
|
|
167
|
+
controlType: "richText",
|
|
168
|
+
galleryResidue: void 0
|
|
169
|
+
};
|
|
119
170
|
}
|
|
120
171
|
function readListItemOptions(sdtPr) {
|
|
121
172
|
const list = require_typed_util.childrenWithTag(sdtPr, "w:dropDownList")[0] ?? require_typed_util.childrenWithTag(sdtPr, "w:comboBox")[0];
|
|
@@ -137,10 +188,12 @@ function readCheckboxState(sdtPr) {
|
|
|
137
188
|
}
|
|
138
189
|
function readContentControlDescriptor(sdt) {
|
|
139
190
|
const sdtPr = require_typed_util.childrenWithTag(sdt, "w:sdtPr")[0];
|
|
191
|
+
const { controlType, galleryResidue } = readControlType(sdtPr);
|
|
140
192
|
const descriptor = {
|
|
141
193
|
kind: "contentControl",
|
|
142
|
-
controlType
|
|
194
|
+
controlType
|
|
143
195
|
};
|
|
196
|
+
if (galleryResidue !== void 0) descriptor.source = galleryResidue;
|
|
144
197
|
if (sdtPr === void 0) return descriptor;
|
|
145
198
|
const tag = require_typed_util.childrenWithTag(sdtPr, "w:tag")[0];
|
|
146
199
|
const tagVal = tag === void 0 ? void 0 : require_typed_util.attr(tag, "w:val");
|
|
@@ -208,4 +261,5 @@ exports.insertConstructMarkers = insertConstructMarkers;
|
|
|
208
261
|
exports.isDeletedChange = isDeletedChange;
|
|
209
262
|
exports.readContentControlDescriptor = readContentControlDescriptor;
|
|
210
263
|
exports.readProvenanceDescriptor = readProvenanceDescriptor;
|
|
264
|
+
exports.runBookmarkExtents = runBookmarkExtents;
|
|
211
265
|
exports.runInstructionText = runInstructionText;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { l as XmlElement } from "../../node-CkBWRjNR.cjs";
|
|
2
|
-
import { AnchorDescriptor, ConstructDescriptor, ContentBlock, ContentControlDescriptor, ProvenanceChange, ProvenanceDescriptor } from "document-schema.js";
|
|
2
|
+
import { AnchorDescriptor, ConstructDescriptor, ContentBlock, ContentControlDescriptor, ProvenanceChange, ProvenanceDescriptor, RunConstructExtent } from "document-schema.js";
|
|
3
3
|
//#region src/typed/docx/constructs.d.ts
|
|
4
4
|
interface ParagraphContentIndex {
|
|
5
5
|
readonly elements: readonly XmlElement[];
|
|
@@ -15,6 +15,14 @@ interface ConstructExtent {
|
|
|
15
15
|
readonly descriptor: ConstructDescriptor;
|
|
16
16
|
}
|
|
17
17
|
declare function insertConstructMarkers(blocks: readonly ContentBlock[], extents: readonly ConstructExtent[]): ContentBlock[];
|
|
18
|
+
interface ParagraphBookmarkHalf {
|
|
19
|
+
readonly element: XmlElement;
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly name: string | undefined;
|
|
22
|
+
readonly kind: 'start' | 'end';
|
|
23
|
+
readonly runPosition: number;
|
|
24
|
+
}
|
|
25
|
+
declare function runBookmarkExtents(halves: readonly ParagraphBookmarkHalf[], index: ParagraphContentIndex): RunConstructExtent[];
|
|
18
26
|
declare const TABLE_OF_CONTENTS_GALLERY = "Table of Contents";
|
|
19
27
|
declare function readContentControlDescriptor(sdt: XmlElement): ContentControlDescriptor;
|
|
20
28
|
declare const PROVENANCE_CHANGE_BY_TAG: ReadonlyMap<string, ProvenanceChange>;
|
|
@@ -24,4 +32,4 @@ declare function bookmarkAnchorDescriptor(name: string): AnchorDescriptor;
|
|
|
24
32
|
declare function runInstructionText(run: XmlElement): string;
|
|
25
33
|
declare function fieldCharType(run: XmlElement): string | undefined;
|
|
26
34
|
//#endregion
|
|
27
|
-
export { ConstructExtent, PROVENANCE_CHANGE_BY_TAG, ParagraphContentIndex, TABLE_OF_CONTENTS_GALLERY, bookmarkAnchorDescriptor, contentBearingChildren, fieldCharType, indexParagraphContent, insertConstructMarkers, isDeletedChange, readContentControlDescriptor, readProvenanceDescriptor, runInstructionText };
|
|
35
|
+
export { ConstructExtent, PROVENANCE_CHANGE_BY_TAG, ParagraphBookmarkHalf, ParagraphContentIndex, TABLE_OF_CONTENTS_GALLERY, bookmarkAnchorDescriptor, contentBearingChildren, fieldCharType, indexParagraphContent, insertConstructMarkers, isDeletedChange, readContentControlDescriptor, readProvenanceDescriptor, runBookmarkExtents, runInstructionText };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { l as XmlElement } from "../../node-CkBWRjNR.js";
|
|
2
|
-
import { AnchorDescriptor, ConstructDescriptor, ContentBlock, ContentControlDescriptor, ProvenanceChange, ProvenanceDescriptor } from "document-schema.js";
|
|
2
|
+
import { AnchorDescriptor, ConstructDescriptor, ContentBlock, ContentControlDescriptor, ProvenanceChange, ProvenanceDescriptor, RunConstructExtent } from "document-schema.js";
|
|
3
3
|
//#region src/typed/docx/constructs.d.ts
|
|
4
4
|
interface ParagraphContentIndex {
|
|
5
5
|
readonly elements: readonly XmlElement[];
|
|
@@ -15,6 +15,14 @@ interface ConstructExtent {
|
|
|
15
15
|
readonly descriptor: ConstructDescriptor;
|
|
16
16
|
}
|
|
17
17
|
declare function insertConstructMarkers(blocks: readonly ContentBlock[], extents: readonly ConstructExtent[]): ContentBlock[];
|
|
18
|
+
interface ParagraphBookmarkHalf {
|
|
19
|
+
readonly element: XmlElement;
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly name: string | undefined;
|
|
22
|
+
readonly kind: 'start' | 'end';
|
|
23
|
+
readonly runPosition: number;
|
|
24
|
+
}
|
|
25
|
+
declare function runBookmarkExtents(halves: readonly ParagraphBookmarkHalf[], index: ParagraphContentIndex): RunConstructExtent[];
|
|
18
26
|
declare const TABLE_OF_CONTENTS_GALLERY = "Table of Contents";
|
|
19
27
|
declare function readContentControlDescriptor(sdt: XmlElement): ContentControlDescriptor;
|
|
20
28
|
declare const PROVENANCE_CHANGE_BY_TAG: ReadonlyMap<string, ProvenanceChange>;
|
|
@@ -24,4 +32,4 @@ declare function bookmarkAnchorDescriptor(name: string): AnchorDescriptor;
|
|
|
24
32
|
declare function runInstructionText(run: XmlElement): string;
|
|
25
33
|
declare function fieldCharType(run: XmlElement): string | undefined;
|
|
26
34
|
//#endregion
|
|
27
|
-
export { ConstructExtent, PROVENANCE_CHANGE_BY_TAG, ParagraphContentIndex, TABLE_OF_CONTENTS_GALLERY, bookmarkAnchorDescriptor, contentBearingChildren, fieldCharType, indexParagraphContent, insertConstructMarkers, isDeletedChange, readContentControlDescriptor, readProvenanceDescriptor, runInstructionText };
|
|
35
|
+
export { ConstructExtent, PROVENANCE_CHANGE_BY_TAG, ParagraphBookmarkHalf, ParagraphContentIndex, TABLE_OF_CONTENTS_GALLERY, bookmarkAnchorDescriptor, contentBearingChildren, fieldCharType, indexParagraphContent, insertConstructMarkers, isDeletedChange, readContentControlDescriptor, readProvenanceDescriptor, runBookmarkExtents, runInstructionText };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { buildXml } from "../../xml/build.js";
|
|
1
2
|
import { attr, childrenWithTag, decodeEntities, textContent } from "../util.js";
|
|
2
3
|
//#region src/typed/docx/constructs.ts
|
|
3
4
|
const NON_CONTENT_PARAGRAPH_CHILDREN = /* @__PURE__ */ new Set([
|
|
@@ -84,6 +85,37 @@ function insertConstructMarkers(blocks, extents) {
|
|
|
84
85
|
}
|
|
85
86
|
return out;
|
|
86
87
|
}
|
|
88
|
+
function isBlockScopedHalf(half, index) {
|
|
89
|
+
const position = index.elements.indexOf(half.element);
|
|
90
|
+
if (position === -1) return false;
|
|
91
|
+
const leading = index.firstContentIndex === -1 || position < index.firstContentIndex;
|
|
92
|
+
const trailing = index.lastContentIndex === -1 || position > index.lastContentIndex;
|
|
93
|
+
return leading || trailing;
|
|
94
|
+
}
|
|
95
|
+
function runBookmarkExtents(halves, index) {
|
|
96
|
+
const byId = /* @__PURE__ */ new Map();
|
|
97
|
+
for (const half of halves) {
|
|
98
|
+
const existing = byId.get(half.id);
|
|
99
|
+
if (existing === void 0) byId.set(half.id, [half]);
|
|
100
|
+
else existing.push(half);
|
|
101
|
+
}
|
|
102
|
+
const extents = [];
|
|
103
|
+
for (const pair of byId.values()) {
|
|
104
|
+
const starts = pair.filter((half) => half.kind === "start");
|
|
105
|
+
const ends = pair.filter((half) => half.kind === "end");
|
|
106
|
+
const open = starts[0];
|
|
107
|
+
const close = ends[0];
|
|
108
|
+
if (starts.length !== 1 || ends.length !== 1 || open === void 0 || close === void 0) continue;
|
|
109
|
+
if (open.name === void 0 || close.runPosition < open.runPosition) continue;
|
|
110
|
+
if (isBlockScopedHalf(open, index) && isBlockScopedHalf(close, index)) continue;
|
|
111
|
+
extents.push({
|
|
112
|
+
descriptor: bookmarkAnchorDescriptor(open.name),
|
|
113
|
+
startRun: open.runPosition,
|
|
114
|
+
endRun: close.runPosition
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return extents;
|
|
118
|
+
}
|
|
87
119
|
const CONTROL_TYPE_BY_TAG = /* @__PURE__ */ new Map([
|
|
88
120
|
["w:richText", "richText"],
|
|
89
121
|
["w:text", "plainText"],
|
|
@@ -104,17 +136,36 @@ const LOCK_BY_VALUE = /* @__PURE__ */ new Map([
|
|
|
104
136
|
]);
|
|
105
137
|
const TABLE_OF_CONTENTS_GALLERY = "Table of Contents";
|
|
106
138
|
function readControlType(sdtPr) {
|
|
107
|
-
if (sdtPr === void 0) return
|
|
139
|
+
if (sdtPr === void 0) return {
|
|
140
|
+
controlType: "richText",
|
|
141
|
+
galleryResidue: void 0
|
|
142
|
+
};
|
|
108
143
|
for (const child of sdtPr.children) {
|
|
109
144
|
if (child.type !== "element") continue;
|
|
110
145
|
const mapped = CONTROL_TYPE_BY_TAG.get(child.tag);
|
|
111
|
-
if (mapped !== void 0) return
|
|
146
|
+
if (mapped !== void 0) return {
|
|
147
|
+
controlType: mapped,
|
|
148
|
+
galleryResidue: void 0
|
|
149
|
+
};
|
|
112
150
|
if (child.tag === "w:docPartObj" || child.tag === "w:docPartList") {
|
|
113
151
|
const gallery = childrenWithTag(child, "w:docPartGallery")[0];
|
|
114
|
-
|
|
152
|
+
if ((gallery === void 0 ? void 0 : attr(gallery, "w:val")) === "Table of Contents") return {
|
|
153
|
+
controlType: "index",
|
|
154
|
+
galleryResidue: void 0
|
|
155
|
+
};
|
|
156
|
+
return {
|
|
157
|
+
controlType: "richText",
|
|
158
|
+
galleryResidue: {
|
|
159
|
+
format: "docx",
|
|
160
|
+
xml: buildXml([child])
|
|
161
|
+
}
|
|
162
|
+
};
|
|
115
163
|
}
|
|
116
164
|
}
|
|
117
|
-
return
|
|
165
|
+
return {
|
|
166
|
+
controlType: "richText",
|
|
167
|
+
galleryResidue: void 0
|
|
168
|
+
};
|
|
118
169
|
}
|
|
119
170
|
function readListItemOptions(sdtPr) {
|
|
120
171
|
const list = childrenWithTag(sdtPr, "w:dropDownList")[0] ?? childrenWithTag(sdtPr, "w:comboBox")[0];
|
|
@@ -136,10 +187,12 @@ function readCheckboxState(sdtPr) {
|
|
|
136
187
|
}
|
|
137
188
|
function readContentControlDescriptor(sdt) {
|
|
138
189
|
const sdtPr = childrenWithTag(sdt, "w:sdtPr")[0];
|
|
190
|
+
const { controlType, galleryResidue } = readControlType(sdtPr);
|
|
139
191
|
const descriptor = {
|
|
140
192
|
kind: "contentControl",
|
|
141
|
-
controlType
|
|
193
|
+
controlType
|
|
142
194
|
};
|
|
195
|
+
if (galleryResidue !== void 0) descriptor.source = galleryResidue;
|
|
143
196
|
if (sdtPr === void 0) return descriptor;
|
|
144
197
|
const tag = childrenWithTag(sdtPr, "w:tag")[0];
|
|
145
198
|
const tagVal = tag === void 0 ? void 0 : attr(tag, "w:val");
|
|
@@ -197,4 +250,4 @@ function fieldCharType(run) {
|
|
|
197
250
|
return fldChar === void 0 ? void 0 : attr(fldChar, "w:fldCharType");
|
|
198
251
|
}
|
|
199
252
|
//#endregion
|
|
200
|
-
export { PROVENANCE_CHANGE_BY_TAG, TABLE_OF_CONTENTS_GALLERY, bookmarkAnchorDescriptor, contentBearingChildren, fieldCharType, indexParagraphContent, insertConstructMarkers, isDeletedChange, readContentControlDescriptor, readProvenanceDescriptor, runInstructionText };
|
|
253
|
+
export { PROVENANCE_CHANGE_BY_TAG, TABLE_OF_CONTENTS_GALLERY, bookmarkAnchorDescriptor, contentBearingChildren, fieldCharType, indexParagraphContent, insertConstructMarkers, isDeletedChange, readContentControlDescriptor, readProvenanceDescriptor, runBookmarkExtents, runInstructionText };
|
package/dist/typed/docx/read.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_read = require("../../read-
|
|
2
|
+
const require_read = require("../../read-Di5UfiSh.cjs");
|
|
3
3
|
exports.CommentSchema = require_read.CommentSchema;
|
|
4
4
|
exports.DocxDocumentSchema = require_read.DocxDocumentSchema;
|
|
5
5
|
exports.FootnoteSchema = require_read.FootnoteSchema;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as Footnote, i as DocxDocumentSchema, n as CommentSchema, o as FootnoteSchema, r as DocxDocument, s as readDocxContent, t as Comment } from "../../read-
|
|
1
|
+
import { a as Footnote, i as DocxDocumentSchema, n as CommentSchema, o as FootnoteSchema, r as DocxDocument, s as readDocxContent, t as Comment } from "../../read-CDX1WbiF.cjs";
|
|
2
2
|
export { Comment, CommentSchema, DocxDocument, DocxDocumentSchema, Footnote, FootnoteSchema, readDocxContent };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as Footnote, i as DocxDocumentSchema, n as CommentSchema, o as FootnoteSchema, r as DocxDocument, s as readDocxContent, t as Comment } from "../../read-
|
|
1
|
+
import { a as Footnote, i as DocxDocumentSchema, n as CommentSchema, o as FootnoteSchema, r as DocxDocument, s as readDocxContent, t as Comment } from "../../read-CxMli3Om.js";
|
|
2
2
|
export { Comment, CommentSchema, DocxDocument, DocxDocumentSchema, Footnote, FootnoteSchema, readDocxContent };
|
package/dist/typed/docx/read.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as readDocxContent, n as DocxDocumentSchema, r as FootnoteSchema, t as CommentSchema } from "../../read-
|
|
1
|
+
import { i as readDocxContent, n as DocxDocumentSchema, r as FootnoteSchema, t as CommentSchema } from "../../read-BQoFiqsb.js";
|
|
2
2
|
export { CommentSchema, DocxDocumentSchema, FootnoteSchema, readDocxContent };
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_util_base64 = require("../../util/base64.cjs");
|
|
3
|
+
const require_xml_parse = require("../../xml/parse.cjs");
|
|
4
|
+
const require_codec = require("../../codec.cjs");
|
|
2
5
|
const require_xml_fragment = require("../../xml/fragment.cjs");
|
|
3
6
|
const require_xml_entities = require("../../xml/entities.cjs");
|
|
4
7
|
const require_typed_shared_units = require("../shared/units.cjs");
|
|
5
8
|
const require_typed_docx_constructs = require("./constructs.cjs");
|
|
9
|
+
const require_typed_xlsx_build = require("../xlsx/build.cjs");
|
|
6
10
|
let document_schema_js = require("document-schema.js");
|
|
7
11
|
//#region src/typed/docx/write.ts
|
|
8
12
|
const WML_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
|
|
@@ -20,14 +24,18 @@ const DRAWING_PIC_NS = "http://schemas.openxmlformats.org/drawingml/2006/picture
|
|
|
20
24
|
const MARKUP_COMPAT_NS = "http://schemas.openxmlformats.org/markup-compatibility/2006";
|
|
21
25
|
const W14_NS = "http://schemas.microsoft.com/office/word/2010/wordml";
|
|
22
26
|
const W15_NS = "http://schemas.microsoft.com/office/word/2012/wordml";
|
|
27
|
+
const VML_OFFICE_NS = "urn:schemas-microsoft-com:office:office";
|
|
23
28
|
const CT_DOCUMENT = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
|
|
24
29
|
const CT_CORE_PROPS = "application/vnd.openxmlformats-package.core-properties+xml";
|
|
25
30
|
const CT_EXTENDED_PROPS = "application/vnd.openxmlformats-officedocument.extended-properties+xml";
|
|
31
|
+
const CT_EMBEDDED_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
32
|
+
const CT_EMBEDDED_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
26
33
|
const REL_OFFICE_DOCUMENT = `${REL_NS}/officeDocument`;
|
|
27
34
|
const REL_CORE_PROPS = `${PKG_RELS_NS}/metadata/core-properties`;
|
|
28
35
|
const REL_EXTENDED_PROPS = `${REL_NS}/extended-properties`;
|
|
29
36
|
const REL_HYPERLINK = `${REL_NS}/hyperlink`;
|
|
30
37
|
const REL_IMAGE = `${REL_NS}/image`;
|
|
38
|
+
const REL_OLE_OBJECT = `${REL_NS}/oleObject`;
|
|
31
39
|
const DOCUMENT_PART_PATH = "word/document.xml";
|
|
32
40
|
function newWriteState() {
|
|
33
41
|
return {
|
|
@@ -35,6 +43,8 @@ function newWriteState() {
|
|
|
35
43
|
hyperlinkIds: /* @__PURE__ */ new Map(),
|
|
36
44
|
mediaIds: /* @__PURE__ */ new Map(),
|
|
37
45
|
mediaParts: /* @__PURE__ */ new Map(),
|
|
46
|
+
embeddingIds: /* @__PURE__ */ new Map(),
|
|
47
|
+
embeddingParts: /* @__PURE__ */ new Map(),
|
|
38
48
|
nextDrawingId: 1,
|
|
39
49
|
nextMarkerId: 1
|
|
40
50
|
};
|
|
@@ -169,10 +179,49 @@ function buildParagraph(paragraph, state, pageBreakBefore, deleted, provenance)
|
|
|
169
179
|
const changeTag = provenance === void 0 ? void 0 : TRACKED_CHANGE_TAG_BY_CHANGE[provenance.change];
|
|
170
180
|
const pPr = properties === void 0 && changeTag !== void 0 ? require_xml_fragment.el("w:pPr", {}, []) : properties;
|
|
171
181
|
if (pPr !== void 0 && changeTag !== void 0 && provenance !== void 0) pPr.children.push(require_xml_fragment.el("w:rPr", {}, [require_xml_fragment.el(changeTag, trackChangeAttrs(state, provenance))]));
|
|
172
|
-
const runs = paragraph.runs.map((run) => buildRun(run, state, deleted));
|
|
182
|
+
const runs = interleaveRunConstructExtents(paragraph.runs.map((run) => buildRun(run, state, deleted)), paragraph, state);
|
|
173
183
|
const content = changeTag === void 0 || provenance === void 0 ? runs : [require_xml_fragment.el(changeTag, trackChangeAttrs(state, provenance), runs)];
|
|
174
184
|
return require_xml_fragment.el("w:p", {}, [...pPr === void 0 ? [] : [pPr], ...content]);
|
|
175
185
|
}
|
|
186
|
+
function interleaveRunConstructExtents(runElements, paragraph, state) {
|
|
187
|
+
if (paragraph.constructs === void 0) return [...runElements];
|
|
188
|
+
const fault = (0, document_schema_js.findRunConstructFault)(paragraph);
|
|
189
|
+
if (fault !== void 0) throw new Error(`buildDocxPackageFromContent: run-level construct extent at index ${String(fault.index)} of a paragraph does not name real runs (${fault.kind})`);
|
|
190
|
+
const bookmarks = paragraph.constructs.filter((extent) => extent.descriptor.kind === "anchor" && extent.descriptor.anchorType === "bookmark");
|
|
191
|
+
if (bookmarks.length === 0) return [...runElements];
|
|
192
|
+
const closingAt = /* @__PURE__ */ new Map();
|
|
193
|
+
const openingAt = /* @__PURE__ */ new Map();
|
|
194
|
+
const pointAt = /* @__PURE__ */ new Map();
|
|
195
|
+
const push = (map, position, element) => {
|
|
196
|
+
const existing = map.get(position);
|
|
197
|
+
if (existing === void 0) map.set(position, [element]);
|
|
198
|
+
else existing.push(element);
|
|
199
|
+
};
|
|
200
|
+
for (const bookmark of bookmarks) {
|
|
201
|
+
const id = String(state.nextMarkerId++);
|
|
202
|
+
const open = require_xml_fragment.el("w:bookmarkStart", {
|
|
203
|
+
"w:id": id,
|
|
204
|
+
"w:name": require_xml_entities.encodeXmlText(bookmark.descriptor.name)
|
|
205
|
+
});
|
|
206
|
+
const close = require_xml_fragment.el("w:bookmarkEnd", { "w:id": id });
|
|
207
|
+
if (bookmark.startRun === bookmark.endRun) {
|
|
208
|
+
push(pointAt, bookmark.startRun, open);
|
|
209
|
+
push(pointAt, bookmark.startRun, close);
|
|
210
|
+
} else {
|
|
211
|
+
push(openingAt, bookmark.startRun, open);
|
|
212
|
+
push(closingAt, bookmark.endRun, close);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const out = [];
|
|
216
|
+
for (let position = 0; position <= runElements.length; position++) {
|
|
217
|
+
for (const close of closingAt.get(position) ?? []) out.push(close);
|
|
218
|
+
for (const open of openingAt.get(position) ?? []) out.push(open);
|
|
219
|
+
for (const half of pointAt.get(position) ?? []) out.push(half);
|
|
220
|
+
const run = runElements[position];
|
|
221
|
+
if (run !== void 0) out.push(run);
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
176
225
|
function trailingEmptyRunElements(paragraph, element) {
|
|
177
226
|
const runElements = [];
|
|
178
227
|
for (const child of element.children) if (child.type === "element" && (child.tag === "w:r" || child.tag === "w:hyperlink")) runElements.push(child);
|
|
@@ -297,6 +346,43 @@ function buildDrawing(image, state) {
|
|
|
297
346
|
require_xml_fragment.el("a:graphic", { "xmlns:a": DRAWINGML_NS }, [require_xml_fragment.el("a:graphicData", { uri: DRAWING_PIC_NS }, [picture])])
|
|
298
347
|
])]);
|
|
299
348
|
}
|
|
349
|
+
function embeddedPayloadOf(document) {
|
|
350
|
+
switch (document.kind) {
|
|
351
|
+
case "wordprocessing": return {
|
|
352
|
+
extension: "docx",
|
|
353
|
+
progId: "Word.Document.12",
|
|
354
|
+
base64: require_util_base64.bytesToBase64(require_codec.encodePackage(buildDocxPackageFromContent(document)))
|
|
355
|
+
};
|
|
356
|
+
case "spreadsheet": return {
|
|
357
|
+
extension: "xlsx",
|
|
358
|
+
progId: "Excel.Sheet.12",
|
|
359
|
+
base64: require_util_base64.bytesToBase64(require_codec.encodePackage(require_typed_xlsx_build.buildXlsxPackageFromContent(document)))
|
|
360
|
+
};
|
|
361
|
+
default: throw new Error(`buildDocxPackageFromContent: an embedded object carrying a ${document.kind} document has no OOXML OLE payload this writer can produce (embedded wordprocessing and spreadsheet documents serialise through their own builders; presentation is read-only in this package, and drawing/formula are ODF/MathML spellings)`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
function embeddedObjectRelationshipId(state, payload) {
|
|
365
|
+
const existing = state.embeddingIds.get(payload.base64);
|
|
366
|
+
if (existing !== void 0) return existing;
|
|
367
|
+
const name = `oleObject${state.embeddingParts.size + 1}.${payload.extension}`;
|
|
368
|
+
state.embeddingParts.set(name, payload);
|
|
369
|
+
const id = addRelationship(state, REL_OLE_OBJECT, `embeddings/${name}`, false);
|
|
370
|
+
state.embeddingIds.set(payload.base64, id);
|
|
371
|
+
return id;
|
|
372
|
+
}
|
|
373
|
+
function buildObjectElement(block, state) {
|
|
374
|
+
const payload = embeddedPayloadOf(block.document);
|
|
375
|
+
const relId = embeddedObjectRelationshipId(state, payload);
|
|
376
|
+
return require_xml_fragment.el("w:object", {
|
|
377
|
+
"w:dxaOrig": String(require_typed_shared_units.ptToTwips(block.frame.widthPt)),
|
|
378
|
+
"w:dyaOrig": String(require_typed_shared_units.ptToTwips(block.frame.heightPt))
|
|
379
|
+
}, [require_xml_fragment.el("o:OLEObject", {
|
|
380
|
+
Type: "Embed",
|
|
381
|
+
ProgID: payload.progId,
|
|
382
|
+
DrawAspect: "Content",
|
|
383
|
+
"r:id": relId
|
|
384
|
+
})]);
|
|
385
|
+
}
|
|
300
386
|
const TRACKED_CHANGE_TAG_BY_CHANGE = {
|
|
301
387
|
insertion: "w:ins",
|
|
302
388
|
deletion: "w:del",
|
|
@@ -357,10 +443,29 @@ function buildSdtProperties(descriptor) {
|
|
|
357
443
|
children.push(require_xml_fragment.el("w:date", descriptor.value === void 0 ? {} : { "w:fullDate": require_xml_entities.encodeXmlText(descriptor.value) }));
|
|
358
444
|
return require_xml_fragment.el("w:sdtPr", {}, children);
|
|
359
445
|
}
|
|
446
|
+
const restored = restoreGalleryElement(descriptor);
|
|
447
|
+
if (restored !== void 0) {
|
|
448
|
+
children.push(restored);
|
|
449
|
+
return require_xml_fragment.el("w:sdtPr", {}, children);
|
|
450
|
+
}
|
|
360
451
|
const typeTag = SDT_TYPE_ELEMENT[descriptor.controlType];
|
|
361
452
|
children.push(require_xml_fragment.el(typeTag ?? "w:richText"));
|
|
362
453
|
return require_xml_fragment.el("w:sdtPr", {}, children);
|
|
363
454
|
}
|
|
455
|
+
function restoreGalleryElement(descriptor) {
|
|
456
|
+
if (descriptor.controlType !== "richText") return;
|
|
457
|
+
const source = descriptor.source;
|
|
458
|
+
if (source?.format !== "docx") return;
|
|
459
|
+
let nodes;
|
|
460
|
+
try {
|
|
461
|
+
nodes = require_xml_parse.parseXml(source.xml);
|
|
462
|
+
} catch (error) {
|
|
463
|
+
throw new Error(`buildDocxPackageFromContent: a content control carries docx residue that does not parse as XML: ${source.xml}`, { cause: error });
|
|
464
|
+
}
|
|
465
|
+
const first = nodes[0];
|
|
466
|
+
if (nodes.length !== 1 || first?.type !== "element" || first.tag !== "w:docPartObj" && first.tag !== "w:docPartList") return;
|
|
467
|
+
return first;
|
|
468
|
+
}
|
|
364
469
|
function parseFlow(blocks) {
|
|
365
470
|
const imbalance = (0, document_schema_js.findConstructMarkerImbalance)(blocks);
|
|
366
471
|
if (imbalance !== void 0) throw new Error(`buildDocxPackageFromContent: construct markers do not balance (${imbalance.kind} at block ${String(imbalance.index)})`);
|
|
@@ -504,8 +609,25 @@ function buildFlowItems(items, state, deleted, provenance) {
|
|
|
504
609
|
availableImageRuns = [];
|
|
505
610
|
continue;
|
|
506
611
|
}
|
|
507
|
-
|
|
508
|
-
|
|
612
|
+
if (block.kind === "embeddedObject") {
|
|
613
|
+
if (pendingPageBreak) {
|
|
614
|
+
const breakParagraph = require_xml_fragment.el("w:p", {}, [require_xml_fragment.el("w:pPr", {}, [require_xml_fragment.el("w:pageBreakBefore")])]);
|
|
615
|
+
nodes.push(breakParagraph);
|
|
616
|
+
lastParagraph = breakParagraph;
|
|
617
|
+
availableImageRuns = [];
|
|
618
|
+
pendingPageBreak = false;
|
|
619
|
+
}
|
|
620
|
+
const object = buildObjectElement(block, state);
|
|
621
|
+
const reusable = availableImageRuns.shift();
|
|
622
|
+
if (reusable !== void 0) reusable.children.push(object);
|
|
623
|
+
else if (lastParagraph !== void 0) lastParagraph.children.push(require_xml_fragment.el("w:r", {}, [object]));
|
|
624
|
+
else {
|
|
625
|
+
const paragraph = require_xml_fragment.el("w:p", {}, [require_xml_fragment.el("w:r", {}, [object])]);
|
|
626
|
+
lastParagraph = paragraph;
|
|
627
|
+
nodes.push(paragraph);
|
|
628
|
+
}
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
509
631
|
}
|
|
510
632
|
if (pendingPageBreak) nodes.push(require_xml_fragment.el("w:p", {}, [require_xml_fragment.el("w:pPr", {}, [require_xml_fragment.el("w:pageBreakBefore")])]));
|
|
511
633
|
return nodes;
|
|
@@ -555,12 +677,14 @@ function buildDocumentPart(sections, state) {
|
|
|
555
677
|
"xmlns:wp": DRAWING_WP_NS,
|
|
556
678
|
"xmlns:pic": DRAWING_PIC_NS,
|
|
557
679
|
"xmlns:mc": MARKUP_COMPAT_NS,
|
|
680
|
+
"xmlns:o": VML_OFFICE_NS,
|
|
558
681
|
"xmlns:w14": W14_NS,
|
|
559
682
|
"xmlns:w15": W15_NS,
|
|
560
683
|
"mc:Ignorable": "w14 w15"
|
|
561
684
|
}, [require_xml_fragment.el("w:body", {}, bodyChildren)]));
|
|
562
685
|
}
|
|
563
|
-
function buildContentTypesPart(
|
|
686
|
+
function buildContentTypesPart(state) {
|
|
687
|
+
const mediaFormats = new Set([...state.mediaParts.values()].map((media) => media.format));
|
|
564
688
|
const defaults = [require_xml_fragment.el("Default", {
|
|
565
689
|
Extension: "rels",
|
|
566
690
|
ContentType: "application/vnd.openxmlformats-package.relationships+xml"
|
|
@@ -576,6 +700,10 @@ function buildContentTypesPart(mediaFormats) {
|
|
|
576
700
|
Extension: "jpeg",
|
|
577
701
|
ContentType: "image/jpeg"
|
|
578
702
|
}));
|
|
703
|
+
const embeddingOverrides = [...state.embeddingParts].map(([name, payload]) => require_xml_fragment.el("Override", {
|
|
704
|
+
PartName: `/word/embeddings/${name}`,
|
|
705
|
+
ContentType: payload.extension === "docx" ? CT_EMBEDDED_DOCX : CT_EMBEDDED_XLSX
|
|
706
|
+
}));
|
|
579
707
|
return xmlPart(require_xml_fragment.el("Types", { xmlns: CONTENT_TYPES_NS }, [
|
|
580
708
|
...defaults,
|
|
581
709
|
require_xml_fragment.el("Override", {
|
|
@@ -589,7 +717,8 @@ function buildContentTypesPart(mediaFormats) {
|
|
|
589
717
|
require_xml_fragment.el("Override", {
|
|
590
718
|
PartName: "/docProps/app.xml",
|
|
591
719
|
ContentType: CT_EXTENDED_PROPS
|
|
592
|
-
})
|
|
720
|
+
}),
|
|
721
|
+
...embeddingOverrides
|
|
593
722
|
]));
|
|
594
723
|
}
|
|
595
724
|
function buildPackageRelsPart() {
|
|
@@ -661,7 +790,7 @@ function buildDocxPackageFromContent(content) {
|
|
|
661
790
|
}] : content.sections, state);
|
|
662
791
|
const metadata = content.metadata ?? {};
|
|
663
792
|
const parts = {
|
|
664
|
-
"[Content_Types].xml": buildContentTypesPart(
|
|
793
|
+
"[Content_Types].xml": buildContentTypesPart(state),
|
|
665
794
|
"_rels/.rels": buildPackageRelsPart(),
|
|
666
795
|
[DOCUMENT_PART_PATH]: documentPart,
|
|
667
796
|
"word/_rels/document.xml.rels": buildDocumentRelsPart(state),
|
|
@@ -672,6 +801,10 @@ function buildDocxPackageFromContent(content) {
|
|
|
672
801
|
kind: "binary",
|
|
673
802
|
base64: media.base64
|
|
674
803
|
};
|
|
804
|
+
for (const [name, payload] of state.embeddingParts) parts[`word/embeddings/${name}`] = {
|
|
805
|
+
kind: "binary",
|
|
806
|
+
base64: payload.base64
|
|
807
|
+
};
|
|
675
808
|
return { parts };
|
|
676
809
|
}
|
|
677
810
|
//#endregion
|