@wordpress/grid 0.5.0 → 0.7.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 (94) hide show
  1. package/CHANGELOG.md +22 -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 +110 -48
  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 +62 -20
  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 +111 -50
  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 +63 -22
  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 +12 -11
  63. package/src/dashboard-grid/grid-item.tsx +21 -7
  64. package/src/dashboard-grid/grid.module.css +6 -6
  65. package/src/dashboard-grid/index.tsx +155 -58
  66. package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
  67. package/src/dashboard-grid/stories/index.story.tsx +169 -0
  68. package/src/dashboard-grid/test/item-interactions.jsdom.test.tsx +64 -0
  69. package/src/dashboard-grid/test/item-limits.jsdom.test.tsx +147 -0
  70. package/src/dashboard-grid/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +4 -3
  71. package/src/dashboard-grid/test/resolve-fill-widths.test.ts +159 -0
  72. package/src/dashboard-grid/types.ts +50 -0
  73. package/src/dashboard-lanes/index.tsx +87 -25
  74. package/src/dashboard-lanes/lanes-item.tsx +34 -6
  75. package/src/dashboard-lanes/lanes.module.css +6 -6
  76. package/src/dashboard-lanes/stories/index.story.tsx +144 -0
  77. package/src/dashboard-lanes/test/item-interactions.jsdom.test.tsx +64 -0
  78. package/src/dashboard-lanes/test/item-limits.jsdom.test.tsx +140 -0
  79. package/src/dashboard-lanes/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +4 -3
  80. package/src/dashboard-lanes/test/lane-placement.test.ts +1 -0
  81. package/src/dashboard-lanes/test/{use-lane-placement.test.tsx → use-lane-placement.jsdom.test.tsx} +21 -13
  82. package/src/dashboard-lanes/types.ts +23 -0
  83. package/src/index.ts +2 -0
  84. package/src/shared/array-move-with-pinned.ts +36 -0
  85. package/src/shared/resize-snap.ts +161 -5
  86. package/src/shared/test/array-move-with-pinned.test.ts +40 -0
  87. package/src/shared/test/resize-snap.test.ts +171 -2
  88. package/src/shared/test/use-span-bounds.jsdom.test.ts +105 -0
  89. package/src/shared/types.ts +35 -0
  90. package/src/shared/use-span-bounds.ts +99 -0
  91. package/build-types/dashboard-grid/stories/index.story.d.ts +0 -92
  92. package/build-types/dashboard-grid/stories/index.story.d.ts.map +0 -1
  93. package/build-types/dashboard-lanes/stories/index.story.d.ts +0 -58
  94. package/build-types/dashboard-lanes/stories/index.story.d.ts.map +0 -1
