@wordpress/grid 0.6.0 → 0.7.1-next.v.202608281122.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +44 -1
  3. package/build/dashboard-grid/grid-item.cjs +21 -7
  4. package/build/dashboard-grid/grid-item.cjs.map +2 -2
  5. package/build/dashboard-grid/index.cjs +107 -45
  6. package/build/dashboard-grid/index.cjs.map +2 -2
  7. package/build/dashboard-grid/resolve-fill-widths.cjs +68 -50
  8. package/build/dashboard-grid/resolve-fill-widths.cjs.map +2 -2
  9. package/build/dashboard-grid/types.cjs.map +1 -1
  10. package/build/dashboard-lanes/index.cjs +59 -17
  11. package/build/dashboard-lanes/index.cjs.map +2 -2
  12. package/build/dashboard-lanes/lanes-item.cjs +14 -6
  13. package/build/dashboard-lanes/lanes-item.cjs.map +2 -2
  14. package/build/dashboard-lanes/types.cjs.map +1 -1
  15. package/build/index.cjs.map +1 -1
  16. package/build/shared/array-move-with-pinned.cjs +49 -0
  17. package/build/shared/array-move-with-pinned.cjs.map +7 -0
  18. package/build/shared/resize-snap.cjs +91 -5
  19. package/build/shared/resize-snap.cjs.map +2 -2
  20. package/build/shared/types.cjs.map +1 -1
  21. package/build/shared/use-span-bounds.cjs +85 -0
  22. package/build/shared/use-span-bounds.cjs.map +7 -0
  23. package/build-module/dashboard-grid/grid-item.mjs +21 -7
  24. package/build-module/dashboard-grid/grid-item.mjs.map +2 -2
  25. package/build-module/dashboard-grid/index.mjs +108 -47
  26. package/build-module/dashboard-grid/index.mjs.map +2 -2
  27. package/build-module/dashboard-grid/resolve-fill-widths.mjs +68 -50
  28. package/build-module/dashboard-grid/resolve-fill-widths.mjs.map +2 -2
  29. package/build-module/dashboard-lanes/index.mjs +60 -19
  30. package/build-module/dashboard-lanes/index.mjs.map +2 -2
  31. package/build-module/dashboard-lanes/lanes-item.mjs +14 -6
  32. package/build-module/dashboard-lanes/lanes-item.mjs.map +2 -2
  33. package/build-module/index.mjs.map +1 -1
  34. package/build-module/shared/array-move-with-pinned.mjs +24 -0
  35. package/build-module/shared/array-move-with-pinned.mjs.map +7 -0
  36. package/build-module/shared/resize-snap.mjs +87 -4
  37. package/build-module/shared/resize-snap.mjs.map +2 -2
  38. package/build-module/shared/use-span-bounds.mjs +62 -0
  39. package/build-module/shared/use-span-bounds.mjs.map +7 -0
  40. package/build-types/dashboard-grid/grid-item.d.ts +1 -1
  41. package/build-types/dashboard-grid/grid-item.d.ts.map +1 -1
  42. package/build-types/dashboard-grid/index.d.ts.map +1 -1
  43. package/build-types/dashboard-grid/resolve-fill-widths.d.ts +12 -1
  44. package/build-types/dashboard-grid/resolve-fill-widths.d.ts.map +1 -1
  45. package/build-types/dashboard-grid/types.d.ts +43 -1
  46. package/build-types/dashboard-grid/types.d.ts.map +1 -1
  47. package/build-types/dashboard-lanes/index.d.ts.map +1 -1
  48. package/build-types/dashboard-lanes/lanes-item.d.ts +18 -1
  49. package/build-types/dashboard-lanes/lanes-item.d.ts.map +1 -1
  50. package/build-types/dashboard-lanes/types.d.ts +20 -1
  51. package/build-types/dashboard-lanes/types.d.ts.map +1 -1
  52. package/build-types/index.d.ts +1 -1
  53. package/build-types/index.d.ts.map +1 -1
  54. package/build-types/shared/array-move-with-pinned.d.ts +13 -0
  55. package/build-types/shared/array-move-with-pinned.d.ts.map +1 -0
  56. package/build-types/shared/resize-snap.d.ts +65 -2
  57. package/build-types/shared/resize-snap.d.ts.map +1 -1
  58. package/build-types/shared/types.d.ts +31 -0
  59. package/build-types/shared/types.d.ts.map +1 -1
  60. package/build-types/shared/use-span-bounds.d.ts +29 -0
  61. package/build-types/shared/use-span-bounds.d.ts.map +1 -0
  62. package/package.json +7 -8
  63. package/src/dashboard-grid/grid-item.tsx +21 -7
  64. package/src/dashboard-grid/index.tsx +154 -57
  65. package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
  66. package/src/dashboard-grid/stories/index.story.tsx +154 -0
  67. package/src/dashboard-grid/test/item-interactions.test.tsx +63 -0
  68. package/src/dashboard-grid/test/item-limits.test.tsx +147 -0
  69. package/src/dashboard-grid/test/resolve-fill-widths.test.ts +158 -0
  70. package/src/dashboard-grid/types.ts +50 -0
  71. package/src/dashboard-lanes/index.tsx +86 -24
  72. package/src/dashboard-lanes/lanes-item.tsx +34 -6
  73. package/src/dashboard-lanes/stories/index.story.tsx +119 -0
  74. package/src/dashboard-lanes/test/item-interactions.test.tsx +63 -0
  75. package/src/dashboard-lanes/test/item-limits.test.tsx +140 -0
  76. package/src/dashboard-lanes/types.ts +23 -0
  77. package/src/index.ts +2 -0
  78. package/src/shared/array-move-with-pinned.ts +36 -0
  79. package/src/shared/resize-snap.ts +161 -5
  80. package/src/shared/test/array-move-with-pinned.test.ts +39 -0
  81. package/src/shared/test/resize-snap.test.ts +170 -2
  82. package/src/shared/test/use-span-bounds.test.ts +104 -0
  83. package/src/shared/types.ts +35 -0
  84. package/src/shared/use-span-bounds.ts +99 -0
