@office-open/core 0.3.2 → 0.3.3

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.
@@ -69,7 +69,7 @@ interface IChartTypeOptions {
69
69
  readonly series: readonly IChartSeriesData[];
70
70
  readonly categories: readonly string[];
71
71
  }
72
- declare const createChartType: (options: IChartTypeOptions) => ScatterChart | BarChart | LineChart | PieChart | AreaChart;
72
+ declare const createChartType: (options: IChartTypeOptions) => BarChart | LineChart | PieChart | AreaChart | ScatterChart;
73
73
  //#endregion
74
74
  //#region src/chart/series/series-data.d.ts
75
75
  declare const createStrRef: (values: string | readonly string[]) => XmlComponent;
package/dist/index.d.mts CHANGED
@@ -83,10 +83,35 @@ interface OutputByType {
83
83
  readonly nodebuffer: Buffer;
84
84
  }
85
85
  type OutputType = keyof OutputByType;
86
+ declare const OoxmlMimeType: {
87
+ readonly DOCX: "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
88
+ readonly PPTX: "application/vnd.openxmlformats-officedocument.presentationml.presentation";
89
+ };
90
+ declare const convertOutput: <T extends OutputType>(data: Uint8Array, type: T, mimeType?: string) => OutputByType[T];
91
+ //#endregion
92
+ //#region src/packer.d.ts
93
+ /**
94
+ * Shared packer utilities for OOXML document generation.
95
+ *
96
+ * @module
97
+ */
98
+ interface IXmlifyedFile {
99
+ readonly path: string;
100
+ readonly data: string | Uint8Array;
101
+ }
102
+ declare const PrettifyType: {
103
+ readonly NONE: "";
104
+ readonly WITH_2_BLANKS: " ";
105
+ readonly WITH_4_BLANKS: " ";
106
+ readonly WITH_TAB: "\t";
107
+ };
108
+ declare const convertPrettifyType: (prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]) => (typeof PrettifyType)[keyof typeof PrettifyType] | undefined;
86
109
  //#endregion
87
110
  //#region src/opc/app-properties.d.ts