@@ -0,0 +1,140 @@
1
+ import { act, fireEvent, render } from '@testing-library/react';
2
+ import { DashboardLanes } from '..';
3
+ import type { DashboardLanesLayoutItem } from '../types';
4
+
5
+ class MockResizeObserver {
6
+ observe() {}
7
+ unobserve() {}
8
+ disconnect() {}
9
+ }
10
+
11
+ let originalResizeObserver: typeof ResizeObserver;
12
+ let originalGetBoundingClientRect: typeof HTMLElement.prototype.getBoundingClientRect;
13
+
14
+ function rect( left: number, top: number, width: number, height: number ) {
15
+ return {
16
+ left,
17
+ top,
18
+ width,
19
+ height,
20
+ right: left + width,
21
+ bottom: top + height,
22
+ x: left,
23
+ y: top,
24
+ toJSON() {},
25
+ } as DOMRect;
26
+ }
27
+
28
+ // Six lanes with the fallback 24px gap. `a` stores one lane and
29
+ // declares a 60px floor that quantizes to two lanes in both geometries
30
+ // below; its rect mirrors that rendered width so the pixel clamp
31
+ // measures the tile on screen. A keyboard step is 25px: one lane on
32
+ // 44px tracks (240px container), none on 64px tracks (360px container).
33
+ const LANES = 6;
34
+ const GAP = 24;
35
+ let containerRect = rect( 0, 0, 0, 0 );
36
+ let itemRects: Record< string, DOMRect > = {};
37
+
38
+ function setGeometry( containerWidth: number ) {
39
+ const laneWidth = ( containerWidth - ( LANES - 1 ) * GAP ) / LANES;
40
+ containerRect = rect( 0, 0, containerWidth, 600 );
41
+ itemRects = { a: rect( 0, 0, 2 * laneWidth + GAP, 100 ) };
42
+ }
43
+
44
+ // Space picks the handle up, one arrow step moves 25px, Space drops.
45
+ // dnd-kit binds its keyboard listener on a timeout after pickup and
46
+ // settles the drop through a microtask and timers, so both flushes run
47
+ // inside `act`.
48
+ async function activateAndStepRight( activator: Element ) {
49
+ fireEvent.keyDown( activator, { code: 'Space' } );
50
+ act( () => {
51
+ jest.runOnlyPendingTimers();
52
+ } );
53
+ fireEvent.keyDown( activator, { code: 'ArrowRight' } );
54
+ fireEvent.keyDown( activator, { code: 'Space' } );
55
+ await act( async () => {
56
+ jest.runOnlyPendingTimers();
57
+ } );
58
+ }
59
+
60
+ beforeEach( () => {
61
+ jest.useFakeTimers();
62
+ originalResizeObserver = global.ResizeObserver;
63
+ ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
64
+ MockResizeObserver;
65
+ originalGetBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
66
+ HTMLElement.prototype.getBoundingClientRect = function () {
67
+ // eslint-disable-next-line testing-library/no-node-access
68
+ const key = this.closest( '[data-wp-grid-item-key]' )?.getAttribute(
69
+ 'data-wp-grid-item-key'
70
+ );
71
+ return ( key && itemRects[ key ] ) || containerRect;
72
+ };
73
+ } );
74
+
75
+ afterEach( () => {
76
+ ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
77
+ originalResizeObserver;
78
+ HTMLElement.prototype.getBoundingClientRect = originalGetBoundingClientRect;
79
+ jest.useRealTimers();
80
+ } );
81
+
82
+ function renderLanes( onChangeLayout: jest.Mock ) {
83
+ const layout: DashboardLanesLayoutItem[] = [ { key: 'a', width: 1 } ];
84
+ return render(
85
+ <DashboardLanes
86
+ layout={ layout }
87
+ columns={ LANES }
88
+ itemLimits={ { a: { minWidth: 60 } } }
89
+ editMode
90
+ onChangeLayout={ onChangeLayout }
91
+ >
92
+ <div key="a">A</div>
93
+ </DashboardLanes>
94
+ );
95
+ }
96
+
97
+ /* eslint-disable testing-library/no-container, testing-library/no-node-access */
98
+ function getResizeHandle( container: HTMLElement, key: string ) {
99
+ const handle = container.querySelector(
100
+ `[data-wp-grid-item-key="${ key }"] [aria-roledescription="draggable"]`
101
+ );
102
+ expect( handle ).not.toBeNull();
103
+ return handle!;
104
+ }
105
+
106
+ describe( 'DashboardLanes item limits', () => {
107
+ it( 'renders a stored span below the floor lifted to it', () => {
108
+ setGeometry( 240 );
109
+ const { container } = renderLanes( jest.fn() );
110
+ const itemA = container.querySelector( '[data-wp-grid-item-key="a"]' );
111
+ expect( itemA ).toHaveStyle( { gridColumn: 'span 2' } );
112
+ } );
113
+
114
+ it( 'resizes from the rendered span, not the stored one', async () => {
115
+ setGeometry( 240 );
116
+ const onChangeLayout = jest.fn();
117
+ const { container } = renderLanes( onChangeLayout );
118
+
119
+ // One lane to the right of the rendered two-lane span.
120
+ await activateAndStepRight( getResizeHandle( container, 'a' ) );
121
+
122
+ expect( onChangeLayout ).toHaveBeenCalledTimes( 1 );
123
+ const committed: DashboardLanesLayoutItem[] =
124
+ onChangeLayout.mock.calls[ 0 ][ 0 ];
125
+ expect( committed.find( ( item ) => item.key === 'a' )?.width ).toBe(
126
+ 3
127
+ );
128
+ } );
129
+
130
+ it( 'commits nothing while the gesture stays inside the rendered span', async () => {
131
+ setGeometry( 360 );
132
+ const onChangeLayout = jest.fn();
133
+ const { container } = renderLanes( onChangeLayout );
134
+
135
+ await activateAndStepRight( getResizeHandle( container, 'a' ) );
136
+
137
+ expect( onChangeLayout ).not.toHaveBeenCalled();
138
+ } );
139
+ } );
140
+ /* eslint-enable testing-library/no-container, testing-library/no-node-access */
@@ -1,4 +1,5 @@
1
1
  import { render } from '@testing-library/react';