@@ -7,7 +7,6 @@ import {
7
7
  useSensors,
8
8
  } from '@dnd-kit/core';
9
9
  import {
10
- arrayMove,
11
10
  SortableContext,
12
11
  sortableKeyboardCoordinates,
13
12
  } from '@dnd-kit/sortable';
@@ -25,8 +24,10 @@ import {
25
24
  useState,
26
25
  } from '@wordpress/element';
27
26
  import { GridItem } from './grid-item';
27
+ import { arrayMoveWithPinned } from '../shared/array-move-with-pinned';
28
28
  import { GridOverlay } from '../shared/grid-overlay';
29
- import { gridSpanToPixelSize } from '../shared/resize-snap';
29
+ import { clampSpan, gridSpanToPixelSize } from '../shared/resize-snap';
30
+ import { useResizePixelLimits, useSpanBounds } from '../shared/use-span-bounds';
30
31
  import layoutAnimationStyles from '../shared/layout-shift-animation.module.css';
31
32
  import { ItemExitOverlay } from '../shared/item-exit-overlay';
32
33
  import {
@@ -107,6 +108,7 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
107
108
  style,
108
109
  rowHeight = 'auto',
109
110
  minColumnWidth,
111
+ itemLimits,
110
112
  editMode = false,
111
113
  onChangeLayout,
112
114
  onPreviewLayout,
@@ -156,7 +158,6 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
156
158
  const childrenCacheRef = useRef< Map< string, React.ReactElement > >(
157
159
  new Map()
158
160
  );
159
- const activeLayout = temporaryLayout ?? layout;
160
161
 
161
162
  const [ gridRoot, setGridRoot ] = useState< HTMLDivElement | null >(
162
163
  null
@@ -227,6 +228,58 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
227
228
  : gridSpanToPixelSize( 1, 1, columnWidth, gapPx, rowHeightPx )
228
229
  .heightPx ?? undefined;
229
230
 
231
+ const spanBoundsByKey = useSpanBounds(
232
+ itemLimits,
233
+ columnWidth,
234
+ gapPx,
235
+ rowHeightPx,
236
+ effectiveColumns
237
+ );
238
+ const resizeLimitsByKey = useResizePixelLimits(
239
+ spanBoundsByKey,
240
+ columnWidth,
241
+ gapPx,
242
+ rowHeightPx
243
+ );
244
+
245
+ // Stored and in-gesture layouts may sit outside an item's limits;
246
+ // render them bounded while the data stays untouched. Gestures
247
+ // commit from the stored `layout`, never from this bounded view,
248
+ // so a bounded span cannot reach `onChangeLayout`. Keeps the
249
+ // source identity when no item needs bounding.
250
+ const sourceLayout = temporaryLayout ?? layout;
251
+ const activeLayout = useMemo( () => {
252
+ if ( spanBoundsByKey.size === 0 ) {
253
+ return sourceLayout;
254
+ }
255
+ let changed = false;
256
+ const bounded = sourceLayout.map( ( item ) => {
257
+ const bounds = spanBoundsByKey.get( item.key );
258
+ if ( ! bounds ) {
259
+ return item;
260
+ }
261
+ const width =
262
+ typeof item.width === 'number'
263
+ ? clampSpan(
264
+ item.width,
265
+ bounds.minWidth,
266
+ bounds.maxWidth
267
+ )
268
+ : item.width;
269
+ const height = clampSpan(
270
+ item.height ?? 1,
271
+ bounds.minHeight,
272
+ bounds.maxHeight
273
+ );
274
+ if ( width === item.width && height === ( item.height ?? 1 ) ) {
275
+ return item;
276
+ }
277
+ changed = true;
278
+ return { ...item, width, height };
279
+ } );
280
+ return changed ? bounded : sourceLayout;
281
+ }, [ sourceLayout, spanBoundsByKey ] );
282
+
230
283
  const layoutMap = useMemo( () => {
231
284
  const map = new Map< string, DashboardGridLayoutItem >();
232
285
  activeLayout.forEach( ( item ) => map.set( item.key, item ) );
@@ -262,12 +315,14 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
262
315
  );
263
316
  const items = sortedItems;
264
317
 
265
- // Resolve `width: 'fill'` items to concrete column spans.
318
+ // Resolve `width: 'fill'` items to concrete column spans; the
319
+ // resolver plans the row around each fill's span bounds.
266
320
  const resolvedItemMap = useMemo( () => {
267
321
  const fillWidths = resolveFillWidths(
268
322
  items,
269
323
  layoutMap,
270
- effectiveColumns
324
+ effectiveColumns,
325
+ spanBoundsByKey
271
326
  );
272
327
  if ( fillWidths.size === 0 ) {
273
328
  return layoutMap;
@@ -281,7 +336,7 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
281
336
  );
282
337
  }
283
338
  return map;
284
- }, [ items, layoutMap, effectiveColumns ] );
339
+ }, [ items, layoutMap, effectiveColumns, spanBoundsByKey ] );
285
340
 
