@json-to-office/core-docx 0.9.0 → 0.13.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 +1 @@
1
- {"version":3,"file":"createDocumentGenerator.d.ts","sourceRoot":"","sources":["../../src/plugin/createDocumentGenerator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAwB,MAAM,WAAW,CAAC;AAEnE,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG9E,OAAO,KAAK,EAEV,wBAAwB,EAKzB,MAAM,SAAS,CAAC;AAcjB;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,kEAAkE;IAClE,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,kFAAkF;IAClF,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC3C,4CAA4C;IAC5C,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,oFAAoF;IACpF,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAseD;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,wBAAwB,CAAC,SAAS,EAAE,CAAC,CAavC"}
1
+ {"version":3,"file":"createDocumentGenerator.d.ts","sourceRoot":"","sources":["../../src/plugin/createDocumentGenerator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAwB,MAAM,WAAW,CAAC;AAEnE,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG9E,OAAO,KAAK,EAEV,wBAAwB,EAOzB,MAAM,SAAS,CAAC;AAejB;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,kEAAkE;IAClE,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,kFAAkF;IAClF,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC3C,4CAA4C;IAC5C,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,oFAAoF;IACpF,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAiiBD;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,wBAAwB,CAAC,SAAS,EAAE,CAAC,CAavC"}
@@ -1888,7 +1888,8 @@ import {
1888
1888
  } from "@json-to-office/shared-docx/validation/unified";
1889
1889
  import {
1890
1890
  DuplicateComponentError,
1891
- ComponentValidationError as ComponentValidationError2
1891
+ ComponentValidationError as ComponentValidationError2,
1892
+ UnknownPreservedComponentError
1892
1893
  } from "@json-to-office/shared/plugin";
1893
1894
  function validateComponentProps(schema, props, componentName) {
1894
1895
  return validateCustomComponentProps(schema.propsSchema, props, {
@@ -1955,6 +1956,9 @@ function getValidatedProps(schema, props) {
1955
1956
  }
1956
1957
  var cleanComponentProps = getValidatedProps;
1957
1958
 
1959
+ // src/plugin/createDocumentGenerator.ts
1960
+ import { UnknownPreservedComponentError as UnknownPreservedComponentError2 } from "@json-to-office/shared/plugin";
1961
+
1958
1962
  // src/plugin/schema.ts
1959
1963
  import { Type } from "@sinclair/typebox";
1960
1964
  import { latestVersion } from "@json-to-office/shared-docx";
@@ -6260,9 +6264,10 @@ async function generateChart(config, servicesConfig) {
6260
6264
  b64: true,
6261
6265
  scale: config.scale
6262
6266
  };
6267
+ const resolvedHeaders = typeof servicesConfig?.headers === "function" ? await servicesConfig.headers(requestBody) : servicesConfig?.headers;
6263
6268
  const headers = {
6264
6269
  "Content-Type": "application/json",
6265
- ...servicesConfig?.headers
6270
+ ...resolvedHeaders
6266
6271
  };
6267
6272
  const response = await fetch(`${serverUrl}/export`, {
6268
6273
  method: "POST",
@@ -6828,17 +6833,19 @@ function createBuilderImpl(state) {
6828
6833
  }
6829
6834
  return getThemeWithFallback(themeName);
6830
6835
  }
6831
- async function processDocumentComponents(components, warningsCollector, resolvedTheme, depth = 0) {
6836
+ async function processDocumentComponents(components, preserveSet, warningsCollector, resolvedTheme, depth = 0) {
6832
6837
  if (depth > 20) {
6833
6838
  throw new Error(
6834
6839
  "Maximum component nesting depth exceeded (20). Check for circular component references."
6835
6840
  );
6836
6841
  }
6837
- const processedComponents = [];
6842
+ const standardOut = [];
6843
+ const preservedOut = [];
6838
6844
  for (const componentData of components) {
6839
6845
  const componentName = componentData?.name;
6840
6846
  if (!componentName) {
6841
- processedComponents.push(componentData);
6847
+ standardOut.push(componentData);
6848
+ preservedOut.push(componentData);
6842
6849
  continue;
6843
6850
  }
6844
6851
  const customComponent = componentMap.get(componentName);
@@ -6861,12 +6868,14 @@ function createBuilderImpl(state) {
6861
6868
  );
6862
6869
  let nestedChildren;
6863
6870
  if (componentWithName.children && Array.isArray(componentWithName.children)) {
6864
- nestedChildren = await processDocumentComponents(
6871
+ const nested = await processDocumentComponents(
6865
6872
  componentWithName.children,
6873
+ preserveSet,
6866
6874
  warningsCollector,
6867
6875
  resolvedTheme,
6868
6876
  depth + 1
6869
6877
  );
6878
+ nestedChildren = nested.standard;
6870
6879
  }
6871
6880
  const versionLabel = componentWithName.version ? `${customComponent.name}@${componentWithName.version}` : customComponent.name;
6872
6881
  const addWarning = (message, context) => {
@@ -6886,15 +6895,21 @@ function createBuilderImpl(state) {
6886
6895
  const resultComponents = Array.isArray(result) ? result : [result];
6887
6896
  const processedResult = await processDocumentComponents(
6888
6897
  resultComponents,
6898
+ preserveSet,
6889
6899
  warningsCollector,
6890
6900
  resolvedTheme,
6891
6901
  depth + 1
6892
6902
  );
6893
- processedComponents.push(...processedResult);
6903
+ standardOut.push(...processedResult.standard);
6904
+ if (preserveSet && preserveSet.has(componentName)) {
6905
+ preservedOut.push(componentData);
6906
+ } else {
6907
+ preservedOut.push(...processedResult.preserved);
6908
+ }
6894
6909
  if (state.debug) {
6895
6910
  console.log(
6896
6911
  `Processed custom component '${versionLabel}':`,
6897
- processedResult
6912
+ processedResult.standard
6898
6913
  );
6899
6914
  }
6900
6915
  } catch (error) {
@@ -6909,20 +6924,26 @@ function createBuilderImpl(state) {
6909
6924
  if ("children" in componentData && Array.isArray(componentData.children)) {
6910
6925
  const processedNested = await processDocumentComponents(
6911
6926
  componentData.children,
6927
+ preserveSet,
6912
6928
  warningsCollector,
6913
6929
  resolvedTheme,
6914
6930
  depth + 1
6915
6931
  );
6916
- processedComponents.push({
6932
+ standardOut.push({
6933
+ ...componentData,
6934
+ children: processedNested.standard
6935
+ });
6936
+ preservedOut.push({
6917
6937
  ...componentData,
6918
- children: processedNested
6938
+ children: processedNested.preserved
6919
6939
  });
6920
6940
  } else {
6921
- processedComponents.push(componentData);
6941
+ standardOut.push(componentData);
6942
+ preservedOut.push(componentData);
6922
6943
  }
6923
6944
  }
6924
6945
  }
6925
- return processedComponents;
6946
+ return { standard: standardOut, preserved: preservedOut };
6926
6947
  }
6927
6948
  function addComponent(component) {
6928
6949
  if (!component.name) {
@@ -6947,8 +6968,24 @@ function createBuilderImpl(state) {
6947
6968
  newState
6948
6969
  );
6949
6970
  }
6950
- async function generate(document) {
6971
+ function resolvePreserveSet(options) {
6972
+ const list = options?.preserveCustomComponents;
6973
+ if (!list || list.length === 0) {
6974
+ return void 0;
6975
+ }
6976
+ const registered = new Set(state.componentNames);
6977
+ const unknown = list.filter((n) => !registered.has(n));
6978
+ if (unknown.length > 0) {
6979
+ throw new UnknownPreservedComponentError2(
6980
+ unknown,
6981
+ Array.from(state.componentNames)
6982
+ );
6983
+ }
6984
+ return new Set(list);
6985
+ }
6986
+ async function generate(document, options) {
6951
6987
  try {
6988
+ const preserveSet = resolvePreserveSet(options);
6952
6989
  const internalDocument = document;
6953
6990
  validateDocument(
6954
6991
  internalDocument,
@@ -6972,14 +7009,15 @@ function createBuilderImpl(state) {
6972
7009
  context: { code: w.code }
6973
7010
  });
6974
7011
  }
6975
- const processedComponents = await processDocumentComponents(
7012
+ const processed = await processDocumentComponents(
6976
7013
  mode.doc.children || [],
7014
+ preserveSet,
6977
7015
  warnings,
6978
7016
  modedTheme
6979
7017
  );
6980
7018
  const processedDocument = {
6981
7019
  ...mode.doc,
6982
- children: processedComponents
7020
+ children: processed.standard
6983
7021
  };
6984
7022
  const [modedDoc] = normalizeDocument(processedDocument);
6985
7023
  await resolveDocumentFonts(modedDoc, modedTheme, state.fonts, warnings);
@@ -6988,9 +7026,15 @@ function createBuilderImpl(state) {
6988
7026
  const generatedDocument = await renderDocument(structure, layout, {
6989
7027
  services: state.services
6990
7028
  });
7029
+ const preservedDefinition = preserveSet ? {
7030
+ ...mode.doc,
7031
+ children: processed.preserved
7032
+ } : void 0;
6991
7033
  return {
6992
7034
  document: generatedDocument,
6993
- warnings: warnings.length > 0 ? warnings : null
7035
+ warnings: warnings.length > 0 ? warnings : null,
7036
+ standardDefinition: modedDoc,
7037
+ preservedDefinition
6994
7038
  };
6995
7039
  } catch (error) {
6996
7040
  if (state.debug) {
@@ -6999,16 +7043,40 @@ function createBuilderImpl(state) {
6999
7043
  throw error;
7000
7044
  }
7001
7045
  }
7002
- async function generateBuffer(document) {
7003
- const { document: doc, warnings } = await generate(document);
7046
+ async function generateBuffer(document, options) {
7047
+ const {
7048
+ document: doc,
7049
+ warnings,
7050
+ standardDefinition,
7051
+ preservedDefinition
7052
+ } = await generate(document, options);
7004
7053
  const buffer = await Packer.toBuffer(doc);
7005
- return { buffer, warnings };
7054
+ return { buffer, warnings, standardDefinition, preservedDefinition };
7006
7055
  }
7007
- async function generateFile(document, outputPath) {
7008
- const { buffer, warnings } = await generateBuffer(document);
7056
+ async function generateFile(document, outputPath, options) {
7057
+ const { buffer, warnings, standardDefinition, preservedDefinition } = await generateBuffer(document, options);
7009
7058
  const fs4 = await import("fs/promises");
7010
7059
  await fs4.writeFile(outputPath, new Uint8Array(buffer));
7011
- return { warnings };
7060
+ let preservedOutputPath;
7061
+ if (preservedDefinition !== void 0) {
7062
+ const path3 = await import("path");
7063
+ preservedOutputPath = options?.preservedOutputPath ?? (() => {
7064
+ const ext = path3.extname(outputPath);
7065
+ const base = ext ? outputPath.slice(0, -ext.length) : outputPath;
7066
+ return `${base}-preserved.json`;
7067
+ })();
7068
+ await fs4.writeFile(
7069
+ preservedOutputPath,
7070
+ JSON.stringify(preservedDefinition, null, 2),
7071
+ "utf8"
7072
+ );
7073
+ }
7074
+ return {
7075
+ warnings,
7076
+ standardDefinition,
7077
+ preservedDefinition,
7078
+ preservedOutputPath
7079
+ };
7012
7080
  }
7013
7081
  function getComponentNames() {
7014
7082
  return Array.from(state.componentNames);
@@ -7042,6 +7110,10 @@ function createBuilderImpl(state) {
7042
7110
  };
7043
7111
  }
7044
7112
  }
7113
+ async function getStandardComponentsDefinition(document) {
7114
+ const { standardDefinition } = await generate(document);
7115
+ return standardDefinition;
7116
+ }
7045
7117
  function generateSchema(includeStandardComponents = true) {
7046
7118
  return generatePluginDocumentSchema(
7047
7119
  state.components,
@@ -7055,40 +7127,6 @@ function createBuilderImpl(state) {
7055
7127
  options
7056
7128
  );
7057
7129
  }
7058
- async function getStandardComponentsDefinition(document) {
7059
- try {
7060
- const internalDocument = document;
7061
- validateDocument(
7062
- internalDocument,
7063
- state.components
7064
- );
7065
- const themeName = internalDocument.props.theme || "minimal";
7066
- const docTheme = resolveDocumentTheme(themeName);
7067
- const warnings = [];
7068
- const mode = applyExportMode({
7069
- doc: internalDocument,
7070
- theme: docTheme,
7071
- fonts: state.fonts
7072
- });
7073
- const modedTheme = mode.theme;
7074
- const processedComponents = await processDocumentComponents(
7075
- mode.doc.children || [],
7076
- warnings,
7077
- modedTheme
7078
- );
7079
- const processedDocument = {
7080
- ...mode.doc,
7081
- children: processedComponents
7082
- };
7083
- const [finalReportComponent] = normalizeDocument(processedDocument);
7084
- return finalReportComponent;
7085
- } catch (error) {
7086
- if (state.debug) {
7087
- console.error("Error getting standard components definition:", error);
7088
- }
7089
- throw error;
7090
- }
7091
- }
7092
7130
  return Object.freeze({
7093
7131
  addComponent,
7094
7132
  generate,