oasis-editor 0.0.63 → 0.0.65

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.
@@ -2483,7 +2483,7 @@ function OasisEditorAppLazy(props = {}) {
2483
2483
  onCleanup(() => {
2484
2484
  cancelled = true;
2485
2485
  });
2486
- import("./OasisEditorApp-BG3tA5Y-.js").then((m) => {
2486
+ import("./OasisEditorApp-Bp4TNPC7.js").then((m) => {
2487
2487
  cancelled = true;
2488
2488
  setProgress(1);
2489
2489
  setTimeout(() => setApp(() => m.OasisEditorApp), 180);
@@ -4542,12 +4542,8 @@ function createEditorCommandBus(editor) {
4542
4542
  return editor.commands.canExecute(resolved.name, resolved.payload);
4543
4543
  },
4544
4544
  state(command) {
4545
- var _a;
4546
4545
  const resolved = resolveCommandRef(command);
4547
- const registered = editor.commands.get(resolved.name);
4548
- return ((_a = registered == null ? void 0 : registered.refresh) == null ? void 0 : _a.call(registered, resolved.payload)) ?? {
4549
- isEnabled: editor.commands.has(resolved.name)
4550
- };
4546
+ return editor.commands.state(resolved.name, resolved.payload);
4551
4547
  }
4552
4548
  };
4553
4549
  }
@@ -12758,6 +12754,422 @@ function projectHeaderFooterBlocks(blocks, pageIndex, totalPages, measuredHeight
12758
12754
  defaultTabStop
12759
12755
  );
12760
12756
  }
12757
+ const MAX_COLUMN_BALANCE_ITERATIONS = 100;
12758
+ function projectColumnedBlocksLayout(context2, count, runTrackLayout) {
12759
+ var _a, _b;
12760
+ const { pageSettings, maxPageHeight, pageOffset = 0, existingPages = [] } = context2;
12761
+ const colWidth = ((_a = getPageColumnRects(pageSettings)[0]) == null ? void 0 : _a.width) ?? getPageContentWidth(pageSettings);
12762
+ const runTracks = (blocks, trackHeight) => runTrackLayout({
12763
+ ...context2,
12764
+ blocks,
12765
+ maxPageHeight: trackHeight,
12766
+ contentWidthOverride: colWidth,
12767
+ pageOffset: 0,
12768
+ existingPages: [],
12769
+ reservedHeightByPageIndex: void 0
12770
+ });
12771
+ let tracks = runTracks(context2.blocks, maxPageHeight);
12772
+ if (tracks.length > 1 || tracks.length === 1 && count > 1) {
12773
+ const trailingStart = Math.floor((tracks.length - 1) / count) * count;
12774
+ const trailingTracks = tracks.slice(trailingStart);
12775
+ const firstTrailingBlock = (_b = trailingTracks[0]) == null ? void 0 : _b.blocks[0];
12776
+ const prevTrack = trailingStart > 0 ? tracks[trailingStart - 1] : void 0;
12777
+ const prevLastBlock = prevTrack == null ? void 0 : prevTrack.blocks[prevTrack.blocks.length - 1];
12778
+ const cleanBoundary = firstTrailingBlock != null && (prevLastBlock == null || prevLastBlock.globalIndex !== firstTrailingBlock.globalIndex);
12779
+ if (cleanBoundary) {
12780
+ const startIndex = firstTrailingBlock.globalIndex;
12781
+ const sourceSubset = context2.blocks.slice(startIndex);
12782
+ const sumHeight = trailingTracks.reduce(
12783
+ (sum, track) => sum + track.height,
12784
+ 0
12785
+ );
12786
+ let target = Math.max(24, Math.ceil(sumHeight / count));
12787
+ let balanced = runTracks(sourceSubset, target);
12788
+ for (let iteration = 0; balanced.length > count && target < maxPageHeight && iteration < MAX_COLUMN_BALANCE_ITERATIONS; iteration += 1) {
12789
+ target = Math.min(maxPageHeight, Math.ceil(target * 1.05) + 4);
12790
+ balanced = runTracks(sourceSubset, target);
12791
+ }
12792
+ if (balanced.length <= count) {
12793
+ tracks = [...tracks.slice(0, trailingStart), ...balanced];
12794
+ }
12795
+ }
12796
+ }
12797
+ const pages = [...existingPages];
12798
+ const startPageIndex = existingPages.length > 0 ? existingPages[existingPages.length - 1].index + 1 : pageOffset;
12799
+ const physicalPageCount = Math.ceil(tracks.length / count);
12800
+ for (let p = 0; p < physicalPageCount; p += 1) {
12801
+ const pageIndex = startPageIndex + p;
12802
+ const pageTracks = tracks.slice(p * count, p * count + count);
12803
+ const blocks = [];
12804
+ let height = 0;
12805
+ pageTracks.forEach((track, columnIndex) => {
12806
+ for (const block of track.blocks) {
12807
+ block.columnIndex = columnIndex;
12808
+ blocks.push(block);
12809
+ }
12810
+ height = Math.max(height, track.height);
12811
+ });
12812
+ pages.push({
12813
+ id: `page:${pageIndex + 1}`,
12814
+ index: pageIndex,
12815
+ height,
12816
+ maxHeight: maxPageHeight,
12817
+ blocks,
12818
+ pageSettings
12819
+ });
12820
+ }
12821
+ if (pages.length === 0) {
12822
+ pages.push({
12823
+ id: `page:${startPageIndex + 1}`,
12824
+ index: startPageIndex,
12825
+ height: 0,
12826
+ maxHeight: maxPageHeight,
12827
+ blocks: [],
12828
+ pageSettings
12829
+ });
12830
+ }
12831
+ return pages;
12832
+ }
12833
+ class PaginationTrack {
12834
+ constructor(pageOffset, maxPageHeight, reservedHeightByPageIndex, pageSettings, existingPages) {
12835
+ __publicField(this, "pages");
12836
+ __publicField(this, "blocks");
12837
+ __publicField(this, "height");
12838
+ this.pageOffset = pageOffset;
12839
+ this.maxPageHeight = maxPageHeight;
12840
+ this.reservedHeightByPageIndex = reservedHeightByPageIndex;
12841
+ this.pageSettings = pageSettings;
12842
+ this.pages = [...existingPages];
12843
+ const currentPage = this.pages[this.pages.length - 1];
12844
+ this.blocks = currentPage ? [...currentPage.blocks] : [];
12845
+ this.height = currentPage ? currentPage.height : 0;
12846
+ if (currentPage) {
12847
+ this.pages.pop();
12848
+ }
12849
+ }
12850
+ /** Index of the track currently being filled. */
12851
+ get pageIndex() {
12852
+ return this.pageOffset + this.pages.length;
12853
+ }
12854
+ /** Usable height of a given page, net of any reserved (e.g. footnote) space. */
12855
+ maxHeightForPage(pageIndex) {
12856
+ var _a;
12857
+ return Math.max(
12858
+ 24,
12859
+ this.maxPageHeight - (((_a = this.reservedHeightByPageIndex) == null ? void 0 : _a.get(pageIndex)) ?? 0)
12860
+ );
12861
+ }
12862
+ /** Usable height of the track currently being filled. */
12863
+ get currentMaxHeight() {
12864
+ return this.maxHeightForPage(this.pageIndex);
12865
+ }
12866
+ /** Seals the current track into a page and starts a fresh, empty track. */
12867
+ flush() {
12868
+ if (this.blocks.length === 0 && this.pages.length > 0) {
12869
+ return;
12870
+ }
12871
+ const pageIndex = this.pageIndex;
12872
+ this.pages.push({
12873
+ id: `page:${pageIndex + 1}`,
12874
+ index: pageIndex,
12875
+ height: this.height,
12876
+ maxHeight: this.maxHeightForPage(pageIndex),
12877
+ blocks: this.blocks,
12878
+ pageSettings: this.pageSettings
12879
+ });
12880
+ this.blocks = [];
12881
+ this.height = 0;
12882
+ }
12883
+ /** Flushes the trailing track and guarantees at least one page exists. */
12884
+ finalize() {
12885
+ this.flush();
12886
+ if (this.pages.length === 0) {
12887
+ const pageIndex = this.pageOffset;
12888
+ this.pages.push({
12889
+ id: `page:${pageIndex + 1}`,
12890
+ index: pageIndex,
12891
+ height: 0,
12892
+ maxHeight: this.maxHeightForPage(pageIndex),
12893
+ blocks: [],
12894
+ pageSettings: this.pageSettings
12895
+ });
12896
+ }
12897
+ return this.pages;
12898
+ }
12899
+ }
12900
+ const TEXT_BOX_AUTOFIT_SAFETY_PX$1 = 2;
12901
+ function estimateTextBoxAutoFitHeight(textBox, styles, measurer, pageIndex, totalPages, defaultTabStop) {
12902
+ var _a, _b, _c, _d, _e;
12903
+ if (!((_a = textBox.body) == null ? void 0 : _a.autoFit)) {
12904
+ return textBox.height;
12905
+ }
12906
+ const padding = {
12907
+ left: ((_b = textBox.body) == null ? void 0 : _b.paddingLeft) ?? 0,
12908
+ top: ((_c = textBox.body) == null ? void 0 : _c.paddingTop) ?? 0,
12909
+ right: ((_d = textBox.body) == null ? void 0 : _d.paddingRight) ?? 0,
12910
+ bottom: ((_e = textBox.body) == null ? void 0 : _e.paddingBottom) ?? 0
12911
+ };
12912
+ const innerWidth = Math.max(1, textBox.width - padding.left - padding.right);
12913
+ const contentHeight = textBox.blocks.reduce((sum, block) => {
12914
+ if (block.type === "paragraph") {
12915
+ const layout = projectParagraphLayout(
12916
+ block,
12917
+ pageIndex,
12918
+ totalPages,
12919
+ styles,
12920
+ innerWidth,
12921
+ measurer,
12922
+ defaultTabStop
12923
+ );
12924
+ return sum + getProjectedParagraphBlockHeight(block, layout, styles);
12925
+ }
12926
+ if (block.type === "table") {
12927
+ return sum + estimateTableBlockHeight(
12928
+ block,
12929
+ styles,
12930
+ innerWidth,
12931
+ measurer,
12932
+ defaultTabStop
12933
+ );
12934
+ }
12935
+ return sum;
12936
+ }, 0);
12937
+ return Math.max(
12938
+ 1,
12939
+ Math.ceil(
12940
+ contentHeight + padding.top + padding.bottom + TEXT_BOX_AUTOFIT_SAFETY_PX$1
12941
+ )
12942
+ );
12943
+ }
12944
+ function paginateParagraphBlock(track, params, sourceBlock, nextBlock, index) {
12945
+ var _a;
12946
+ const {
12947
+ pageSettings,
12948
+ contentWidth,
12949
+ measurer,
12950
+ styles,
12951
+ totalPages,
12952
+ defaultTabStop,
12953
+ measuredHeights,
12954
+ measuredParagraphLayouts
12955
+ } = params;
12956
+ const pageIndex = track.pageIndex;
12957
+ const projectedParagraphLayout = projectParagraphLayoutWithExclusions(
12958
+ sourceBlock,
12959
+ pageSettings,
12960
+ contentWidth,
12961
+ measurer,
12962
+ pageIndex,
12963
+ totalPages,
12964
+ styles,
12965
+ defaultTabStop,
12966
+ (textBox) => estimateTextBoxAutoFitHeight(
12967
+ textBox,
12968
+ styles,
12969
+ measurer,
12970
+ pageIndex,
12971
+ totalPages,
12972
+ defaultTabStop
12973
+ )
12974
+ );
12975
+ const measuredParagraphLayout = measuredParagraphLayouts == null ? void 0 : measuredParagraphLayouts[sourceBlock.id];
12976
+ const paragraphLayout = measuredParagraphLayout && isMeasuredLayoutCurrent(projectedParagraphLayout, measuredParagraphLayout) ? applyMeasuredLineGeometry(
12977
+ projectedParagraphLayout,
12978
+ measuredParagraphLayout
12979
+ ) : projectedParagraphLayout;
12980
+ let collapseWithPrevious = false;
12981
+ let contextualAdjustedPreviousBlock;
12982
+ let contextualAdjustedAmount = 0;
12983
+ let paragraphTotalHeight = (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) ?? getProjectedParagraphBlockHeight(sourceBlock, paragraphLayout, styles);
12984
+ const paragraphStyle = getEffectiveParagraphStyle(sourceBlock, styles);
12985
+ const nextBlockHeight = (nextBlock == null ? void 0 : nextBlock.type) === "paragraph" ? (measuredHeights == null ? void 0 : measuredHeights[nextBlock.id]) ?? estimateParagraphBlockHeight(
12986
+ nextBlock,
12987
+ styles,
12988
+ contentWidth,
12989
+ measurer,
12990
+ {
12991
+ allowSpacingBefore: !shouldCollapseContextualSpacing(
12992
+ sourceBlock,
12993
+ nextBlock,
12994
+ styles
12995
+ )
12996
+ },
12997
+ defaultTabStop
12998
+ ) : nextBlock ? (measuredHeights == null ? void 0 : measuredHeights[nextBlock.id]) ?? estimateTableBlockHeight(
12999
+ nextBlock,
13000
+ styles,
13001
+ contentWidth,
13002
+ measurer,
13003
+ defaultTabStop
13004
+ ) : 0;
13005
+ if (paragraphStyle.keepWithNext && track.blocks.length > 0 && track.height + paragraphTotalHeight + nextBlockHeight > track.currentMaxHeight && paragraphTotalHeight + nextBlockHeight <= track.currentMaxHeight) {
13006
+ track.flush();
13007
+ }
13008
+ const previousBlock = track.blocks.at(-1);
13009
+ const previousParagraph = (previousBlock == null ? void 0 : previousBlock.blockType) === "paragraph" && ((_a = previousBlock.sourceBlock) == null ? void 0 : _a.type) === "paragraph" ? previousBlock.sourceBlock : void 0;
13010
+ collapseWithPrevious = track.blocks.length > 0 && shouldCollapseContextualSpacing(previousParagraph, sourceBlock, styles);
13011
+ if (collapseWithPrevious && previousBlock && previousParagraph) {
13012
+ const previousStyle = getEffectiveParagraphStyle(previousParagraph, styles);
13013
+ const collapsedSpacingAfter = previousStyle.spacingAfter ?? 0;
13014
+ if (collapsedSpacingAfter > 0) {
13015
+ previousBlock.estimatedHeight = Math.max(
13016
+ 0,
13017
+ previousBlock.estimatedHeight - collapsedSpacingAfter
13018
+ );
13019
+ track.height = Math.max(0, track.height - collapsedSpacingAfter);
13020
+ contextualAdjustedPreviousBlock = previousBlock;
13021
+ contextualAdjustedAmount = collapsedSpacingAfter;
13022
+ }
13023
+ paragraphTotalHeight = (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) ?? getProjectedParagraphBlockHeight(
13024
+ sourceBlock,
13025
+ paragraphLayout,
13026
+ styles,
13027
+ false
13028
+ );
13029
+ }
13030
+ if (paragraphStyle.keepLinesTogether && paragraphTotalHeight <= track.currentMaxHeight) {
13031
+ if (track.blocks.length > 0 && track.height + paragraphTotalHeight > track.currentMaxHeight) {
13032
+ track.flush();
13033
+ }
13034
+ const segmentId = `${sourceBlock.id}:segment:0`;
13035
+ const rawMeasuredHeight = getParagraphMeasuredHeight(
13036
+ measuredHeights,
13037
+ sourceBlock.id,
13038
+ segmentId,
13039
+ true,
13040
+ paragraphTotalHeight
13041
+ );
13042
+ const hasMeasuredHeight = (measuredHeights == null ? void 0 : measuredHeights[segmentId]) !== void 0 || (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) !== void 0;
13043
+ const measuredHeight = collapseWithPrevious && hasMeasuredHeight ? Math.max(0, rawMeasuredHeight - (paragraphStyle.spacingBefore ?? 0)) : rawMeasuredHeight;
13044
+ track.blocks.push({
13045
+ blockId: segmentId,
13046
+ sourceBlockId: sourceBlock.id,
13047
+ blockType: sourceBlock.type,
13048
+ paragraphId: sourceBlock.id,
13049
+ globalIndex: index,
13050
+ estimatedHeight: measuredHeight,
13051
+ layout: paragraphLayout,
13052
+ sourceBlock
13053
+ });
13054
+ track.height += measuredHeight;
13055
+ return;
13056
+ }
13057
+ let startLineIndex = 0;
13058
+ let segmentIndex = 0;
13059
+ while (startLineIndex < paragraphLayout.lines.length) {
13060
+ const remainingHeight = track.currentMaxHeight - track.height;
13061
+ let lineEndIndex = startLineIndex;
13062
+ let segmentHeight = 0;
13063
+ while (lineEndIndex < paragraphLayout.lines.length) {
13064
+ const candidateLines = paragraphLayout.lines.slice(
13065
+ startLineIndex,
13066
+ lineEndIndex + 1
13067
+ );
13068
+ const candidateHeight = getParagraphSegmentHeight(
13069
+ sourceBlock,
13070
+ candidateLines,
13071
+ startLineIndex === 0,
13072
+ lineEndIndex === paragraphLayout.lines.length - 1,
13073
+ styles,
13074
+ !collapseWithPrevious
13075
+ );
13076
+ const candidateFitHeight = getParagraphSegmentFitHeight(
13077
+ sourceBlock,
13078
+ candidateHeight,
13079
+ lineEndIndex === paragraphLayout.lines.length - 1,
13080
+ styles
13081
+ );
13082
+ const tolerance = 1.5;
13083
+ if (candidateFitHeight > remainingHeight + tolerance && lineEndIndex === startLineIndex && track.blocks.length > 0) {
13084
+ break;
13085
+ }
13086
+ if (candidateFitHeight > remainingHeight + tolerance && lineEndIndex > startLineIndex) {
13087
+ break;
13088
+ }
13089
+ segmentHeight = candidateHeight;
13090
+ lineEndIndex += 1;
13091
+ }
13092
+ if (lineEndIndex === startLineIndex && track.blocks.length > 0) {
13093
+ if (contextualAdjustedPreviousBlock && contextualAdjustedAmount > 0) {
13094
+ contextualAdjustedPreviousBlock.estimatedHeight += contextualAdjustedAmount;
13095
+ track.height += contextualAdjustedAmount;
13096
+ contextualAdjustedPreviousBlock = void 0;
13097
+ contextualAdjustedAmount = 0;
13098
+ }
13099
+ collapseWithPrevious = false;
13100
+ track.flush();
13101
+ continue;
13102
+ }
13103
+ if (lineEndIndex === startLineIndex) {
13104
+ lineEndIndex = Math.min(paragraphLayout.lines.length, startLineIndex + 1);
13105
+ segmentHeight = getParagraphSegmentHeight(
13106
+ sourceBlock,
13107
+ paragraphLayout.lines.slice(startLineIndex, lineEndIndex),
13108
+ startLineIndex === 0,
13109
+ lineEndIndex === paragraphLayout.lines.length,
13110
+ styles,
13111
+ !collapseWithPrevious
13112
+ );
13113
+ }
13114
+ if (lineEndIndex < paragraphLayout.lines.length) {
13115
+ const widowOrphanAdjusted = applyWidowOrphanControl(
13116
+ sourceBlock,
13117
+ paragraphLayout.lines,
13118
+ startLineIndex,
13119
+ lineEndIndex,
13120
+ styles,
13121
+ !collapseWithPrevious,
13122
+ true,
13123
+ track.blocks.length > 0
13124
+ );
13125
+ lineEndIndex = widowOrphanAdjusted.endLineIndexExclusive;
13126
+ segmentHeight = widowOrphanAdjusted.height;
13127
+ if (lineEndIndex === startLineIndex) {
13128
+ if (contextualAdjustedPreviousBlock && contextualAdjustedAmount > 0) {
13129
+ contextualAdjustedPreviousBlock.estimatedHeight += contextualAdjustedAmount;
13130
+ track.height += contextualAdjustedAmount;
13131
+ contextualAdjustedPreviousBlock = void 0;
13132
+ contextualAdjustedAmount = 0;
13133
+ }
13134
+ collapseWithPrevious = false;
13135
+ track.flush();
13136
+ continue;
13137
+ }
13138
+ }
13139
+ const segmentLayout = createParagraphSegmentLayout(
13140
+ paragraphLayout,
13141
+ startLineIndex,
13142
+ lineEndIndex
13143
+ );
13144
+ const segmentId = `${sourceBlock.id}:segment:${segmentIndex}`;
13145
+ const isWholeParagraphSegment = startLineIndex === 0 && lineEndIndex === paragraphLayout.lines.length;
13146
+ const rawMeasuredHeight = getParagraphMeasuredHeight(
13147
+ measuredHeights,
13148
+ sourceBlock.id,
13149
+ segmentId,
13150
+ isWholeParagraphSegment,
13151
+ segmentHeight
13152
+ );
13153
+ const hasMeasuredHeight = (measuredHeights == null ? void 0 : measuredHeights[segmentId]) !== void 0 || isWholeParagraphSegment && (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) !== void 0;
13154
+ const measuredHeight = collapseWithPrevious && startLineIndex === 0 && hasMeasuredHeight ? Math.max(0, rawMeasuredHeight - (paragraphStyle.spacingBefore ?? 0)) : rawMeasuredHeight;
13155
+ track.blocks.push({
13156
+ blockId: segmentId,
13157
+ sourceBlockId: sourceBlock.id,
13158
+ blockType: sourceBlock.type,
13159
+ paragraphId: sourceBlock.id,
13160
+ globalIndex: index,
13161
+ estimatedHeight: measuredHeight,
13162
+ layout: segmentLayout,
13163
+ sourceBlock
13164
+ });
13165
+ track.height += measuredHeight;
13166
+ startLineIndex = lineEndIndex;
13167
+ segmentIndex += 1;
13168
+ if (startLineIndex < paragraphLayout.lines.length) {
13169
+ track.flush();
13170
+ }
13171
+ }
13172
+ }
12761
13173
  function normalizeCellStartPositions(row, starts, legacyStarts) {
12762
13174
  return row.cells.map((_, cellIdx) => {
12763
13175
  var _a, _b;
@@ -12943,125 +13355,283 @@ function positionsFinishedRow(row, ends) {
12943
13355
  }
12944
13356
  );
12945
13357
  }
12946
- const MAX_COLUMN_BALANCE_ITERATIONS = 100;
12947
- function projectColumnedBlocksLayout(context2, count, runTrackLayout) {
12948
- var _a, _b;
12949
- const { pageSettings, maxPageHeight, pageOffset = 0, existingPages = [] } = context2;
12950
- const colWidth = ((_a = getPageColumnRects(pageSettings)[0]) == null ? void 0 : _a.width) ?? getPageContentWidth(pageSettings);
12951
- const runTracks = (blocks, trackHeight) => runTrackLayout({
12952
- ...context2,
12953
- blocks,
12954
- maxPageHeight: trackHeight,
12955
- contentWidthOverride: colWidth,
12956
- pageOffset: 0,
12957
- existingPages: [],
12958
- reservedHeightByPageIndex: void 0
12959
- });
12960
- let tracks = runTracks(context2.blocks, maxPageHeight);
12961
- if (tracks.length > 1 || tracks.length === 1 && count > 1) {
12962
- const trailingStart = Math.floor((tracks.length - 1) / count) * count;
12963
- const trailingTracks = tracks.slice(trailingStart);
12964
- const firstTrailingBlock = (_b = trailingTracks[0]) == null ? void 0 : _b.blocks[0];
12965
- const prevTrack = trailingStart > 0 ? tracks[trailingStart - 1] : void 0;
12966
- const prevLastBlock = prevTrack == null ? void 0 : prevTrack.blocks[prevTrack.blocks.length - 1];
12967
- const cleanBoundary = firstTrailingBlock != null && (prevLastBlock == null || prevLastBlock.globalIndex !== firstTrailingBlock.globalIndex);
12968
- if (cleanBoundary) {
12969
- const startIndex = firstTrailingBlock.globalIndex;
12970
- const sourceSubset = context2.blocks.slice(startIndex);
12971
- const sumHeight = trailingTracks.reduce(
12972
- (sum, track) => sum + track.height,
12973
- 0
12974
- );
12975
- let target = Math.max(24, Math.ceil(sumHeight / count));
12976
- let balanced = runTracks(sourceSubset, target);
12977
- for (let iteration = 0; balanced.length > count && target < maxPageHeight && iteration < MAX_COLUMN_BALANCE_ITERATIONS; iteration += 1) {
12978
- target = Math.min(maxPageHeight, Math.ceil(target * 1.05) + 4);
12979
- balanced = runTracks(sourceSubset, target);
13358
+ function paginateTableBlock(track, params, sourceBlock, index) {
13359
+ const { contentWidth, measurer, styles, defaultTabStop, measuredHeights } = params;
13360
+ const tableHeight = (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) ?? estimateTableBlockHeight(
13361
+ sourceBlock,
13362
+ styles,
13363
+ contentWidth,
13364
+ measurer,
13365
+ defaultTabStop
13366
+ );
13367
+ const maxHeightForCurrentPage = track.currentMaxHeight;
13368
+ const firstRow = sourceBlock.rows[0];
13369
+ const canSplitSingleRow = canSplitTableRow(firstRow);
13370
+ if (!canSplitSingleRow && sourceBlock.rows.length <= 1 || track.height + tableHeight <= maxHeightForCurrentPage) {
13371
+ if (track.blocks.length > 0 && track.height + tableHeight > maxHeightForCurrentPage) {
13372
+ track.flush();
13373
+ }
13374
+ track.blocks.push({
13375
+ blockId: sourceBlock.id,
13376
+ sourceBlockId: sourceBlock.id,
13377
+ blockType: sourceBlock.type,
13378
+ globalIndex: index,
13379
+ estimatedHeight: tableHeight,
13380
+ sourceBlock
13381
+ });
13382
+ track.height += tableHeight;
13383
+ return;
13384
+ }
13385
+ const rowGroups = getTableRowGroups(sourceBlock);
13386
+ const headerRowCount = getRepeatableHeaderRowCount(
13387
+ sourceBlock,
13388
+ getTableHeaderRowCount(sourceBlock),
13389
+ rowGroups
13390
+ );
13391
+ let groupStartIndex = 0;
13392
+ let segmentIndex = 0;
13393
+ let currentCellBlockPositions;
13394
+ while (groupStartIndex < rowGroups.length) {
13395
+ const startRowIndex = rowGroups[groupStartIndex].startRowIndex;
13396
+ const repeatedHeaderRowCount = startRowIndex > 0 ? headerRowCount : 0;
13397
+ const remainingHeight = track.currentMaxHeight - track.height;
13398
+ let groupEndIndex = groupStartIndex;
13399
+ let segmentHeight = 0;
13400
+ let splitEnds;
13401
+ let splitEndPositions;
13402
+ let isLastRowSplit = false;
13403
+ while (groupEndIndex < rowGroups.length) {
13404
+ const candidateEndRowIndex = rowGroups[groupEndIndex].endRowIndexExclusive;
13405
+ let candidateHeight = 0;
13406
+ if (groupEndIndex === groupStartIndex && currentCellBlockPositions) {
13407
+ const firstRow2 = sourceBlock.rows[startRowIndex];
13408
+ const starts = normalizeCellStartPositions(
13409
+ firstRow2,
13410
+ currentCellBlockPositions,
13411
+ void 0
13412
+ );
13413
+ const ends = firstRow2.cells.map((cell) => ({
13414
+ blockIndex: cell.blocks.length
13415
+ }));
13416
+ const slicedFirstRow = buildRowSliceFromPositions(
13417
+ firstRow2,
13418
+ starts,
13419
+ ends
13420
+ );
13421
+ candidateHeight = getTableSegmentHeight(
13422
+ {
13423
+ ...sourceBlock,
13424
+ rows: [slicedFirstRow]
13425
+ },
13426
+ 0,
13427
+ 1,
13428
+ repeatedHeaderRowCount,
13429
+ styles,
13430
+ contentWidth,
13431
+ measurer,
13432
+ defaultTabStop
13433
+ );
13434
+ } else {
13435
+ candidateHeight = getTableSegmentHeight(
13436
+ sourceBlock,
13437
+ startRowIndex,
13438
+ candidateEndRowIndex,
13439
+ repeatedHeaderRowCount,
13440
+ styles,
13441
+ contentWidth,
13442
+ measurer,
13443
+ defaultTabStop
13444
+ );
12980
13445
  }
12981
- if (balanced.length <= count) {
12982
- tracks = [...tracks.slice(0, trailingStart), ...balanced];
13446
+ if (candidateHeight <= remainingHeight) {
13447
+ segmentHeight = candidateHeight;
13448
+ groupEndIndex += 1;
13449
+ continue;
13450
+ }
13451
+ const isSingleRowGroup = candidateEndRowIndex === rowGroups[groupEndIndex].startRowIndex + 1;
13452
+ const targetRow = sourceBlock.rows[rowGroups[groupEndIndex].startRowIndex];
13453
+ const isSplitCandidate = isSingleRowGroup && canSplitTableRow(targetRow);
13454
+ if (isSplitCandidate) {
13455
+ const precedingHeight = groupEndIndex > groupStartIndex ? getTableSegmentHeight(
13456
+ sourceBlock,
13457
+ startRowIndex,
13458
+ rowGroups[groupEndIndex].startRowIndex,
13459
+ repeatedHeaderRowCount,
13460
+ styles,
13461
+ contentWidth,
13462
+ measurer,
13463
+ defaultTabStop
13464
+ ) : 0;
13465
+ const availableForSplitRow = remainingHeight - precedingHeight;
13466
+ if (availableForSplitRow > 0) {
13467
+ const starts = normalizeCellStartPositions(
13468
+ targetRow,
13469
+ groupEndIndex === groupStartIndex ? currentCellBlockPositions : void 0,
13470
+ void 0
13471
+ );
13472
+ const ends = targetRow.cells.map(
13473
+ (_, cellIdx) => findCellSplitEndPosition(
13474
+ targetRow,
13475
+ cellIdx,
13476
+ starts[cellIdx] ?? { blockIndex: 0 },
13477
+ availableForSplitRow,
13478
+ styles,
13479
+ measurer,
13480
+ defaultTabStop,
13481
+ contentWidth,
13482
+ sourceBlock,
13483
+ rowGroups[groupEndIndex].startRowIndex
13484
+ )
13485
+ );
13486
+ const canProgress = positionsProgressed(starts, ends);
13487
+ if (canProgress) {
13488
+ splitEnds = positionsToBlockIndexes(ends);
13489
+ isLastRowSplit = true;
13490
+ const slicedLastRow = buildRowSliceFromPositions(
13491
+ targetRow,
13492
+ starts,
13493
+ ends
13494
+ );
13495
+ const segmentRows = [
13496
+ ...sourceBlock.rows.slice(
13497
+ startRowIndex,
13498
+ rowGroups[groupEndIndex].startRowIndex
13499
+ ),
13500
+ slicedLastRow
13501
+ ];
13502
+ segmentHeight = getTableSegmentHeight(
13503
+ { ...sourceBlock, rows: segmentRows },
13504
+ 0,
13505
+ segmentRows.length,
13506
+ repeatedHeaderRowCount,
13507
+ styles,
13508
+ contentWidth,
13509
+ measurer,
13510
+ defaultTabStop
13511
+ );
13512
+ splitEndPositions = ends;
13513
+ groupEndIndex += 1;
13514
+ break;
13515
+ }
13516
+ }
12983
13517
  }
13518
+ break;
12984
13519
  }
12985
- }
12986
- const pages = [...existingPages];
12987
- const startPageIndex = existingPages.length > 0 ? existingPages[existingPages.length - 1].index + 1 : pageOffset;
12988
- const physicalPageCount = Math.ceil(tracks.length / count);
12989
- for (let p = 0; p < physicalPageCount; p += 1) {
12990
- const pageIndex = startPageIndex + p;
12991
- const pageTracks = tracks.slice(p * count, p * count + count);
12992
- const blocks = [];
12993
- let height = 0;
12994
- pageTracks.forEach((track, columnIndex) => {
12995
- for (const block of track.blocks) {
12996
- block.columnIndex = columnIndex;
12997
- blocks.push(block);
13520
+ if (groupEndIndex === groupStartIndex && track.blocks.length > 0) {
13521
+ track.flush();
13522
+ continue;
13523
+ }
13524
+ if (groupEndIndex === groupStartIndex) {
13525
+ const targetRow = sourceBlock.rows[startRowIndex];
13526
+ const isSingleRowGroup = rowGroups[groupStartIndex].endRowIndexExclusive === startRowIndex + 1;
13527
+ const isSplitCandidate = isSingleRowGroup && canSplitTableRow(targetRow);
13528
+ if (isSplitCandidate) {
13529
+ const starts = normalizeCellStartPositions(
13530
+ targetRow,
13531
+ currentCellBlockPositions,
13532
+ void 0
13533
+ );
13534
+ let ends = starts.map(
13535
+ (start, cellIdx) => findCellSplitEndPosition(
13536
+ targetRow,
13537
+ cellIdx,
13538
+ start,
13539
+ track.currentMaxHeight,
13540
+ styles,
13541
+ measurer,
13542
+ defaultTabStop,
13543
+ contentWidth,
13544
+ sourceBlock,
13545
+ startRowIndex
13546
+ )
13547
+ );
13548
+ if (!positionsProgressed(starts, ends)) {
13549
+ ends = starts.map((start, cellIdx) => ({
13550
+ blockIndex: Math.min(
13551
+ targetRow.cells[cellIdx].blocks.length,
13552
+ start.blockIndex + 1
13553
+ )
13554
+ }));
13555
+ }
13556
+ splitEnds = positionsToBlockIndexes(ends);
13557
+ splitEndPositions = ends;
13558
+ isLastRowSplit = true;
13559
+ const slicedLastRow = buildRowSliceFromPositions(
13560
+ targetRow,
13561
+ starts,
13562
+ ends
13563
+ );
13564
+ segmentHeight = getTableSegmentHeight(
13565
+ { ...sourceBlock, rows: [slicedLastRow] },
13566
+ 0,
13567
+ 1,
13568
+ repeatedHeaderRowCount,
13569
+ styles,
13570
+ contentWidth,
13571
+ measurer,
13572
+ defaultTabStop
13573
+ );
13574
+ groupEndIndex += 1;
13575
+ } else {
13576
+ groupEndIndex = rowGroups[groupStartIndex].endRowIndexExclusive;
13577
+ segmentHeight = getTableSegmentHeight(
13578
+ sourceBlock,
13579
+ startRowIndex,
13580
+ groupEndIndex,
13581
+ repeatedHeaderRowCount,
13582
+ styles,
13583
+ contentWidth,
13584
+ measurer,
13585
+ defaultTabStop
13586
+ );
12998
13587
  }
12999
- height = Math.max(height, track.height);
13000
- });
13001
- pages.push({
13002
- id: `page:${pageIndex + 1}`,
13003
- index: pageIndex,
13004
- height,
13005
- maxHeight: maxPageHeight,
13006
- blocks,
13007
- pageSettings
13008
- });
13009
- }
13010
- if (pages.length === 0) {
13011
- pages.push({
13012
- id: `page:${startPageIndex + 1}`,
13013
- index: startPageIndex,
13014
- height: 0,
13015
- maxHeight: maxPageHeight,
13016
- blocks: [],
13017
- pageSettings
13588
+ }
13589
+ const segmentId = `${sourceBlock.id}:segment:${segmentIndex}`;
13590
+ const measuredSegmentHeight = (measuredHeights == null ? void 0 : measuredHeights[segmentId]) ?? segmentHeight;
13591
+ const endRowIndex = rowGroups[groupEndIndex - 1].endRowIndexExclusive;
13592
+ track.blocks.push({
13593
+ blockId: segmentId,
13594
+ sourceBlockId: sourceBlock.id,
13595
+ blockType: sourceBlock.type,
13596
+ globalIndex: index,
13597
+ estimatedHeight: measuredSegmentHeight,
13598
+ tableSegment: {
13599
+ startRowIndex,
13600
+ endRowIndex,
13601
+ repeatedHeaderRowCount,
13602
+ startRowCellBlockStarts: positionsToBlockIndexes(
13603
+ currentCellBlockPositions
13604
+ ),
13605
+ endRowCellBlockEnds: splitEnds,
13606
+ startRowCellBlockPositions: hasPartialPositions(
13607
+ currentCellBlockPositions
13608
+ ) ? currentCellBlockPositions : void 0,
13609
+ endRowCellBlockPositions: hasPartialPositions(splitEndPositions) ? splitEndPositions : void 0
13610
+ },
13611
+ sourceBlock
13018
13612
  });
13019
- }
13020
- return pages;
13021
- }
13022
- const TEXT_BOX_AUTOFIT_SAFETY_PX$1 = 2;
13023
- function estimateTextBoxAutoFitHeight(textBox, styles, measurer, pageIndex, totalPages, defaultTabStop) {
13024
- var _a, _b, _c, _d, _e;
13025
- if (!((_a = textBox.body) == null ? void 0 : _a.autoFit)) {
13026
- return textBox.height;
13027
- }
13028
- const padding = {
13029
- left: ((_b = textBox.body) == null ? void 0 : _b.paddingLeft) ?? 0,
13030
- top: ((_c = textBox.body) == null ? void 0 : _c.paddingTop) ?? 0,
13031
- right: ((_d = textBox.body) == null ? void 0 : _d.paddingRight) ?? 0,
13032
- bottom: ((_e = textBox.body) == null ? void 0 : _e.paddingBottom) ?? 0
13033
- };
13034
- const innerWidth = Math.max(1, textBox.width - padding.left - padding.right);
13035
- const contentHeight = textBox.blocks.reduce((sum, block) => {
13036
- if (block.type === "paragraph") {
13037
- const layout = projectParagraphLayout(
13038
- block,
13039
- pageIndex,
13040
- totalPages,
13041
- styles,
13042
- innerWidth,
13043
- measurer,
13044
- defaultTabStop
13045
- );
13046
- return sum + getProjectedParagraphBlockHeight(block, layout, styles);
13613
+ track.height += measuredSegmentHeight;
13614
+ segmentIndex += 1;
13615
+ if (isLastRowSplit) {
13616
+ const lastRowIndex = rowGroups[groupEndIndex - 1].startRowIndex;
13617
+ const lastRow = sourceBlock.rows[lastRowIndex];
13618
+ const ends = splitEndPositions ?? (splitEnds == null ? void 0 : splitEnds.map((blockIndex) => ({ blockIndex }))) ?? [];
13619
+ const isFinished = positionsFinishedRow(lastRow, ends);
13620
+ if (isFinished) {
13621
+ currentCellBlockPositions = void 0;
13622
+ groupStartIndex = groupEndIndex;
13623
+ } else {
13624
+ currentCellBlockPositions = ends;
13625
+ groupStartIndex = groupEndIndex - 1;
13626
+ }
13627
+ } else {
13628
+ currentCellBlockPositions = void 0;
13629
+ groupStartIndex = groupEndIndex;
13047
13630
  }
13048
- if (block.type === "table") {
13049
- return sum + estimateTableBlockHeight(
13050
- block,
13051
- styles,
13052
- innerWidth,
13053
- measurer,
13054
- defaultTabStop
13055
- );
13631
+ if (groupStartIndex < rowGroups.length) {
13632
+ track.flush();
13056
13633
  }
13057
- return sum;
13058
- }, 0);
13059
- return Math.max(
13060
- 1,
13061
- Math.ceil(
13062
- contentHeight + padding.top + padding.bottom + TEXT_BOX_AUTOFIT_SAFETY_PX$1
13063
- )
13064
- );
13634
+ }
13065
13635
  }
13066
13636
  function projectBlocksLayout(context2) {
13067
13637
  const columns = context2.pageSettings.columns;
@@ -13075,7 +13645,7 @@ function projectBlocksLayout(context2) {
13075
13645
  return projectColumnTrackLayout(context2);
13076
13646
  }
13077
13647
  function projectColumnTrackLayout(context2) {
13078
- var _a, _b;
13648
+ var _a;
13079
13649
  const {
13080
13650
  blocks,
13081
13651
  pageSettings,
@@ -13092,565 +13662,36 @@ function projectColumnTrackLayout(context2) {
13092
13662
  contentWidthOverride
13093
13663
  } = context2;
13094
13664
  const contentWidth = contentWidthOverride ?? getPageContentWidth(pageSettings);
13095
- const pages = [...existingPages];
13096
- const currentPage = pages[pages.length - 1];
13097
- let currentBlocks = currentPage ? [...currentPage.blocks] : [];
13098
- let currentHeight = currentPage ? currentPage.height : 0;
13099
- const getCurrentPageIndex = () => pageOffset + pages.length;
13100
- const getMaxHeightForPage = (pageIndex) => Math.max(
13101
- 24,
13102
- maxPageHeight - ((reservedHeightByPageIndex == null ? void 0 : reservedHeightByPageIndex.get(pageIndex)) ?? 0)
13665
+ const track = new PaginationTrack(
13666
+ pageOffset,
13667
+ maxPageHeight,
13668
+ reservedHeightByPageIndex,
13669
+ pageSettings,
13670
+ existingPages
13103
13671
  );
13104
- if (currentPage) {
13105
- pages.pop();
13106
- }
13107
- const flushPage = () => {
13108
- if (currentBlocks.length === 0 && pages.length > 0) {
13109
- return;
13110
- }
13111
- const pageIndex = getCurrentPageIndex();
13112
- const currentMaxHeight = getMaxHeightForPage(pageIndex);
13113
- pages.push({
13114
- id: `page:${pageIndex + 1}`,
13115
- index: pageIndex,
13116
- height: currentHeight,
13117
- maxHeight: currentMaxHeight,
13118
- blocks: currentBlocks,
13119
- pageSettings
13120
- });
13121
- currentBlocks = [];
13122
- currentHeight = 0;
13672
+ const params = {
13673
+ pageSettings,
13674
+ contentWidth,
13675
+ measurer,
13676
+ styles,
13677
+ totalPages,
13678
+ defaultTabStop,
13679
+ measuredHeights,
13680
+ measuredParagraphLayouts
13123
13681
  };
13124
13682
  for (let index = 0; index < blocks.length; index += 1) {
13125
13683
  const sourceBlock = blocks[index];
13126
13684
  const nextBlock = blocks[index + 1];
13127
- if (currentBlocks.length > 0 && (sourceBlock.type === "paragraph" ? getEffectiveParagraphStyle(sourceBlock, styles).pageBreakBefore : (_a = sourceBlock.style) == null ? void 0 : _a.pageBreakBefore)) {
13128
- flushPage();
13685
+ if (track.blocks.length > 0 && (sourceBlock.type === "paragraph" ? getEffectiveParagraphStyle(sourceBlock, styles).pageBreakBefore : (_a = sourceBlock.style) == null ? void 0 : _a.pageBreakBefore)) {
13686
+ track.flush();
13129
13687
  }
13130
13688
  if (sourceBlock.type === "paragraph") {
13131
- const pageIndex = pageOffset + pages.length;
13132
- const projectedParagraphLayout = projectParagraphLayoutWithExclusions(
13133
- sourceBlock,
13134
- pageSettings,
13135
- contentWidth,
13136
- measurer,
13137
- pageIndex,
13138
- totalPages,
13139
- styles,
13140
- defaultTabStop,
13141
- (textBox) => estimateTextBoxAutoFitHeight(
13142
- textBox,
13143
- styles,
13144
- measurer,
13145
- pageIndex,
13146
- totalPages,
13147
- defaultTabStop
13148
- )
13149
- );
13150
- const measuredParagraphLayout = measuredParagraphLayouts == null ? void 0 : measuredParagraphLayouts[sourceBlock.id];
13151
- const paragraphLayout = measuredParagraphLayout && isMeasuredLayoutCurrent(
13152
- projectedParagraphLayout,
13153
- measuredParagraphLayout
13154
- ) ? applyMeasuredLineGeometry(
13155
- projectedParagraphLayout,
13156
- measuredParagraphLayout
13157
- ) : projectedParagraphLayout;
13158
- let collapseWithPrevious = false;
13159
- let contextualAdjustedPreviousBlock;
13160
- let contextualAdjustedAmount = 0;
13161
- let paragraphTotalHeight = (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) ?? getProjectedParagraphBlockHeight(sourceBlock, paragraphLayout, styles);
13162
- const paragraphStyle = getEffectiveParagraphStyle(sourceBlock, styles);
13163
- const nextBlockHeight = (nextBlock == null ? void 0 : nextBlock.type) === "paragraph" ? (measuredHeights == null ? void 0 : measuredHeights[nextBlock.id]) ?? estimateParagraphBlockHeight(
13164
- nextBlock,
13165
- styles,
13166
- contentWidth,
13167
- measurer,
13168
- {
13169
- allowSpacingBefore: !shouldCollapseContextualSpacing(
13170
- sourceBlock,
13171
- nextBlock,
13172
- styles
13173
- )
13174
- },
13175
- defaultTabStop
13176
- ) : nextBlock ? (measuredHeights == null ? void 0 : measuredHeights[nextBlock.id]) ?? estimateTableBlockHeight(
13177
- nextBlock,
13178
- styles,
13179
- contentWidth,
13180
- measurer,
13181
- defaultTabStop
13182
- ) : 0;
13183
- if (paragraphStyle.keepWithNext && currentBlocks.length > 0 && currentHeight + paragraphTotalHeight + nextBlockHeight > getMaxHeightForPage(getCurrentPageIndex()) && paragraphTotalHeight + nextBlockHeight <= getMaxHeightForPage(getCurrentPageIndex())) {
13184
- flushPage();
13185
- }
13186
- const previousBlock = currentBlocks.at(-1);
13187
- const previousParagraph = (previousBlock == null ? void 0 : previousBlock.blockType) === "paragraph" && ((_b = previousBlock.sourceBlock) == null ? void 0 : _b.type) === "paragraph" ? previousBlock.sourceBlock : void 0;
13188
- collapseWithPrevious = currentBlocks.length > 0 && shouldCollapseContextualSpacing(previousParagraph, sourceBlock, styles);
13189
- if (collapseWithPrevious && previousBlock && previousParagraph) {
13190
- const previousStyle = getEffectiveParagraphStyle(
13191
- previousParagraph,
13192
- styles
13193
- );
13194
- const collapsedSpacingAfter = previousStyle.spacingAfter ?? 0;
13195
- if (collapsedSpacingAfter > 0) {
13196
- previousBlock.estimatedHeight = Math.max(
13197
- 0,
13198
- previousBlock.estimatedHeight - collapsedSpacingAfter
13199
- );
13200
- currentHeight = Math.max(0, currentHeight - collapsedSpacingAfter);
13201
- contextualAdjustedPreviousBlock = previousBlock;
13202
- contextualAdjustedAmount = collapsedSpacingAfter;
13203
- }
13204
- paragraphTotalHeight = (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) ?? getProjectedParagraphBlockHeight(
13205
- sourceBlock,
13206
- paragraphLayout,
13207
- styles,
13208
- false
13209
- );
13210
- }
13211
- if (paragraphStyle.keepLinesTogether && paragraphTotalHeight <= getMaxHeightForPage(getCurrentPageIndex())) {
13212
- if (currentBlocks.length > 0 && currentHeight + paragraphTotalHeight > getMaxHeightForPage(getCurrentPageIndex())) {
13213
- flushPage();
13214
- }
13215
- const segmentId = `${sourceBlock.id}:segment:0`;
13216
- const rawMeasuredHeight = getParagraphMeasuredHeight(
13217
- measuredHeights,
13218
- sourceBlock.id,
13219
- segmentId,
13220
- true,
13221
- paragraphTotalHeight
13222
- );
13223
- const hasMeasuredHeight = (measuredHeights == null ? void 0 : measuredHeights[segmentId]) !== void 0 || (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) !== void 0;
13224
- const measuredHeight = collapseWithPrevious && hasMeasuredHeight ? Math.max(
13225
- 0,
13226
- rawMeasuredHeight - (paragraphStyle.spacingBefore ?? 0)
13227
- ) : rawMeasuredHeight;
13228
- currentBlocks.push({
13229
- blockId: segmentId,
13230
- sourceBlockId: sourceBlock.id,
13231
- blockType: sourceBlock.type,
13232
- paragraphId: sourceBlock.id,
13233
- globalIndex: index,
13234
- estimatedHeight: measuredHeight,
13235
- layout: paragraphLayout,
13236
- sourceBlock
13237
- });
13238
- currentHeight += measuredHeight;
13239
- continue;
13240
- }
13241
- let startLineIndex = 0;
13242
- let segmentIndex2 = 0;
13243
- while (startLineIndex < paragraphLayout.lines.length) {
13244
- const remainingHeight = getMaxHeightForPage(getCurrentPageIndex()) - currentHeight;
13245
- let lineEndIndex = startLineIndex;
13246
- let segmentHeight = 0;
13247
- while (lineEndIndex < paragraphLayout.lines.length) {
13248
- const candidateLines = paragraphLayout.lines.slice(
13249
- startLineIndex,
13250
- lineEndIndex + 1
13251
- );
13252
- const candidateHeight = getParagraphSegmentHeight(
13253
- sourceBlock,
13254
- candidateLines,
13255
- startLineIndex === 0,
13256
- lineEndIndex === paragraphLayout.lines.length - 1,
13257
- styles,
13258
- !collapseWithPrevious
13259
- );
13260
- const candidateFitHeight = getParagraphSegmentFitHeight(
13261
- sourceBlock,
13262
- candidateHeight,
13263
- lineEndIndex === paragraphLayout.lines.length - 1,
13264
- styles
13265
- );
13266
- const tolerance = 1.5;
13267
- if (candidateFitHeight > remainingHeight + tolerance && lineEndIndex === startLineIndex && currentBlocks.length > 0) {
13268
- break;
13269
- }
13270
- if (candidateFitHeight > remainingHeight + tolerance && lineEndIndex > startLineIndex) {
13271
- break;
13272
- }
13273
- segmentHeight = candidateHeight;
13274
- lineEndIndex += 1;
13275
- }
13276
- if (lineEndIndex === startLineIndex && currentBlocks.length > 0) {
13277
- if (contextualAdjustedPreviousBlock && contextualAdjustedAmount > 0) {
13278
- contextualAdjustedPreviousBlock.estimatedHeight += contextualAdjustedAmount;
13279
- currentHeight += contextualAdjustedAmount;
13280
- contextualAdjustedPreviousBlock = void 0;
13281
- contextualAdjustedAmount = 0;
13282
- }
13283
- collapseWithPrevious = false;
13284
- flushPage();
13285
- continue;
13286
- }
13287
- if (lineEndIndex === startLineIndex) {
13288
- lineEndIndex = Math.min(
13289
- paragraphLayout.lines.length,
13290
- startLineIndex + 1
13291
- );
13292
- segmentHeight = getParagraphSegmentHeight(
13293
- sourceBlock,
13294
- paragraphLayout.lines.slice(startLineIndex, lineEndIndex),
13295
- startLineIndex === 0,
13296
- lineEndIndex === paragraphLayout.lines.length,
13297
- styles,
13298
- !collapseWithPrevious
13299
- );
13300
- }
13301
- if (lineEndIndex < paragraphLayout.lines.length) {
13302
- const widowOrphanAdjusted = applyWidowOrphanControl(
13303
- sourceBlock,
13304
- paragraphLayout.lines,
13305
- startLineIndex,
13306
- lineEndIndex,
13307
- styles,
13308
- !collapseWithPrevious,
13309
- true,
13310
- currentBlocks.length > 0
13311
- );
13312
- lineEndIndex = widowOrphanAdjusted.endLineIndexExclusive;
13313
- segmentHeight = widowOrphanAdjusted.height;
13314
- if (lineEndIndex === startLineIndex) {
13315
- if (contextualAdjustedPreviousBlock && contextualAdjustedAmount > 0) {
13316
- contextualAdjustedPreviousBlock.estimatedHeight += contextualAdjustedAmount;
13317
- currentHeight += contextualAdjustedAmount;
13318
- contextualAdjustedPreviousBlock = void 0;
13319
- contextualAdjustedAmount = 0;
13320
- }
13321
- collapseWithPrevious = false;
13322
- flushPage();
13323
- continue;
13324
- }
13325
- }
13326
- const segmentLayout = createParagraphSegmentLayout(
13327
- paragraphLayout,
13328
- startLineIndex,
13329
- lineEndIndex
13330
- );
13331
- const segmentId = `${sourceBlock.id}:segment:${segmentIndex2}`;
13332
- const isWholeParagraphSegment = startLineIndex === 0 && lineEndIndex === paragraphLayout.lines.length;
13333
- const rawMeasuredHeight = getParagraphMeasuredHeight(
13334
- measuredHeights,
13335
- sourceBlock.id,
13336
- segmentId,
13337
- isWholeParagraphSegment,
13338
- segmentHeight
13339
- );
13340
- const hasMeasuredHeight = (measuredHeights == null ? void 0 : measuredHeights[segmentId]) !== void 0 || isWholeParagraphSegment && (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) !== void 0;
13341
- const measuredHeight = collapseWithPrevious && startLineIndex === 0 && hasMeasuredHeight ? Math.max(
13342
- 0,
13343
- rawMeasuredHeight - (paragraphStyle.spacingBefore ?? 0)
13344
- ) : rawMeasuredHeight;
13345
- currentBlocks.push({
13346
- blockId: segmentId,
13347
- sourceBlockId: sourceBlock.id,
13348
- blockType: sourceBlock.type,
13349
- paragraphId: sourceBlock.id,
13350
- globalIndex: index,
13351
- estimatedHeight: measuredHeight,
13352
- layout: segmentLayout,
13353
- sourceBlock
13354
- });
13355
- currentHeight += measuredHeight;
13356
- startLineIndex = lineEndIndex;
13357
- segmentIndex2 += 1;
13358
- if (startLineIndex < paragraphLayout.lines.length) {
13359
- flushPage();
13360
- }
13361
- }
13362
- continue;
13363
- }
13364
- const tableHeight = (measuredHeights == null ? void 0 : measuredHeights[sourceBlock.id]) ?? estimateTableBlockHeight(
13365
- sourceBlock,
13366
- styles,
13367
- contentWidth,
13368
- measurer,
13369
- defaultTabStop
13370
- );
13371
- const maxHeightForCurrentPage = getMaxHeightForPage(getCurrentPageIndex());
13372
- const firstRow = sourceBlock.rows[0];
13373
- const canSplitSingleRow = canSplitTableRow(firstRow);
13374
- if (!canSplitSingleRow && sourceBlock.rows.length <= 1 || currentHeight + tableHeight <= maxHeightForCurrentPage) {
13375
- if (currentBlocks.length > 0 && currentHeight + tableHeight > maxHeightForCurrentPage) {
13376
- flushPage();
13377
- }
13378
- currentBlocks.push({
13379
- blockId: sourceBlock.id,
13380
- sourceBlockId: sourceBlock.id,
13381
- blockType: sourceBlock.type,
13382
- globalIndex: index,
13383
- estimatedHeight: tableHeight,
13384
- sourceBlock
13385
- });
13386
- currentHeight += tableHeight;
13689
+ paginateParagraphBlock(track, params, sourceBlock, nextBlock, index);
13387
13690
  continue;
13388
13691
  }
13389
- const rowGroups = getTableRowGroups(sourceBlock);
13390
- const headerRowCount = getRepeatableHeaderRowCount(
13391
- sourceBlock,
13392
- getTableHeaderRowCount(sourceBlock),
13393
- rowGroups
13394
- );
13395
- let groupStartIndex = 0;
13396
- let segmentIndex = 0;
13397
- let currentCellBlockPositions;
13398
- while (groupStartIndex < rowGroups.length) {
13399
- const startRowIndex = rowGroups[groupStartIndex].startRowIndex;
13400
- const repeatedHeaderRowCount = startRowIndex > 0 ? headerRowCount : 0;
13401
- const remainingHeight = getMaxHeightForPage(getCurrentPageIndex()) - currentHeight;
13402
- let groupEndIndex = groupStartIndex;
13403
- let segmentHeight = 0;
13404
- let splitEnds;
13405
- let splitEndPositions;
13406
- let isLastRowSplit = false;
13407
- while (groupEndIndex < rowGroups.length) {
13408
- const candidateEndRowIndex = rowGroups[groupEndIndex].endRowIndexExclusive;
13409
- let candidateHeight = 0;
13410
- if (groupEndIndex === groupStartIndex && currentCellBlockPositions) {
13411
- const firstRow2 = sourceBlock.rows[startRowIndex];
13412
- const starts = normalizeCellStartPositions(
13413
- firstRow2,
13414
- currentCellBlockPositions,
13415
- void 0
13416
- );
13417
- const ends = firstRow2.cells.map((cell) => ({
13418
- blockIndex: cell.blocks.length
13419
- }));
13420
- const slicedFirstRow = buildRowSliceFromPositions(
13421
- firstRow2,
13422
- starts,
13423
- ends
13424
- );
13425
- candidateHeight = getTableSegmentHeight(
13426
- {
13427
- ...sourceBlock,
13428
- rows: [slicedFirstRow]
13429
- },
13430
- 0,
13431
- 1,
13432
- repeatedHeaderRowCount,
13433
- styles,
13434
- contentWidth,
13435
- measurer,
13436
- defaultTabStop
13437
- );
13438
- } else {
13439
- candidateHeight = getTableSegmentHeight(
13440
- sourceBlock,
13441
- startRowIndex,
13442
- candidateEndRowIndex,
13443
- repeatedHeaderRowCount,
13444
- styles,
13445
- contentWidth,
13446
- measurer,
13447
- defaultTabStop
13448
- );
13449
- }
13450
- if (candidateHeight <= remainingHeight) {
13451
- segmentHeight = candidateHeight;
13452
- groupEndIndex += 1;
13453
- continue;
13454
- }
13455
- const isSingleRowGroup = candidateEndRowIndex === rowGroups[groupEndIndex].startRowIndex + 1;
13456
- const targetRow = sourceBlock.rows[rowGroups[groupEndIndex].startRowIndex];
13457
- const isSplitCandidate = isSingleRowGroup && canSplitTableRow(targetRow);
13458
- if (isSplitCandidate) {
13459
- const precedingHeight = groupEndIndex > groupStartIndex ? getTableSegmentHeight(
13460
- sourceBlock,
13461
- startRowIndex,
13462
- rowGroups[groupEndIndex].startRowIndex,
13463
- repeatedHeaderRowCount,
13464
- styles,
13465
- contentWidth,
13466
- measurer,
13467
- defaultTabStop
13468
- ) : 0;
13469
- const availableForSplitRow = remainingHeight - precedingHeight;
13470
- if (availableForSplitRow > 0) {
13471
- const starts = normalizeCellStartPositions(
13472
- targetRow,
13473
- groupEndIndex === groupStartIndex ? currentCellBlockPositions : void 0,
13474
- void 0
13475
- );
13476
- const ends = targetRow.cells.map(
13477
- (_, cellIdx) => findCellSplitEndPosition(
13478
- targetRow,
13479
- cellIdx,
13480
- starts[cellIdx] ?? { blockIndex: 0 },
13481
- availableForSplitRow,
13482
- styles,
13483
- measurer,
13484
- defaultTabStop,
13485
- contentWidth,
13486
- sourceBlock,
13487
- rowGroups[groupEndIndex].startRowIndex
13488
- )
13489
- );
13490
- const canProgress = positionsProgressed(starts, ends);
13491
- if (canProgress) {
13492
- splitEnds = positionsToBlockIndexes(ends);
13493
- isLastRowSplit = true;
13494
- const slicedLastRow = buildRowSliceFromPositions(
13495
- targetRow,
13496
- starts,
13497
- ends
13498
- );
13499
- const segmentRows = [
13500
- ...sourceBlock.rows.slice(
13501
- startRowIndex,
13502
- rowGroups[groupEndIndex].startRowIndex
13503
- ),
13504
- slicedLastRow
13505
- ];
13506
- segmentHeight = getTableSegmentHeight(
13507
- { ...sourceBlock, rows: segmentRows },
13508
- 0,
13509
- segmentRows.length,
13510
- repeatedHeaderRowCount,
13511
- styles,
13512
- contentWidth,
13513
- measurer,
13514
- defaultTabStop
13515
- );
13516
- splitEndPositions = ends;
13517
- groupEndIndex += 1;
13518
- break;
13519
- }
13520
- }
13521
- }
13522
- break;
13523
- }
13524
- if (groupEndIndex === groupStartIndex && currentBlocks.length > 0) {
13525
- flushPage();
13526
- continue;
13527
- }
13528
- if (groupEndIndex === groupStartIndex) {
13529
- const targetRow = sourceBlock.rows[startRowIndex];
13530
- const isSingleRowGroup = rowGroups[groupStartIndex].endRowIndexExclusive === startRowIndex + 1;
13531
- const isSplitCandidate = isSingleRowGroup && canSplitTableRow(targetRow);
13532
- if (isSplitCandidate) {
13533
- const starts = normalizeCellStartPositions(
13534
- targetRow,
13535
- currentCellBlockPositions,
13536
- void 0
13537
- );
13538
- let ends = starts.map(
13539
- (start, cellIdx) => findCellSplitEndPosition(
13540
- targetRow,
13541
- cellIdx,
13542
- start,
13543
- getMaxHeightForPage(getCurrentPageIndex()),
13544
- styles,
13545
- measurer,
13546
- defaultTabStop,
13547
- contentWidth,
13548
- sourceBlock,
13549
- startRowIndex
13550
- )
13551
- );
13552
- if (!positionsProgressed(starts, ends)) {
13553
- ends = starts.map((start, cellIdx) => ({
13554
- blockIndex: Math.min(
13555
- targetRow.cells[cellIdx].blocks.length,
13556
- start.blockIndex + 1
13557
- )
13558
- }));
13559
- }
13560
- splitEnds = positionsToBlockIndexes(ends);
13561
- splitEndPositions = ends;
13562
- isLastRowSplit = true;
13563
- const slicedLastRow = buildRowSliceFromPositions(
13564
- targetRow,
13565
- starts,
13566
- ends
13567
- );
13568
- segmentHeight = getTableSegmentHeight(
13569
- { ...sourceBlock, rows: [slicedLastRow] },
13570
- 0,
13571
- 1,
13572
- repeatedHeaderRowCount,
13573
- styles,
13574
- contentWidth,
13575
- measurer,
13576
- defaultTabStop
13577
- );
13578
- groupEndIndex += 1;
13579
- } else {
13580
- groupEndIndex = rowGroups[groupStartIndex].endRowIndexExclusive;
13581
- segmentHeight = getTableSegmentHeight(
13582
- sourceBlock,
13583
- startRowIndex,
13584
- groupEndIndex,
13585
- repeatedHeaderRowCount,
13586
- styles,
13587
- contentWidth,
13588
- measurer,
13589
- defaultTabStop
13590
- );
13591
- }
13592
- }
13593
- const segmentId = `${sourceBlock.id}:segment:${segmentIndex}`;
13594
- const measuredSegmentHeight = (measuredHeights == null ? void 0 : measuredHeights[segmentId]) ?? segmentHeight;
13595
- const endRowIndex = rowGroups[groupEndIndex - 1].endRowIndexExclusive;
13596
- currentBlocks.push({
13597
- blockId: segmentId,
13598
- sourceBlockId: sourceBlock.id,
13599
- blockType: sourceBlock.type,
13600
- globalIndex: index,
13601
- estimatedHeight: measuredSegmentHeight,
13602
- tableSegment: {
13603
- startRowIndex,
13604
- endRowIndex,
13605
- repeatedHeaderRowCount,
13606
- startRowCellBlockStarts: positionsToBlockIndexes(
13607
- currentCellBlockPositions
13608
- ),
13609
- endRowCellBlockEnds: splitEnds,
13610
- startRowCellBlockPositions: hasPartialPositions(
13611
- currentCellBlockPositions
13612
- ) ? currentCellBlockPositions : void 0,
13613
- endRowCellBlockPositions: hasPartialPositions(splitEndPositions) ? splitEndPositions : void 0
13614
- },
13615
- sourceBlock
13616
- });
13617
- currentHeight += measuredSegmentHeight;
13618
- segmentIndex += 1;
13619
- if (isLastRowSplit) {
13620
- const lastRowIndex = rowGroups[groupEndIndex - 1].startRowIndex;
13621
- const lastRow = sourceBlock.rows[lastRowIndex];
13622
- const ends = splitEndPositions ?? (splitEnds == null ? void 0 : splitEnds.map((blockIndex) => ({ blockIndex }))) ?? [];
13623
- const isFinished = positionsFinishedRow(lastRow, ends);
13624
- if (isFinished) {
13625
- currentCellBlockPositions = void 0;
13626
- groupStartIndex = groupEndIndex;
13627
- } else {
13628
- currentCellBlockPositions = ends;
13629
- groupStartIndex = groupEndIndex - 1;
13630
- }
13631
- } else {
13632
- currentCellBlockPositions = void 0;
13633
- groupStartIndex = groupEndIndex;
13634
- }
13635
- if (groupStartIndex < rowGroups.length) {
13636
- flushPage();
13637
- }
13638
- }
13639
- }
13640
- flushPage();
13641
- if (pages.length === 0) {
13642
- const pageIndex = pageOffset;
13643
- const currentMaxHeight = getMaxHeightForPage(pageIndex);
13644
- pages.push({
13645
- id: `page:${pageIndex + 1}`,
13646
- index: pageIndex,
13647
- height: 0,
13648
- maxHeight: currentMaxHeight,
13649
- blocks: [],
13650
- pageSettings
13651
- });
13692
+ paginateTableBlock(track, params, sourceBlock, index);
13652
13693
  }
13653
- return pages;
13694
+ return track.finalize();
13654
13695
  }
13655
13696
  function getProjectedBlocksHeight(blocks) {
13656
13697
  if (!blocks || blocks.length === 0) {