@wordpress/grid 0.5.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.
- package/CHANGELOG.md +22 -0
- package/README.md +44 -1
- package/build/dashboard-grid/grid-item.cjs +21 -7
- package/build/dashboard-grid/grid-item.cjs.map +2 -2
- package/build/dashboard-grid/index.cjs +110 -48
- package/build/dashboard-grid/index.cjs.map +2 -2
- package/build/dashboard-grid/resolve-fill-widths.cjs +68 -50
- package/build/dashboard-grid/resolve-fill-widths.cjs.map +2 -2
- package/build/dashboard-grid/types.cjs.map +1 -1
- package/build/dashboard-lanes/index.cjs +62 -20
- package/build/dashboard-lanes/index.cjs.map +2 -2
- package/build/dashboard-lanes/lanes-item.cjs +14 -6
- package/build/dashboard-lanes/lanes-item.cjs.map +2 -2
- package/build/dashboard-lanes/types.cjs.map +1 -1
- package/build/index.cjs.map +1 -1
- package/build/shared/array-move-with-pinned.cjs +49 -0
- package/build/shared/array-move-with-pinned.cjs.map +7 -0
- package/build/shared/resize-snap.cjs +91 -5
- package/build/shared/resize-snap.cjs.map +2 -2
- package/build/shared/types.cjs.map +1 -1
- package/build/shared/use-span-bounds.cjs +85 -0
- package/build/shared/use-span-bounds.cjs.map +7 -0
- package/build-module/dashboard-grid/grid-item.mjs +21 -7
- package/build-module/dashboard-grid/grid-item.mjs.map +2 -2
- package/build-module/dashboard-grid/index.mjs +111 -50
- package/build-module/dashboard-grid/index.mjs.map +2 -2
- package/build-module/dashboard-grid/resolve-fill-widths.mjs +68 -50
- package/build-module/dashboard-grid/resolve-fill-widths.mjs.map +2 -2
- package/build-module/dashboard-lanes/index.mjs +63 -22
- package/build-module/dashboard-lanes/index.mjs.map +2 -2
- package/build-module/dashboard-lanes/lanes-item.mjs +14 -6
- package/build-module/dashboard-lanes/lanes-item.mjs.map +2 -2
- package/build-module/index.mjs.map +1 -1
- package/build-module/shared/array-move-with-pinned.mjs +24 -0
- package/build-module/shared/array-move-with-pinned.mjs.map +7 -0
- package/build-module/shared/resize-snap.mjs +87 -4
- package/build-module/shared/resize-snap.mjs.map +2 -2
- package/build-module/shared/use-span-bounds.mjs +62 -0
- package/build-module/shared/use-span-bounds.mjs.map +7 -0
- package/build-types/dashboard-grid/grid-item.d.ts +1 -1
- package/build-types/dashboard-grid/grid-item.d.ts.map +1 -1
- package/build-types/dashboard-grid/index.d.ts.map +1 -1
- package/build-types/dashboard-grid/resolve-fill-widths.d.ts +12 -1
- package/build-types/dashboard-grid/resolve-fill-widths.d.ts.map +1 -1
- package/build-types/dashboard-grid/types.d.ts +43 -1
- package/build-types/dashboard-grid/types.d.ts.map +1 -1
- package/build-types/dashboard-lanes/index.d.ts.map +1 -1
- package/build-types/dashboard-lanes/lanes-item.d.ts +18 -1
- package/build-types/dashboard-lanes/lanes-item.d.ts.map +1 -1
- package/build-types/dashboard-lanes/types.d.ts +20 -1
- package/build-types/dashboard-lanes/types.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/shared/array-move-with-pinned.d.ts +13 -0
- package/build-types/shared/array-move-with-pinned.d.ts.map +1 -0
- package/build-types/shared/resize-snap.d.ts +65 -2
- package/build-types/shared/resize-snap.d.ts.map +1 -1
- package/build-types/shared/types.d.ts +31 -0
- package/build-types/shared/types.d.ts.map +1 -1
- package/build-types/shared/use-span-bounds.d.ts +29 -0
- package/build-types/shared/use-span-bounds.d.ts.map +1 -0
- package/package.json +10 -10
- package/src/dashboard-grid/grid-item.tsx +21 -7
- package/src/dashboard-grid/grid.module.css +6 -6
- package/src/dashboard-grid/index.tsx +155 -58
- package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
- package/src/dashboard-grid/stories/index.story.tsx +169 -0
- package/src/dashboard-grid/test/item-interactions.test.tsx +63 -0
- package/src/dashboard-grid/test/item-limits.test.tsx +147 -0
- package/src/dashboard-grid/test/resolve-fill-widths.test.ts +158 -0
- package/src/dashboard-grid/types.ts +50 -0
- package/src/dashboard-lanes/index.tsx +87 -25
- package/src/dashboard-lanes/lanes-item.tsx +34 -6
- package/src/dashboard-lanes/lanes.module.css +6 -6
- package/src/dashboard-lanes/stories/index.story.tsx +144 -0
- package/src/dashboard-lanes/test/item-interactions.test.tsx +63 -0
- package/src/dashboard-lanes/test/item-limits.test.tsx +140 -0
- package/src/dashboard-lanes/test/use-lane-placement.test.tsx +10 -3
- package/src/dashboard-lanes/types.ts +23 -0
- package/src/index.ts +2 -0
- package/src/shared/array-move-with-pinned.ts +36 -0
- package/src/shared/resize-snap.ts +161 -5
- package/src/shared/test/array-move-with-pinned.test.ts +39 -0
- package/src/shared/test/resize-snap.test.ts +170 -2
- package/src/shared/test/use-span-bounds.test.ts +104 -0
- package/src/shared/types.ts +35 -0
- package/src/shared/use-span-bounds.ts +99 -0
- package/build-types/dashboard-grid/stories/index.story.d.ts +0 -92
- package/build-types/dashboard-grid/stories/index.story.d.ts.map +0 -1
- package/build-types/dashboard-lanes/stories/index.story.d.ts +0 -58
- 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 */
|
|
@@ -77,9 +77,16 @@ function setNativeSupport( supported: boolean ) {
|
|
|
77
77
|
if ( property === 'display' && value === 'grid-lanes' ) {
|
|
78
78
|
return supported;
|
|
79
79
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
if ( ! originalSupports ) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
/*
|
|
84
|
+
* `call` on the overloaded `CSS.supports` resolves to the
|
|
85
|
+
* single-argument signature, so pass equivalent condition text.
|
|
86
|
+
*/
|
|
87
|
+
return value === undefined
|
|
88
|
+
? originalSupports.call( CSS, property )
|
|
89
|
+
: originalSupports.call( CSS, `(${ property }: ${ value })` );
|
|
83
90
|
};
|
|
84
91
|
}
|
|
85
92
|
|
|
@@ -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
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
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,39 @@
|
|
|
1
|
+
import { arrayMoveWithPinned } from '../array-move-with-pinned';
|
|
2
|
+
|
|
3
|
+
const pinned = new Set( [ 'p' ] );
|
|
4
|
+
const isPinned = ( key: string ) => pinned.has( key );
|
|
5
|
+
|
|
6
|
+
describe( 'arrayMoveWithPinned', () => {
|
|
7
|
+
it( 'moves like arrayMove when nothing is pinned', () => {
|
|
8
|
+
expect(
|
|
9
|
+
arrayMoveWithPinned( [ 'a', 'b', 'c' ], 2, 0, () => false )
|
|
10
|
+
).toEqual( [ 'c', 'a', 'b' ] );
|
|
11
|
+
} );
|
|
12
|
+
|
|
13
|
+
it( 'keeps a pinned item at its index when another crosses it', () => {
|
|
14
|
+
expect(
|
|
15
|
+
arrayMoveWithPinned( [ 'a', 'p', 'b' ], 2, 0, isPinned )
|
|
16
|
+
).toEqual( [ 'b', 'p', 'a' ] );
|
|
17
|
+
} );
|
|
18
|
+
|
|
19
|
+
it( 'keeps a pinned first item first', () => {
|
|
20
|
+
expect(
|
|
21
|
+
arrayMoveWithPinned( [ 'p', 'a', 'b' ], 2, 0, isPinned )
|
|
22
|
+
).toEqual( [ 'p', 'b', 'a' ] );
|
|
23
|
+
} );
|
|
24
|
+
|
|
25
|
+
it( 'does not move a pinned item', () => {
|
|
26
|
+
expect(
|
|
27
|
+
arrayMoveWithPinned( [ 'a', 'p', 'b' ], 1, 2, isPinned )
|
|
28
|
+
).toEqual( [ 'a', 'p', 'b' ] );
|
|
29
|
+
} );
|
|
30
|
+
|
|
31
|
+
it( 'holds several pinned items at once', () => {
|
|
32
|
+
const many = new Set( [ 'p', 'q' ] );
|
|
33
|
+
expect(
|
|
34
|
+
arrayMoveWithPinned( [ 'p', 'a', 'q', 'b', 'c' ], 4, 1, ( key ) =>
|
|
35
|
+
many.has( key )
|
|
36
|
+
)
|
|
37
|
+
).toEqual( [ 'p', 'c', 'q', 'a', 'b' ] );
|
|
38
|
+
} );
|
|
39
|
+
} );
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
clampResizeDelta,
|
|
3
|
+
clampSpan,
|
|
4
|
+
gridSpanToPixelSize,
|
|
5
|
+
pixelLimitsToSpanBounds,
|
|
6
|
+
} from '../resize-snap';
|
|
2
7
|
|
|
3
8
|
describe( 'gridSpanToPixelSize', () => {
|
|
4
9
|
it( 'returns one column track width for a single-column span', () => {
|
|
@@ -8,6 +13,139 @@ describe( 'gridSpanToPixelSize', () => {
|
|
|
8
13
|
} );
|
|
9
14
|
} );
|
|
10
15
|
|
|
16
|
+
describe( 'pixelLimitsToSpanBounds', () => {
|
|
17
|
+
it( 'rounds minimums up to whole tracks', () => {
|
|
18
|
+
expect(
|
|
19
|
+
pixelLimitsToSpanBounds( { minWidth: 480 }, 292, 24, 300, 4 )
|
|
20
|
+
).toEqual( {
|
|
21
|
+
minWidth: 2,
|
|
22
|
+
minHeight: 1,
|
|
23
|
+
maxWidth: 4,
|
|
24
|
+
maxHeight: Infinity,
|
|
25
|
+
} );
|
|
26
|
+
} );
|
|
27
|
+
|
|
28
|
+
it( 'covers both minimum axes when a row height is known', () => {
|
|
29
|
+
expect(
|
|
30
|
+
pixelLimitsToSpanBounds(
|
|
31
|
+
{ minWidth: 480, minHeight: 360 },
|
|
32
|
+
292,
|
|
33
|
+
24,
|
|
34
|
+
300,
|
|
35
|
+
4
|
|
36
|
+
)
|
|
37
|
+
).toEqual( {
|
|
38
|
+
minWidth: 2,
|
|
39
|
+
minHeight: 2,
|
|
40
|
+
maxWidth: 4,
|
|
41
|
+
maxHeight: Infinity,
|
|
42
|
+
} );
|
|
43
|
+
} );
|
|
44
|
+
|
|
45
|
+
it( 'rounds maximums down to whole tracks', () => {
|
|
46
|
+
expect(
|
|
47
|
+
pixelLimitsToSpanBounds( { maxWidth: 700 }, 292, 24, null, 4 )
|
|
48
|
+
).toEqual( {
|
|
49
|
+
minWidth: 1,
|
|
50
|
+
minHeight: 1,
|
|
51
|
+
maxWidth: 2,
|
|
52
|
+
maxHeight: Infinity,
|
|
53
|
+
} );
|
|
54
|
+
} );
|
|
55
|
+
|
|
56
|
+
it( 'keeps a maximum at one track or more', () => {
|
|
57
|
+
expect(
|
|
58
|
+
pixelLimitsToSpanBounds( { maxWidth: 100 }, 292, 24, null, 4 )
|
|
59
|
+
.maxWidth
|
|
60
|
+
).toBe( 1 );
|
|
61
|
+
} );
|
|
62
|
+
|
|
63
|
+
it( 'lets the minimum win over a tighter maximum', () => {
|
|
64
|
+
expect(
|
|
65
|
+
pixelLimitsToSpanBounds(
|
|
66
|
+
{ minWidth: 480, maxWidth: 300 },
|
|
67
|
+
292,
|
|
68
|
+
24,
|
|
69
|
+
null,
|
|
70
|
+
4
|
|
71
|
+
)
|
|
72
|
+
).toEqual( {
|
|
73
|
+
minWidth: 2,
|
|
74
|
+
minHeight: 1,
|
|
75
|
+
maxWidth: 2,
|
|
76
|
+
maxHeight: Infinity,
|
|
77
|
+
} );
|
|
78
|
+
} );
|
|
79
|
+
|
|
80
|
+
it( 'saturates width bounds at the column count', () => {
|
|
81
|
+
expect(
|
|
82
|
+
pixelLimitsToSpanBounds(
|
|
83
|
+
{ minWidth: 4000, maxWidth: 9000 },
|
|
84
|
+
292,
|
|
85
|
+
24,
|
|
86
|
+
null,
|
|
87
|
+
4
|
|
88
|
+
)
|
|
89
|
+
).toEqual( {
|
|
90
|
+
minWidth: 4,
|
|
91
|
+
minHeight: 1,
|
|
92
|
+
maxWidth: 4,
|
|
93
|
+
maxHeight: Infinity,
|
|
94
|
+
} );
|
|
95
|
+
} );
|
|
96
|
+
|
|
97
|
+
it( 'bounds heights against the row track', () => {
|
|
98
|
+
expect(
|
|
99
|
+
pixelLimitsToSpanBounds(
|
|
100
|
+
{ minHeight: 360, maxHeight: 800 },
|
|
101
|
+
292,
|
|
102
|
+
24,
|
|
103
|
+
300,
|
|
104
|
+
4
|
|
105
|
+
)
|
|
106
|
+
).toEqual( {
|
|
107
|
+
minWidth: 1,
|
|
108
|
+
minHeight: 2,
|
|
109
|
+
maxWidth: 4,
|
|
110
|
+
maxHeight: 2,
|
|
111
|
+
} );
|
|
112
|
+
} );
|
|
113
|
+
|
|
114
|
+
it( 'leaves height bounds open when rows are content-sized', () => {
|
|
115
|
+
expect(
|
|
116
|
+
pixelLimitsToSpanBounds(
|
|
117
|
+
{ minHeight: 360, maxHeight: 600 },
|
|
118
|
+
292,
|
|
119
|
+
24,
|
|
120
|
+
null,
|
|
121
|
+
4
|
|
122
|
+
)
|
|
123
|
+
).toEqual( {
|
|
124
|
+
minWidth: 1,
|
|
125
|
+
minHeight: 1,
|
|
126
|
+
maxWidth: 4,
|
|
127
|
+
maxHeight: Infinity,
|
|
128
|
+
} );
|
|
129
|
+
} );
|
|
130
|
+
|
|
131
|
+
it( 'falls back to the full range while the surface is unmeasured', () => {
|
|
132
|
+
expect(
|
|
133
|
+
pixelLimitsToSpanBounds(
|
|
134
|
+
{ minWidth: 480, maxWidth: 600 },
|
|
135
|
+
0,
|
|
136
|
+
24,
|
|
137
|
+
null,
|
|
138
|
+
4
|
|
139
|
+
)
|
|
140
|
+
).toEqual( {
|
|
141
|
+
minWidth: 1,
|
|
142
|
+
minHeight: 1,
|
|
143
|
+
maxWidth: 4,
|
|
144
|
+
maxHeight: Infinity,
|
|
145
|
+
} );
|
|
146
|
+
} );
|
|
147
|
+
} );
|
|
148
|
+
|
|
11
149
|
describe( 'clampResizeDelta', () => {
|
|
12
150
|
it( 'does not shrink width below one column', () => {
|
|
13
151
|
const initial = { width: 216, height: 120 };
|
|
@@ -25,11 +163,41 @@ describe( 'clampResizeDelta', () => {
|
|
|
25
163
|
).toEqual( { width: 0, height: -96 } );
|
|
26
164
|
} );
|
|
27
165
|
|
|
28
|
-
it( 'leaves growth deltas unchanged', () => {
|
|
166
|
+
it( 'leaves growth deltas unchanged without a maximum', () => {
|
|
29
167
|
const initial = { width: 100, height: 48 };
|
|
30
168
|
const min = { width: 100, height: 48 };
|
|
31
169
|
expect(
|
|
32
170
|
clampResizeDelta( { width: 80, height: 40 }, initial, min )
|
|
33
171
|
).toEqual( { width: 80, height: 40 } );
|
|
34
172
|
} );
|
|
173
|
+
|
|
174
|
+
it( 'does not grow width past the maximum', () => {
|
|
175
|
+
const initial = { width: 216, height: 120 };
|
|
176
|
+
const min = { width: 100 };
|
|
177
|
+
const max = { width: 300 };
|
|
178
|
+
expect(
|
|
179
|
+
clampResizeDelta( { width: 200, height: 0 }, initial, min, max )
|
|
180
|
+
).toEqual( { width: 84, height: 0 } );
|
|
181
|
+
} );
|
|
182
|
+
|
|
183
|
+
it( 'does not grow height past the maximum when one applies', () => {
|
|
184
|
+
const initial = { width: 200, height: 144 };
|
|
185
|
+
const min = { width: 100, height: 48 };
|
|
186
|
+
const max = { width: 400, height: 200 };
|
|
187
|
+
expect(
|
|
188
|
+
clampResizeDelta( { width: 0, height: 120 }, initial, min, max )
|
|
189
|
+
).toEqual( { width: 0, height: 56 } );
|
|
190
|
+
} );
|
|
191
|
+
} );
|
|
192
|
+
|
|
193
|
+
describe( 'clampSpan', () => {
|
|
194
|
+
it( 'keeps a span inside the inclusive range', () => {
|
|
195
|
+
expect( clampSpan( 0, 1, 4 ) ).toBe( 1 );
|
|
196
|
+
expect( clampSpan( 3, 1, 4 ) ).toBe( 3 );
|
|
197
|
+
expect( clampSpan( 9, 1, 4 ) ).toBe( 4 );
|
|
198
|
+
} );
|
|
199
|
+
|
|
200
|
+
it( 'treats an infinite maximum as open', () => {
|
|
201
|
+
expect( clampSpan( 40, 1, Infinity ) ).toBe( 40 );
|
|
202
|
+
} );
|
|
35
203
|
} );
|