@extend-ai/react-docx 0.8.2 → 0.8.4

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.cjs CHANGED
@@ -16594,14 +16594,53 @@ function resolveListParagraphIndent(paragraph, numberingDefinitions) {
16594
16594
  hangingTwips: nextHangingTwips
16595
16595
  };
16596
16596
  }
16597
- function resolveNumberingMarkerBoxWidthPx(paragraph, numberingDefinitions, numberingLabel) {
16597
+ function numberingIndentCandidateWidthPx(paragraph, numberingDefinitions) {
16598
16598
  const resolvedIndent = resolveListParagraphIndent(
16599
16599
  paragraph,
16600
16600
  numberingDefinitions
16601
16601
  );
16602
16602
  const hangingIndentPx = twipsToSignedPixels(resolvedIndent?.hangingTwips);
16603
16603
  const firstLineIndentPx = twipsToSignedPixels(resolvedIndent?.firstLineTwips);
16604
- const candidateWidthPx = Number.isFinite(hangingIndentPx) && Math.abs(hangingIndentPx) > 0 ? Math.abs(hangingIndentPx) : Number.isFinite(firstLineIndentPx) && firstLineIndentPx < 0 ? Math.abs(firstLineIndentPx) : void 0;
16604
+ return Number.isFinite(hangingIndentPx) && Math.abs(hangingIndentPx) > 0 ? Math.abs(hangingIndentPx) : Number.isFinite(firstLineIndentPx) && firstLineIndentPx < 0 ? Math.abs(firstLineIndentPx) : void 0;
16605
+ }
16606
+ function resolvedNumberingLevelForParagraph(paragraph, numberingDefinitions) {
16607
+ const numbering = paragraph.style?.numbering;
16608
+ if (!numberingDefinitions || !numbering || !Number.isFinite(numbering.numId) || numbering.numId <= 0) {
16609
+ return void 0;
16610
+ }
16611
+ const effectiveNumId = effectiveNumberingNumIdForParagraph(paragraph, numberingDefinitions) ?? numbering.numId;
16612
+ return findNumberingLevelDefinition(
16613
+ numberingDefinitions,
16614
+ effectiveNumId,
16615
+ Math.max(0, Math.round(numbering.ilvl ?? 0))
16616
+ );
16617
+ }
16618
+ function numberingTabAdvanceWidthPx(paragraph, numberingDefinitions, numberingLabel) {
16619
+ if (!numberingLabel?.text || numberingLabel.imageSrc || isTableOfContentsParagraph(paragraph)) {
16620
+ return void 0;
16621
+ }
16622
+ const level = resolvedNumberingLevelForParagraph(
16623
+ paragraph,
16624
+ numberingDefinitions
16625
+ );
16626
+ const suffix = level?.suffix ?? "tab";
16627
+ if (suffix !== "tab") {
16628
+ return void 0;
16629
+ }
16630
+ const labelWidthPx = Math.ceil(
16631
+ measureTextWidthPx2(
16632
+ numberingLabel.text.replace(/\t/g, " ").replace(/\u00a0/g, " "),
16633
+ numberingLabel.style,
16634
+ paragraphBaseFontSizePx(paragraph)
16635
+ )
16636
+ );
16637
+ return (Math.floor(labelWidthPx / DEFAULT_TAB_STOP_PX) + 1) * DEFAULT_TAB_STOP_PX;
16638
+ }
16639
+ function resolveNumberingMarkerBoxWidthPx(paragraph, numberingDefinitions, numberingLabel) {
16640
+ const candidateWidthPx = numberingIndentCandidateWidthPx(
16641
+ paragraph,
16642
+ numberingDefinitions
16643
+ );
16605
16644
  const labelTextForWidth = numberingLabel?.imageSrc ? numberingLabel.trailingText ?? "" : numberingLabel?.text ?? "";
16606
16645
  const normalizedLabelTextForWidth = labelTextForWidth.replace(/\t/g, " ").replace(/\u00a0/g, " ");
16607
16646
  const measuredLabelWidthPx = normalizedLabelTextForWidth.length > 0 ? Math.ceil(
@@ -16627,6 +16666,18 @@ function resolveNumberingMarkerBoxWidthPx(paragraph, numberingDefinitions, numbe
16627
16666
  tocMarkerTargetWidthPx
16628
16667
  );
16629
16668
  if (!Number.isFinite(candidateWidthPx) || candidateWidthPx < 8) {
16669
+ const tabAdvanceWidthPx = numberingTabAdvanceWidthPx(
16670
+ paragraph,
16671
+ numberingDefinitions,
16672
+ numberingLabel
16673
+ );
16674
+ if (tabAdvanceWidthPx !== void 0) {
16675
+ return clampNumber(
16676
+ Math.round(Math.max(tabAdvanceWidthPx, minimumVisualWidthPx)),
16677
+ 8,
16678
+ 220
16679
+ );
16680
+ }
16630
16681
  return minimumVisualWidthPx > 0 ? clampNumber(Math.round(minimumVisualWidthPx), 8, 220) : void 0;
16631
16682
  }
16632
16683
  return Math.max(
@@ -16641,11 +16692,16 @@ function numberingMarkerStyle(paragraph, numberingDefinitions, numberingLabel, b
16641
16692
  numberingLabel
16642
16693
  );
16643
16694
  const markerGapPx = isTableOfContentsParagraph(paragraph) ? 6 : markerBoxWidthPx ? 0 : 2;
16695
+ const tabSynthesized = markerBoxWidthPx !== void 0 && numberingIndentCandidateWidthPx(paragraph, numberingDefinitions) === void 0 && numberingTabAdvanceWidthPx(
16696
+ paragraph,
16697
+ numberingDefinitions,
16698
+ numberingLabel
16699
+ ) !== void 0;
16644
16700
  return {
16645
16701
  ...baseStyle ?? {},
16646
16702
  display: "inline-flex",
16647
16703
  alignItems: "baseline",
16648
- justifyContent: "flex-end",
16704
+ justifyContent: tabSynthesized ? "flex-start" : "flex-end",
16649
16705
  verticalAlign: "baseline",
16650
16706
  width: markerBoxWidthPx ? `${markerBoxWidthPx}px` : void 0,
16651
16707
  minWidth: markerBoxWidthPx ? `${markerBoxWidthPx}px` : "1.1em",
@@ -19668,40 +19724,157 @@ function numberToLetters(value) {
19668
19724
  if (!Number.isFinite(value) || value <= 0) {
19669
19725
  return "";
19670
19726
  }
19671
- let remaining = Math.floor(value);
19672
- let output = "";
19673
- while (remaining > 0) {
19674
- const offset = (remaining - 1) % 26;
19675
- output = String.fromCharCode(65 + offset) + output;
19676
- remaining = Math.floor((remaining - 1) / 26);
19727
+ const rounded = Math.floor(value);
19728
+ const letter = String.fromCharCode(65 + (rounded - 1) % 26);
19729
+ return letter.repeat(Math.floor((rounded - 1) / 26) + 1);
19730
+ }
19731
+ var CARDINAL_ONES = [
19732
+ "zero",
19733
+ "one",
19734
+ "two",
19735
+ "three",
19736
+ "four",
19737
+ "five",
19738
+ "six",
19739
+ "seven",
19740
+ "eight",
19741
+ "nine",
19742
+ "ten",
19743
+ "eleven",
19744
+ "twelve",
19745
+ "thirteen",
19746
+ "fourteen",
19747
+ "fifteen",
19748
+ "sixteen",
19749
+ "seventeen",
19750
+ "eighteen",
19751
+ "nineteen"
19752
+ ];
19753
+ var CARDINAL_TENS = [
19754
+ "",
19755
+ "",
19756
+ "twenty",
19757
+ "thirty",
19758
+ "forty",
19759
+ "fifty",
19760
+ "sixty",
19761
+ "seventy",
19762
+ "eighty",
19763
+ "ninety"
19764
+ ];
19765
+ var ORDINAL_ONES = [
19766
+ "zeroth",
19767
+ "first",
19768
+ "second",
19769
+ "third",
19770
+ "fourth",
19771
+ "fifth",
19772
+ "sixth",
19773
+ "seventh",
19774
+ "eighth",
19775
+ "ninth",
19776
+ "tenth",
19777
+ "eleventh",
19778
+ "twelfth",
19779
+ "thirteenth",
19780
+ "fourteenth",
19781
+ "fifteenth",
19782
+ "sixteenth",
19783
+ "seventeenth",
19784
+ "eighteenth",
19785
+ "nineteenth"
19786
+ ];
19787
+ var ORDINAL_TENS = [
19788
+ "",
19789
+ "",
19790
+ "twentieth",
19791
+ "thirtieth",
19792
+ "fortieth",
19793
+ "fiftieth",
19794
+ "sixtieth",
19795
+ "seventieth",
19796
+ "eightieth",
19797
+ "ninetieth"
19798
+ ];
19799
+ function numberToCardinalText(value) {
19800
+ if (value < 20) {
19801
+ return CARDINAL_ONES[value];
19802
+ }
19803
+ if (value < 100) {
19804
+ const tens = Math.floor(value / 10);
19805
+ const ones = value % 10;
19806
+ return ones === 0 ? CARDINAL_TENS[tens] : `${CARDINAL_TENS[tens]}-${CARDINAL_ONES[ones]}`;
19807
+ }
19808
+ if (value < 1e3) {
19809
+ const hundreds = Math.floor(value / 100);
19810
+ const rest = value % 100;
19811
+ const head = `${CARDINAL_ONES[hundreds]} hundred`;
19812
+ return rest === 0 ? head : `${head} ${numberToCardinalText(rest)}`;
19813
+ }
19814
+ return String(value);
19815
+ }
19816
+ function numberToOrdinalText(value) {
19817
+ if (value < 20) {
19818
+ return ORDINAL_ONES[value];
19819
+ }
19820
+ if (value < 100) {
19821
+ const tens = Math.floor(value / 10);
19822
+ const ones = value % 10;
19823
+ return ones === 0 ? ORDINAL_TENS[tens] : `${CARDINAL_TENS[tens]}-${ORDINAL_ONES[ones]}`;
19824
+ }
19825
+ if (value < 1e3) {
19826
+ const hundreds = Math.floor(value / 100);
19827
+ const rest = value % 100;
19828
+ return rest === 0 ? `${CARDINAL_ONES[hundreds]} hundredth` : `${CARDINAL_ONES[hundreds]} hundred ${numberToOrdinalText(rest)}`;
19829
+ }
19830
+ return String(value);
19831
+ }
19832
+ function numberToOrdinalDigits(value) {
19833
+ const mod100 = value % 100;
19834
+ if (mod100 >= 11 && mod100 <= 13) {
19835
+ return `${value}th`;
19836
+ }
19837
+ switch (value % 10) {
19838
+ case 1:
19839
+ return `${value}st`;
19840
+ case 2:
19841
+ return `${value}nd`;
19842
+ case 3:
19843
+ return `${value}rd`;
19844
+ default:
19845
+ return `${value}th`;
19677
19846
  }
19678
- return output;
19679
19847
  }
19680
19848
  function formatNumberingCounter(format, value) {
19681
- if (!Number.isFinite(value) || value <= 0) {
19849
+ if (!Number.isFinite(value) || value < 0) {
19682
19850
  return "";
19683
19851
  }
19852
+ const rounded = Math.round(value);
19684
19853
  switch ((format ?? "decimal").toLowerCase()) {
19685
19854
  case "decimal":
19686
- return String(value);
19855
+ return String(rounded);
19856
+ case "decimalzero":
19857
+ return rounded < 10 ? `0${rounded}` : String(rounded);
19687
19858
  case "upperroman":
19688
- return numberToRoman(value);
19859
+ return rounded === 0 ? String(rounded) : numberToRoman(rounded);
19689
19860
  case "lowerroman":
19690
- return numberToRoman(value).toLowerCase();
19861
+ return rounded === 0 ? String(rounded) : numberToRoman(rounded).toLowerCase();
19691
19862
  case "upperletter":
19692
- return numberToLetters(value);
19863
+ return rounded === 0 ? String(rounded) : numberToLetters(rounded);
19693
19864
  case "lowerletter":
19694
- return numberToLetters(value).toLowerCase();
19865
+ return rounded === 0 ? String(rounded) : numberToLetters(rounded).toLowerCase();
19695
19866
  case "ordinal":
19867
+ return numberToOrdinalDigits(rounded);
19696
19868
  case "cardinaltext":
19869
+ return numberToCardinalText(rounded);
19697
19870
  case "ordinaltext":
19698
- return String(value);
19871
+ return numberToOrdinalText(rounded);
19699
19872
  case "bullet":
19700
19873
  return "\u2022";
19701
19874
  case "none":
19702
19875
  return "";
19703
19876
  default:
19704
- return String(value);
19877
+ return String(rounded);
19705
19878
  }
19706
19879
  }
19707
19880
  function formatPageFieldValue(value, format) {
@@ -19816,10 +19989,10 @@ function numberingStartValue(numbering, numId, ilvl) {
19816
19989
  const instance = numbering.instances.find((item) => item.numId === numId);
19817
19990
  const override = instance?.levelStartOverrides?.[String(ilvl)];
19818
19991
  if (Number.isFinite(override)) {
19819
- return Math.max(1, Math.round(override));
19992
+ return Math.max(0, Math.round(override));
19820
19993
  }
19821
19994
  const level = findNumberingLevelDefinition(numbering, numId, ilvl);
19822
- if (Number.isFinite(level?.start) && level?.start > 0) {
19995
+ if (Number.isFinite(level?.start) && level?.start >= 0) {
19823
19996
  return Math.round(level?.start);
19824
19997
  }
19825
19998
  return 1;
@@ -19890,14 +20063,27 @@ function buildParagraphNumberingLabels(model) {
19890
20063
  if (!template || template.trim().length === 0) {
19891
20064
  return;
19892
20065
  }
20066
+ const restartTriggerCeiling = (levelIndex) => {
20067
+ const rawRestart = findNumberingLevelDefinition(
20068
+ numbering,
20069
+ numId,
20070
+ levelIndex
20071
+ )?.lvlRestart;
20072
+ if (rawRestart === void 0 || !Number.isFinite(rawRestart) || Math.round(rawRestart) > levelIndex) {
20073
+ return levelIndex - 1;
20074
+ }
20075
+ const restart = Math.round(rawRestart);
20076
+ return restart <= 0 ? -1 : restart - 1;
20077
+ };
20078
+ const currentRestartCeiling = restartTriggerCeiling(ilvl);
19893
20079
  const counters = countersByNumId.get(numId) ?? [];
19894
20080
  const sharedAbstractCounters = Number.isFinite(abstractNumId) ? abstractCountersByAbstractNumId.get(abstractNumId) ?? [] : void 0;
19895
20081
  let parentCountersChanged = false;
19896
20082
  for (let index = 0; index < ilvl; index += 1) {
19897
- const abstractCounterValue = sharedAbstractCounters && Number.isFinite(sharedAbstractCounters[index]) ? Math.max(1, Math.round(sharedAbstractCounters[index])) : void 0;
20083
+ const abstractCounterValue = sharedAbstractCounters && Number.isFinite(sharedAbstractCounters[index]) ? Math.max(0, Math.round(sharedAbstractCounters[index])) : void 0;
19898
20084
  if (abstractCounterValue !== void 0) {
19899
20085
  const previousCounter = counters[index];
19900
- if (!Number.isFinite(previousCounter) || Math.max(1, Math.round(previousCounter)) !== abstractCounterValue) {
20086
+ if ((!Number.isFinite(previousCounter) || Math.max(0, Math.round(previousCounter)) !== abstractCounterValue) && index <= currentRestartCeiling) {
19901
20087
  parentCountersChanged = true;
19902
20088
  }
19903
20089
  counters[index] = abstractCounterValue;
@@ -19910,7 +20096,9 @@ function buildParagraphNumberingLabels(model) {
19910
20096
  const currentValue = parentCountersChanged ? void 0 : counters[ilvl];
19911
20097
  counters[ilvl] = Number.isFinite(currentValue) ? currentValue + 1 : numberingStartValue(numbering, numId, ilvl);
19912
20098
  for (let index = ilvl + 1; index < counters.length; index += 1) {
19913
- counters[index] = void 0;
20099
+ if (ilvl <= restartTriggerCeiling(index)) {
20100
+ counters[index] = void 0;
20101
+ }
19914
20102
  }
19915
20103
  countersByNumId.set(numId, counters);
19916
20104
  if (sharedAbstractCounters && Number.isFinite(abstractNumId)) {
@@ -19918,7 +20106,9 @@ function buildParagraphNumberingLabels(model) {
19918
20106
  sharedAbstractCounters[index] = counters[index];
19919
20107
  }
19920
20108
  for (let index = ilvl + 1; index < sharedAbstractCounters.length; index += 1) {
19921
- sharedAbstractCounters[index] = void 0;
20109
+ if (ilvl <= restartTriggerCeiling(index)) {
20110
+ sharedAbstractCounters[index] = void 0;
20111
+ }
19922
20112
  }
19923
20113
  abstractCountersByAbstractNumId.set(
19924
20114
  abstractNumId,
@@ -19928,12 +20118,8 @@ function buildParagraphNumberingLabels(model) {
19928
20118
  const label = template.replaceAll(/%(\d+)/g, (_, rawLevel) => {
19929
20119
  const levelIndex = Math.max(0, Number(rawLevel) - 1);
19930
20120
  const levelValue = counters[levelIndex];
19931
- const safeValue = Number.isFinite(levelValue) ? Math.max(1, Math.round(levelValue)) : numberingStartValue(numbering, numId, levelIndex);
19932
- const levelFormat = findNumberingLevelDefinition(
19933
- numbering,
19934
- numId,
19935
- levelIndex
19936
- )?.format;
20121
+ const safeValue = Number.isFinite(levelValue) ? Math.max(0, Math.round(levelValue)) : numberingStartValue(numbering, numId, levelIndex);
20122
+ const levelFormat = level?.isLgl ? "decimal" : findNumberingLevelDefinition(numbering, numId, levelIndex)?.format;
19937
20123
  return formatNumberingCounter(levelFormat, safeValue);
19938
20124
  });
19939
20125
  if (!label || label.trim().length === 0) {
@@ -28272,6 +28458,7 @@ var DocxDetachedThumbnailSurfaceRenderer = class {
28272
28458
  root;
28273
28459
  activePageIndex;
28274
28460
  activeRenderKey;
28461
+ rootGeneration = 0;
28275
28462
  async renderPageSurface(params) {
28276
28463
  if (typeof document === "undefined" || typeof window === "undefined") {
28277
28464
  return void 0;
@@ -28297,16 +28484,24 @@ var DocxDetachedThumbnailSurfaceRenderer = class {
28297
28484
  return this.waitForPageSurface(registry, pageIndex);
28298
28485
  }
28299
28486
  clear() {
28300
- if (this.root) {
28301
- this.root.unmount();
28302
- this.root = void 0;
28303
- }
28304
- if (this.host?.parentNode) {
28305
- this.host.parentNode.removeChild(this.host);
28306
- }
28487
+ const root = this.root;
28488
+ const host = this.host;
28489
+ this.rootGeneration += 1;
28490
+ this.root = void 0;
28307
28491
  this.host = void 0;
28308
28492
  this.activePageIndex = void 0;
28309
28493
  this.activeRenderKey = void 0;
28494
+ const dispose = () => {
28495
+ root?.unmount();
28496
+ if (host?.parentNode) {
28497
+ host.parentNode.removeChild(host);
28498
+ }
28499
+ };
28500
+ if (root && typeof window !== "undefined") {
28501
+ window.setTimeout(dispose, 0);
28502
+ } else {
28503
+ dispose();
28504
+ }
28310
28505
  }
28311
28506
  async ensureRoot() {
28312
28507
  if (this.root) {
@@ -28315,6 +28510,7 @@ var DocxDetachedThumbnailSurfaceRenderer = class {
28315
28510
  if (typeof document === "undefined") {
28316
28511
  return;
28317
28512
  }
28513
+ const generation = this.rootGeneration;
28318
28514
  const host = document.createElement("div");
28319
28515
  host.setAttribute("data-docx-thumbnail-detached-renderer", "true");
28320
28516
  Object.assign(host.style, {
@@ -28331,6 +28527,12 @@ var DocxDetachedThumbnailSurfaceRenderer = class {
28331
28527
  document.body.appendChild(host);
28332
28528
  this.host = host;
28333
28529
  const { createRoot } = await import("react-dom/client");
28530
+ if (generation !== this.rootGeneration) {
28531
+ if (host.parentNode) {
28532
+ host.parentNode.removeChild(host);
28533
+ }
28534
+ return;
28535
+ }
28334
28536
  this.root = createRoot(host);
28335
28537
  }
28336
28538
  async waitForPageSurface(registry, pageIndex) {