2
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
3
  import { DashboardLanes } from '..';
3
4
 
4
5
  class MockResizeObserver {
@@ -17,13 +18,13 @@ class MockResizeObserver {
17
18
  let originalResizeObserver: typeof ResizeObserver;
18
19
 
19
20
  beforeEach( () => {
20
- originalResizeObserver = global.ResizeObserver;
21
- ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
21
+ originalResizeObserver = globalThis.ResizeObserver;
22
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
22
23
  MockResizeObserver;
23
24
  } );
24
25
 
25
26
  afterEach( () => {
26
- ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
27
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
27
28
  originalResizeObserver;
28
29
  } );
29
30
 
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import { computeLanePlacements } from '../lane-placement';
2
3
  import type { LanePlacementItem } from '../lane-placement';
3
4
 
@@ -1,4 +1,5 @@
1
1
  import { render, act, screen } from '@testing-library/react';
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
3
  import { useState } from '@wordpress/element';
3
4
  import { GRID_ITEM_DATA_KEY } from '../../shared/grid-item-key';
4
5
  import { useLanePlacement } from '../use-lane-placement';
@@ -54,20 +55,20 @@ let originalSupports: typeof CSS.supports | undefined;
54
55
  let originalRaf: typeof requestAnimationFrame;
55
56
 
56
57
  function installMockObserver() {
57
- originalResizeObserver = global.ResizeObserver;
58
+ originalResizeObserver = globalThis.ResizeObserver;
58
59
  MockResizeObserver.reset();
59
- ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
60
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
60
61
  MockResizeObserver;
61
62
  }
62
63
 
63
64
  function restoreObserver() {
64
- ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
65
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
65
66
  originalResizeObserver;
66
67
  }
67
68
 
68
69
  function setNativeSupport( supported: boolean ) {
69
70
  if ( typeof CSS === 'undefined' ) {
70
- ( global as unknown as { CSS: unknown } ).CSS = {
71
+ ( globalThis as unknown as { CSS: unknown } ).CSS = {
71
72
  supports: () => supported,
72
73
  };
73
74
  return;
@@ -77,9 +78,16 @@ function setNativeSupport( supported: boolean ) {
77
78
  if ( property === 'display' && value === 'grid-lanes' ) {
78
79
  return supported;
79
80
  }
80
- return originalSupports
81
- ? originalSupports.call( CSS, property, value as string )
82
- : false;
81
+ if ( ! originalSupports ) {
82
+ return false;
83
+ }
84
+ /*
85
+ * `call` on the overloaded `CSS.supports` resolves to the
86
+ * single-argument signature, so pass equivalent condition text.
87
+ */
88
+ return value === undefined
89
+ ? originalSupports.call( CSS, property )
90
+ : originalSupports.call( CSS, `(${ property }: ${ value })` );
83
91
  };
84
92
  }
85
93
 
@@ -93,20 +101,20 @@ function restoreSupport() {
93
101
  function flushRaf() {
94
102
  // jsdom polyfills `requestAnimationFrame` via `setTimeout`; an
95
103
  // `act` boundary lets React commit any state set inside the rAF.
96
- jest.runAllTimers();
104
+ vi.runAllTimers();
97
105
  }
98
106
 
99
107
  beforeEach( () => {
100
108
  installMockObserver();
101
- originalRaf = global.requestAnimationFrame;
102
- global.requestAnimationFrame = ( cb ) =>
109
+ originalRaf = globalThis.requestAnimationFrame;
110
+ globalThis.requestAnimationFrame = ( cb ) =>
103
111
  setTimeout( () => cb( performance.now() ), 0 ) as unknown as number;
104
- jest.useFakeTimers();
112
+ vi.useFakeTimers();
105
113
  } );
106
114
 
107
115
  afterEach( () => {
108
- jest.useRealTimers();
109
- global.requestAnimationFrame = originalRaf;
116
+ vi.useRealTimers();
117
+ globalThis.requestAnimationFrame = originalRaf;
110
118
  restoreObserver();
111
119
  restoreSupport();
112
120
  } );
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  DragPreviewRenderProps,
3
+ GridItemWidthLimits,
3
4
  GridOverlayRenderProps,
4
5
  ResizeHandleRenderProps,
5
6
  } from '../shared/types';
@@ -35,6 +36,22 @@ export type DashboardLanesLayoutItem = {
35
36
  */
36
37
  lane?: number;
37
38
 
39
+ /**
40
+ * Whether the item can be dragged while the surface is in edit mode.
41
+ * When `false`, the item is pinned: it also holds its index while the
42
+ * other items reorder around it.
43
+ *
44
+ * @default true
45
+ */
46
+ draggable?: boolean;
47
+
48
+ /**
49
+ * Whether the item can be resized while the surface is in edit mode.
50
+ *
51
+ * @default true
52
+ */
53
+ resizable?: boolean;
54
+
38
55
  /**
39
56
  * Display order. Lower values render first. When omitted, the
40
57
  * item falls back to its index in the `layout` array.
@@ -170,4 +187,10 @@ export interface DashboardLanesProps
170
187
  * down to 1.
171
188
  */
172
189
  minColumnWidth?: number;
190
+
191
+ /**
192
+ * Per-item width limits in pixels, keyed by layout item key. Lane
193
+ * heights are content-driven, so there is no height axis.
194
+ */
195
+ itemLimits?: Record< string, GridItemWidthLimits >;
173
196
  }
package/src/index.ts CHANGED
@@ -11,6 +11,8 @@ export type {
11
11
  } from './dashboard-lanes/types';
12
12
  export type {
13
13
  DragPreviewRenderProps,
14
+ GridItemLimits,
15
+ GridItemWidthLimits,
14
16
  GridOverlayRenderProps,
15
17
  ResizeDelta,
16
18
  ResizeHandleRenderProps,
@@ -0,0 +1,36 @@
1
+ import { arrayMove } from '@dnd-kit/sortable';
2
+
3
+ /**
4
+ * Moves the item at `from` to `to` while pinned items hold their index:
5
+ * the free items reorder among the remaining slots. Moving a pinned item
6
+ * is a no-op.
7
+ *
8
+ * @param items Items in display order.
9
+ * @param from Index of the moved item.
10
+ * @param to Target index.
11
+ * @param isPinned Whether an item holds its index.
12
+ * @return The reordered items.
13
+ */
14
+ export function arrayMoveWithPinned< T >(
15
+ items: T[],
16
+ from: number,
17
+ to: number,
18
+ isPinned: ( item: T ) => boolean
19
+ ): T[] {
20
+ const pinned = new Set( items.filter( isPinned ) );
21
+ if ( pinned.size === 0 ) {
22
+ return arrayMove( items, from, to );
23
+ }
24
+ const free = arrayMove( items, from, to ).filter(
25
+ ( item ) => ! pinned.has( item )
26
+ );
27
+ let nextFree = 0;
28
+ return items.map( ( item ) => {
29
+ if ( pinned.has( item ) ) {
30
+ return item;
31
+ }
32
+ const next = free[ nextFree ];
33
+ nextFree += 1;
34
+ return next;
35
+ } );
36
+ }
@@ -1,4 +1,4 @@
1
- import type { ResizeDelta } from './types';
1
+ import type { GridItemLimits, ResizeDelta } from './types';
2
2
 
3
3
  /**
4
4
  * Pixel dimensions for the snapped resize preview outline.
@@ -11,7 +11,7 @@ export type ResizeSnapSize = {
11
11
 
12
12
  /**
13
13
  * Clamps a resize delta so the tile cannot shrink below the given
14
- * minimum width (and height when provided).
14
+ * minimum size, nor grow past the given maximum when one applies.
15
15
  *
16
16
  * @param delta Cursor offset from the gesture start in pixels.
17
17
  * @param initialSize Size captured at gesture start.
@@ -20,19 +20,29 @@ export type ResizeSnapSize = {
20
20
  * @param minSize Minimum tile size in pixels.
21
21
  * @param minSize.width Minimum width in pixels.
22
22
  * @param minSize.height Minimum height in pixels, when vertical resize applies.
23
+ * @param maxSize Maximum tile size in pixels, when one applies.
24
+ * @param maxSize.width Maximum width in pixels.
25
+ * @param maxSize.height Maximum height in pixels, when vertical resize applies.
23
26
  */
24
27
  export function clampResizeDelta(
25
28
  delta: ResizeDelta,
26
29
  initialSize: { width: number; height: number },
27
- minSize: { width: number; height?: number }
30
+ minSize: { width: number; height?: number },
31
+ maxSize?: { width?: number; height?: number }
28
32
  ): ResizeDelta {
29
33
  const maxShrinkWidth = initialSize.width - minSize.width;
30
- const width = Math.max( delta.width, -maxShrinkWidth );
34
+ let width = Math.max( delta.width, -maxShrinkWidth );
35
+ if ( maxSize?.width !== undefined ) {
36
+ width = Math.min( width, maxSize.width - initialSize.width );
37
+ }
31
38
  if ( minSize.height === undefined ) {
32
39
  return { ...delta, width };
33
40
  }
34
41
  const maxShrinkHeight = initialSize.height - minSize.height;
35
- const height = Math.max( delta.height, -maxShrinkHeight );
42
+ let height = Math.max( delta.height, -maxShrinkHeight );
43
+ if ( maxSize?.height !== undefined ) {
44
+ height = Math.min( height, maxSize.height - initialSize.height );
45
+ }
36
46
  return { width, height };
37
47
  }
38
48
 
@@ -61,3 +71,149 @@ export function gridSpanToPixelSize(
61
71
  : rowSpan * rowHeightPx + ( rowSpan - 1 ) * gapPx;
62
72
  return { widthPx, heightPx };
63
73
  }
74
+
75
+ /**
76
+ * Span bounds derived from per-item pixel limits, in whole tracks.
77
+ * Maximums are concrete: an undeclared width maximum resolves to the
78
+ * column count, an undeclared or inapplicable height maximum to
79
+ * `Infinity`.
80
+ */
81
+ export type SpanBounds = {
82
+ minWidth: number;
83
+ minHeight: number;
84
+ maxWidth: number;
85
+ maxHeight: number;
86
+ };
87
+
88
+ /**
89
+ * Converts per-item pixel limits to span bounds with the track math of
90
+ * `gridSpanToPixelSize`. Minimums round up, maximums round down, every
91
+ * bound stays at one track or more, and a minimum wins over a smaller
92
+ * maximum. Width bounds saturate at the column count; height bounds
93
+ * resolve to `1`/`Infinity` when rows are content-sized.
94
+ *
95
+ * @param limits Size limits in pixels.
96
+ * @param columnWidth Width of one column track in pixels.
97
+ * @param gapPx Gap between tracks in pixels.
98
+ * @param rowHeightPx Row track height in pixels, or `null` when rows
99
+ * are content-sized.
100
+ * @param maxColumns Number of column tracks in the surface.
101
+ */
102
+ export function pixelLimitsToSpanBounds(
103
+ limits: GridItemLimits,
104
+ columnWidth: number,
105
+ gapPx: number,
106
+ rowHeightPx: number | null,
107
+ maxColumns: number
108
+ ): SpanBounds {
109
+ let minWidth = 1;
110
+ let maxWidth = maxColumns;
111
+ if ( columnWidth > 0 ) {
112
+ const columnTrack = columnWidth + gapPx;
113
+ if ( limits.minWidth ) {
114
+ minWidth = Math.min(
115
+ Math.max(
116
+ 1,
117
+ Math.ceil( ( limits.minWidth + gapPx ) / columnTrack )
118
+ ),
119
+ maxColumns
120
+ );
121
+ }
122
+ if ( limits.maxWidth ) {
123
+ maxWidth = Math.min(
124
+ Math.max(
125
+ 1,
126
+ Math.floor( ( limits.maxWidth + gapPx ) / columnTrack )
127
+ ),
128
+ maxColumns
129
+ );
130
+ }
131
+ }
132
+
133
+ let minHeight = 1;
134
+ let maxHeight = Infinity;
135
+ if ( rowHeightPx !== null && rowHeightPx > 0 ) {
136
+ const rowTrack = rowHeightPx + gapPx;
137
+ if ( limits.minHeight ) {
138
+ minHeight = Math.max(
139
+ 1,
140
+ Math.ceil( ( limits.minHeight + gapPx ) / rowTrack )
141
+ );
142
+ }
143
+ if ( limits.maxHeight ) {
144
+ maxHeight = Math.max(
145
+ 1,
146
+ Math.floor( ( limits.maxHeight + gapPx ) / rowTrack )
147
+ );
148
+ }
149
+ }
150
+
151
+ return {
152
+ minWidth,
153
+ minHeight,
154
+ maxWidth: Math.max( maxWidth, minWidth ),
155
+ maxHeight: Math.max( maxHeight, minHeight ),
156
+ };
157
+ }
158
+
159
+ /**
160
+ * Clamps a span to the inclusive `[ min, max ]` range.
161
+ *
162
+ * @param span Span in tracks.
163
+ * @param min Lowest allowed span.
164
+ * @param max Highest allowed span.
165
+ */
166
+ export function clampSpan( span: number, min: number, max: number ): number {
167
+ return Math.min( Math.max( span, min ), max );
168
+ }
169
+
170
+ /**
171
+ * Pixel limits for the resize gesture, derived from span bounds.
172
+ * Heights are `null` when rows are content-sized or the height
173
+ * bound is open.
174
+ */
175
+ export type ResizePixelLimits = {
176
+ minWidthPx: number;
177
+ minHeightPx: number | null;
178
+ maxWidthPx: number;
179
+ maxHeightPx: number | null;
180
+ };
181
+
182
+ /**
183
+ * Converts span bounds back to pixel limits for the resize gesture,
184
+ * using the same track math as `gridSpanToPixelSize`.
185
+ *
186
+ * @param bounds Span bounds in whole tracks.
187
+ * @param columnWidth Width of one column track in pixels.
188
+ * @param gapPx Gap between tracks in pixels.
189
+ * @param rowHeightPx Row track height in pixels, or `null` when rows
190
+ * are content-sized.
191
+ */
192
+ export function spanBoundsToPixelLimits(
193
+ bounds: SpanBounds,
194
+ columnWidth: number,
195
+ gapPx: number,
196
+ rowHeightPx: number | null
197
+ ): ResizePixelLimits {
198
+ const min = gridSpanToPixelSize(
199
+ bounds.minWidth,
200
+ bounds.minHeight,
201
+ columnWidth,
202
+ gapPx,
203
+ rowHeightPx
204
+ );
205
+ const hasMaxHeight = Number.isFinite( bounds.maxHeight );
206
+ const max = gridSpanToPixelSize(
207
+ bounds.maxWidth,
208
+ hasMaxHeight ? bounds.maxHeight : 1,
209
+ columnWidth,
210
+ gapPx,
211
+ hasMaxHeight ? rowHeightPx : null
212
+ );
213
+ return {
214
+ minWidthPx: min.widthPx,
215
+ minHeightPx: min.heightPx,
216
+ maxWidthPx: max.widthPx,
217
+ maxHeightPx: max.heightPx,
218
+ };
219
+ }
@@ -0,0 +1,40 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { arrayMoveWithPinned } from '../array-move-with-pinned';
3
+
4
+ const pinned = new Set( [ 'p' ] );
5
+ const isPinned = ( key: string ) => pinned.has( key );
6
+
7
+ describe( 'arrayMoveWithPinned', () => {
8
+ it( 'moves like arrayMove when nothing is pinned', () => {
9
+ expect(
10
+ arrayMoveWithPinned( [ 'a', 'b', 'c' ], 2, 0, () => false )
11
+ ).toEqual( [ 'c', 'a', 'b' ] );
12
+ } );
13
+
14
+ it( 'keeps a pinned item at its index when another crosses it', () => {
15
+ expect(
16
+ arrayMoveWithPinned( [ 'a', 'p', 'b' ], 2, 0, isPinned )
17
+ ).toEqual( [ 'b', 'p', 'a' ] );
18
+ } );
19
+
20
+ it( 'keeps a pinned first item first', () => {
21
+ expect(
22
+ arrayMoveWithPinned( [ 'p', 'a', 'b' ], 2, 0, isPinned )
23
+ ).toEqual( [ 'p', 'b', 'a' ] );
24
+ } );
25
+
26
+ it( 'does not move a pinned item', () => {
27
+ expect(
28
+ arrayMoveWithPinned( [ 'a', 'p', 'b' ], 1, 2, isPinned )
29
+ ).toEqual( [ 'a', 'p', 'b' ] );
30
+ } );
31
+
32
+ it( 'holds several pinned items at once', () => {
33
+ const many = new Set( [ 'p', 'q' ] );
34
+ expect(
35
+ arrayMoveWithPinned( [ 'p', 'a', 'q', 'b', 'c' ], 4, 1, ( key ) =>
36
+ many.has( key )
37
+ )
38
+ ).toEqual( [ 'p', 'c', 'q', 'a', 'b' ] );
39
+ } );
40
+ } );