@superdoc-dev/mcp 0.3.0-next.54 → 0.3.0-next.56

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.
Files changed (2) hide show
  1. package/dist/index.js +735 -161
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -51837,7 +51837,7 @@ var init_remark_gfm_BhnWr3yf_es = __esm(() => {
51837
51837
  emptyOptions2 = {};
51838
51838
  });
51839
51839
 
51840
- // ../../packages/superdoc/dist/chunks/SuperConverter-DFi0X147.es.js
51840
+ // ../../packages/superdoc/dist/chunks/SuperConverter-ing-1fvK.es.js
51841
51841
  function getExtensionConfigField(extension$1, field, context = { name: "" }) {
51842
51842
  const fieldValue = extension$1.config[field];
51843
51843
  if (typeof fieldValue === "function")
@@ -82193,6 +82193,9 @@ function extractSectionType(elements) {
82193
82193
  return val;
82194
82194
  return "nextPage";
82195
82195
  }
82196
+ function extractSectionTypeIsExplicit(elements) {
82197
+ return elements.some((el) => el?.name === "w:type");
82198
+ }
82196
82199
  function extractPageSizeAndOrientation(elements) {
82197
82200
  const pgSz = elements.find((el) => el?.name === "w:pgSz");
82198
82201
  if (!pgSz?.attributes)
@@ -82303,6 +82306,7 @@ function extractSectionData(para) {
82303
82306
  footerPx
82304
82307
  };
82305
82308
  const type = extractSectionType(sectPrElements);
82309
+ const typeIsExplicit = extractSectionTypeIsExplicit(sectPrElements);
82306
82310
  const { pageSizePx, orientation } = extractPageSizeAndOrientation(sectPrElements);
82307
82311
  const titlePg = sectPrElements.some((el) => el?.name === "w:titlePg");
82308
82312
  const fallbackMargins = extractFallbackMargins(sectPrElements, headerPx, footerPx);
@@ -82322,6 +82326,7 @@ function extractSectionData(para) {
82322
82326
  bottomPx,
82323
82327
  leftPx,
82324
82328
  type,
82329
+ typeIsExplicit,
82325
82330
  pageSizePx,
82326
82331
  orientation,
82327
82332
  columnsPx,
@@ -82365,6 +82370,7 @@ function createSectionBreakBlock(section, blockIdGen, extraAttrs) {
82365
82370
  attrs: {
82366
82371
  source: "sectPr",
82367
82372
  sectionIndex: section.sectionIndex,
82373
+ ...section.typeIsExplicit ? { typeIsExplicit: true } : {},
82368
82374
  ...extraAttrs
82369
82375
  },
82370
82376
  ...section.pageSize && { pageSize: section.pageSize },
@@ -82390,6 +82396,21 @@ function shouldRequirePageBoundary(current, next) {
82390
82396
  function hasIntrinsicBoundarySignals(_) {
82391
82397
  return false;
82392
82398
  }
82399
+ function maybeEmitNextSectionBreakForNode(args) {
82400
+ const { sectionState, nextBlockId, pushBlock } = args;
82401
+ if (sectionState.ranges.length === 0)
82402
+ return;
82403
+ if (sectionState.currentSectionIndex >= sectionState.ranges.length - 1)
82404
+ return;
82405
+ const nextSection = sectionState.ranges[sectionState.currentSectionIndex + 1];
82406
+ if (!nextSection)
82407
+ return;
82408
+ if (sectionState.currentNodeIndex !== nextSection.startNodeIndex)
82409
+ return;
82410
+ const currentSection = sectionState.ranges[sectionState.currentSectionIndex];
82411
+ pushBlock(createSectionBreakBlock(nextSection, nextBlockId, shouldRequirePageBoundary(currentSection, nextSection) || hasIntrinsicBoundarySignals(nextSection) ? { requirePageBoundary: true } : undefined));
82412
+ sectionState.currentSectionIndex++;
82413
+ }
82393
82414
  function emitPendingSectionBreakForParagraph(args) {
82394
82415
  const { sectionState, nextBlockId, blocks, recordBlockKind } = args;
82395
82416
  if (!sectionState || sectionState.ranges.length === 0)
@@ -82423,6 +82444,7 @@ function shouldIgnoreSectionBreak(paragraph2, index2, total, hasBodySectPr) {
82423
82444
  function findParagraphsWithSectPr(doc$2) {
82424
82445
  const paragraphs = [];
82425
82446
  let paragraphIndex = 0;
82447
+ let nodeIndex = 0;
82426
82448
  const getNodeChildren = (node2) => {
82427
82449
  if (Array.isArray(node2.content))
82428
82450
  return node2.content;
@@ -82436,30 +82458,35 @@ function findParagraphsWithSectPr(doc$2) {
82436
82458
  }
82437
82459
  return [];
82438
82460
  };
82439
- const visitNode = (node2) => {
82461
+ const visitNode = (node2, outerNodeIndex) => {
82440
82462
  if (node2.type === "paragraph") {
82441
82463
  if (hasSectPr(node2))
82442
82464
  paragraphs.push({
82443
82465
  index: paragraphIndex,
82466
+ nodeIndex: outerNodeIndex,
82444
82467
  node: node2
82445
82468
  });
82446
82469
  paragraphIndex++;
82447
82470
  return;
82448
82471
  }
82449
82472
  if (node2.type === "index" || node2.type === "bibliography" || node2.type === "tableOfAuthorities" || node2.type === "documentPartObject" || node2.type === "tableOfContents")
82450
- getNodeChildren(node2).forEach(visitNode);
82473
+ getNodeChildren(node2).forEach((child) => visitNode(child, outerNodeIndex));
82451
82474
  };
82452
82475
  if (doc$2.content)
82453
- for (const node2 of doc$2.content)
82454
- visitNode(node2);
82476
+ for (const node2 of doc$2.content) {
82477
+ visitNode(node2, nodeIndex);
82478
+ nodeIndex++;
82479
+ }
82455
82480
  return {
82456
82481
  paragraphs,
82457
- totalCount: paragraphIndex
82482
+ totalCount: paragraphIndex,
82483
+ totalNodeCount: nodeIndex
82458
82484
  };
82459
82485
  }
82460
82486
  function buildSectionRangesFromParagraphs(paragraphs, hasBodySectPr) {
82461
82487
  const ranges = [];
82462
82488
  let currentStart = 0;
82489
+ let currentStartNode = 0;
82463
82490
  paragraphs.forEach((item, idx) => {
82464
82491
  if (shouldIgnoreSectionBreak(item.node, idx, paragraphs.length, hasBodySectPr))
82465
82492
  return;
@@ -82470,6 +82497,8 @@ function buildSectionRangesFromParagraphs(paragraphs, hasBodySectPr) {
82470
82497
  const hasAnyMargin = sectionData.headerPx != null || sectionData.footerPx != null || sectionData.topPx != null || sectionData.rightPx != null || sectionData.bottomPx != null || sectionData.leftPx != null;
82471
82498
  const range = {
82472
82499
  sectionIndex: idx,
82500
+ startNodeIndex: currentStartNode,
82501
+ endNodeIndex: item.nodeIndex,
82473
82502
  startParagraphIndex: currentStart,
82474
82503
  endParagraphIndex: item.index,
82475
82504
  sectPr,
@@ -82485,6 +82514,7 @@ function buildSectionRangesFromParagraphs(paragraphs, hasBodySectPr) {
82485
82514
  orientation: sectionData.orientation ?? null,
82486
82515
  columns: sectionData.columnsPx ?? null,
82487
82516
  type: sectionData.type ?? DEFAULT_PARAGRAPH_SECTION_TYPE,
82517
+ typeIsExplicit: sectionData.typeIsExplicit ?? false,
82488
82518
  titlePg: sectionData.titlePg ?? false,
82489
82519
  headerRefs: sectionData.headerRefs,
82490
82520
  footerRefs: sectionData.footerRefs,
@@ -82493,6 +82523,7 @@ function buildSectionRangesFromParagraphs(paragraphs, hasBodySectPr) {
82493
82523
  };
82494
82524
  ranges.push(range);
82495
82525
  currentStart = item.index + 1;
82526
+ currentStartNode = item.nodeIndex + 1;
82496
82527
  });
82497
82528
  return ranges;
82498
82529
  }
@@ -82513,7 +82544,7 @@ function publishSectionMetadata(sectionRanges, options) {
82513
82544
  });
82514
82545
  });
82515
82546
  }
82516
- function createFinalSectionFromBodySectPr(bodySectPr, currentStart, totalParagraphs, sectionIndex) {
82547
+ function createFinalSectionFromBodySectPr(bodySectPr, currentStart, totalParagraphs, sectionIndex, nodeBounds) {
82517
82548
  const clampedStart = Math.max(0, Math.min(currentStart, Math.max(totalParagraphs - 1, 0)));
82518
82549
  const bodySectionData = extractSectionData({
82519
82550
  type: "paragraph",
@@ -82522,8 +82553,12 @@ function createFinalSectionFromBodySectPr(bodySectPr, currentStart, totalParagra
82522
82553
  if (!bodySectionData)
82523
82554
  return null;
82524
82555
  const hasAnyMargin = bodySectionData.headerPx != null || bodySectionData.footerPx != null || bodySectionData.topPx != null || bodySectionData.rightPx != null || bodySectionData.bottomPx != null || bodySectionData.leftPx != null;
82556
+ const totalNodes = nodeBounds?.totalNodeCount ?? totalParagraphs;
82557
+ const startNodeIndex = nodeBounds ? Math.max(0, Math.min(nodeBounds.startNodeIndex, Math.max(totalNodes - 1, 0))) : clampedStart;
82525
82558
  return {
82526
82559
  sectionIndex,
82560
+ startNodeIndex,
82561
+ endNodeIndex: Math.max(startNodeIndex, totalNodes - 1),
82527
82562
  startParagraphIndex: clampedStart,
82528
82563
  endParagraphIndex: totalParagraphs - 1,
82529
82564
  sectPr: bodySectPr,
@@ -82539,6 +82574,7 @@ function createFinalSectionFromBodySectPr(bodySectPr, currentStart, totalParagra
82539
82574
  orientation: bodySectionData.orientation ?? null,
82540
82575
  columns: bodySectionData.columnsPx ?? null,
82541
82576
  type: bodySectionData.type ?? DEFAULT_BODY_SECTION_TYPE,
82577
+ typeIsExplicit: bodySectionData.typeIsExplicit ?? false,
82542
82578
  titlePg: bodySectionData.titlePg ?? false,
82543
82579
  headerRefs: bodySectionData.headerRefs,
82544
82580
  footerRefs: bodySectionData.footerRefs,
@@ -82546,9 +82582,13 @@ function createFinalSectionFromBodySectPr(bodySectPr, currentStart, totalParagra
82546
82582
  vAlign: bodySectionData.vAlign
82547
82583
  };
82548
82584
  }
82549
- function createDefaultFinalSection(currentStart, totalParagraphs, sectionIndex) {
82585
+ function createDefaultFinalSection(currentStart, totalParagraphs, sectionIndex, nodeBounds) {
82586
+ const totalNodes = nodeBounds?.totalNodeCount ?? totalParagraphs;
82587
+ const startNodeIndex = nodeBounds?.startNodeIndex ?? currentStart;
82550
82588
  return {
82551
82589
  sectionIndex,
82590
+ startNodeIndex,
82591
+ endNodeIndex: Math.max(startNodeIndex, totalNodes - 1),
82552
82592
  startParagraphIndex: currentStart,
82553
82593
  endParagraphIndex: totalParagraphs - 1,
82554
82594
  sectPr: null,
@@ -82557,21 +82597,30 @@ function createDefaultFinalSection(currentStart, totalParagraphs, sectionIndex)
82557
82597
  orientation: null,
82558
82598
  columns: null,
82559
82599
  type: DEFAULT_BODY_SECTION_TYPE,
82600
+ typeIsExplicit: false,
82560
82601
  titlePg: false,
82561
82602
  headerRefs: undefined,
82562
82603
  footerRefs: undefined
82563
82604
  };
82564
82605
  }
82565
82606
  function analyzeSectionRanges(doc$2, bodySectPr) {
82566
- const { paragraphs, totalCount } = findParagraphsWithSectPr(doc$2);
82607
+ const { paragraphs, totalCount, totalNodeCount } = findParagraphsWithSectPr(doc$2);
82567
82608
  const ranges = buildSectionRangesFromParagraphs(paragraphs, Boolean(bodySectPr));
82568
- const currentStart = ranges.length > 0 ? ranges[ranges.length - 1].endParagraphIndex + 1 : 0;
82609
+ const last = ranges[ranges.length - 1];
82610
+ const currentStart = last ? last.endParagraphIndex + 1 : 0;
82611
+ const currentStartNode = last ? last.endNodeIndex + 1 : 0;
82569
82612
  if (isSectPrElement$1(bodySectPr)) {
82570
- const finalSection = createFinalSectionFromBodySectPr(bodySectPr, Math.min(currentStart, totalCount), totalCount, ranges.length);
82613
+ const finalSection = createFinalSectionFromBodySectPr(bodySectPr, Math.min(currentStart, totalCount), totalCount, ranges.length, {
82614
+ startNodeIndex: Math.min(currentStartNode, totalNodeCount),
82615
+ totalNodeCount
82616
+ });
82571
82617
  if (finalSection)
82572
82618
  ranges.push(finalSection);
82573
82619
  } else if (ranges.length > 0) {
82574
- const fallbackFinal = createDefaultFinalSection(Math.min(currentStart, totalCount), totalCount, ranges.length);
82620
+ const fallbackFinal = createDefaultFinalSection(Math.min(currentStart, totalCount), totalCount, ranges.length, {
82621
+ startNodeIndex: Math.min(currentStartNode, totalNodeCount),
82622
+ totalNodeCount
82623
+ });
82575
82624
  if (fallbackFinal) {
82576
82625
  fallbackFinal.type = DEFAULT_PARAGRAPH_SECTION_TYPE;
82577
82626
  ranges.push(fallbackFinal);
@@ -84149,16 +84198,20 @@ function readOddEvenHeadersFlag(editor) {
84149
84198
  return settingsRoot.elements.some((entry) => entry.name === "w:evenAndOddHeaders");
84150
84199
  }
84151
84200
  function createSyntheticRange(bodySectPr, paragraphCount) {
84201
+ const lastIndex = Math.max(paragraphCount - 1, 0);
84152
84202
  return {
84153
84203
  sectionIndex: 0,
84204
+ startNodeIndex: 0,
84205
+ endNodeIndex: lastIndex,
84154
84206
  startParagraphIndex: 0,
84155
- endParagraphIndex: Math.max(paragraphCount - 1, 0),
84207
+ endParagraphIndex: lastIndex,
84156
84208
  sectPr: bodySectPr ?? null,
84157
84209
  margins: null,
84158
84210
  pageSize: null,
84159
84211
  orientation: null,
84160
84212
  columns: null,
84161
84213
  type: SectionType.CONTINUOUS,
84214
+ typeIsExplicit: false,
84162
84215
  titlePg: false,
84163
84216
  headerRefs: undefined,
84164
84217
  footerRefs: undefined,
@@ -104354,7 +104407,7 @@ var isRegExp = (value) => {
104354
104407
  state.kern = kernNode.attributes["w:val"];
104355
104408
  }
104356
104409
  }, SuperConverter;
104357
- var init_SuperConverter_DFi0X147_es = __esm(() => {
104410
+ var init_SuperConverter_ing_1fvK_es = __esm(() => {
104358
104411
  init_rolldown_runtime_Bg48TavK_es();
104359
104412
  init_jszip_C49i9kUs_es();
104360
104413
  init_xml_js_CqGKpaft_es();
@@ -141955,7 +142008,7 @@ var init_SuperConverter_DFi0X147_es = __esm(() => {
141955
142008
  };
141956
142009
  });
141957
142010
 
141958
- // ../../packages/superdoc/dist/chunks/create-headless-toolbar-C9nvGdZQ.es.js
142011
+ // ../../packages/superdoc/dist/chunks/create-headless-toolbar-B_1fvPL0.es.js
141959
142012
  function parseSizeUnit(val = "0") {
141960
142013
  const length = val.toString() || "0";
141961
142014
  const value = Number.parseFloat(length);
@@ -144591,8 +144644,8 @@ var CSS_DIMENSION_REGEX, DOM_SIZE_UNITS, Extension = class Extension2 {
144591
144644
  }
144592
144645
  };
144593
144646
  };
144594
- var init_create_headless_toolbar_C9nvGdZQ_es = __esm(() => {
144595
- init_SuperConverter_DFi0X147_es();
144647
+ var init_create_headless_toolbar_B_1fvPL0_es = __esm(() => {
144648
+ init_SuperConverter_ing_1fvK_es();
144596
144649
  init_constants_DrU4EASo_es();
144597
144650
  init_dist_B8HfvhaK_es();
144598
144651
  CSS_DIMENSION_REGEX = /[\d-.]+(\w+)$/;
@@ -198800,7 +198853,7 @@ var init_remark_gfm_eZN6yzWQ_es = __esm(() => {
198800
198853
  init_remark_gfm_BhnWr3yf_es();
198801
198854
  });
198802
198855
 
198803
- // ../../packages/superdoc/dist/chunks/src-JQdl170N.es.js
198856
+ // ../../packages/superdoc/dist/chunks/src-4bO9QmBD.es.js
198804
198857
  function deleteProps(obj, propOrProps) {
198805
198858
  const props = typeof propOrProps === "string" ? [propOrProps] : propOrProps;
198806
198859
  const removeNested = (target, pathParts, index2 = 0) => {
@@ -245238,7 +245291,8 @@ function toFlowBlocks(pmDoc, options) {
245238
245291
  sectionState: {
245239
245292
  ranges: sectionRanges,
245240
245293
  currentSectionIndex: 0,
245241
- currentParagraphIndex: 0
245294
+ currentParagraphIndex: 0,
245295
+ currentNodeIndex: 0
245242
245296
  },
245243
245297
  converters,
245244
245298
  themeColors,
@@ -245247,9 +245301,18 @@ function toFlowBlocks(pmDoc, options) {
245247
245301
  trackedListLastOrdinals: /* @__PURE__ */ new Map
245248
245302
  };
245249
245303
  doc$12.content.forEach((node2) => {
245304
+ maybeEmitNextSectionBreakForNode({
245305
+ sectionState: handlerContext.sectionState,
245306
+ nextBlockId,
245307
+ pushBlock: (block) => {
245308
+ blocks2.push(block);
245309
+ recordBlockKind(block.kind);
245310
+ }
245311
+ });
245250
245312
  const handler2 = nodeHandlers2[node2.type];
245251
245313
  if (handler2)
245252
245314
  handler2(node2, handlerContext);
245315
+ handlerContext.sectionState.currentNodeIndex++;
245253
245316
  });
245254
245317
  if (sectionRanges.length > 0) {
245255
245318
  const lastSectionIndex = sectionRanges.length - 1;
@@ -247737,6 +247800,167 @@ function computeDisplayPageNumber(pages, sections) {
247737
247800
  }
247738
247801
  return result;
247739
247802
  }
247803
+ function calculateBalancedColumnHeight(ctx$1, config3 = DEFAULT_BALANCING_CONFIG) {
247804
+ if (ctx$1.columnCount <= 1)
247805
+ return createSingleColumnResult(ctx$1);
247806
+ if (ctx$1.contentBlocks.length === 0)
247807
+ return {
247808
+ targetColumnHeight: 0,
247809
+ columnAssignments: /* @__PURE__ */ new Map,
247810
+ success: true,
247811
+ iterations: 0
247812
+ };
247813
+ const totalHeight = ctx$1.contentBlocks.reduce((sum, b$1) => sum + b$1.measuredHeight, 0);
247814
+ const maxBlockHeight = ctx$1.contentBlocks.reduce((m$1, b$1) => Math.max(m$1, b$1.measuredHeight), 0);
247815
+ if (totalHeight < config3.minColumnHeight * ctx$1.columnCount)
247816
+ return createSingleColumnResult(ctx$1);
247817
+ let lo = Math.max(maxBlockHeight, config3.minColumnHeight);
247818
+ let hi = Math.min(totalHeight, ctx$1.availableHeight);
247819
+ if (lo > hi)
247820
+ lo = hi;
247821
+ let bestResult = null;
247822
+ let bestH = hi;
247823
+ let iterations = 0;
247824
+ while (lo <= hi) {
247825
+ iterations++;
247826
+ const mid = Math.floor((lo + hi) / 2);
247827
+ const sim = simulateBalancedLayout(ctx$1, mid, config3);
247828
+ const maxCol = Math.max(...sim.columnHeights);
247829
+ if (sim.assignments.size === ctx$1.contentBlocks.length && maxCol <= mid) {
247830
+ bestResult = sim;
247831
+ bestH = mid;
247832
+ hi = mid - 1;
247833
+ } else
247834
+ lo = mid + 1;
247835
+ if (iterations >= config3.maxIterations)
247836
+ break;
247837
+ }
247838
+ if (bestResult)
247839
+ return {
247840
+ targetColumnHeight: bestH,
247841
+ columnAssignments: bestResult.assignments,
247842
+ success: true,
247843
+ iterations,
247844
+ blockBreakPoints: bestResult.breakPoints.size > 0 ? bestResult.breakPoints : undefined
247845
+ };
247846
+ return createSequentialResult(ctx$1);
247847
+ }
247848
+ function simulateBalancedLayout(ctx$1, targetHeight, config3) {
247849
+ const assignments = /* @__PURE__ */ new Map;
247850
+ const breakPoints = /* @__PURE__ */ new Map;
247851
+ const columnHeights = new Array(ctx$1.columnCount).fill(0);
247852
+ let currentColumn = 0;
247853
+ for (let i4 = 0;i4 < ctx$1.contentBlocks.length; i4++) {
247854
+ const block = ctx$1.contentBlocks[i4];
247855
+ const nextBlock = ctx$1.contentBlocks[i4 + 1];
247856
+ if (columnHeights[currentColumn] + block.measuredHeight > targetHeight && currentColumn < ctx$1.columnCount - 1) {
247857
+ if (block.keepWithNext && nextBlock) {
247858
+ const combinedHeight = block.measuredHeight + nextBlock.measuredHeight;
247859
+ if (columnHeights[currentColumn] + combinedHeight <= targetHeight) {
247860
+ assignments.set(block.blockId, currentColumn);
247861
+ columnHeights[currentColumn] += block.measuredHeight;
247862
+ continue;
247863
+ }
247864
+ }
247865
+ if (block.canBreak && block.lineHeights && block.lineHeights.length > 1) {
247866
+ const breakPoint = calculateParagraphBreakPoint(block, targetHeight - columnHeights[currentColumn], block.orphanLines ?? 1, block.widowLines ?? 1);
247867
+ if (breakPoint.canBreak && breakPoint.breakAfterLine >= 0) {
247868
+ const heightBefore = block.lineHeights.slice(0, breakPoint.breakAfterLine + 1).reduce((sum, h$2) => sum + h$2, 0);
247869
+ const heightAfter = block.measuredHeight - heightBefore;
247870
+ breakPoints.set(block.blockId, {
247871
+ blockId: block.blockId,
247872
+ breakAfterLine: breakPoint.breakAfterLine,
247873
+ heightBeforeBreak: heightBefore,
247874
+ heightAfterBreak: heightAfter
247875
+ });
247876
+ assignments.set(block.blockId, currentColumn);
247877
+ columnHeights[currentColumn] += heightBefore;
247878
+ currentColumn++;
247879
+ columnHeights[currentColumn] += heightAfter;
247880
+ continue;
247881
+ }
247882
+ }
247883
+ currentColumn++;
247884
+ }
247885
+ assignments.set(block.blockId, currentColumn);
247886
+ columnHeights[currentColumn] += block.measuredHeight;
247887
+ }
247888
+ return {
247889
+ assignments,
247890
+ columnHeights,
247891
+ hasOverflow: columnHeights.some((h$2) => h$2 > ctx$1.availableHeight),
247892
+ breakPoints
247893
+ };
247894
+ }
247895
+ function calculateParagraphBreakPoint(block, availableHeight, orphanLines, widowLines) {
247896
+ if (!block.lineHeights || block.lineHeights.length === 0)
247897
+ return {
247898
+ breakAfterLine: -1,
247899
+ canBreak: false
247900
+ };
247901
+ const lines = block.lineHeights;
247902
+ let heightSoFar = 0;
247903
+ for (let i4 = 0;i4 < lines.length; i4++) {
247904
+ heightSoFar += lines[i4];
247905
+ if (heightSoFar > availableHeight) {
247906
+ const linesBeforeBreak = i4;
247907
+ const linesAfterBreak = lines.length - i4;
247908
+ if (linesAfterBreak < widowLines) {
247909
+ const adjustedBreak = Math.max(0, i4 - (widowLines - linesAfterBreak));
247910
+ if (adjustedBreak < orphanLines)
247911
+ return {
247912
+ breakAfterLine: -1,
247913
+ canBreak: false
247914
+ };
247915
+ return {
247916
+ breakAfterLine: adjustedBreak - 1,
247917
+ canBreak: true
247918
+ };
247919
+ }
247920
+ if (linesBeforeBreak < orphanLines)
247921
+ return {
247922
+ breakAfterLine: -1,
247923
+ canBreak: false
247924
+ };
247925
+ return {
247926
+ breakAfterLine: i4 - 1,
247927
+ canBreak: true
247928
+ };
247929
+ }
247930
+ }
247931
+ return {
247932
+ breakAfterLine: lines.length - 1,
247933
+ canBreak: true
247934
+ };
247935
+ }
247936
+ function createSingleColumnResult(ctx$1) {
247937
+ const assignments = /* @__PURE__ */ new Map;
247938
+ for (const block of ctx$1.contentBlocks)
247939
+ assignments.set(block.blockId, 0);
247940
+ return {
247941
+ targetColumnHeight: ctx$1.availableHeight,
247942
+ columnAssignments: assignments,
247943
+ success: true,
247944
+ iterations: 0
247945
+ };
247946
+ }
247947
+ function createSequentialResult(ctx$1) {
247948
+ const assignments = /* @__PURE__ */ new Map;
247949
+ const columnHeights = new Array(ctx$1.columnCount).fill(0);
247950
+ let currentColumn = 0;
247951
+ for (const block of ctx$1.contentBlocks) {
247952
+ if (columnHeights[currentColumn] + block.measuredHeight > ctx$1.availableHeight && currentColumn < ctx$1.columnCount - 1)
247953
+ currentColumn++;
247954
+ assignments.set(block.blockId, currentColumn);
247955
+ columnHeights[currentColumn] += block.measuredHeight;
247956
+ }
247957
+ return {
247958
+ targetColumnHeight: Math.max(...columnHeights),
247959
+ columnAssignments: assignments,
247960
+ success: false,
247961
+ iterations: 0
247962
+ };
247963
+ }
247740
247964
  function shouldSkipBalancing(ctx$1, config3 = DEFAULT_BALANCING_CONFIG) {
247741
247965
  if (!config3.enabled)
247742
247966
  return true;
@@ -247768,76 +247992,211 @@ function getFragmentHeight(fragment, measureMap) {
247768
247992
  return sum;
247769
247993
  }
247770
247994
  if (fragment.kind === "image" || fragment.kind === "drawing" || fragment.kind === "table") {
247771
- if (typeof fragment.height === "number")
247995
+ if (typeof fragment.height === "number" && fragment.height > 0)
247772
247996
  return fragment.height;
247773
247997
  const measure = measureMap.get(fragment.blockId);
247774
- if (measure && typeof measure.height === "number")
247775
- return measure.height;
247998
+ if (measure) {
247999
+ if (typeof measure.height === "number" && measure.height > 0)
248000
+ return measure.height;
248001
+ if (fragment.kind === "table" && typeof measure.totalHeight === "number")
248002
+ return measure.totalHeight;
248003
+ }
248004
+ if (typeof fragment.height === "number")
248005
+ return fragment.height;
247776
248006
  }
247777
248007
  return 0;
247778
248008
  }
247779
- function balancePageColumns(fragments, columns, margins, topMargin, availableHeight, measureMap) {
247780
- if (columns.count <= 1 || fragments.length === 0)
247781
- return;
247782
- const columnX = (columnIndex) => {
247783
- return margins.left + columnIndex * (columns.width + columns.gap);
247784
- };
247785
- const rowMap = /* @__PURE__ */ new Map;
247786
- fragments.forEach((fragment, idx) => {
247787
- const y$1 = Math.round(fragment.y);
247788
- if (!rowMap.has(y$1))
247789
- rowMap.set(y$1, []);
247790
- const height = getFragmentHeight(fragment, measureMap);
247791
- rowMap.get(y$1).push({
247792
- fragment,
247793
- height,
247794
- originalIndex: idx
247795
- });
247796
- });
247797
- const sortedRows = [...rowMap.entries()].sort((a2, b$1) => a2[0] - b$1[0]);
247798
- let totalHeight = 0;
247799
- const contentBlocks = [];
247800
- for (const [, rowFragments] of sortedRows) {
247801
- const maxHeight = Math.max(...rowFragments.map((f2) => f2.height));
247802
- totalHeight += maxHeight;
247803
- contentBlocks.push({
247804
- blockId: rowFragments[0]?.fragment.blockId ?? `row-${contentBlocks.length}`,
247805
- measuredHeight: maxHeight,
247806
- canBreak: false,
247807
- keepWithNext: false,
247808
- keepTogether: true
247809
- });
248009
+ function getBalancingHeight(fragment, measureMap, sectPrMarkerBlockIds) {
248010
+ if (fragment.kind === "para" && sectPrMarkerBlockIds && sectPrMarkerBlockIds.has(fragment.blockId))
248011
+ return 0;
248012
+ return getFragmentHeight(fragment, measureMap);
248013
+ }
248014
+ function balanceSectionOnPage(args$1) {
248015
+ const { sectionColumns, sectionHasExplicitColumnBreak, sectionIndex, blockSectionMap, fragments } = args$1;
248016
+ if (sectionColumns.count <= 1)
248017
+ return null;
248018
+ if (sectionHasExplicitColumnBreak)
248019
+ return null;
248020
+ if (sectionColumns.equalWidth === false && Array.isArray(sectionColumns.widths) && sectionColumns.widths.length > 0)
248021
+ return null;
248022
+ const sectionFragments = fragments.filter((f2) => blockSectionMap.get(f2.blockId) === sectionIndex);
248023
+ if (sectionFragments.length === 0)
248024
+ return null;
248025
+ const columnCount = sectionColumns.count;
248026
+ const columnGap = sectionColumns.gap;
248027
+ const columnWidth = sectionColumns.width ?? 0;
248028
+ if (columnWidth <= 0)
248029
+ return null;
248030
+ let sectionTopY = Number.POSITIVE_INFINITY;
248031
+ for (const f2 of sectionFragments)
248032
+ if (f2.y < sectionTopY)
248033
+ sectionTopY = f2.y;
248034
+ if (!Number.isFinite(sectionTopY))
248035
+ sectionTopY = args$1.topMargin;
248036
+ const remainingHeight = args$1.availableHeight - (sectionTopY - args$1.topMargin);
248037
+ if (remainingHeight <= 0)
248038
+ return null;
248039
+ let precedingHeightBeforeTable = 0;
248040
+ for (const f2 of sectionFragments) {
248041
+ if (f2.kind === "table")
248042
+ break;
248043
+ precedingHeightBeforeTable += getBalancingHeight(f2, args$1.measureMap, args$1.sectPrMarkerBlockIds);
247810
248044
  }
248045
+ const splitResult = splitDominantTableAtRowBoundary({
248046
+ sectionFragments,
248047
+ fragments,
248048
+ columnCount,
248049
+ measureMap: args$1.measureMap,
248050
+ sectPrMarkerBlockIds: args$1.sectPrMarkerBlockIds,
248051
+ precedingHeight: precedingHeightBeforeTable
248052
+ });
248053
+ const ordered = [...sectionFragments].sort((a2, b$1) => {
248054
+ if (a2.x !== b$1.x)
248055
+ return a2.x - b$1.x;
248056
+ return a2.y - b$1.y;
248057
+ });
248058
+ const contentBlocks = ordered.map((f2, i4) => ({
248059
+ blockId: `${f2.blockId}#${i4}`,
248060
+ measuredHeight: getBalancingHeight(f2, args$1.measureMap, args$1.sectPrMarkerBlockIds),
248061
+ canBreak: false,
248062
+ keepWithNext: false,
248063
+ keepTogether: true
248064
+ }));
247811
248065
  if (shouldSkipBalancing({
247812
- columnCount: columns.count,
247813
- columnWidth: columns.width,
247814
- columnGap: columns.gap,
247815
- availableHeight,
248066
+ columnCount,
248067
+ columnWidth,
248068
+ columnGap,
248069
+ availableHeight: remainingHeight,
247816
248070
  contentBlocks
247817
- }))
247818
- return;
247819
- const targetHeight = totalHeight / columns.count;
247820
- if (targetHeight < DEFAULT_BALANCING_CONFIG.minColumnHeight)
247821
- return;
247822
- let currentColumn = 0;
247823
- let currentColumnHeight = 0;
247824
- let currentY = topMargin;
247825
- for (const [, rowFragments] of sortedRows) {
247826
- const rowHeight = Math.max(...rowFragments.map((f2) => f2.height));
247827
- if (currentColumnHeight > 0 && currentColumnHeight + rowHeight >= targetHeight && currentColumn < columns.count - 1) {
247828
- currentColumn++;
247829
- currentColumnHeight = 0;
247830
- currentY = topMargin;
247831
- }
247832
- const colX = columnX(currentColumn);
247833
- for (const info of rowFragments) {
247834
- info.fragment.x = colX;
247835
- info.fragment.y = currentY;
247836
- info.fragment.width = columns.width;
248071
+ })) {
248072
+ splitResult?.rollback();
248073
+ return null;
248074
+ }
248075
+ const result = calculateBalancedColumnHeight({
248076
+ columnCount,
248077
+ columnWidth,
248078
+ columnGap,
248079
+ availableHeight: remainingHeight,
248080
+ contentBlocks
248081
+ }, DEFAULT_BALANCING_CONFIG);
248082
+ const columnX = (columnIndex) => args$1.margins.left + columnIndex * (columnWidth + columnGap);
248083
+ const colCursors = new Array(columnCount).fill(sectionTopY);
248084
+ let maxY = sectionTopY;
248085
+ for (let i4 = 0;i4 < ordered.length; i4++) {
248086
+ const f2 = ordered[i4];
248087
+ const block = contentBlocks[i4];
248088
+ const col = result.columnAssignments.get(block.blockId) ?? 0;
248089
+ f2.x = columnX(col);
248090
+ f2.y = colCursors[col];
248091
+ f2.width = columnWidth;
248092
+ colCursors[col] += block.measuredHeight;
248093
+ if (colCursors[col] > maxY)
248094
+ maxY = colCursors[col];
248095
+ }
248096
+ return { maxY };
248097
+ }
248098
+ function splitDominantTableAtRowBoundary(args$1) {
248099
+ const { sectionFragments, fragments, columnCount, measureMap, sectPrMarkerBlockIds } = args$1;
248100
+ const precedingHeight = Math.max(0, args$1.precedingHeight ?? 0);
248101
+ if (columnCount <= 1)
248102
+ return null;
248103
+ const tables = sectionFragments.filter((f2) => f2.kind === "table");
248104
+ if (tables.length !== 1)
248105
+ return null;
248106
+ const table2 = tables[0];
248107
+ const fromRow = table2.fromRow ?? 0;
248108
+ const toRow = table2.toRow ?? fromRow;
248109
+ if (toRow - fromRow < 2)
248110
+ return null;
248111
+ const measure = measureMap.get(table2.blockId);
248112
+ if (!measure || measure.kind !== "table" || !Array.isArray(measure.rows))
248113
+ return null;
248114
+ const totalSectionHeight = sectionFragments.reduce((sum, f2) => sum + getBalancingHeight(f2, measureMap, sectPrMarkerBlockIds), 0);
248115
+ if (totalSectionHeight <= 0)
248116
+ return null;
248117
+ const target = Math.max(1, totalSectionHeight / columnCount - precedingHeight);
248118
+ if (getBalancingHeight(table2, measureMap, sectPrMarkerBlockIds) <= target + 1)
248119
+ return null;
248120
+ let running = 0;
248121
+ let splitRow = fromRow + 1;
248122
+ for (let r$1 = fromRow;r$1 < toRow - 1; r$1++) {
248123
+ const h$2 = measure.rows[r$1]?.height ?? 0;
248124
+ if (running + h$2 >= target) {
248125
+ splitRow = r$1 + 1;
248126
+ break;
247837
248127
  }
247838
- currentColumnHeight += rowHeight;
247839
- currentY += rowHeight;
248128
+ running += h$2;
248129
+ splitRow = r$1 + 2;
247840
248130
  }
248131
+ if (splitRow <= fromRow || splitRow >= toRow)
248132
+ return null;
248133
+ const firstHalfRows = measure.rows.slice(fromRow, splitRow);
248134
+ const secondHalfRows = measure.rows.slice(splitRow, toRow);
248135
+ const firstHalfHeight = firstHalfRows.reduce((s2, r$1) => s2 + (r$1.height ?? 0), 0);
248136
+ const secondHalfHeight = secondHalfRows.reduce((s2, r$1) => s2 + (r$1.height ?? 0), 0);
248137
+ const makeRowBoundaries = (rows, startIndex) => {
248138
+ const out = [];
248139
+ let y$1 = 0.5;
248140
+ for (let i4 = 0;i4 < rows.length; i4++) {
248141
+ const h$2 = rows[i4].height ?? 0;
248142
+ out.push({
248143
+ index: startIndex + i4,
248144
+ y: y$1,
248145
+ height: h$2,
248146
+ minHeight: h$2,
248147
+ resizable: true
248148
+ });
248149
+ y$1 += h$2;
248150
+ }
248151
+ return out;
248152
+ };
248153
+ const originalMetadata = table2.metadata;
248154
+ const firstMetadata = originalMetadata ? {
248155
+ ...originalMetadata,
248156
+ rowBoundaries: makeRowBoundaries(firstHalfRows, 0)
248157
+ } : undefined;
248158
+ const secondMetadata = originalMetadata ? {
248159
+ ...originalMetadata,
248160
+ rowBoundaries: makeRowBoundaries(secondHalfRows, 0)
248161
+ } : undefined;
248162
+ const originalToRow = table2.toRow;
248163
+ const originalHeight = table2.height;
248164
+ const originalContinuesOnNext = table2.continuesOnNext ?? false;
248165
+ table2.toRow = splitRow;
248166
+ table2.height = firstHalfHeight;
248167
+ table2.continuesOnNext = true;
248168
+ if (firstMetadata)
248169
+ table2.metadata = firstMetadata;
248170
+ const secondHalf = {
248171
+ ...table2,
248172
+ fromRow: splitRow,
248173
+ toRow,
248174
+ height: secondHalfHeight,
248175
+ continuesFromPrev: true,
248176
+ continuesOnNext: originalContinuesOnNext,
248177
+ metadata: secondMetadata ?? table2.metadata
248178
+ };
248179
+ const fragIdx = fragments.indexOf(table2);
248180
+ if (fragIdx >= 0)
248181
+ fragments.splice(fragIdx + 1, 0, secondHalf);
248182
+ const sectIdx = sectionFragments.indexOf(table2);
248183
+ if (sectIdx >= 0)
248184
+ sectionFragments.splice(sectIdx + 1, 0, secondHalf);
248185
+ return {
248186
+ applied: true,
248187
+ rollback: () => {
248188
+ table2.toRow = originalToRow;
248189
+ table2.height = originalHeight;
248190
+ table2.continuesOnNext = originalContinuesOnNext;
248191
+ table2.metadata = originalMetadata;
248192
+ const fIdx = fragments.indexOf(secondHalf);
248193
+ if (fIdx >= 0)
248194
+ fragments.splice(fIdx, 1);
248195
+ const sIdx = sectionFragments.indexOf(secondHalf);
248196
+ if (sIdx >= 0)
248197
+ sectionFragments.splice(sIdx, 1);
248198
+ }
248199
+ };
247841
248200
  }
247842
248201
  function resolvePageNumberTokens(layout, blocks2, measures, numberingCtx) {
247843
248202
  const affectedBlockIds = /* @__PURE__ */ new Set;
@@ -248666,6 +249025,48 @@ function layoutDocument(blocks2, measures, options = {}) {
248666
249025
  right: activeRightMargin
248667
249026
  }, activePageSize.w);
248668
249027
  };
249028
+ const balancingMeasureMap = /* @__PURE__ */ new Map;
249029
+ const blockSectionMap = /* @__PURE__ */ new Map;
249030
+ const sectionColumnsMap = /* @__PURE__ */ new Map;
249031
+ const sectionHasExplicitColumnBreak = /* @__PURE__ */ new Set;
249032
+ const sectionEndBreakType = /* @__PURE__ */ new Map;
249033
+ const sectionTypeIsExplicit = /* @__PURE__ */ new Map;
249034
+ let lastSectionIdx = null;
249035
+ const sectPrMarkerBlockIds = /* @__PURE__ */ new Set;
249036
+ let documentHasExplicitColumnBreak = false;
249037
+ let documentHasAnySectionBreak = false;
249038
+ const alreadyBalancedSections = /* @__PURE__ */ new Set;
249039
+ let currentSectionIdx = null;
249040
+ blocks2.forEach((block, idx) => {
249041
+ const measure = measures[idx];
249042
+ if (measure)
249043
+ balancingMeasureMap.set(block.id, measure);
249044
+ const blockWithAttrs = block;
249045
+ const attrSectionIdx = blockWithAttrs.attrs?.sectionIndex;
249046
+ if (block.kind === "sectionBreak") {
249047
+ documentHasAnySectionBreak = true;
249048
+ if (typeof attrSectionIdx === "number") {
249049
+ currentSectionIdx = attrSectionIdx;
249050
+ lastSectionIdx = attrSectionIdx;
249051
+ if (block.columns)
249052
+ sectionColumnsMap.set(attrSectionIdx, cloneColumnLayout(block.columns));
249053
+ if (typeof block.type === "string")
249054
+ sectionEndBreakType.set(attrSectionIdx, block.type);
249055
+ if (typeof blockWithAttrs.attrs?.typeIsExplicit === "boolean")
249056
+ sectionTypeIsExplicit.set(attrSectionIdx, blockWithAttrs.attrs.typeIsExplicit);
249057
+ }
249058
+ }
249059
+ if (currentSectionIdx !== null) {
249060
+ blockSectionMap.set(block.id, currentSectionIdx);
249061
+ if (block.kind === "columnBreak") {
249062
+ sectionHasExplicitColumnBreak.add(currentSectionIdx);
249063
+ documentHasExplicitColumnBreak = true;
249064
+ }
249065
+ } else if (block.kind === "columnBreak")
249066
+ documentHasExplicitColumnBreak = true;
249067
+ if (block.kind === "paragraph" && blockWithAttrs.attrs?.sectPrMarker === true)
249068
+ sectPrMarkerBlockIds.add(block.id);
249069
+ });
248669
249070
  const anchoredByParagraph = collectAnchoredDrawings(blocks2, measures);
248670
249071
  const anchoredTables = collectAnchoredTables(blocks2, measures);
248671
249072
  const anchoredTablesByParagraph = anchoredTables.byParagraph;
@@ -248888,7 +249289,47 @@ function layoutDocument(blocks2, measures, options = {}) {
248888
249289
  let state = paginator.ensurePage();
248889
249290
  const columnIndexBefore = state.columnIndex;
248890
249291
  const newColumns = updatedState.pendingColumns;
248891
- if (columnIndexBefore >= newColumns.count)
249292
+ let endingSectionIndex = null;
249293
+ for (let i4 = state.page.fragments.length - 1;i4 >= 0; i4--) {
249294
+ const mapped = blockSectionMap.get(state.page.fragments[i4].blockId);
249295
+ if (typeof mapped === "number" && mapped !== metadataIndex) {
249296
+ endingSectionIndex = mapped;
249297
+ break;
249298
+ }
249299
+ }
249300
+ const endingSectionColumns = endingSectionIndex !== null ? sectionColumnsMap.get(endingSectionIndex) : undefined;
249301
+ const willBalance = endingSectionIndex !== null && !!endingSectionColumns && endingSectionColumns.count > 1 && !sectionHasExplicitColumnBreak.has(endingSectionIndex);
249302
+ let balanceResult = null;
249303
+ if (willBalance) {
249304
+ const activeRegionTop = state.constraintBoundaries[state.constraintBoundaries.length - 1]?.y ?? activeTopMargin;
249305
+ const availableHeight = activePageSize.h - activeBottomMargin - activeRegionTop;
249306
+ const normalized = normalizeColumns(endingSectionColumns, activePageSize.w - (activeLeftMargin + activeRightMargin));
249307
+ balanceResult = balanceSectionOnPage({
249308
+ fragments: state.page.fragments,
249309
+ sectionIndex: endingSectionIndex,
249310
+ sectionColumns: {
249311
+ count: normalized.count,
249312
+ gap: normalized.gap,
249313
+ width: normalized.width,
249314
+ widths: endingSectionColumns.widths,
249315
+ equalWidth: endingSectionColumns.equalWidth
249316
+ },
249317
+ sectionHasExplicitColumnBreak: false,
249318
+ blockSectionMap,
249319
+ margins: { left: activeLeftMargin },
249320
+ topMargin: activeRegionTop,
249321
+ columnWidth: normalized.width,
249322
+ availableHeight,
249323
+ measureMap: balancingMeasureMap,
249324
+ sectPrMarkerBlockIds
249325
+ });
249326
+ if (balanceResult) {
249327
+ state.cursorY = balanceResult.maxY;
249328
+ state.maxCursorY = balanceResult.maxY;
249329
+ alreadyBalancedSections.add(endingSectionIndex);
249330
+ }
249331
+ }
249332
+ if (balanceResult === null && columnIndexBefore >= newColumns.count)
248892
249333
  state = paginator.startNewPage();
248893
249334
  startMidPageRegion(state, newColumns);
248894
249335
  }
@@ -249252,46 +249693,74 @@ function layoutDocument(blocks2, measures, options = {}) {
249252
249693
  for (const fragment of page.fragments)
249253
249694
  fragment.y += yOffset;
249254
249695
  }
249255
- if (activeColumns.count > 1) {
249256
- const contentWidth = pageSize.w - (activeLeftMargin + activeRightMargin);
249257
- const normalizedCols = normalizeColumns(activeColumns, contentWidth);
249258
- const measureMap = /* @__PURE__ */ new Map;
249259
- const blockSectionMap = /* @__PURE__ */ new Map;
249260
- const sectionColumnsMap = /* @__PURE__ */ new Map;
249261
- blocks2.forEach((block, idx) => {
249262
- const measure = measures[idx];
249263
- if (measure)
249264
- measureMap.set(block.id, measure);
249265
- const sectionIdx = block.attrs?.sectionIndex;
249266
- if (typeof sectionIdx === "number") {
249267
- blockSectionMap.set(block.id, sectionIdx);
249268
- if (block.kind === "sectionBreak" && block.columns)
249269
- sectionColumnsMap.set(sectionIdx, cloneColumnLayout(block.columns));
249270
- }
249271
- });
249272
- for (const page of pages)
249273
- if (page === pages[pages.length - 1] && page.fragments.length > 0) {
249274
- const finalSectionColumns = sectionColumnsMap.get(activeSectionIndex) ?? activeColumns;
249275
- if (finalSectionColumns?.equalWidth === false && Array.isArray(finalSectionColumns.widths) && finalSectionColumns.widths.length > 0)
249276
- continue;
249277
- if (new Set(page.fragments.map((f2) => Math.round(f2.x))).size > 1)
249278
- continue;
249279
- if (new Set(page.fragments.map((f2) => Math.round(f2.width))).size > 1)
249280
- continue;
249281
- const fragmentSections = /* @__PURE__ */ new Set;
249282
- for (const f2 of page.fragments) {
249283
- const section = blockSectionMap.get(f2.blockId);
249284
- if (section !== undefined)
249285
- fragmentSections.add(section);
249286
- }
249287
- const fragmentsToBalance = fragmentSections.size > 1 ? page.fragments.filter((f2) => {
249288
- return blockSectionMap.get(f2.blockId) === activeSectionIndex;
249289
- }) : page.fragments;
249290
- if (fragmentsToBalance.length > 0) {
249291
- const availableHeight = pageSize.h - activeBottomMargin - activeTopMargin;
249292
- balancePageColumns(fragmentsToBalance, normalizedCols, { left: activeLeftMargin }, activeTopMargin, availableHeight, measureMap);
249696
+ const FALLBACK_SECTION_IDX = -1;
249697
+ if (sectionColumnsMap.size === 0 && !documentHasAnySectionBreak && activeColumns.count > 1 && !documentHasExplicitColumnBreak) {
249698
+ sectionColumnsMap.set(FALLBACK_SECTION_IDX, cloneColumnLayout(activeColumns));
249699
+ for (const block of blocks2)
249700
+ blockSectionMap.set(block.id, FALLBACK_SECTION_IDX);
249701
+ }
249702
+ for (const [sectionIdx, sectionCols] of sectionColumnsMap) {
249703
+ if (sectionCols.count <= 1)
249704
+ continue;
249705
+ if (sectionHasExplicitColumnBreak.has(sectionIdx))
249706
+ continue;
249707
+ if (alreadyBalancedSections.has(sectionIdx))
249708
+ continue;
249709
+ if (sectionIdx !== FALLBACK_SECTION_IDX) {
249710
+ const endBreakType = sectionEndBreakType.get(sectionIdx);
249711
+ const typeIsExplicit = sectionTypeIsExplicit.get(sectionIdx) === true;
249712
+ const isLast = lastSectionIdx !== null && sectionIdx === lastSectionIdx;
249713
+ const bodyExplicitContinuousIdx = lastSectionIdx !== null && sectionTypeIsExplicit.get(lastSectionIdx) === true && sectionEndBreakType.get(lastSectionIdx) === "continuous" ? lastSectionIdx : null;
249714
+ const isExplicitNonContinuous = typeIsExplicit && (endBreakType === "nextPage" || endBreakType === "evenPage" || endBreakType === "oddPage");
249715
+ let sectionPagesCount = 0;
249716
+ for (const p$12 of pages)
249717
+ if (p$12.fragments.some((f2) => blockSectionMap.get(f2.blockId) === sectionIdx)) {
249718
+ sectionPagesCount += 1;
249719
+ if (sectionPagesCount > 1)
249720
+ break;
249293
249721
  }
249294
- }
249722
+ const isMultiPage = sectionPagesCount > 1;
249723
+ if (isMultiPage && !isLast)
249724
+ continue;
249725
+ const allowedByMidDocContinuous = endBreakType === "continuous" && !isLast;
249726
+ const allowedByBodyExplicitContinuous = bodyExplicitContinuousIdx !== null && sectionIdx === bodyExplicitContinuousIdx - 1 && !isExplicitNonContinuous;
249727
+ if (!allowedByMidDocContinuous && !allowedByBodyExplicitContinuous && !isMultiPage)
249728
+ continue;
249729
+ }
249730
+ let lastPageForSection = null;
249731
+ for (const p$12 of pages)
249732
+ if (p$12.fragments.some((f2) => blockSectionMap.get(f2.blockId) === sectionIdx))
249733
+ lastPageForSection = p$12;
249734
+ if (!lastPageForSection)
249735
+ continue;
249736
+ const sectionPageSize = lastPageForSection.size ?? pageSize;
249737
+ const sectionPageMargins = lastPageForSection.margins;
249738
+ const sectionLeftMargin = sectionPageMargins?.left ?? activeLeftMargin;
249739
+ const sectionRightMargin = sectionPageMargins?.right ?? activeRightMargin;
249740
+ const sectionTopMarginPx = sectionPageMargins?.top ?? activeTopMargin;
249741
+ const sectionBottomMargin = sectionPageMargins?.bottom ?? activeBottomMargin;
249742
+ const sectionContentWidth = sectionPageSize.w - (sectionLeftMargin + sectionRightMargin);
249743
+ const sectionAvailableHeight = sectionPageSize.h - sectionBottomMargin - sectionTopMarginPx;
249744
+ const normalized = normalizeColumns(sectionCols, sectionContentWidth);
249745
+ balanceSectionOnPage({
249746
+ fragments: lastPageForSection.fragments,
249747
+ sectionIndex: sectionIdx,
249748
+ sectionColumns: {
249749
+ count: normalized.count,
249750
+ gap: normalized.gap,
249751
+ width: normalized.width,
249752
+ widths: sectionCols.widths,
249753
+ equalWidth: sectionCols.equalWidth
249754
+ },
249755
+ sectionHasExplicitColumnBreak: false,
249756
+ blockSectionMap,
249757
+ margins: { left: sectionLeftMargin },
249758
+ topMargin: sectionTopMarginPx,
249759
+ columnWidth: normalized.width,
249760
+ availableHeight: sectionAvailableHeight,
249761
+ measureMap: balancingMeasureMap,
249762
+ sectPrMarkerBlockIds
249763
+ });
249295
249764
  }
249296
249765
  for (const state of states) {
249297
249766
  const boundaries = state.constraintBoundaries;
@@ -258893,12 +259362,43 @@ async function measureParagraphBlock(block, maxWidth) {
258893
259362
  if (lineToTrim.naturalWidth != null && typeof lineToTrim.naturalWidth === "number")
258894
259363
  lineToTrim.naturalWidth = roundValue(Math.max(0, lineToTrim.naturalWidth - delta));
258895
259364
  };
258896
- const getAlignmentStopForOrdinal = (ordinal) => {
259365
+ const tabSegmentInfo = /* @__PURE__ */ new Map;
259366
+ {
259367
+ let segmentTabRunIndices = [];
259368
+ const closeSegment = () => {
259369
+ const total = segmentTabRunIndices.length;
259370
+ segmentTabRunIndices.forEach((runIdx, ord) => {
259371
+ tabSegmentInfo.set(runIdx, {
259372
+ localOrdinal: ord,
259373
+ segmentTotal: total
259374
+ });
259375
+ });
259376
+ segmentTabRunIndices = [];
259377
+ };
259378
+ for (let i4 = 0;i4 < runsToProcess.length; i4++) {
259379
+ const r$1 = runsToProcess[i4];
259380
+ if (isLineBreakRun(r$1) || r$1.kind === "break" && r$1.breakType === "line")
259381
+ closeSegment();
259382
+ else if (isTabRun(r$1))
259383
+ segmentTabRunIndices.push(i4);
259384
+ }
259385
+ closeSegment();
259386
+ }
259387
+ const getAlignmentStopForOrdinal = (ordinal, runIdx) => {
258897
259388
  if (alignmentTabStopsPx.length === 0 || totalTabRuns === 0 || !Number.isFinite(ordinal))
258898
259389
  return null;
258899
- if (ordinal < 0 || ordinal >= totalTabRuns)
259390
+ let scopeOrdinal = ordinal;
259391
+ let scopeTotal = totalTabRuns;
259392
+ if (runIdx !== undefined) {
259393
+ const info = tabSegmentInfo.get(runIdx);
259394
+ if (info) {
259395
+ scopeOrdinal = info.localOrdinal;
259396
+ scopeTotal = info.segmentTotal;
259397
+ }
259398
+ }
259399
+ if (scopeOrdinal < 0 || scopeOrdinal >= scopeTotal)
258900
259400
  return null;
258901
- const remainingTabs = totalTabRuns - ordinal - 1;
259401
+ const remainingTabs = scopeTotal - scopeOrdinal - 1;
258902
259402
  const targetIndex = alignmentTabStopsPx.length - 1 - remainingTabs;
258903
259403
  if (targetIndex < 0 || targetIndex >= alignmentTabStopsPx.length)
258904
259404
  return null;
@@ -259012,16 +259512,16 @@ async function measureParagraphBlock(block, maxWidth) {
259012
259512
  let target;
259013
259513
  const resolvedTabIndex = typeof run2.tabIndex === "number" && Number.isFinite(run2.tabIndex) ? run2.tabIndex : sequentialTabIndex;
259014
259514
  sequentialTabIndex = Math.max(sequentialTabIndex, resolvedTabIndex + 1);
259015
- const forcedAlignment = getAlignmentStopForOrdinal(resolvedTabIndex);
259515
+ const greedy = getNextTabStopPx(absCurrentX, tabStops, tabStopCursor);
259516
+ const forcedAlignment = greedy.stop?.source === "default" ? getAlignmentStopForOrdinal(resolvedTabIndex, runIndex) : null;
259016
259517
  if (forcedAlignment && forcedAlignment.stop.pos > absCurrentX + TAB_EPSILON) {
259017
259518
  stop = forcedAlignment.stop;
259018
259519
  target = forcedAlignment.stop.pos;
259019
259520
  tabStopCursor = forcedAlignment.index + 1;
259020
259521
  } else {
259021
- const nextStop = getNextTabStopPx(absCurrentX, tabStops, tabStopCursor);
259022
- target = nextStop.target;
259023
- tabStopCursor = nextStop.nextIndex;
259024
- stop = nextStop.stop;
259522
+ target = greedy.target;
259523
+ tabStopCursor = greedy.nextIndex;
259524
+ stop = greedy.stop;
259025
259525
  }
259026
259526
  const maxAbsWidth = currentLine.maxWidth + effectiveIndent;
259027
259527
  const clampedTarget = Math.min(target, maxAbsWidth);
@@ -278949,7 +279449,16 @@ menclose::after {
278949
279449
  misses: this.#misses
278950
279450
  };
278951
279451
  }
278952
- }, MIN_BORDER_SIZE_PX2 = 0.5, MAX_BORDER_SIZE_PX2 = 100, borderSizeToPx = (size$1) => eighthPointsToPixels(size$1, { clamp: true }), clampPixelBorderWidth = (width) => Math.min(MAX_BORDER_SIZE_PX2, Math.max(MIN_BORDER_SIZE_PX2, width)), resolveBorderWidth = (size$1, unit) => {
279452
+ }, MIN_BORDER_SIZE_PX2 = 0.5, MAX_BORDER_SIZE_PX2 = 100, borderSizeToPx = (size$1) => {
279453
+ if (!isFiniteNumber(size$1))
279454
+ return;
279455
+ if (size$1 <= 0)
279456
+ return 0;
279457
+ const width = eighthPointsToPixels(size$1);
279458
+ if (width == null)
279459
+ return;
279460
+ return clampPixelBorderWidth(width);
279461
+ }, clampPixelBorderWidth = (width) => Math.min(MAX_BORDER_SIZE_PX2, Math.max(MIN_BORDER_SIZE_PX2, width)), resolveBorderWidth = (size$1, unit) => {
278953
279462
  if (unit === "eighthPoints")
278954
279463
  return borderSizeToPx(size$1);
278955
279464
  return clampPixelBorderWidth(size$1);
@@ -282235,12 +282744,43 @@ menclose::after {
282235
282744
  stop,
282236
282745
  index: index2
282237
282746
  })).filter(({ stop }) => stop.val === "end" || stop.val === "center" || stop.val === "decimal");
282238
- const getAlignmentStopForOrdinal = (ordinal) => {
282747
+ const tabSegmentInfo = /* @__PURE__ */ new Map;
282748
+ {
282749
+ let segmentTabRunIndices = [];
282750
+ const closeSegment = () => {
282751
+ const total = segmentTabRunIndices.length;
282752
+ segmentTabRunIndices.forEach((runIdx, ord) => {
282753
+ tabSegmentInfo.set(runIdx, {
282754
+ localOrdinal: ord,
282755
+ segmentTotal: total
282756
+ });
282757
+ });
282758
+ segmentTabRunIndices = [];
282759
+ };
282760
+ for (let i4 = 0;i4 < runs2.length; i4++) {
282761
+ const r$1 = runs2[i4];
282762
+ if (r$1.kind === "lineBreak" || r$1.kind === "break" && r$1.breakType === "line")
282763
+ closeSegment();
282764
+ else if (r$1.kind === "tab")
282765
+ segmentTabRunIndices.push(i4);
282766
+ }
282767
+ closeSegment();
282768
+ }
282769
+ const getAlignmentStopForOrdinal = (ordinal, runIdx) => {
282239
282770
  if (alignmentTabStopsPx.length === 0 || totalTabRuns === 0 || !Number.isFinite(ordinal))
282240
282771
  return null;
282241
- if (ordinal < 0 || ordinal >= totalTabRuns)
282772
+ let scopeOrdinal = ordinal;
282773
+ let scopeTotal = totalTabRuns;
282774
+ if (runIdx !== undefined) {
282775
+ const info = tabSegmentInfo.get(runIdx);
282776
+ if (info) {
282777
+ scopeOrdinal = info.localOrdinal;
282778
+ scopeTotal = info.segmentTotal;
282779
+ }
282780
+ }
282781
+ if (scopeOrdinal < 0 || scopeOrdinal >= scopeTotal)
282242
282782
  return null;
282243
- const remainingTabs = totalTabRuns - ordinal - 1;
282783
+ const remainingTabs = scopeTotal - scopeOrdinal - 1;
282244
282784
  const targetIndex = alignmentTabStopsPx.length - 1 - remainingTabs;
282245
282785
  if (targetIndex < 0 || targetIndex >= alignmentTabStopsPx.length)
282246
282786
  return null;
@@ -282265,21 +282805,21 @@ menclose::after {
282265
282805
  const leaders = [];
282266
282806
  const effectiveIndent = lineIndex === 0 ? indentLeft + rawFirstLineOffset : indentLeft;
282267
282807
  const maxAbsWidth = typeof line.maxWidth === "number" && Number.isFinite(line.maxWidth) ? line.maxWidth + effectiveIndent : Number.POSITIVE_INFINITY;
282268
- const applyTab = (startRunIndex, startChar, run2, tabOrdinal) => {
282808
+ const applyTab = (startRunIndex, startChar, run2, tabOrdinal, tabRunIdx) => {
282269
282809
  const originX = cursorX;
282270
282810
  const absCurrentX = cursorX + effectiveIndent;
282271
282811
  let stop;
282272
282812
  let target;
282273
- const forcedAlignment = typeof tabOrdinal === "number" && Number.isFinite(tabOrdinal) ? getAlignmentStopForOrdinal(tabOrdinal) : null;
282813
+ const greedy = getNextTabStopPx$1(absCurrentX, tabStops, tabStopCursor);
282814
+ const forcedAlignment = greedy.stop?.source === "default" && typeof tabOrdinal === "number" && Number.isFinite(tabOrdinal) ? getAlignmentStopForOrdinal(tabOrdinal, tabRunIdx) : null;
282274
282815
  if (forcedAlignment && forcedAlignment.stop.pos > absCurrentX + TAB_EPSILON$1) {
282275
282816
  stop = forcedAlignment.stop;
282276
282817
  target = forcedAlignment.stop.pos;
282277
282818
  tabStopCursor = forcedAlignment.index + 1;
282278
282819
  } else {
282279
- const next2 = getNextTabStopPx$1(absCurrentX, tabStops, tabStopCursor);
282280
- stop = next2.stop;
282281
- target = next2.target;
282282
- tabStopCursor = next2.nextIndex;
282820
+ stop = greedy.stop;
282821
+ target = greedy.target;
282822
+ tabStopCursor = greedy.nextIndex;
282283
282823
  }
282284
282824
  const clampedTarget = Number.isFinite(maxAbsWidth) ? Math.min(target, maxAbsWidth) : target;
282285
282825
  const relativeTarget = clampedTarget - effectiveIndent;
@@ -282324,7 +282864,7 @@ menclose::after {
282324
282864
  continue;
282325
282865
  if (run2.kind === "tab") {
282326
282866
  const ordinal = consumeTabOrdinal(run2.tabIndex);
282327
- applyTab(runIndex + 1, 0, run2, ordinal);
282867
+ applyTab(runIndex + 1, 0, run2, ordinal, runIndex);
282328
282868
  continue;
282329
282869
  }
282330
282870
  const text5 = runText(run2);
@@ -290063,12 +290603,12 @@ menclose::after {
290063
290603
  return;
290064
290604
  console.log(...args$1);
290065
290605
  }, HEADER_FOOTER_INIT_BUDGET_MS = 200, MAX_ZOOM_WARNING_THRESHOLD = 10, MAX_SELECTION_RECTS_PER_USER = 100, SEMANTIC_RESIZE_DEBOUNCE_MS = 120, MIN_SEMANTIC_CONTENT_WIDTH_PX = 1, GLOBAL_PERFORMANCE, PresentationEditor, ICONS, TEXTS, tableActionsOptions;
290066
- var init_src_JQdl170N_es = __esm(() => {
290606
+ var init_src_4bO9QmBD_es = __esm(() => {
290067
290607
  init_rolldown_runtime_Bg48TavK_es();
290068
- init_SuperConverter_DFi0X147_es();
290608
+ init_SuperConverter_ing_1fvK_es();
290069
290609
  init_jszip_C49i9kUs_es();
290070
290610
  init_uuid_qzgm05fK_es();
290071
- init_create_headless_toolbar_C9nvGdZQ_es();
290611
+ init_create_headless_toolbar_B_1fvPL0_es();
290072
290612
  init_constants_DrU4EASo_es();
290073
290613
  init_dist_B8HfvhaK_es();
290074
290614
  init_unified_Dsuw2be5_es();
@@ -317625,7 +318165,10 @@ function print() { __p += __j.call(arguments, '') }
317625
318165
  const separatorPositions = this.getColumnSeparatorPositions(columns, leftMargin, contentWidth);
317626
318166
  if (separatorPositions.length === 0)
317627
318167
  continue;
318168
+ const fragmentsInRegion = page.fragments.filter((f2) => f2.y >= yStart - 0.5 && f2.y < yEnd + 0.5);
317628
318169
  for (const separatorX of separatorPositions) {
318170
+ if (!fragmentsInRegion.some((f2) => f2.x >= separatorX))
318171
+ continue;
317629
318172
  const separatorEl = this.doc.createElement("div");
317630
318173
  separatorEl.dataset.superdocColumnSeparator = "true";
317631
318174
  separatorEl.style.position = "absolute";
@@ -327474,11 +328017,11 @@ function print() { __p += __j.call(arguments, '') }
327474
328017
  ];
327475
328018
  });
327476
328019
 
327477
- // ../../packages/superdoc/dist/chunks/create-super-doc-ui-_B0b9R4K.es.js
328020
+ // ../../packages/superdoc/dist/chunks/create-super-doc-ui-CPZeYqzS.es.js
327478
328021
  var MOD_ALIASES, ALT_ALIASES, CTRL_ALIASES, SHIFT_ALIASES, BUILTIN_CONTEXT_MENU_GROUPS, BUILTIN_GROUP_ORDER, RESERVED_PROXY_PROPERTY_NAMES, ALL_TOOLBAR_COMMAND_IDS, EMPTY_ACTIVE_IDS;
327479
- var init_create_super_doc_ui__B0b9R4K_es = __esm(() => {
327480
- init_SuperConverter_DFi0X147_es();
327481
- init_create_headless_toolbar_C9nvGdZQ_es();
328022
+ var init_create_super_doc_ui_CPZeYqzS_es = __esm(() => {
328023
+ init_SuperConverter_ing_1fvK_es();
328024
+ init_create_headless_toolbar_B_1fvPL0_es();
327482
328025
  MOD_ALIASES = new Set([
327483
328026
  "Mod",
327484
328027
  "Meta",
@@ -327520,16 +328063,16 @@ var init_zipper_BxRAi0_5_es = __esm(() => {
327520
328063
 
327521
328064
  // ../../packages/superdoc/dist/super-editor.es.js
327522
328065
  var init_super_editor_es = __esm(() => {
327523
- init_src_JQdl170N_es();
327524
- init_SuperConverter_DFi0X147_es();
328066
+ init_src_4bO9QmBD_es();
328067
+ init_SuperConverter_ing_1fvK_es();
327525
328068
  init_jszip_C49i9kUs_es();
327526
328069
  init_xml_js_CqGKpaft_es();
327527
- init_create_headless_toolbar_C9nvGdZQ_es();
328070
+ init_create_headless_toolbar_B_1fvPL0_es();
327528
328071
  init_constants_DrU4EASo_es();
327529
328072
  init_dist_B8HfvhaK_es();
327530
328073
  init_unified_Dsuw2be5_es();
327531
328074
  init_DocxZipper_CUX64E5K_es();
327532
- init_create_super_doc_ui__B0b9R4K_es();
328075
+ init_create_super_doc_ui_CPZeYqzS_es();
327533
328076
  init_ui_CGB3qmy3_es();
327534
328077
  init_eventemitter3_UwU_CLPU_es();
327535
328078
  init_errors_C_DoKMoN_es();
@@ -347653,6 +348196,9 @@ function extractSectionType2(elements) {
347653
348196
  }
347654
348197
  return "nextPage";
347655
348198
  }
348199
+ function extractSectionTypeIsExplicit2(elements) {
348200
+ return elements.some((el) => el?.name === "w:type");
348201
+ }
347656
348202
  function extractPageSizeAndOrientation2(elements) {
347657
348203
  const pgSz = elements.find((el) => el?.name === "w:pgSz");
347658
348204
  if (!pgSz?.attributes) {
@@ -347763,6 +348309,7 @@ function extractSectionData2(para) {
347763
348309
  return headerPx == null && footerPx == null ? null : { headerPx, footerPx };
347764
348310
  }
347765
348311
  const type = extractSectionType2(sectPrElements);
348312
+ const typeIsExplicit = extractSectionTypeIsExplicit2(sectPrElements);
347766
348313
  const { pageSizePx, orientation } = extractPageSizeAndOrientation2(sectPrElements);
347767
348314
  const titlePg = sectPrElements.some((el) => el?.name === "w:titlePg");
347768
348315
  const fallbackMargins = extractFallbackMargins2(sectPrElements, headerPx, footerPx);
@@ -347782,6 +348329,7 @@ function extractSectionData2(para) {
347782
348329
  bottomPx,
347783
348330
  leftPx,
347784
348331
  type,
348332
+ typeIsExplicit,
347785
348333
  pageSizePx,
347786
348334
  orientation,
347787
348335
  columnsPx,
@@ -347857,6 +348405,7 @@ function shouldIgnoreSectionBreak2(paragraph3, index2, total, hasBodySectPr) {
347857
348405
  function findParagraphsWithSectPr2(doc4) {
347858
348406
  const paragraphs = [];
347859
348407
  let paragraphIndex = 0;
348408
+ let nodeIndex = 0;
347860
348409
  const getNodeChildren = (node3) => {
347861
348410
  if (Array.isArray(node3.content))
347862
348411
  return node3.content;
@@ -347870,28 +348419,30 @@ function findParagraphsWithSectPr2(doc4) {
347870
348419
  }
347871
348420
  return [];
347872
348421
  };
347873
- const visitNode = (node3) => {
348422
+ const visitNode = (node3, outerNodeIndex) => {
347874
348423
  if (node3.type === "paragraph") {
347875
348424
  if (hasSectPr2(node3)) {
347876
- paragraphs.push({ index: paragraphIndex, node: node3 });
348425
+ paragraphs.push({ index: paragraphIndex, nodeIndex: outerNodeIndex, node: node3 });
347877
348426
  }
347878
348427
  paragraphIndex++;
347879
348428
  return;
347880
348429
  }
347881
348430
  if (node3.type === "index" || node3.type === "bibliography" || node3.type === "tableOfAuthorities" || node3.type === "documentPartObject" || node3.type === "tableOfContents") {
347882
- getNodeChildren(node3).forEach(visitNode);
348431
+ getNodeChildren(node3).forEach((child) => visitNode(child, outerNodeIndex));
347883
348432
  }
347884
348433
  };
347885
348434
  if (doc4.content) {
347886
348435
  for (const node3 of doc4.content) {
347887
- visitNode(node3);
348436
+ visitNode(node3, nodeIndex);
348437
+ nodeIndex++;
347888
348438
  }
347889
348439
  }
347890
- return { paragraphs, totalCount: paragraphIndex };
348440
+ return { paragraphs, totalCount: paragraphIndex, totalNodeCount: nodeIndex };
347891
348441
  }
347892
348442
  function buildSectionRangesFromParagraphs2(paragraphs, hasBodySectPr) {
347893
348443
  const ranges = [];
347894
348444
  let currentStart = 0;
348445
+ let currentStartNode = 0;
347895
348446
  paragraphs.forEach((item, idx) => {
347896
348447
  if (shouldIgnoreSectionBreak2(item.node, idx, paragraphs.length, hasBodySectPr)) {
347897
348448
  return;
@@ -347903,6 +348454,8 @@ function buildSectionRangesFromParagraphs2(paragraphs, hasBodySectPr) {
347903
348454
  const hasAnyMargin = sectionData.headerPx != null || sectionData.footerPx != null || sectionData.topPx != null || sectionData.rightPx != null || sectionData.bottomPx != null || sectionData.leftPx != null;
347904
348455
  const range = {
347905
348456
  sectionIndex: idx,
348457
+ startNodeIndex: currentStartNode,
348458
+ endNodeIndex: item.nodeIndex,
347906
348459
  startParagraphIndex: currentStart,
347907
348460
  endParagraphIndex: item.index,
347908
348461
  sectPr,
@@ -347918,6 +348471,7 @@ function buildSectionRangesFromParagraphs2(paragraphs, hasBodySectPr) {
347918
348471
  orientation: sectionData.orientation ?? null,
347919
348472
  columns: sectionData.columnsPx ?? null,
347920
348473
  type: sectionData.type ?? DEFAULT_PARAGRAPH_SECTION_TYPE2,
348474
+ typeIsExplicit: sectionData.typeIsExplicit ?? false,
347921
348475
  titlePg: sectionData.titlePg ?? false,
347922
348476
  headerRefs: sectionData.headerRefs,
347923
348477
  footerRefs: sectionData.footerRefs,
@@ -347926,10 +348480,11 @@ function buildSectionRangesFromParagraphs2(paragraphs, hasBodySectPr) {
347926
348480
  };
347927
348481
  ranges.push(range);
347928
348482
  currentStart = item.index + 1;
348483
+ currentStartNode = item.nodeIndex + 1;
347929
348484
  });
347930
348485
  return ranges;
347931
348486
  }
347932
- function createFinalSectionFromBodySectPr2(bodySectPr, currentStart, totalParagraphs, sectionIndex) {
348487
+ function createFinalSectionFromBodySectPr2(bodySectPr, currentStart, totalParagraphs, sectionIndex, nodeBounds) {
347933
348488
  const clampedStart = Math.max(0, Math.min(currentStart, Math.max(totalParagraphs - 1, 0)));
347934
348489
  const tempNode = {
347935
348490
  type: "paragraph",
@@ -347941,8 +348496,12 @@ function createFinalSectionFromBodySectPr2(bodySectPr, currentStart, totalParagr
347941
348496
  if (!bodySectionData)
347942
348497
  return null;
347943
348498
  const hasAnyMargin = bodySectionData.headerPx != null || bodySectionData.footerPx != null || bodySectionData.topPx != null || bodySectionData.rightPx != null || bodySectionData.bottomPx != null || bodySectionData.leftPx != null;
348499
+ const totalNodes = nodeBounds?.totalNodeCount ?? totalParagraphs;
348500
+ const startNodeIndex = nodeBounds ? Math.max(0, Math.min(nodeBounds.startNodeIndex, Math.max(totalNodes - 1, 0))) : clampedStart;
347944
348501
  return {
347945
348502
  sectionIndex,
348503
+ startNodeIndex,
348504
+ endNodeIndex: Math.max(startNodeIndex, totalNodes - 1),
347946
348505
  startParagraphIndex: clampedStart,
347947
348506
  endParagraphIndex: totalParagraphs - 1,
347948
348507
  sectPr: bodySectPr,
@@ -347958,6 +348517,7 @@ function createFinalSectionFromBodySectPr2(bodySectPr, currentStart, totalParagr
347958
348517
  orientation: bodySectionData.orientation ?? null,
347959
348518
  columns: bodySectionData.columnsPx ?? null,
347960
348519
  type: bodySectionData.type ?? DEFAULT_BODY_SECTION_TYPE2,
348520
+ typeIsExplicit: bodySectionData.typeIsExplicit ?? false,
347961
348521
  titlePg: bodySectionData.titlePg ?? false,
347962
348522
  headerRefs: bodySectionData.headerRefs,
347963
348523
  footerRefs: bodySectionData.footerRefs,
@@ -347965,9 +348525,13 @@ function createFinalSectionFromBodySectPr2(bodySectPr, currentStart, totalParagr
347965
348525
  vAlign: bodySectionData.vAlign
347966
348526
  };
347967
348527
  }
347968
- function createDefaultFinalSection2(currentStart, totalParagraphs, sectionIndex) {
348528
+ function createDefaultFinalSection2(currentStart, totalParagraphs, sectionIndex, nodeBounds) {
348529
+ const totalNodes = nodeBounds?.totalNodeCount ?? totalParagraphs;
348530
+ const startNodeIndex = nodeBounds?.startNodeIndex ?? currentStart;
347969
348531
  return {
347970
348532
  sectionIndex,
348533
+ startNodeIndex,
348534
+ endNodeIndex: Math.max(startNodeIndex, totalNodes - 1),
347971
348535
  startParagraphIndex: currentStart,
347972
348536
  endParagraphIndex: totalParagraphs - 1,
347973
348537
  sectPr: null,
@@ -347976,23 +348540,29 @@ function createDefaultFinalSection2(currentStart, totalParagraphs, sectionIndex)
347976
348540
  orientation: null,
347977
348541
  columns: null,
347978
348542
  type: DEFAULT_BODY_SECTION_TYPE2,
348543
+ typeIsExplicit: false,
347979
348544
  titlePg: false,
347980
348545
  headerRefs: undefined,
347981
348546
  footerRefs: undefined
347982
348547
  };
347983
348548
  }
347984
348549
  function analyzeSectionRanges2(doc4, bodySectPr) {
347985
- const { paragraphs, totalCount } = findParagraphsWithSectPr2(doc4);
348550
+ const { paragraphs, totalCount, totalNodeCount } = findParagraphsWithSectPr2(doc4);
347986
348551
  const hasBody = Boolean(bodySectPr);
347987
348552
  const ranges = buildSectionRangesFromParagraphs2(paragraphs, hasBody);
347988
- const currentStart = ranges.length > 0 ? ranges[ranges.length - 1].endParagraphIndex + 1 : 0;
348553
+ const last2 = ranges[ranges.length - 1];
348554
+ const currentStart = last2 ? last2.endParagraphIndex + 1 : 0;
348555
+ const currentStartNode = last2 ? last2.endNodeIndex + 1 : 0;
347989
348556
  if (isSectPrElement2(bodySectPr)) {
347990
- const finalSection = createFinalSectionFromBodySectPr2(bodySectPr, Math.min(currentStart, totalCount), totalCount, ranges.length);
348557
+ const finalSection = createFinalSectionFromBodySectPr2(bodySectPr, Math.min(currentStart, totalCount), totalCount, ranges.length, { startNodeIndex: Math.min(currentStartNode, totalNodeCount), totalNodeCount });
347991
348558
  if (finalSection) {
347992
348559
  ranges.push(finalSection);
347993
348560
  }
347994
348561
  } else if (ranges.length > 0) {
347995
- const fallbackFinal = createDefaultFinalSection2(Math.min(currentStart, totalCount), totalCount, ranges.length);
348562
+ const fallbackFinal = createDefaultFinalSection2(Math.min(currentStart, totalCount), totalCount, ranges.length, {
348563
+ startNodeIndex: Math.min(currentStartNode, totalNodeCount),
348564
+ totalNodeCount
348565
+ });
347996
348566
  if (fallbackFinal) {
347997
348567
  fallbackFinal.type = DEFAULT_PARAGRAPH_SECTION_TYPE2;
347998
348568
  ranges.push(fallbackFinal);
@@ -354346,16 +354916,20 @@ function readOddEvenHeadersFlag2(editor) {
354346
354916
  return settingsRoot.elements.some((entry) => entry.name === "w:evenAndOddHeaders");
354347
354917
  }
354348
354918
  function createSyntheticRange2(bodySectPr, paragraphCount) {
354919
+ const lastIndex = Math.max(paragraphCount - 1, 0);
354349
354920
  return {
354350
354921
  sectionIndex: 0,
354922
+ startNodeIndex: 0,
354923
+ endNodeIndex: lastIndex,
354351
354924
  startParagraphIndex: 0,
354352
- endParagraphIndex: Math.max(paragraphCount - 1, 0),
354925
+ endParagraphIndex: lastIndex,
354353
354926
  sectPr: bodySectPr ?? null,
354354
354927
  margins: null,
354355
354928
  pageSize: null,
354356
354929
  orientation: null,
354357
354930
  columns: null,
354358
354931
  type: "continuous" /* CONTINUOUS */,
354932
+ typeIsExplicit: false,
354359
354933
  titlePg: false,
354360
354934
  headerRefs: undefined,
354361
354935
  footerRefs: undefined,