286
341
  const [ childrenMap, actionableAreaMap, remaining, renderedByKey ] =
287
342
  useMemo( () => {
@@ -403,11 +458,27 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
403
458
  return;
404
459
  }
405
460
 
406
- const updatedItems = arrayMove( items, currentIndex, newIndex );
407
- const updatedLayout = activeLayout.map( ( item ) => ( {
408
- ...item,
409
- order: updatedItems.indexOf( item.key ),
410
- } ) );
461
+ // Non-draggable items are pinned: they hold their index while
462
+ // the others reorder around them.
463
+ const updatedItems = arrayMoveWithPinned(
464
+ items,
465
+ currentIndex,
466
+ newIndex,
467
+ ( key ) => layoutMap.get( key )?.draggable === false
468
+ );
469
+ if (
470
+ updatedItems.every( ( key, index ) => key === items[ index ] )
471
+ ) {
472
+ return;
473
+ }
474
+ // Commit from the stored layout (or the pending commit), not
475
+ // the bounded view, so untouched tiles keep their stored spans.
476
+ const updatedLayout = ( latestLayoutRef.current ?? layout ).map(
477
+ ( item ) => ( {
478
+ ...item,
479
+ order: updatedItems.indexOf( item.key ),
480
+ } )
481
+ );
411
482
 
412
483
  lastReorderCursorRef.current = {
413
484
  x: activeCenterX,
@@ -458,16 +529,15 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
458
529
  // with the live (already mutated) `activeLayout` width would
459
530
  // compound and oscillate — and stepping back through the
460
531
  // zero-delta zone would never restore the original size.
532
+ const bounds = spanBoundsByKey.get( id );
461
533
  if ( ! resizeBaselineRef.current ) {
462
- const baseItem = activeLayout.find(
463
- ( item ) => item.key === id
464
- );
534
+ const baseItem = layoutMap.get( id );
465
535
  const resolvedItem = resolvedItemMap.get( id );
466
536
  // `'fill'`/`'full'` resize from the rendered span
467
537
  // and convert to a numeric width.
468
538
  let baseWidth: number;
469
539
  if ( baseItem?.width === 'full' ) {
470
- baseWidth = effectiveColumns;
540
+ baseWidth = bounds?.maxWidth ?? effectiveColumns;
471
541
  } else if ( baseItem?.width === 'fill' ) {
472
542
  baseWidth =
473
543
  typeof resolvedItem?.width === 'number'
@@ -482,16 +552,15 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
482
552
  };
483
553
  }
484
554
  const baseline = resizeBaselineRef.current;
485
- const newWidth = Math.max(
486
- 1,
487
- Math.min(
488
- baseline.width + relativeDelta.width,
489
- effectiveColumns
490
- )
555
+ const newWidth = clampSpan(
556
+ baseline.width + relativeDelta.width,
557
+ bounds?.minWidth ?? 1,
558
+ bounds?.maxWidth ?? effectiveColumns
491
559
  );
492
- const newHeight = Math.max(
493
- 1,
494
- baseline.height + relativeDelta.height
560
+ const newHeight = clampSpan(
561
+ baseline.height + relativeDelta.height,
562
+ bounds?.minHeight ?? 1,
563
+ bounds?.maxHeight ?? Infinity
495
564
  );
496
565
 
497
566
  setResizeSnapPreview( {
@@ -512,8 +581,7 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
512
581
  const pendingItem = latestLayoutRef.current?.find(
513
582
  ( item ) => item.key === id
514
583
  );
515
- const currentItem =
516
- pendingItem ?? activeLayout.find( ( item ) => item.key === id );
584
+ const currentItem = pendingItem ?? layoutMap.get( id );
517
585
  if (
518
586
  currentItem &&
519
587
  currentItem.width === newWidth &&
@@ -522,10 +590,14 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
522
590
  return;
523
591
  }
524
592
 
525
- const updatedLayout = activeLayout.map( ( item ) =>
526
- item.key === id
527
- ? { ...item, width: newWidth, height: newHeight }
528
- : item
593
+ // Commit from the stored layout (or the pending commit), not
594
+ // the bounded view: the resized tile takes its new span and
595
+ // every other tile keeps its stored one.
596
+ const updatedLayout = ( latestLayoutRef.current ?? layout ).map(
597
+ ( item ) =>
598
+ item.key === id
599
+ ? { ...item, width: newWidth, height: newHeight }
600
+ : item
529
601
  );
530
602
 
531
603
  latestLayoutRef.current = updatedLayout;
@@ -652,34 +724,59 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
652
724
  } }
653
725
  >
654
726
  { gridOverlay }
655
- { items.map( ( id ) => (
656
- <GridItem
657
- key={ id }
658
- item={
659
- resolvedItemMap.get(
727
+ { items.map( ( id ) => {
728
+ const limitsPx = resizeLimitsByKey.get( id );
729
+ return (
730
+ <GridItem
731
+ key={ id }
732
+ item={
733
+ resolvedItemMap.get(
734
+ id
735
+ ) as DashboardGridLayoutItem
736
+ }
737
+ maxColumns={
738
+ spanBoundsByKey.get( id )?.maxWidth ??
739
+ effectiveColumns
740
+ }
741
+ disabled={ ! editMode }
742
+ draggable={
743
+ layoutMap.get( id )?.draggable !== false
744
+ }
745
+ resizable={
746
+ layoutMap.get( id )?.resizable !== false
747
+ }
748
+ verticalResizable={ rowHeight !== 'auto' }
749
+ interacting={
750
+ activeId !== null || isResizing
751
+ }
752
+ dragging={ activeId !== null }
753
+ onResize={ handleResize }
754
+ onResizeEnd={ persistTemporaryLayout }
755
+ resizeSnapPreview={
756
+ resizeSnapPreview?.id === id
757
+ ? resizeSnapPreview.snap
758
+ : null
759
+ }
760
+ minResizeWidthPx={
761
+ limitsPx?.minWidthPx ?? minResizeWidthPx
762
+ }
763
+ minResizeHeightPx={
764
+ limitsPx?.minHeightPx ??
765
+ minResizeHeightPx
766
+ }
767
+ maxResizeWidthPx={ limitsPx?.maxWidthPx }
768
+ maxResizeHeightPx={
769
+ limitsPx?.maxHeightPx ?? undefined
770
+ }
771
+ actionableArea={ actionableAreaMap.get(
660
772
  id
661
- ) as DashboardGridLayoutItem
662
- }
663
- maxColumns={ effectiveColumns }
664
- disabled={ ! editMode }
665
- verticalResizable={ rowHeight !== 'auto' }
666
- interacting={ activeId !== null || isResizing }
667
- dragging={ activeId !== null }
668
- onResize={ handleResize }
669
- onResizeEnd={ persistTemporaryLayout }
670
- resizeSnapPreview={
671
- resizeSnapPreview?.id === id
672
- ? resizeSnapPreview.snap
673
- : null
674
- }
675
- minResizeWidthPx={ minResizeWidthPx }
676
- minResizeHeightPx={ minResizeHeightPx }
677
- actionableArea={ actionableAreaMap.get( id ) }
678
- renderResizeHandle={ renderResizeHandle }
679
- >
680
- { childrenMap.get( id ) }
681
- </GridItem>
682
- ) ) }
773
+ ) }
774
+ renderResizeHandle={ renderResizeHandle }
775
+ >
776
+ { childrenMap.get( id ) }
777
+ </GridItem>
778
+ );
779
+ } ) }
683
780
  { remaining }
684
781
  { exitingItems.map( ( { key, rect, child } ) => (
685
782
  <ItemExitOverlay
@@ -15,21 +15,31 @@ import type { DashboardGridLayoutItem } from './types';
15
15
  * scanned (rows bounded by the sum of item heights), not only on
16
16
  * `maxColumns`.
17
17
  *
18
+ * Fill items honor per-item span bounds: the row plan reserves at least
19
+ * the fill's minimum span (wrapping it to the next free run when the
20
+ * current one is too narrow), and the resolved span never exceeds its
21
+ * maximum. A `'full'` item capped below the column count places like a
22
+ * fixed item of its capped width. Fixed-width items arrive already
23
+ * bounded by the caller.
24
+ *
18
25
  * @param sortedKeys - Item keys in display order.
19
26
  * @param layoutMap - Map of key to DashboardGridLayoutItem.
20
27
  * @param maxColumns - Total columns in the grid.
28
+ * @param itemBounds - Optional per-key span bounds for fill and full items.
21
29
  * @return Map of fill item keys to their resolved column spans.
22
30
  */
23
31
  export function resolveFillWidths(
24
32
  sortedKeys: string[],
25
33
  layoutMap: Map< string, DashboardGridLayoutItem >,
26
- maxColumns: number
34
+ maxColumns: number,
35
+ itemBounds?: ReadonlyMap< string, { minWidth: number; maxWidth: number } >
27
36
  ): Map< string, number > {
28
37
  const resolved = new Map< string, number >();
29
38
  const n = sortedKeys.length;
30
39
 
31
40
  // Pre-extract items, clamp widths and heights, detect which path to take.
32
41
  const items = new Array< DashboardGridLayoutItem | undefined >( n );
42
+ const kinds = new Array< 'fixed' | 'fill' | 'full' | undefined >( n );
33
43
  const widths = new Array< number >( n );
34
44
  const heights = new Array< number >( n );
35
45
  let hasFill = false;
@@ -39,17 +49,31 @@ export function resolveFillWidths(
39
49
  for ( let i = 0; i < n; i++ ) {
40
50
  const item = layoutMap.get( sortedKeys[ i ] );
41
51
  items[ i ] = item;
42
- widths[ i ] =
43
- item && typeof item.width === 'number'
44
- ? Math.min( item.width, maxColumns )
45
- : 1;
52
+ if ( ! item ) {
53
+ continue;
54
+ }
55
+ if ( item.width === 'fill' ) {
56
+ kinds[ i ] = 'fill';
57
+ widths[ i ] = 1;
58
+ hasFill = true;
59
+ } else if ( item.width === 'full' ) {
60
+ const cap = Math.min(
61
+ itemBounds?.get( item.key )?.maxWidth ?? maxColumns,
62
+ maxColumns
63
+ );
64
+ kinds[ i ] = cap < maxColumns ? 'fixed' : 'full';
65
+ widths[ i ] = cap;
66
+ } else {
67
+ kinds[ i ] = 'fixed';
68
+ widths[ i ] =
69
+ typeof item.width === 'number'
70
+ ? Math.min( item.width, maxColumns )
71
+ : 1;
72
+ }
46
73
  // Clamp to a positive integer so `0`, fractional, or negative
47
74
  // values match the `|| 1` defaulting used in GridItem styles.
48
- const h = Math.max( 1, Math.floor( item?.height ?? 1 ) );
75
+ const h = Math.max( 1, Math.floor( item.height ?? 1 ) );
49
76
  heights[ i ] = h;
50
- if ( item?.width === 'fill' ) {
51
- hasFill = true;
52
- }
53
77
  if ( h > 1 ) {
54
78
  hasMultiRow = true;
55
79
  }
@@ -69,32 +93,34 @@ export function resolveFillWidths(
69
93
  continue;
70
94
  }
71
95
 
72
- if ( item.width === 'full' ) {
96
+ if ( kinds[ i ] === 'full' ) {
73
97
  currentCol = 0;
74
98
  continue;
75
99
  }
76
100
 
77
- if ( item.width === 'fill' ) {
101
+ if ( kinds[ i ] === 'fill' ) {
102
+ const bounds = itemBounds?.get( item.key );
103
+ const minW = Math.min( bounds?.minWidth ?? 1, maxColumns );
104
+ const maxW = bounds?.maxWidth ?? maxColumns;
105
+ // Wrap first when the row cannot host the fill's floor.
106
+ if ( currentCol + minW > maxColumns ) {
107
+ currentCol = 0;
108
+ }
78
109
  let reserved = 0;
79
110
  for ( let j = i + 1; j < n; j++ ) {
80
- const next = items[ j ];
81
- if (
82
- ! next ||
83
- next.width === 'full' ||
84
- next.width === 'fill'
85
- ) {
111
+ if ( kinds[ j ] !== 'fixed' ) {
86
112
  break;
87
113
  }
88
114
  const nextW = widths[ j ];
89
- if ( currentCol + 1 + reserved + nextW <= maxColumns ) {
115
+ if ( currentCol + minW + reserved + nextW <= maxColumns ) {
90
116
  reserved += nextW;
91
117
  } else {
92
118
  break;
93
119
  }
94
120
  }
95
- const fillCols = Math.max(
96
- 1,
97
- maxColumns - currentCol - reserved
121
+ const fillCols = Math.min(
122
+ Math.max( minW, maxColumns - currentCol - reserved ),
123
+ maxW
98
124
  );
99
125
  resolved.set( item.key, fillCols );
100
126
  currentCol += fillCols;
@@ -121,6 +147,30 @@ export function resolveFillWidths(
121
147
  let cursorRow = 0;
122
148
  let cursorCol = 0;
123
149
 
150
+ // First row and column, in row-major order from the cursor, where
151
+ // `w` consecutive columns are free.
152
+ const findFreeRun = ( w: number ): [ number, number ] => {
153
+ let r = cursorRow;
154
+ let c = cursorCol;
155
+ place: for ( ; r <= totalRows; r++ ) {
156
+ c = r === cursorRow ? cursorCol : 0;
157
+ while ( c + w <= maxColumns ) {
158
+ let blocked = -1;
159
+ for ( let k = 0; k < w; k++ ) {
160
+ if ( rowOccupancy[ c + k ] > r ) {
161
+ blocked = c + k;
162
+ break;
163
+ }
164
+ }
165
+ if ( blocked === -1 ) {
166
+ break place;
167
+ }
168
+ c = blocked + 1;
169
+ }
170
+ }
171
+ return [ r, c ];
172
+ };
173
+
124
174
  for ( let i = 0; i < n; i++ ) {
125
175
  const item = items[ i ];
126
176
  if ( ! item ) {
@@ -129,7 +179,7 @@ export function resolveFillWidths(
129
179
 
130
180
  const h = heights[ i ];
131
181
 
132
- if ( item.width === 'full' ) {
182
+ if ( kinds[ i ] === 'full' ) {
133
183
  let r = cursorRow;
134
184
  for ( let c = 0; c < maxColumns; c++ ) {
135
185
  if ( rowOccupancy[ c ] > r ) {
@@ -144,20 +194,12 @@ export function resolveFillWidths(
144
194
  continue;
145
195
  }
146
196
 
147
- if ( item.width === 'fill' ) {
148
- let r = cursorRow;
149
- let c = cursorCol;
150
- scan: for ( ; r <= totalRows; r++ ) {
151
- const start = r === cursorRow ? cursorCol : 0;
152
- for ( c = start; c < maxColumns; c++ ) {
153
- if ( rowOccupancy[ c ] <= r ) {
154
- break scan;
155
- }
156
- }
157
- }
158
- const fillStartRow = r;
159
- const fillStartCol = c;
160
- let runLength = 0;
197
+ if ( kinds[ i ] === 'fill' ) {
198
+ const bounds = itemBounds?.get( item.key );
199
+ const minW = Math.min( bounds?.minWidth ?? 1, maxColumns );
200
+ const maxW = bounds?.maxWidth ?? maxColumns;
201
+ const [ fillStartRow, fillStartCol ] = findFreeRun( minW );
202
+ let runLength = minW;
161
203
  while (
162
204
  fillStartCol + runLength < maxColumns &&
163
205
  rowOccupancy[ fillStartCol + runLength ] <= fillStartRow
@@ -166,22 +208,20 @@ export function resolveFillWidths(
166
208
  }
167
209
  let reserved = 0;
168
210
  for ( let j = i + 1; j < n; j++ ) {
169
- const next = items[ j ];
170
- if (
171
- ! next ||
172
- next.width === 'full' ||
173
- next.width === 'fill'
174
- ) {
211
+ if ( kinds[ j ] !== 'fixed' ) {
175
212
  break;
176
213
  }
177
214
  const nextW = widths[ j ];
178
- if ( 1 + reserved + nextW <= runLength ) {
215
+ if ( minW + reserved + nextW <= runLength ) {
179
216
  reserved += nextW;
180
217
  } else {
181
218
  break;
182
219
  }
183
220
  }
184
- const fillCols = Math.max( 1, runLength - reserved );
221
+ const fillCols = Math.min(
222
+ Math.max( minW, runLength - reserved ),
223
+ maxW
224
+ );
185
225
  resolved.set( item.key, fillCols );
186
226
  for ( let k = 0; k < fillCols; k++ ) {
187
227
  rowOccupancy[ fillStartCol + k ] = fillStartRow + h;
@@ -192,24 +232,7 @@ export function resolveFillWidths(
192
232
  }
193
233
 
194
234
  const w = widths[ i ];
195
- let r = cursorRow;
196
- let c = cursorCol;
197
- place: for ( ; r <= totalRows; r++ ) {
198
- c = r === cursorRow ? cursorCol : 0;
199
- while ( c + w <= maxColumns ) {
200
- let blocked = -1;
201
- for ( let k = 0; k < w; k++ ) {
202
- if ( rowOccupancy[ c + k ] > r ) {
203
- blocked = c + k;
204
- break;
205
- }
206
- }
207
- if ( blocked === -1 ) {
208
- break place;
209
- }
210
- c = blocked + 1;
211
- }
212
- }
235
+ const [ r, c ] = findFreeRun( w );
213
236
  for ( let k = 0; k < w; k++ ) {
214
237
  rowOccupancy[ c + k ] = r + h;
215
238
  }