@json-to-office/core-docx 0.12.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.
package/dist/index.d.ts CHANGED
@@ -15,6 +15,6 @@ export { examples, getExample, getExampleNames, loadJsonExample, } from './templ
15
15
  export { runExample } from './utils/exampleRunner';
16
16
  import './components/index';
17
17
  export type { ChartGenerationResult, } from './components/highcharts';
18
- export { createComponent, createVersion, createDocumentGenerator, validateComponentProps, validateDocument, cleanComponentProps, ComponentValidationError, generatePluginDocumentSchema, exportPluginSchema, generateComponentSchemas, mergeSchemas, type CustomComponent, type ComponentVersion, type ComponentVersionMap, type RenderFunction, type RenderContext as ComponentRenderContext, type DocumentGenerator, type DocumentGeneratorOptions, type GenerationResult, type BufferGenerationResult, type FileGenerationResult, type ValidationResult, type ValidationError, type ExtractCustomComponentType, type CustomComponentUnion, type ExtendedComponentDefinition, type ExtendedReportComponent, type InferCustomComponents, } from './plugin/index';
18
+ export { createComponent, createVersion, createDocumentGenerator, validateComponentProps, validateDocument, cleanComponentProps, ComponentValidationError, UnknownPreservedComponentError, generatePluginDocumentSchema, exportPluginSchema, generateComponentSchemas, mergeSchemas, type CustomComponent, type ComponentVersion, type ComponentVersionMap, type RenderFunction, type RenderContext as ComponentRenderContext, type DocumentGenerator, type DocumentGeneratorOptions, type GenerateOptions, type GenerateFileOptions, type GenerationResult, type BufferGenerationResult, type FileGenerationResult, type ValidationResult, type ValidationError, type ExtractCustomComponentType, type CustomComponentUnion, type ExtendedComponentDefinition, type ExtendedReportComponent, type InferCustomComponents, } from './plugin/index';
19
19
  export type { PluginComponent, PluginRenderFunction, PluginValidationError, PluginValidationResult, } from './types/plugin';
20
20
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAGD,OAAO,EACL,gBA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ation/unified";
7269
7269
  import {
7270
7270
  DuplicateComponentError,
7271
- ComponentValidationError as ComponentValidationError2
7271
+ ComponentValidationError as ComponentValidationError2,
7272
+ UnknownPreservedComponentError
7272
7273
  } from "@json-to-office/shared/plugin";