88
- declare class AppProperties extends XmlComponent {
111
+ declare class AppProperties extends ImportedXmlComponent {
112
+ private static instance;
89
113
  constructor();
114
+ prepForXml(): Readonly<Record<string, any>> | undefined;
90
115
  }
91
116
  //#endregion
92
117
  //#region src/opc/relationships.d.ts
@@ -156,26 +181,31 @@ declare class RawPassthrough extends BaseXmlComponent {
156
181
  //#endregion
157
182
  //#region src/placeholder.d.ts
158
183
  /**
159
- * Placeholder detection utilities for compiler post-processing optimization.
184
+ * Placeholder detection and replacement utilities for compiler post-processing.
160
185
  *
161
186
  * Both DOCX and PPTX compilers use placeholder tokens (e.g. `{chart:key}`)
162
187
  * in XML strings to defer relationship ID assignment. This module provides
163
- * fast pre-check utilities to avoid unnecessary regex scans when no placeholders exist.
164
- */
165
- /**
166
- * Check if an XML string contains any placeholder tokens.
167
- * Uses a simple `{` character check which is O(n) but much cheaper
168
- * than running multiple regex scans.
188
+ * fast pre-check utilities and shared replacement functions.
169
189
  */
190
+ type IdFormat = "rId" | "plain";
191
+ declare const formatId: (offset: number, index: number, style: IdFormat) => string;
192
+ declare function escapeRegex(str: string): string;
170
193
  declare function hasPlaceholders(xml: string): boolean;
171
- /**
172
- * Collect all unique placeholder keys with a given prefix from an XML string.
173
- * Single-pass scan using indexOf, avoiding RegExp overhead.
174
- *
175
- * @param xml - The XML string to scan
176
- * @param prefix - The placeholder prefix (e.g. "chart:", "smartart:", "hlink:")
177
- * @returns Array of unique keys found after the prefix
178
- */
179
194
  declare function collectPlaceholderKeys(xml: string, prefix: string): string[];
195
+ declare function replaceImagePlaceholders(xml: string, mediaData: readonly {
196
+ readonly fileName: string;
197
+ }[], offset: number, idFormat?: IdFormat): string;
198
+ declare function getReferencedMedia(xml: string, mediaArray: readonly {
199
+ readonly fileName: string;
200
+ }[]): readonly {
201
+ readonly fileName: string;
202
+ }[];
203
+ declare function replaceChartPlaceholders(xml: string, chartKeys: readonly string[], offset: number, idFormat?: IdFormat): string;
204
+ interface SmartArtRelOptions {
205
+ readonly pathPrefix: string;
206
+ readonly styleRelType: RelationshipType;
207
+ }
208
+ declare function replaceSmartArtPlaceholders(xml: string, keys: readonly string[], dataOffset: number, idFormat?: IdFormat): string;
209
+ declare function addSmartArtRelationships(keys: readonly string[], addRel: (id: number, type: RelationshipType, target: string, targetMode?: string) => void, baseOffset: number, globalStartIndex: number, options: SmartArtRelOptions): void;
180
210
  //#endregion
181
- export { AppProperties, AttributeData, AttributeMap, AttributePayload, BaseXmlComponent, BuilderElement, COLOR_CATEGORIES, Connection, CoreProperties, DataModel, EMPTY_OBJECT, EmptyElement, Formatter, HpsMeasureElement, IContext, ISmartArtData, ITreeNode, IXmlAttribute, IXmlableObject, IgnoreIfEmptyXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent, InitializableXmlComponent, LAYOUT_CATEGORIES, MixedChildren, NextAttributeComponent, NumberValueElement, OnOffElement, OutputByType, OutputType, Percentage, Point, PositivePercentage, PositiveUniversalMeasure, RawElement, RawPassthrough, Relationship, type RelationshipType, Relationships, RelativeMeasure, STYLE_CATEGORIES, SmartArtCollection, StringContainer, StringEnumValueElement, StringValueElement, TargetModeType, ThemeColor, ThemeFont, TransPoint, UniqueNumericIdCreator, UniversalMeasure, XmlAttributeComponent, XmlComponent, chartAttr, collectPlaceholderKeys, convertEmuToInches, convertEmuToPixels, convertEmuToPoints, convertInchesToEmu, convertInchesToTwip, convertMillimetersToTwip, convertPixelsToEmu, convertPointsToEmu, convertToXmlComponent, createDataModel, dateTimeValue, decimalNumber, eighthPointMeasureValue, elementToXml, findRel, findRelsByType, getImageType, hasPlaceholders, hashedId, hexColorValue, hpsMeasureObj, hpsMeasureValue, isRaw, listFiles, longHexNumber, measurementOrPercentValue, numberValObj, onOffObj, parseCoreProperties, parseRels, percentageValue, pointMeasureValue, positiveUniversalMeasureValue, readAllXmlParts, readBinaryFromZip, readTextFromZip, readXmlFromZip, shortHexNumber, signedHpsMeasureValue, signedTwipsMeasureValue, stringContainerObj, stringEnumValObj, stringValObj, twipsMeasureValue, uCharHexNumber, uint8ToBase64, uniqueId, uniqueNumericIdCreator, uniqueUuid, universalMeasureValue, unsignedDecimalNumber, unzipToMap, wrapEl, zipToBuffer };
211
+ export { AppProperties, AttributeData, AttributeMap, AttributePayload, BaseXmlComponent, BuilderElement, COLOR_CATEGORIES, Connection, CoreProperties, DataModel, EMPTY_OBJECT, EmptyElement, Formatter, HpsMeasureElement, IContext, ISmartArtData, ITreeNode, IXmlAttribute, IXmlableObject, IXmlifyedFile, IdFormat, IgnoreIfEmptyXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent, InitializableXmlComponent, LAYOUT_CATEGORIES, MixedChildren, NextAttributeComponent, NumberValueElement, OnOffElement, OoxmlMimeType, OutputByType, OutputType, Percentage, Point, PositivePercentage, PositiveUniversalMeasure, PrettifyType, RawElement, RawPassthrough, Relationship, type RelationshipType, Relationships, RelativeMeasure, STYLE_CATEGORIES, SmartArtCollection, SmartArtRelOptions, StringContainer, StringEnumValueElement, StringValueElement, TargetModeType, ThemeColor, ThemeFont, TransPoint, UniqueNumericIdCreator, UniversalMeasure, XmlAttributeComponent, XmlComponent, addSmartArtRelationships, chartAttr, collectPlaceholderKeys, convertEmuToInches, convertEmuToPixels, convertEmuToPoints, convertInchesToEmu, convertInchesToTwip, convertMillimetersToTwip, convertOutput, convertPixelsToEmu, convertPointsToEmu, convertPrettifyType, convertToXmlComponent, createDataModel, dateTimeValue, decimalNumber, eighthPointMeasureValue, elementToXml, escapeRegex, findRel, findRelsByType, formatId, getImageType, getReferencedMedia, hasPlaceholders, hashedId, hexColorValue, hpsMeasureObj, hpsMeasureValue, isRaw, listFiles, longHexNumber, measurementOrPercentValue, numberValObj, onOffObj, parseCoreProperties, parseRels, percentageValue, pointMeasureValue, positiveUniversalMeasureValue, readAllXmlParts, readBinaryFromZip, readTextFromZip, readXmlFromZip, replaceChartPlaceholders, replaceImagePlaceholders, replaceSmartArtPlaceholders, shortHexNumber, signedHpsMeasureValue, signedTwipsMeasureValue, stringContainerObj, stringEnumValObj, stringValObj, twipsMeasureValue, uCharHexNumber, uint8ToBase64, uniqueId, uniqueNumericIdCreator, uniqueUuid, universalMeasureValue, unsignedDecimalNumber, unzipToMap, wrapEl, zipToBuffer };
package/dist/index.mjs CHANGED
@@ -4,20 +4,50 @@ import { a as convertEmuToInches, c as convertInchesToEmu, d as convertPixelsToE
4
4
  import { COLOR_CATEGORIES, Connection, DataModel, LAYOUT_CATEGORIES, Point, STYLE_CATEGORIES, SmartArtCollection, TransPoint, createDataModel } from "./smartart/index.mjs";
5
5
  import { elementToXml, findRel, findRelsByType, getImageType, listFiles, parseRels, readAllXmlParts, readBinaryFromZip, readTextFromZip, readXmlFromZip, uint8ToBase64, unzipToMap, zipToBuffer } from "./archive.mjs";
6
6
  import { textOf } from "@office-open/xml";
7
- //#region src/opc/app-properties.ts
8
- var AppPropertiesAttributes = class extends XmlAttributeComponent {
9
- xmlKeys = {
10
- vt: "xmlns:vt",
11
- xmlns: "xmlns"
12
- };
7
+ import { strFromU8 } from "fflate";
8
+ //#region src/output-type.ts
9
+ /**
10
+ * Output type definitions for OOXML document export.
11
+ *
12
+ * @module
13
+ */
14
+ const OoxmlMimeType = {
15
+ DOCX: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
16
+ PPTX: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
13
17
  };
14
- var AppProperties = class extends XmlComponent {
18
+ const convertOutput = (data, type, mimeType = OoxmlMimeType.DOCX) => {
19
+ switch (type) {
20
+ case "nodebuffer": return Buffer.from(data);
21
+ case "blob": return new Blob([data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength)], { type: mimeType });
22
+ case "arraybuffer": return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
23
+ case "uint8array": return data;
24
+ case "base64": return btoa(strFromU8(data, true));
25
+ case "string":
26
+ case "text":
27
+ case "binarystring": return strFromU8(data, true);
28
+ case "array": return [...data];
29
+ default: return data;
30
+ }
31
+ };
32
+ //#endregion
33
+ //#region src/packer.ts
34
+ const PrettifyType = {
35
+ NONE: "",
36
+ WITH_2_BLANKS: " ",
37
+ WITH_4_BLANKS: " ",
38
+ WITH_TAB: " "
39
+ };
40
+ const convertPrettifyType = (prettify) => prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? void 0 : prettify;
41
+ //#endregion
42
+ //#region src/opc/app-properties.ts
43
+ const APP_PROPS_XML = "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\"/>";
44
+ var AppProperties = class AppProperties extends ImportedXmlComponent {
45
+ static instance = ImportedXmlComponent.fromXmlString(APP_PROPS_XML);
15
46
  constructor() {
16
47
  super("Properties");
17
- this.root.push(new AppPropertiesAttributes({
18
- vt: "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
19
- xmlns: "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
20
- }));
48
+ }
49
+ prepForXml() {
50
+ return AppProperties.instance.prepForXml({ stack: [] });
21
51
  }
22
52
  };
23
53
  //#endregion
@@ -163,29 +193,13 @@ var RawPassthrough = class extends BaseXmlComponent {
163
193
  };
164
194
  //#endregion
165
195
  //#region src/placeholder.ts
166
- /**
167
- * Placeholder detection utilities for compiler post-processing optimization.
168
- *
169
- * Both DOCX and PPTX compilers use placeholder tokens (e.g. `{chart:key}`)
170
- * in XML strings to defer relationship ID assignment. This module provides
171
- * fast pre-check utilities to avoid unnecessary regex scans when no placeholders exist.
172
- */
173
- /**
174
- * Check if an XML string contains any placeholder tokens.
175
- * Uses a simple `{` character check which is O(n) but much cheaper
176
- * than running multiple regex scans.
177
- */
196
+ const formatId = (offset, index, style) => style === "rId" ? `rId${offset + index}` : `${offset + index}`;
197
+ function escapeRegex(str) {
198
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
199
+ }
178
200
  function hasPlaceholders(xml) {
179
201
  return xml.includes("{");
180
202
  }
181
- /**
182
- * Collect all unique placeholder keys with a given prefix from an XML string.
183
- * Single-pass scan using indexOf, avoiding RegExp overhead.
184
- *
185
- * @param xml - The XML string to scan
186
- * @param prefix - The placeholder prefix (e.g. "chart:", "smartart:", "hlink:")
187
- * @returns Array of unique keys found after the prefix
188
- */
189
203
  function collectPlaceholderKeys(xml, prefix) {
190
204
  const keys = [];
191
205
  const search = `{${prefix}`;
@@ -200,5 +214,51 @@ function collectPlaceholderKeys(xml, prefix) {
200
214
  }
201
215
  return keys;
202
216
  }
217
+ function replaceImagePlaceholders(xml, mediaData, offset, idFormat = "rId") {
218
+ let result = xml;
219
+ mediaData.forEach((image, i) => {
220
+ result = result.replace(new RegExp(`\\{${escapeRegex(image.fileName)}\\}`, "g"), formatId(offset, i, idFormat));
221
+ });
222
+ return result;
223
+ }
224
+ function getReferencedMedia(xml, mediaArray) {
225
+ return mediaArray.filter((image) => xml.includes(`{${image.fileName}}`));
226
+ }
227
+ function replaceChartPlaceholders(xml, chartKeys, offset, idFormat = "rId") {
228
+ let result = xml;
229
+ chartKeys.forEach((key, i) => {
230
+ result = result.replace(new RegExp(`\\{chart:${escapeRegex(key)}\\}`, "g"), formatId(offset, i, idFormat));
231
+ });
232
+ return result;
233
+ }
234
+ function replaceSmartArtPlaceholders(xml, keys, dataOffset, idFormat = "rId") {
235
+ let result = xml;
236
+ const count = keys.length;
237
+ const loOffset = dataOffset + count;
238
+ const qsOffset = loOffset + count;
239
+ const csOffset = qsOffset + count;
240
+ keys.forEach((key, i) => {
241
+ result = result.replace(new RegExp(`\\{smartart:${escapeRegex(key)}\\}`, "g"), formatId(dataOffset, i, idFormat));
242
+ result = result.replace(new RegExp(`\\{smartart-lo:${escapeRegex(key)}\\}`, "g"), formatId(loOffset, i, idFormat));
243
+ result = result.replace(new RegExp(`\\{smartart-qs:${escapeRegex(key)}\\}`, "g"), formatId(qsOffset, i, idFormat));
244
+ result = result.replace(new RegExp(`\\{smartart-cs:${escapeRegex(key)}\\}`, "g"), formatId(csOffset, i, idFormat));
245
+ });
246
+ return result;
247
+ }
248
+ function addSmartArtRelationships(keys, addRel, baseOffset, globalStartIndex, options) {
249
+ const { pathPrefix, styleRelType } = options;
250
+ const count = keys.length;
251
+ const loOffset = baseOffset + count;
252
+ const qsOffset = loOffset + count;
253
+ const csOffset = qsOffset + count;
254
+ keys.forEach((_key, i) => {
255
+ const gi = globalStartIndex + i;
256
+ addRel(baseOffset + i, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData", `${pathPrefix}diagrams/data${gi + 1}.xml`);
257
+ addRel(loOffset + i, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramLayout", `${pathPrefix}diagrams/layout${gi + 1}.xml`);
258
+ addRel(qsOffset + i, styleRelType, `${pathPrefix}diagrams/quickStyle${gi + 1}.xml`);
259
+ addRel(csOffset + i, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramColors", `${pathPrefix}diagrams/colors${gi + 1}.xml`);
260
+ addRel(csOffset + count + i, "http://schemas.microsoft.com/office/2007/relationships/diagramDrawing", `${pathPrefix}diagrams/drawing${gi + 1}.xml`);
261
+ });
262
+ }
203
263
  //#endregion
204
- export { AppProperties, BaseXmlComponent, BuilderElement, COLOR_CATEGORIES, Connection, DataModel, EMPTY_OBJECT, EmptyElement, Formatter, HpsMeasureElement, IgnoreIfEmptyXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent, InitializableXmlComponent, LAYOUT_CATEGORIES, NextAttributeComponent, NumberValueElement, OnOffElement, Point, RawPassthrough, Relationships, STYLE_CATEGORIES, SmartArtCollection, StringContainer, StringEnumValueElement, StringValueElement, TargetModeType, ThemeColor, ThemeFont, TransPoint, XmlAttributeComponent, XmlComponent, chartAttr, collectPlaceholderKeys, convertEmuToInches, convertEmuToPixels, convertEmuToPoints, convertInchesToEmu, convertInchesToTwip, convertMillimetersToTwip, convertPixelsToEmu, convertPointsToEmu, convertToXmlComponent, createDataModel, dateTimeValue, decimalNumber, eighthPointMeasureValue, elementToXml, findRel, findRelsByType, getImageType, hasPlaceholders, hashedId, hexColorValue, hpsMeasureObj, hpsMeasureValue, isRaw, listFiles, longHexNumber, measurementOrPercentValue, numberValObj, onOffObj, parseCoreProperties, parseRels, percentageValue, pointMeasureValue, positiveUniversalMeasureValue, readAllXmlParts, readBinaryFromZip, readTextFromZip, readXmlFromZip, shortHexNumber, signedHpsMeasureValue, signedTwipsMeasureValue, stringContainerObj, stringEnumValObj, stringValObj, twipsMeasureValue, uCharHexNumber, uint8ToBase64, uniqueId, uniqueNumericIdCreator, uniqueUuid, universalMeasureValue, unsignedDecimalNumber, unzipToMap, wrapEl, zipToBuffer };
264
+ export { AppProperties, BaseXmlComponent, BuilderElement, COLOR_CATEGORIES, Connection, DataModel, EMPTY_OBJECT, EmptyElement, Formatter, HpsMeasureElement, IgnoreIfEmptyXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent, InitializableXmlComponent, LAYOUT_CATEGORIES, NextAttributeComponent, NumberValueElement, OnOffElement, OoxmlMimeType, Point, PrettifyType, RawPassthrough, Relationships, STYLE_CATEGORIES, SmartArtCollection, StringContainer, StringEnumValueElement, StringValueElement, TargetModeType, ThemeColor, ThemeFont, TransPoint, XmlAttributeComponent, XmlComponent, addSmartArtRelationships, chartAttr, collectPlaceholderKeys, convertEmuToInches, convertEmuToPixels, convertEmuToPoints, convertInchesToEmu, convertInchesToTwip, convertMillimetersToTwip, convertOutput, convertPixelsToEmu, convertPointsToEmu, convertPrettifyType, convertToXmlComponent, createDataModel, dateTimeValue, decimalNumber, eighthPointMeasureValue, elementToXml, escapeRegex, findRel, findRelsByType, formatId, getImageType, getReferencedMedia, hasPlaceholders, hashedId, hexColorValue, hpsMeasureObj, hpsMeasureValue, isRaw, listFiles, longHexNumber, measurementOrPercentValue, numberValObj, onOffObj, parseCoreProperties, parseRels, percentageValue, pointMeasureValue, positiveUniversalMeasureValue, readAllXmlParts, readBinaryFromZip, readTextFromZip, readXmlFromZip, replaceChartPlaceholders, replaceImagePlaceholders, replaceSmartArtPlaceholders, shortHexNumber, signedHpsMeasureValue, signedTwipsMeasureValue, stringContainerObj, stringEnumValObj, stringValObj, twipsMeasureValue, uCharHexNumber, uint8ToBase64, uniqueId, uniqueNumericIdCreator, uniqueUuid, universalMeasureValue, unsignedDecimalNumber, unzipToMap, wrapEl, zipToBuffer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@office-open/core",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Shared OOXML infrastructure: XmlComponent, value validators, unit converters",
5
5
  "keywords": [
6
6
  "core",
@@ -59,7 +59,7 @@
59
59
  "fflate": "0.8.2",
60
60
  "hash.js": "1.1.7",
61
61
  "nanoid": "5.1.9",
62
- "@office-open/xml": "0.3.2"
62
+ "@office-open/xml": "0.3.3"
63
63
  },
64
64
  "scripts": {
65
65
  "dev": "basis build --stub",