vue-grid-layout-headless 4.3.1 → 4.4.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/README.md CHANGED
@@ -5,12 +5,14 @@
5
5
  [![NPM downloads][badge-npm-downloads]][url-npm]
6
6
  [![NPM Unpacked Size (with version)][badge-npm-unpacked-size]][url-npm]
7
7
 
8
- A headless grid: `useGridLayout` returns the placement of items on a grid of cells and the pointer handlers that move, resize, drop and compact them, with no styling. The pure placement functions are also exported from its `/placement` entry.
8
+ A headless grid: `useGridLayout` returns the placement of items on a grid of cells, their tiles and a `startGesture` handler that moves or resizes them, with no styling.
9
+
10
+ Design notes live in [the specs](https://github.com/Esposter/Esposter/tree/main/openspec/specs/vue-grid-layout-headless).
9
11
 
10
12
  ## Table of Contents
11
13
 
12
14
  - 🚀 [Getting Started](#getting-started)
13
- - 📖 [Documentation](#documentation)
15
+ - 🧩 [Key Exports](#key-exports)
14
16
  - ⚖️ [License](#license)
15
17
 
16
18
  ---
@@ -21,9 +23,65 @@ A headless grid: `useGridLayout` returns the placement of items on a grid of cel
21
23
  pnpm i vue-grid-layout-headless
22
24
  ```
23
25
 
24
- ## <a name="documentation">📖 Documentation</a>
26
+ ```vue
27
+ <script setup lang="ts">
28
+ import { CollisionMode, CompactType, useGridLayout } from "vue-grid-layout-headless";
29
+ import { shallowRef, useTemplateRef } from "vue";
30
+
31
+ const root = useTemplateRef<HTMLElement>("root");
32
+ const items = shallowRef([{ h: 2, id: "a", w: 4, x: 0, y: 0 }]);
33
+ const { gridHeight, startGesture, tiles } = useGridLayout(root, () => ({
34
+ collision: CollisionMode.Push,
35
+ columnNumber: 12,
36
+ compactType: CompactType.Vertical,
37
+ dragStartDistance: 4,
38
+ isEditable: true,
39
+ items: items.value,
40
+ rowHeight: 40,
41
+ spacing: { gap: 10, padding: 10 },
42
+ // A release replaces the cells it changed and drops the ids it removed
43
+ onCommit: ({ changes, removed }) => {
44
+ const changedIds = new Set(changes.map(({ id }) => id));
45
+ items.value = [...items.value.filter(({ id }) => !changedIds.has(id) && !removed.includes(id)), ...changes];
46
+ },
47
+ }));
48
+ </script>
49
+
50
+ <template>
51
+ <div ref="root" :style="{ height: `${gridHeight}px`, position: 'relative' }">
52
+ <div
53
+ v-for="{ geometry, item, offset } in tiles"
54
+ :key="item.id"
55
+ :style="{
56
+ height: `${geometry.height}px`,
57
+ left: `${geometry.left}px`,
58
+ position: 'absolute',
59
+ top: `${geometry.top}px`,
60
+ transform: `translate(${offset.x}px, ${offset.y}px)`,
61
+ width: `${geometry.width}px`,
62
+ }"
63
+ @pointerdown="startGesture($event, item)"
64
+ />
65
+ </div>
66
+ </template>
67
+ ```
68
+
69
+ Each tile's `geometry` is the box to draw it in, in pixels from the grid's top left corner, and its `offset` moves a dragged tile so it stays under the pointer.
70
+
71
+ ## <a name="key-exports">🧩 Key Exports</a>
25
72
 
26
- We highly recommend you take a look at the [documentation](https://github.com/Esposter/Esposter/tree/main/openspec/specs/vue-grid-layout-headless) to level up.
73
+ | Export | Kind | Purpose |
74
+ | ------------------- | ---------- | ---------------------------------------------------------------------------------------------------------- |
75
+ | `CollisionMode` | enum | What a dropped item does to the items it lands on: push, swap, or prevent the drop |
76
+ | `CompactType` | enum | The direction a layout compacts in: vertical, horizontal, or none |
77
+ | `GridCommit` | interface | What a release writes back: the cells one gesture changed or added, and the ids it removed |
78
+ | `GridItem` | interface | A cell the grid places, named by the entity it lays out |
79
+ | `GridLayout` | interface | What a grid hands its consumer: the tiles to draw, the drag placeholder, the height and the press handler |
80
+ | `GridLayoutOptions` | interface | What a grid lays out and how its gestures read the pointer, passed to the composable as a getter |
81
+ | `GridTile` | interface | An item as the grid draws it: its box, and the offset a dragged item is drawn at |
82
+ | `ResizeEdge` | enum | The edge or corner a resize pulls |
83
+ | `startExternalDrag` | function | Drags an item from outside every grid, such as a palette entry, and lands it on the grid under the release |
84
+ | `useGridLayout` | composable | Places items on a grid, and previews and commits the moves and resizes a pointer makes of them |
27
85
 
28
86
  ## <a name="license">⚖️ License</a>
29
87
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-grid-layout-headless",
3
- "version": "4.3.1",
3
+ "version": "4.4.0",
4
4
  "description": "A headless grid: the placement of items on a grid of cells, and the pointer gestures that move and resize them, with no styling.",
5
5
  "keywords": [
6
6
  "dashboard",
@@ -31,7 +31,6 @@
31
31
  },
32
32
  "exports": {
33
33
  ".": "./dist/index.js",
34
- "./placement": "./dist/placement.js",
35
34
  "./package.json": "./package.json"
36
35
  },
37
36
  "publishConfig": {
@@ -42,19 +41,19 @@
42
41
  "lint": "eslint .",
43
42
  "lint:fix": "eslint --fix .",
44
43
  "test": "vitest",
45
- "typecheck": "tsc"
44
+ "typecheck": "tsrs --fast"
46
45
  },
47
46
  "dependencies": {
48
- "@esposter/shared": "4.3.1"
47
+ "@esposter/shared": "4.4.0"
49
48
  },
50
49
  "devDependencies": {
51
- "@esposter/configuration": "4.3.1",
50
+ "@esposter/configuration": "4.4.0",
52
51
  "@vue/test-utils": "^2.5.1",
53
52
  "happy-dom": "^21.0.4",
54
- "tsdown": "^0.23.1"
53
+ "tsdown": "0.23.2"
55
54
  },
56
55
  "peerDependencies": {
57
56
  "vue": "^3.5.44"
58
57
  },
59
- "gitHead": "777a39636b789c13af33ed13370246ccf6dbaf31"
58
+ "gitHead": "e1a416c4b91da69cc2aa259b50a4d656f204d272"
60
59
  }
@@ -1,66 +0,0 @@
1
- //#region src/models/GridCell.d.ts
2
- interface GridCell {
3
- h: number;
4
- w: number;
5
- x: number;
6
- y: number;
7
- }
8
- //#endregion
9
- //#region src/models/GridItem.d.ts
10
- interface GridItem extends GridCell {
11
- id: string;
12
- isAutoHeight?: boolean;
13
- }
14
- //#endregion
15
- //#region src/models/CompactType.d.ts
16
- declare enum CompactType {
17
- Horizontal = "Horizontal",
18
- None = "None",
19
- Vertical = "Vertical"
20
- }
21
- //#endregion
22
- //#region src/models/GridSpacing.d.ts
23
- interface GridSpacing {
24
- gap: number;
25
- padding: number;
26
- }
27
- //#endregion
28
- //#region src/models/ResizeEdge.d.ts
29
- declare enum ResizeEdge {
30
- East = "East",
31
- North = "North",
32
- NorthEast = "NorthEast",
33
- NorthWest = "NorthWest",
34
- South = "South",
35
- SouthEast = "SouthEast",
36
- SouthWest = "SouthWest",
37
- West = "West"
38
- }
39
- //#endregion
40
- //#region src/models/CollisionMode.d.ts
41
- declare enum CollisionMode {
42
- Prevent = "Prevent",
43
- Push = "Push",
44
- Swap = "Swap"
45
- }
46
- //#endregion
47
- //#region src/models/GridCommit.d.ts
48
- interface GridCommit {
49
- changes: GridItem[];
50
- removed: string[];
51
- }
52
- //#endregion
53
- //#region src/models/GridLayoutOptions.d.ts
54
- interface GridLayoutOptions<T extends GridItem = GridItem> {
55
- collision: CollisionMode;
56
- columnNumber: number;
57
- compactType: CompactType;
58
- dragStartDistance: number;
59
- isEditable: boolean;
60
- items: T[];
61
- onCommit: (commit: GridCommit) => void;
62
- rowHeight: number;
63
- spacing: GridSpacing;
64
- }
65
- //#endregion
66
- export { GridSpacing as a, GridCell as c, ResizeEdge as i, GridCommit as n, CompactType as o, CollisionMode as r, GridItem as s, GridLayoutOptions as t };
package/dist/index.d.ts DELETED
@@ -1,52 +0,0 @@
1
- import { a as GridSpacing, c as GridCell, i as ResizeEdge, n as GridCommit, o as CompactType, r as CollisionMode, s as GridItem, t as GridLayoutOptions } from "./GridLayoutOptions-BT3wVbSx.js";
2
- import { ComputedRef, MaybeRefOrGetter, ShallowRef } from "vue";
3
- //#region src/models/GridGeometry.d.ts
4
- interface GridGeometry {
5
- height: number;
6
- left: number;
7
- top: number;
8
- width: number;
9
- }
10
- //#endregion
11
- //#region src/models/GridPoint.d.ts
12
- interface GridPoint {
13
- x: number;
14
- y: number;
15
- }
16
- //#endregion
17
- //#region src/models/GridTile.d.ts
18
- interface GridTile<T extends GridItem = GridItem> {
19
- geometry: GridGeometry;
20
- isDragged: boolean;
21
- item: T;
22
- offset: GridPoint;
23
- }
24
- //#endregion
25
- //#region src/models/GridLayout.d.ts
26
- interface GridLayout<T extends GridItem = GridItem> {
27
- gridHeight: ComputedRef<number>;
28
- placeholder: ComputedRef<GridGeometry | undefined>;
29
- reportContentHeight: (id: string, contentHeight: number) => void;
30
- startGesture: (event: PointerEvent, item: T, edge?: ResizeEdge) => void;
31
- tiles: ComputedRef<GridTile<T>[]>;
32
- }
33
- //#endregion
34
- //#region src/composables/useGridLayout.d.ts
35
- export declare const useGridLayout: <T extends GridItem>(root: Readonly<ShallowRef<HTMLElement | null>>, options: MaybeRefOrGetter<GridLayoutOptions<T>>) => GridLayout<T>;
36
- //#endregion
37
- //#region src/models/GridGesture.d.ts
38
- interface GridGesture<T extends GridItem = GridItem> {
39
- cell?: GridItem;
40
- edge?: ResizeEdge;
41
- element: Element;
42
- isStarted: boolean;
43
- offset: GridPoint;
44
- origin: T;
45
- pointerId: number;
46
- start: GridPoint;
47
- }
48
- //#endregion
49
- //#region src/services/startExternalDrag.d.ts
50
- export declare const startExternalDrag: (event: PointerEvent, item: Pick<GridItem, "h" | "id" | "w">) => void;
51
- //#endregion
52
- export { CollisionMode, CompactType, type GridCell, type GridCommit, type GridGeometry, type GridGesture, type GridItem, type GridLayout, type GridLayoutOptions, type GridPoint, type GridSpacing, type GridTile, ResizeEdge };
package/dist/index.js DELETED
@@ -1,267 +0,0 @@
1
- import { _ as CollisionMode, a as getGestureCell, c as getCellSpan, d as fitLayout, f as correctBounds, g as checkIsColliding, h as checkIsSameCell, i as getLayoutBottom, l as getCellSize, m as CompactType, o as ResizeEdge, p as compactLayout, s as getColumnPitch, t as placeMove, u as getCellPosition } from "./placeMove-DxcB1xac.js";
2
- import { checkIsNestedControl, takeOne } from "@esposter/shared";
3
- import { computed, onMounted, onUnmounted, ref, shallowRef, toValue, watch } from "vue";
4
- //#region src/services/mountedGrids.ts
5
- const mountedGrids = /* @__PURE__ */ new Map();
6
- //#endregion
7
- //#region src/services/findGridRoot.ts
8
- const findGridRoot = (element) => {
9
- let current = element;
10
- while (current && !mountedGrids.has(current)) current = current.parentElement;
11
- return current ?? void 0;
12
- };
13
- //#endregion
14
- //#region src/services/findGridAt.ts
15
- const findGridAt = (point, element) => {
16
- const hit = window.document.elementsFromPoint(point.x, point.y).find((candidate) => !element.contains(candidate));
17
- const root = findGridRoot(hit ?? null);
18
- return root ? mountedGrids.get(root) : void 0;
19
- };
20
- //#endregion
21
- //#region src/composables/useGridLayout.ts
22
- const useGridLayout = (root, options) => {
23
- const getOptions = () => toValue(options);
24
- const width = ref(0);
25
- const gesture = shallowRef();
26
- const preview = shallowRef();
27
- const columnPitch = computed(() => getColumnPitch(width.value, getOptions().columnNumber, getOptions().spacing));
28
- const rowPitch = computed(() => getOptions().rowHeight + getOptions().spacing.gap);
29
- const layout = computed(() => fitLayout(getOptions().items, getOptions().columnNumber, getOptions().compactType));
30
- const savedItemMap = computed(() => new Map(getOptions().items.map((item) => [item.id, item])));
31
- const previewItemMap = computed(() => new Map((preview.value ?? []).map((item) => [item.id, item])));
32
- const draggedGesture = computed(() => gesture.value?.isStarted && !gesture.value.edge ? gesture.value : void 0);
33
- const getGeometry = (cell) => {
34
- const { gap, padding } = getOptions().spacing;
35
- return {
36
- height: getCellSize(cell.h, rowPitch.value, gap),
37
- left: getCellPosition(cell.x, columnPitch.value, padding),
38
- top: getCellPosition(cell.y, rowPitch.value, padding),
39
- width: getCellSize(cell.w, columnPitch.value, gap)
40
- };
41
- };
42
- const tiles = computed(() => {
43
- if (width.value === 0) return [];
44
- return layout.value.map((item) => {
45
- const dragged = draggedGesture.value?.origin.id === item.id ? draggedGesture.value : void 0;
46
- const cell = dragged ? item : previewItemMap.value.get(item.id) ?? item;
47
- return {
48
- geometry: getGeometry(cell),
49
- isDragged: dragged !== void 0,
50
- item,
51
- offset: dragged?.offset ?? {
52
- x: 0,
53
- y: 0
54
- }
55
- };
56
- });
57
- });
58
- const placeholder = computed(() => {
59
- if (!gesture.value?.isStarted) return void 0;
60
- const target = previewItemMap.value.get(gesture.value.origin.id);
61
- return target ? getGeometry(target) : void 0;
62
- });
63
- const gridHeight = computed(() => {
64
- const { gap, padding } = getOptions().spacing;
65
- const layoutBottom = getLayoutBottom(preview.value ?? layout.value);
66
- return Math.max(0, getCellSize(layoutBottom, rowPitch.value, gap)) + 2 * padding;
67
- });
68
- const resetGesture = () => {
69
- gesture.value = void 0;
70
- preview.value = void 0;
71
- };
72
- watch(layout, () => resetGesture());
73
- const startGesture = (event, item, edge) => {
74
- if (event.button !== 0) return;
75
- if (!getOptions().isEditable || checkIsNestedControl(event)) return;
76
- if (gesture.value && gesture.value.pointerId !== event.pointerId) return;
77
- if (!(event.currentTarget instanceof Element) || !(event.target instanceof Element)) return;
78
- if (findGridRoot(event.target) !== root.value) return;
79
- gesture.value = {
80
- edge,
81
- element: event.currentTarget,
82
- isStarted: false,
83
- offset: {
84
- x: 0,
85
- y: 0
86
- },
87
- origin: item,
88
- pointerId: event.pointerId,
89
- start: {
90
- x: event.clientX,
91
- y: event.clientY
92
- }
93
- };
94
- };
95
- const handlePointerMove = (event) => {
96
- const activeGesture = gesture.value;
97
- if (!activeGesture || event.pointerId !== activeGesture.pointerId) return;
98
- if (!activeGesture.isStarted && event.buttons === 0) {
99
- resetGesture();
100
- return;
101
- }
102
- const offset = {
103
- x: event.clientX - activeGesture.start.x,
104
- y: event.clientY - activeGesture.start.y
105
- };
106
- if (!activeGesture.isStarted && Math.hypot(offset.x, offset.y) < getOptions().dragStartDistance) return;
107
- if (!activeGesture.isStarted) activeGesture.element.setPointerCapture(event.pointerId);
108
- const cell = getGestureCell({
109
- columnNumber: getOptions().columnNumber,
110
- columnPitch: columnPitch.value,
111
- edge: activeGesture.edge,
112
- offset,
113
- origin: activeGesture.origin,
114
- rowPitch: rowPitch.value
115
- });
116
- const isCellChanged = !activeGesture.cell || !checkIsSameCell(cell, activeGesture.cell);
117
- gesture.value = {
118
- ...activeGesture,
119
- cell,
120
- isStarted: true,
121
- offset
122
- };
123
- if (isCellChanged) preview.value = placeMove(layout.value, cell, getOptions());
124
- };
125
- const getChangedCells = (changedLayout) => changedLayout.flatMap((item) => {
126
- const savedItem = savedItemMap.value.get(item.id);
127
- return !savedItem || !checkIsSameCell(item, savedItem) ? [{
128
- h: item.h,
129
- id: item.id,
130
- w: item.w,
131
- x: item.x,
132
- y: item.y
133
- }] : [];
134
- });
135
- const accept = (item, point) => {
136
- const element = root.value;
137
- if (!element || !getOptions().isEditable) return false;
138
- const { left, top } = element.getBoundingClientRect();
139
- const { padding } = getOptions().spacing;
140
- const offset = {
141
- x: point.x - left - padding,
142
- y: point.y - top - padding
143
- };
144
- const columnIndex = Math.floor(offset.x / columnPitch.value);
145
- const rowIndex = Math.floor(offset.y / rowPitch.value);
146
- const y = Math.max(0, rowIndex);
147
- const cell = takeOne(correctBounds([{
148
- h: item.h,
149
- id: item.id,
150
- w: item.w,
151
- x: columnIndex,
152
- y
153
- }], getOptions().columnNumber));
154
- if (getOptions().collision === "Prevent" && layout.value.some((other) => checkIsColliding(cell, other))) return false;
155
- const landedLayout = placeMove([...layout.value, cell], cell, getOptions());
156
- const changes = getChangedCells(landedLayout);
157
- getOptions().onCommit({
158
- changes,
159
- removed: []
160
- });
161
- return true;
162
- };
163
- const self = { accept };
164
- const handlePointerUp = (event) => {
165
- const activeGesture = gesture.value;
166
- if (!activeGesture || event.pointerId !== activeGesture.pointerId) return;
167
- const point = {
168
- x: event.clientX,
169
- y: event.clientY
170
- };
171
- const dropGrid = draggedGesture.value ? findGridAt(point, activeGesture.element) : self;
172
- const changes = getChangedCells(preview.value ?? []);
173
- resetGesture();
174
- if (dropGrid === self) {
175
- if (changes.length > 0) getOptions().onCommit({
176
- changes,
177
- removed: []
178
- });
179
- return;
180
- }
181
- if (!dropGrid) return;
182
- if (!dropGrid.accept(activeGesture.origin, point)) return;
183
- const remainingItems = layout.value.filter((item) => item.id !== activeGesture.origin.id);
184
- const remainingLayout = compactLayout(remainingItems, getOptions().columnNumber, getOptions().compactType);
185
- getOptions().onCommit({
186
- changes: getChangedCells(remainingLayout),
187
- removed: [activeGesture.origin.id]
188
- });
189
- };
190
- const reportContentHeight = (id, contentHeight) => {
191
- const savedItem = savedItemMap.value.get(id);
192
- if (!savedItem?.isAutoHeight) return;
193
- const h = getCellSpan(contentHeight, rowPitch.value, getOptions().spacing.gap);
194
- if (h === savedItem.h) return;
195
- const otherItems = getOptions().items.filter((item) => item.id !== id);
196
- const changes = getChangedCells(fitLayout([...otherItems, {
197
- ...savedItem,
198
- h
199
- }], getOptions().columnNumber, getOptions().compactType));
200
- if (changes.length > 0) getOptions().onCommit({
201
- changes,
202
- removed: []
203
- });
204
- };
205
- const handlePointerCancel = (event) => {
206
- if (event.pointerId === gesture.value?.pointerId) resetGesture();
207
- };
208
- const attached = shallowRef();
209
- onMounted(() => {
210
- const element = root.value;
211
- if (!element) return;
212
- const controller = new AbortController();
213
- const { signal } = controller;
214
- const observer = new ResizeObserver(([entry]) => {
215
- width.value = entry?.contentRect.width ?? 0;
216
- });
217
- observer.observe(element);
218
- element.addEventListener("pointermove", handlePointerMove, { signal });
219
- element.addEventListener("pointerup", handlePointerUp, { signal });
220
- element.addEventListener("pointercancel", handlePointerCancel, { signal });
221
- mountedGrids.set(element, self);
222
- attached.value = {
223
- controller,
224
- element,
225
- observer
226
- };
227
- });
228
- onUnmounted(() => {
229
- if (!attached.value) return;
230
- const { controller, element, observer } = attached.value;
231
- observer.disconnect();
232
- mountedGrids.delete(element);
233
- controller.abort();
234
- });
235
- return {
236
- gridHeight,
237
- placeholder,
238
- reportContentHeight,
239
- startGesture,
240
- tiles
241
- };
242
- };
243
- //#endregion
244
- //#region src/services/startExternalDrag.ts
245
- const startExternalDrag = (event, item) => {
246
- if (event.button !== 0) return;
247
- if (!(event.currentTarget instanceof HTMLElement)) return;
248
- const element = event.currentTarget;
249
- const { pointerId } = event;
250
- element.setPointerCapture(pointerId);
251
- const controller = new AbortController();
252
- const { signal } = controller;
253
- element.addEventListener("pointerup", (releaseEvent) => {
254
- if (releaseEvent.pointerId !== pointerId) return;
255
- controller.abort();
256
- const point = {
257
- x: releaseEvent.clientX,
258
- y: releaseEvent.clientY
259
- };
260
- findGridAt(point, element)?.accept(item, point);
261
- }, { signal });
262
- element.addEventListener("pointercancel", (cancelEvent) => {
263
- if (cancelEvent.pointerId === pointerId) controller.abort();
264
- }, { signal });
265
- };
266
- //#endregion
267
- export { CollisionMode, CompactType, ResizeEdge, startExternalDrag, useGridLayout };
@@ -1,229 +0,0 @@
1
- //#region src/models/CollisionMode.ts
2
- let CollisionMode = /* @__PURE__ */ function(CollisionMode) {
3
- CollisionMode["Prevent"] = "Prevent";
4
- CollisionMode["Push"] = "Push";
5
- CollisionMode["Swap"] = "Swap";
6
- return CollisionMode;
7
- }({});
8
- //#endregion
9
- //#region src/placement/checkIsColliding.ts
10
- const checkIsColliding = (item, other) => item.id !== other.id && item.x < other.x + other.w && item.x + item.w > other.x && item.y < other.y + other.h && item.y + item.h > other.y;
11
- //#endregion
12
- //#region src/placement/checkIsSameCell.ts
13
- const checkIsSameCell = (cell, other) => cell.h === other.h && cell.w === other.w && cell.x === other.x && cell.y === other.y;
14
- //#endregion
15
- //#region src/models/CompactType.ts
16
- let CompactType = /* @__PURE__ */ function(CompactType) {
17
- CompactType["Horizontal"] = "Horizontal";
18
- CompactType["None"] = "None";
19
- CompactType["Vertical"] = "Vertical";
20
- return CompactType;
21
- }({});
22
- //#endregion
23
- //#region src/placement/compactLayout.ts
24
- const compactLayout = (layout, columnNumber, compactType) => {
25
- const copies = layout.map((item) => ({ ...item }));
26
- const isHorizontal = compactType === "Horizontal";
27
- const axis = isHorizontal ? "x" : "y";
28
- const crossAxis = isHorizontal ? "y" : "x";
29
- const placedItems = [];
30
- const checkIsBlocked = (candidate) => placedItems.some((placedItem) => checkIsColliding(candidate, placedItem));
31
- const riseAlongAxis = (item) => {
32
- while (item[axis] > 0 && !checkIsBlocked({
33
- ...item,
34
- [axis]: item[axis] - 1
35
- })) item[axis] -= 1;
36
- };
37
- const pushAlongAxis = (item) => {
38
- if (isHorizontal && item.x + item.w + 1 > columnNumber) {
39
- item.x = columnNumber - item.w;
40
- item.y += 1;
41
- riseAlongAxis(item);
42
- } else item[axis] += 1;
43
- };
44
- const sortedItems = copies.toSorted((firstItem, secondItem) => firstItem[axis] - secondItem[axis] || firstItem[crossAxis] - secondItem[crossAxis]);
45
- for (const item of sortedItems) {
46
- if (compactType !== "None") riseAlongAxis(item);
47
- while (checkIsBlocked(item)) pushAlongAxis(item);
48
- placedItems.push(item);
49
- }
50
- return placedItems;
51
- };
52
- //#endregion
53
- //#region src/placement/correctBounds.ts
54
- const correctBounds = (layout, columnNumber) => layout.map((item) => {
55
- const h = Math.max(1, item.h);
56
- const w = Math.max(1, Math.min(item.w, columnNumber));
57
- const x = Math.min(Math.max(item.x, 0), columnNumber - w);
58
- return {
59
- ...item,
60
- h,
61
- w,
62
- x
63
- };
64
- });
65
- //#endregion
66
- //#region src/placement/fitLayout.ts
67
- const fitLayout = (layout, columnNumber, compactType) => compactLayout(correctBounds(layout, columnNumber), columnNumber, compactType);
68
- //#endregion
69
- //#region src/placement/getCellPosition.ts
70
- const getCellPosition = (index, pitch, padding) => Math.round(index * pitch + padding);
71
- //#endregion
72
- //#region src/placement/getCellSize.ts
73
- const getCellSize = (span, pitch, gap) => span * pitch - gap;
74
- //#endregion
75
- //#region src/placement/getCellSpan.ts
76
- const getCellSpan = (size, pitch, gap) => Math.max(1, Math.ceil((size + gap) / pitch));
77
- //#endregion
78
- //#region src/placement/getColumnPitch.ts
79
- const getColumnPitch = (width, columnNumber, { gap, padding }) => (width - 2 * padding + gap) / columnNumber;
80
- //#endregion
81
- //#region src/models/ResizeEdge.ts
82
- let ResizeEdge = /* @__PURE__ */ function(ResizeEdge) {
83
- ResizeEdge["East"] = "East";
84
- ResizeEdge["North"] = "North";
85
- ResizeEdge["NorthEast"] = "NorthEast";
86
- ResizeEdge["NorthWest"] = "NorthWest";
87
- ResizeEdge["South"] = "South";
88
- ResizeEdge["SouthEast"] = "SouthEast";
89
- ResizeEdge["SouthWest"] = "SouthWest";
90
- ResizeEdge["West"] = "West";
91
- return ResizeEdge;
92
- }({});
93
- //#endregion
94
- //#region src/placement/getGestureCell.ts
95
- const ResizeEdgeSidesMap = {
96
- ["East"]: {
97
- isEast: true,
98
- isNorth: false,
99
- isSouth: false,
100
- isWest: false
101
- },
102
- ["North"]: {
103
- isEast: false,
104
- isNorth: true,
105
- isSouth: false,
106
- isWest: false
107
- },
108
- ["NorthEast"]: {
109
- isEast: true,
110
- isNorth: true,
111
- isSouth: false,
112
- isWest: false
113
- },
114
- ["NorthWest"]: {
115
- isEast: false,
116
- isNorth: true,
117
- isSouth: false,
118
- isWest: true
119
- },
120
- ["South"]: {
121
- isEast: false,
122
- isNorth: false,
123
- isSouth: true,
124
- isWest: false
125
- },
126
- ["SouthEast"]: {
127
- isEast: true,
128
- isNorth: false,
129
- isSouth: true,
130
- isWest: false
131
- },
132
- ["SouthWest"]: {
133
- isEast: false,
134
- isNorth: false,
135
- isSouth: true,
136
- isWest: true
137
- },
138
- ["West"]: {
139
- isEast: false,
140
- isNorth: false,
141
- isSouth: false,
142
- isWest: true
143
- }
144
- };
145
- const getGestureCell = ({ columnNumber, columnPitch, edge, offset, origin, rowPitch }) => {
146
- const columnDelta = Math.round(offset.x / columnPitch);
147
- const rowDelta = Math.round(offset.y / rowPitch);
148
- if (!edge) {
149
- const x = Math.min(Math.max(origin.x + columnDelta, 0), columnNumber - origin.w);
150
- const y = Math.max(0, origin.y + rowDelta);
151
- return {
152
- h: origin.h,
153
- id: origin.id,
154
- w: origin.w,
155
- x,
156
- y
157
- };
158
- }
159
- const { isEast, isNorth, isSouth, isWest } = ResizeEdgeSidesMap[edge];
160
- const right = origin.x + origin.w;
161
- const bottom = origin.y + origin.h;
162
- const leftX = Math.min(Math.max(origin.x + columnDelta, 0), right - 1);
163
- const eastWidth = Math.min(Math.max(1, origin.w + columnDelta), columnNumber - origin.x);
164
- const topY = Math.min(Math.max(origin.y + rowDelta, 0), bottom - 1);
165
- const southHeight = Math.max(1, origin.h + rowDelta);
166
- const x = isWest ? leftX : origin.x;
167
- const widthFromRight = isEast ? eastWidth : origin.w;
168
- const w = isWest ? right - leftX : widthFromRight;
169
- const y = isNorth ? topY : origin.y;
170
- const heightFromBottom = isSouth ? southHeight : origin.h;
171
- return {
172
- h: isNorth ? bottom - topY : heightFromBottom,
173
- id: origin.id,
174
- w,
175
- x,
176
- y
177
- };
178
- };
179
- //#endregion
180
- //#region src/placement/getLayoutBottom.ts
181
- const getLayoutBottom = (layout) => Math.max(0, ...layout.map((item) => item.y + item.h));
182
- //#endregion
183
- //#region src/placement/moveItem.ts
184
- const moveItem = (layout, target) => {
185
- const movedLayout = layout.map((item) => item.id === target.id ? {
186
- ...item,
187
- ...target
188
- } : { ...item });
189
- const pushedIds = /* @__PURE__ */ new Set([target.id]);
190
- const pushBelow = (pusher) => {
191
- for (const item of movedLayout) {
192
- if (pushedIds.has(item.id) || !checkIsColliding(pusher, item)) continue;
193
- if (item.y < pusher.y && pusher.y - item.y > item.h * .25) continue;
194
- pushedIds.add(item.id);
195
- item.y = pusher.y + pusher.h;
196
- pushBelow(item);
197
- }
198
- };
199
- const movedItem = movedLayout.find((item) => item.id === target.id);
200
- if (movedItem) pushBelow(movedItem);
201
- return movedLayout;
202
- };
203
- //#endregion
204
- //#region src/placement/resolveMove.ts
205
- const resolveMove = (layout, target, collision) => {
206
- const landedItems = layout.filter((item) => item.id !== target.id && checkIsColliding(target, item));
207
- const [landedItem] = landedItems;
208
- const movedItem = layout.find((item) => item.id === target.id);
209
- if (collision === "Prevent" && landedItems.length > 0) return layout.map((item) => ({ ...item }));
210
- const isMove = movedItem !== void 0 && movedItem.w === target.w && movedItem.h === target.h;
211
- if (collision === "Swap" && isMove && landedItems.length === 1 && landedItem && movedItem) {
212
- const swappedLayout = layout.map((item) => item.id === landedItem.id ? {
213
- ...item,
214
- x: movedItem.x,
215
- y: movedItem.y
216
- } : { ...item });
217
- return moveItem(swappedLayout, {
218
- ...target,
219
- x: landedItem.x,
220
- y: landedItem.y
221
- });
222
- }
223
- return moveItem(layout, target);
224
- };
225
- //#endregion
226
- //#region src/placement/placeMove.ts
227
- const placeMove = (layout, target, { collision, columnNumber, compactType }) => compactLayout(resolveMove(layout, target, collision), columnNumber, compactType);
228
- //#endregion
229
- export { CollisionMode as _, getGestureCell as a, getCellSpan as c, fitLayout as d, correctBounds as f, checkIsColliding as g, checkIsSameCell as h, getLayoutBottom as i, getCellSize as l, CompactType as m, resolveMove as n, ResizeEdge as o, compactLayout as p, moveItem as r, getColumnPitch as s, placeMove as t, getCellPosition as u };
@@ -1,53 +0,0 @@
1
- import { a as GridSpacing, c as GridCell, i as ResizeEdge, o as CompactType, r as CollisionMode, s as GridItem, t as GridLayoutOptions } from "./GridLayoutOptions-BT3wVbSx.js";
2
- //#region src/placement/checkIsColliding.d.ts
3
- export declare const checkIsColliding: (item: GridItem, other: GridItem) => boolean;
4
- //#endregion
5
- //#region src/placement/checkIsSameCell.d.ts
6
- export declare const checkIsSameCell: (cell: GridCell, other: GridCell) => boolean;
7
- //#endregion
8
- //#region src/placement/compactLayout.d.ts
9
- export declare const compactLayout: <T extends GridItem>(layout: T[], columnNumber: number, compactType: CompactType) => T[];
10
- //#endregion
11
- //#region src/placement/correctBounds.d.ts
12
- export declare const correctBounds: <T extends GridItem>(layout: T[], columnNumber: number) => T[];
13
- //#endregion
14
- //#region src/placement/fitLayout.d.ts
15
- export declare const fitLayout: <T extends GridItem>(layout: T[], columnNumber: number, compactType: CompactType) => T[];
16
- //#endregion
17
- //#region src/placement/getCellPosition.d.ts
18
- export declare const getCellPosition: (index: number, pitch: number, padding: number) => number;
19
- //#endregion
20
- //#region src/placement/getCellSize.d.ts
21
- export declare const getCellSize: (span: number, pitch: number, gap: number) => number;
22
- //#endregion
23
- //#region src/placement/getCellSpan.d.ts
24
- export declare const getCellSpan: (size: number, pitch: number, gap: number) => number;
25
- //#endregion
26
- //#region src/placement/getColumnPitch.d.ts
27
- export declare const getColumnPitch: (width: number, columnNumber: number, { gap, padding }: GridSpacing) => number;
28
- //#endregion
29
- //#region src/placement/getGestureCell.d.ts
30
- export declare const getGestureCell: ({ columnNumber, columnPitch, edge, offset, origin, rowPitch }: {
31
- columnNumber: number;
32
- columnPitch: number;
33
- edge?: ResizeEdge;
34
- offset: {
35
- x: number;
36
- y: number;
37
- };
38
- origin: GridItem;
39
- rowPitch: number;
40
- }) => GridItem;
41
- //#endregion
42
- //#region src/placement/getLayoutBottom.d.ts
43
- export declare const getLayoutBottom: (layout: GridItem[]) => number;
44
- //#endregion
45
- //#region src/placement/moveItem.d.ts
46
- export declare const moveItem: <T extends GridItem>(layout: T[], target: GridItem) => T[];
47
- //#endregion
48
- //#region src/placement/placeMove.d.ts
49
- export declare const placeMove: <T extends GridItem>(layout: T[], target: GridItem, { collision, columnNumber, compactType }: Pick<GridLayoutOptions, "collision" | "columnNumber" | "compactType">) => T[];
50
- //#endregion
51
- //#region src/placement/resolveMove.d.ts
52
- export declare const resolveMove: <T extends GridItem>(layout: T[], target: GridItem, collision: CollisionMode) => T[];
53
- //#endregion
package/dist/placement.js DELETED
@@ -1 +0,0 @@
1
- export { g as checkIsColliding, h as checkIsSameCell, p as compactLayout, f as correctBounds, d as fitLayout, u as getCellPosition, l as getCellSize, c as getCellSpan, s as getColumnPitch, a as getGestureCell, i as getLayoutBottom, r as moveItem, t as placeMove, n as resolveMove } from "./placeMove-DxcB1xac.js";