7273
7274
  function validateComponentProps(schema, props, componentName) {
7274
7275
  return validateCustomComponentProps(schema.propsSchema, props, {
@@ -7335,6 +7336,9 @@ function getValidatedProps(schema, props) {
7335
7336
  }
7336
7337
  var cleanComponentProps = getValidatedProps;
7337
7338
 
7339
+ // src/plugin/createDocumentGenerator.ts
7340
+ import { UnknownPreservedComponentError as UnknownPreservedComponentError2 } from "@json-to-office/shared/plugin";
7341
+
7338
7342
  // src/plugin/schema.ts
7339
7343
  import { Type as Type2 } from "@sinclair/typebox";
7340
7344
  import { latestVersion } from "@json-to-office/shared-docx";
@@ -7510,17 +7514,19 @@ function createBuilderImpl(state) {
7510
7514
  }
7511
7515
  return getThemeWithFallback(themeName);
7512
7516
  }
7513
- async function processDocumentComponents(components, warningsCollector, resolvedTheme, depth = 0) {
7517
+ async function processDocumentComponents(components, preserveSet, warningsCollector, resolvedTheme, depth = 0) {
7514
7518
  if (depth > 20) {
7515
7519
  throw new Error(
7516
7520
  "Maximum component nesting depth exceeded (20). Check for circular component references."
7517
7521
  );
7518
7522
  }
7519
- const processedComponents = [];
7523
+ const standardOut = [];
7524
+ const preservedOut = [];
7520
7525
  for (const componentData of components) {
7521
7526
  const componentName = componentData?.name;
7522
7527
  if (!componentName) {
7523
- processedComponents.push(componentData);
7528
+ standardOut.push(componentData);
7529
+ preservedOut.push(componentData);
7524
7530
  continue;
7525
7531
  }
7526
7532
  const customComponent = componentMap.get(componentName);
@@ -7543,12 +7549,14 @@ function createBuilderImpl(state) {
7543
7549
  );
7544
7550
  let nestedChildren;
7545
7551
  if (componentWithName.children && Array.isArray(componentWithName.children)) {
7546
- nestedChildren = await processDocumentComponents(
7552
+ const nested = await processDocumentComponents(
7547
7553
  componentWithName.children,
7554
+ preserveSet,
7548
7555
  warningsCollector,
7549
7556
  resolvedTheme,
7550
7557
  depth + 1
7551
7558
  );
7559
+ nestedChildren = nested.standard;
7552
7560
  }
7553
7561
  const versionLabel = componentWithName.version ? `${customComponent.name}@${componentWithName.version}` : customComponent.name;
7554
7562
  const addWarning = (message, context) => {
@@ -7568,15 +7576,21 @@ function createBuilderImpl(state) {
7568
7576
  const resultComponents = Array.isArray(result) ? result : [result];
7569
7577
  const processedResult = await processDocumentComponents(
7570
7578
  resultComponents,
7579
+ preserveSet,
7571
7580
  warningsCollector,
7572
7581
  resolvedTheme,
7573
7582
  depth + 1
7574
7583
  );
7575
- processedComponents.push(...processedResult);
7584
+ standardOut.push(...processedResult.standard);
7585
+ if (preserveSet && preserveSet.has(componentName)) {
7586
+ preservedOut.push(componentData);
7587
+ } else {
7588
+ preservedOut.push(...processedResult.preserved);
7589
+ }
7576
7590
  if (state.debug) {
7577
7591
  console.log(
7578
7592
  `Processed custom component '${versionLabel}':`,
7579
- processedResult
7593
+ processedResult.standard
7580
7594
  );
7581
7595
  }
7582
7596
  } catch (error) {
@@ -7591,20 +7605,26 @@ function createBuilderImpl(state) {
7591
7605
  if ("children" in componentData && Array.isArray(componentData.children)) {
7592
7606
  const processedNested = await processDocumentComponents(
7593
7607
  componentData.children,
7608
+ preserveSet,
7594
7609
  warningsCollector,
7595
7610
  resolvedTheme,
7596
7611
  depth + 1
7597
7612
  );
7598
- processedComponents.push({
7613
+ standardOut.push({
7614
+ ...componentData,
7615
+ children: processedNested.standard
7616
+ });
7617
+ preservedOut.push({
7599
7618
  ...componentData,
7600
- children: processedNested
7619
+ children: processedNested.preserved
7601
7620
  });
7602
7621
  } else {
7603
- processedComponents.push(componentData);
7622
+ standardOut.push(componentData);
7623
+ preservedOut.push(componentData);
7604
7624
  }
7605
7625
  }
7606
7626
  }
7607
- return processedComponents;
7627
+ return { standard: standardOut, preserved: preservedOut };
7608
7628
  }
7609
7629
  function addComponent(component) {
7610
7630
  if (!component.name) {
@@ -7629,8 +7649,24 @@ function createBuilderImpl(state) {
7629
7649
  newState
7630
7650
  );
7631
7651
  }
7632
- async function generate(document) {
7652
+ function resolvePreserveSet(options) {
7653
+ const list = options?.preserveCustomComponents;
7654
+ if (!list || list.length === 0) {
7655
+ return void 0;
7656
+ }
7657
+ const registered = new Set(state.componentNames);
7658
+ const unknown = list.filter((n) => !registered.has(n));
7659
+ if (unknown.length > 0) {
7660
+ throw new UnknownPreservedComponentError2(
7661
+ unknown,
7662
+ Array.from(state.componentNames)
7663
+ );
7664
+ }
7665
+ return new Set(list);
7666
+ }
7667
+ async function generate(document, options) {
7633
7668
  try {
7669
+ const preserveSet = resolvePreserveSet(options);
7634
7670
  const internalDocument = document;
7635
7671
  validateDocument(
7636
7672
  internalDocument,
@@ -7654,14 +7690,15 @@ function createBuilderImpl(state) {
7654
7690
  context: { code: w.code }
7655
7691
  });
7656
7692
  }
7657
- const processedComponents = await processDocumentComponents(
7693
+ const processed = await processDocumentComponents(
7658
7694
  mode.doc.children || [],
7695
+ preserveSet,
7659
7696
  warnings,
7660
7697
  modedTheme
7661
7698
  );
7662
7699
  const processedDocument = {
7663
7700
  ...mode.doc,
7664
- children: processedComponents
7701
+ children: processed.standard
7665
7702
  };
7666
7703
  const [modedDoc] = normalizeDocument(processedDocument);
7667
7704
  await resolveDocumentFonts(modedDoc, modedTheme, state.fonts, warnings);
@@ -7670,9 +7707,15 @@ function createBuilderImpl(state) {
7670
7707
  const generatedDocument = await renderDocument(structure, layout, {
7671
7708
  services: state.services
7672
7709
  });
7710
+ const preservedDefinition = preserveSet ? {
7711
+ ...mode.doc,
7712
+ children: processed.preserved
7713
+ } : void 0;
7673
7714
  return {
7674
7715
  document: generatedDocument,
7675
- warnings: warnings.length > 0 ? warnings : null
7716
+ warnings: warnings.length > 0 ? warnings : null,
7717
+ standardDefinition: modedDoc,
7718
+ preservedDefinition
7676
7719
  };
7677
7720
  } catch (error) {
7678
7721
  if (state.debug) {
@@ -7681,16 +7724,40 @@ function createBuilderImpl(state) {
7681
7724
  throw error;
7682
7725
  }
7683
7726
  }
7684
- async function generateBuffer(document) {
7685
- const { document: doc, warnings } = await generate(document);
7727
+ async function generateBuffer(document, options) {
7728
+ const {
7729
+ document: doc,
7730
+ warnings,
7731
+ standardDefinition,
7732
+ preservedDefinition
7733
+ } = await generate(document, options);
7686
7734
  const buffer = await Packer2.toBuffer(doc);
7687
- return { buffer, warnings };
7735
+ return { buffer, warnings, standardDefinition, preservedDefinition };
7688
7736
  }
7689
- async function generateFile(document, outputPath) {
7690
- const { buffer, warnings } = await generateBuffer(document);
7737
+ async function generateFile(document, outputPath, options) {
7738
+ const { buffer, warnings, standardDefinition, preservedDefinition } = await generateBuffer(document, options);
7691
7739
  const fs6 = await import("fs/promises");
7692
7740
  await fs6.writeFile(outputPath, new Uint8Array(buffer));
7693
- return { warnings };
7741
+ let preservedOutputPath;
7742
+ if (preservedDefinition !== void 0) {
7743
+ const path4 = await import("path");
7744
+ preservedOutputPath = options?.preservedOutputPath ?? (() => {
7745
+ const ext = path4.extname(outputPath);
7746
+ const base = ext ? outputPath.slice(0, -ext.length) : outputPath;
7747
+ return `${base}-preserved.json`;
7748
+ })();
7749
+ await fs6.writeFile(
7750
+ preservedOutputPath,
7751
+ JSON.stringify(preservedDefinition, null, 2),
7752
+ "utf8"
7753
+ );
7754
+ }
7755
+ return {
7756
+ warnings,
7757
+ standardDefinition,
7758
+ preservedDefinition,
7759
+ preservedOutputPath
7760
+ };
7694
7761
  }
7695
7762
  function getComponentNames() {
7696
7763
  return Array.from(state.componentNames);
@@ -7724,6 +7791,10 @@ function createBuilderImpl(state) {
7724
7791
  };
7725
7792
  }
7726
7793
  }
7794
+ async function getStandardComponentsDefinition(document) {
7795
+ const { standardDefinition } = await generate(document);
7796
+ return standardDefinition;
7797
+ }
7727
7798
  function generateSchema(includeStandardComponents = true) {
7728
7799
  return generatePluginDocumentSchema(
7729
7800
  state.components,
@@ -7737,40 +7808,6 @@ function createBuilderImpl(state) {
7737
7808
  options
7738
7809
  );
7739
7810
  }
7740
- async function getStandardComponentsDefinition(document) {
7741
- try {
7742
- const internalDocument = document;
7743
- validateDocument(
7744
- internalDocument,
7745
- state.components
7746
- );
7747
- const themeName = internalDocument.props.theme || "minimal";
7748
- const docTheme = resolveDocumentTheme(themeName);
7749
- const warnings = [];
7750
- const mode = applyExportMode2({
7751
- doc: internalDocument,
7752
- theme: docTheme,
7753
- fonts: state.fonts
7754
- });
7755
- const modedTheme = mode.theme;
7756
- const processedComponents = await processDocumentComponents(
7757
- mode.doc.children || [],
7758
- warnings,
7759
- modedTheme
7760
- );
7761
- const processedDocument = {
7762
- ...mode.doc,
7763
- children: processedComponents
7764
- };
7765
- const [finalReportComponent] = normalizeDocument(processedDocument);
7766
- return finalReportComponent;
7767
- } catch (error) {
7768
- if (state.debug) {
7769
- console.error("Error getting standard components definition:", error);
7770
- }
7771
- throw error;
7772
- }
7773
- }
7774
7811
  return Object.freeze({
7775
7812
  addComponent,
7776
7813
  generate,
@@ -7816,6 +7853,7 @@ export {
7816
7853
  ThemeFileError,
7817
7854
  ThemeParseError,
7818
7855
  ThemeValidationError,
7856
+ UnknownPreservedComponentError,
7819
7857
  cleanComponentProps,
7820
7858
  clearComponentCache,
7821
7859
  corporateTheme,