vue-grid-layout-headless 4.3.3 → 4.5.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,25 +5,98 @@
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
+ This package is a headless grid with no styling. `useGridLayout` returns the placement of items on a grid of cells, their tiles, and a `startGesture` handler that moves or resizes them.
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
  ---
17
19
 
18
20
  ## <a name="getting-started">🚀 Getting Started</a>
19
21
 
22
+ 1. Install the package.
23
+
20
24
  ```bash
21
25
  pnpm i vue-grid-layout-headless
22
26
  ```
23
27
 
24
- ## <a name="documentation">📖 Documentation</a>
28
+ 2. Create a component from this code. After a release, `items` holds the changed cells.
29
+
30
+ ```vue
31
+ <script setup lang="ts">
32
+ import { CollisionMode, CompactType, ResizeEdge, useGridLayout } from "vue-grid-layout-headless";
33
+ import { shallowRef, useTemplateRef } from "vue";
34
+
35
+ const root = useTemplateRef<HTMLElement>("root");
36
+ const items = shallowRef([{ h: 2, id: "a", isAutoHeight: false, w: 4, x: 0, y: 0 }]);
37
+ const { gridHeight, startGesture, tiles } = useGridLayout(root, () => ({
38
+ collision: CollisionMode.Push,
39
+ columnNumber: 12,
40
+ compactType: CompactType.Vertical,
41
+ dragStartDistance: 4,
42
+ isEditable: true,
43
+ items: items.value,
44
+ rowHeight: 40,
45
+ spacing: { gap: 10, padding: 10 },
46
+ // A release replaces the cells it changed, and keeps each item's other fields
47
+ onCommit: ({ changes }) => {
48
+ items.value = items.value.map((item) => ({ ...item, ...changes.find(({ id }) => id === item.id) }));
49
+ },
50
+ }));
51
+ </script>
52
+
53
+ <template>
54
+ <div ref="root" :style="{ height: `${gridHeight}px`, position: 'relative' }">
55
+ <div
56
+ v-for="{ geometry, item, offset } in tiles"
57
+ :key="item.id"
58
+ :style="{
59
+ height: `${geometry.height}px`,
60
+ left: `${geometry.left}px`,
61
+ position: 'absolute',
62
+ top: `${geometry.top}px`,
63
+ transform: `translate(${offset.x}px, ${offset.y}px)`,
64
+ width: `${geometry.width}px`,
65
+ }"
66
+ @pointerdown="startGesture($event, item)"
67
+ >
68
+ <span class="resize-handle" @pointerdown.stop="startGesture($event, item, ResizeEdge.SouthEast)" />
69
+ </div>
70
+ </div>
71
+ </template>
72
+ ```
73
+
74
+ Each tile supplies these values:
75
+
76
+ - `geometry` is the box that draws the tile, in pixels from the grid's top left corner.
77
+ - `offset` moves a dragged tile, so that it stays under the pointer.
78
+
79
+ ## <a name="key-exports">🧩 Key Exports</a>
25
80
 
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.
81
+ | Export | Kind | Purpose |
82
+ | -------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
83
+ | `checkChangesHeight` | function | Whether pulling an edge or corner changes an item's height, since a north, south or corner pull has a vertical part |
84
+ | `CollisionMode` | enum | What a dropped item does to the items it lands on: push, swap, or prevent the drop |
85
+ | `CompactType` | enum | The direction a layout compacts in: vertical, horizontal, or none |
86
+ | `getFirstFreeCell` | function | The first cell a new item of a given span fits in, scanning rows from the top and columns from the left |
87
+ | `GridBlock` | interface | A cell the grid places, named by its id: what a move, a commit or a placement carries |
88
+ | `GridCommit` | interface | What a release writes back: the cells that one gesture changed |
89
+ | `GridItem` | interface | A block the grid places, named by the entity it lays out, with `isAutoHeight` for whether that entity takes its height from its content |
90
+ | `GridLayout` | interface | What a grid hands its consumer: the tiles to draw, the placeholder, the height, the press and the step |
91
+ | `GridLayoutOptions` | interface | What a grid lays out, how its gestures read the pointer, which items it refuses and which are solid, and where a body drag released over another grid goes, passed to the composable as a getter |
92
+ | `GridPlaceholder` | interface | The box a drag draws where its item would land, marked when the grid would refuse the landing |
93
+ | `GridRefusal` | type | Whether a grid refuses an item dragged into it, asked before the grid previews or lands it |
94
+ | `GridStep` | interface | A keyboard step: the whole cells an item moves by and the whole cells its span grows by |
95
+ | `GridTile` | interface | An item as the grid draws it: its box, and the offset a dragged item is drawn at |
96
+ | `GridTransfer` | interface | The hand-over of a body drag released over another grid: the item, the cell it lands in there and that grid's root element |
97
+ | `ResizeEdge` | enum | The edge or corner that a resize pulls |
98
+ | `startExternalDrag` | function | Drags an item from outside every grid, such as a palette entry. Previews it on the grid under the pointer, passes over a grid a refusal names for it, and passes the cell it lands on, and that grid's root element, to a drop callback |
99
+ | `useGridLayout` | composable | Places items on a grid. Previews and commits the moves and resizes that a pointer makes, keeps a body drag's other items in place while another grid previews it, and steps an item from the keyboard |
27
100
 
28
101
  ## <a name="license">⚖️ License</a>
29
102
 
package/dist/index.d.ts CHANGED
@@ -1,11 +1,32 @@
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-D0ot9xMz.js";
2
1
  import { ComputedRef, MaybeRefOrGetter, ShallowRef } from "vue";
2
+ interface GridCell {
3
+ h: number;
4
+ w: number;
5
+ x: number;
6
+ y: number;
7
+ }
8
+ interface GridBlock extends GridCell {
9
+ id: string;
10
+ }
11
+ interface GridItem extends GridBlock {
12
+ isAutoHeight: boolean;
13
+ }
3
14
  interface GridGeometry {
4
15
  height: number;
5
16
  left: number;
6
17
  top: number;
7
18
  width: number;
8
19
  }
20
+ interface GridPlaceholder {
21
+ geometry: GridGeometry;
22
+ isRefused: boolean;
23
+ }
24
+ interface GridStep {
25
+ dh?: number;
26
+ dw?: number;
27
+ dx?: number;
28
+ dy?: number;
29
+ }
9
30
  interface GridPoint {
10
31
  x: number;
11
32
  y: number;
@@ -16,16 +37,70 @@ interface GridTile<T extends GridItem = GridItem> {
16
37
  item: T;
17
38
  offset: GridPoint;
18
39
  }
40
+ export declare enum ResizeEdge {
41
+ East = "East",
42
+ North = "North",
43
+ NorthEast = "NorthEast",
44
+ NorthWest = "NorthWest",
45
+ South = "South",
46
+ SouthEast = "SouthEast",
47
+ SouthWest = "SouthWest",
48
+ West = "West"
49
+ }
19
50
  interface GridLayout<T extends GridItem = GridItem> {
20
51
  gridHeight: ComputedRef<number>;
21
- placeholder: ComputedRef<GridGeometry | undefined>;
52
+ placeholder: ComputedRef<GridPlaceholder | undefined>;
22
53
  reportContentHeight: (id: string, contentHeight: number) => void;
23
54
  startGesture: (event: PointerEvent, item: T, edge?: ResizeEdge) => void;
55
+ stepItem: (id: string, step: GridStep) => boolean;
24
56
  tiles: ComputedRef<GridTile<T>[]>;
25
57
  }
58
+ export declare enum CollisionMode {
59
+ Prevent = "Prevent",
60
+ Push = "Push",
61
+ Swap = "Swap"
62
+ }
63
+ export declare enum CompactType {
64
+ Horizontal = "Horizontal",
65
+ None = "None",
66
+ Vertical = "Vertical"
67
+ }
68
+ interface GridCommit {
69
+ changes: GridBlock[];
70
+ }
71
+ type GridRefusal = (item: GridItem, target: HTMLElement) => boolean;
72
+ interface GridSpacing {
73
+ gap: number;
74
+ padding: number;
75
+ }
76
+ interface GridTransfer<T extends GridItem = GridItem> {
77
+ cell: GridCell;
78
+ item: T;
79
+ target: HTMLElement;
80
+ }
81
+ interface GridLayoutOptions<T extends GridItem = GridItem> {
82
+ checkIsRefused?: GridRefusal;
83
+ checkIsSolid?: (item: T) => boolean;
84
+ collision: CollisionMode;
85
+ columnNumber: number;
86
+ compactType: CompactType;
87
+ dragStartDistance: number;
88
+ isEditable: boolean;
89
+ items: T[];
90
+ onCommit: (commit: GridCommit) => void;
91
+ onTransfer?: (transfer: GridTransfer<T>) => void;
92
+ rowHeight: number;
93
+ spacing: GridSpacing;
94
+ }
26
95
  export declare const useGridLayout: <T extends GridItem>(root: Readonly<ShallowRef<HTMLElement | null>>, options: MaybeRefOrGetter<GridLayoutOptions<T>>) => GridLayout<T>;
27
- interface GridGesture<T extends GridItem = GridItem> {
28
- cell?: GridItem;
96
+ interface MountedGrid {
97
+ accept: (item: GridItem, point: GridPoint) => GridCell | undefined;
98
+ clearPreview: () => void;
99
+ element: HTMLElement;
100
+ preview: (item: GridItem, point: GridPoint) => boolean;
101
+ }
102
+ interface GridGesture<T extends GridBlock = GridBlock> {
103
+ cell?: GridBlock;
29
104
  edge?: ResizeEdge;
30
105
  element: Element;
31
106
  isStarted: boolean;
@@ -33,6 +108,12 @@ interface GridGesture<T extends GridItem = GridItem> {
33
108
  origin: T;
34
109
  pointerId: number;
35
110
  start: GridPoint;
111
+ transfer?: MountedGrid;
36
112
  }
37
- export declare const startExternalDrag: (event: PointerEvent, item: Pick<GridItem, "h" | "id" | "w">) => void;
38
- export { CollisionMode, CompactType, type GridCell, type GridCommit, type GridGeometry, type GridGesture, type GridItem, type GridLayout, type GridLayoutOptions, type GridPoint, type GridSpacing, type GridTile, ResizeEdge };
113
+ export declare const checkChangesHeight: (edge: ResizeEdge) => boolean;
114
+ export declare const getFirstFreeCell: (layout: GridBlock[], columnNumber: number, span: {
115
+ h: number;
116
+ w: number;
117
+ }) => GridCell;
118
+ export declare const startExternalDrag: (event: PointerEvent, item: GridItem, onDrop: (cell: GridCell, target: HTMLElement) => void, checkIsRefused?: GridRefusal) => void;
119
+ export type { GridBlock, GridCell, GridCommit, GridGeometry, GridGesture, GridItem, GridLayout, GridLayoutOptions, GridPlaceholder, GridPoint, GridRefusal, GridSpacing, GridStep, GridTile, GridTransfer };
package/dist/index.js CHANGED
@@ -1,6 +1,232 @@
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-BPQPi70h.js";
2
- import { checkIsNestedControl, takeOne } from "@esposter/shared";
1
+ import { checkIsNestedControl } from "@esposter/shared";
3
2
  import { computed, onMounted, onUnmounted, ref, shallowRef, toValue, watch } from "vue";
3
+ // src/models/ResizeEdge.ts
4
+ let ResizeEdge = /* @__PURE__ */ function(ResizeEdge) {
5
+ ResizeEdge["East"] = "East";
6
+ ResizeEdge["North"] = "North";
7
+ ResizeEdge["NorthEast"] = "NorthEast";
8
+ ResizeEdge["NorthWest"] = "NorthWest";
9
+ ResizeEdge["South"] = "South";
10
+ ResizeEdge["SouthEast"] = "SouthEast";
11
+ ResizeEdge["SouthWest"] = "SouthWest";
12
+ ResizeEdge["West"] = "West";
13
+ return ResizeEdge;
14
+ }({});
15
+ // src/placement/checkChangesHeight.ts
16
+ const HEIGHT_EDGES = /* @__PURE__ */ new Set([
17
+ "North",
18
+ "NorthEast",
19
+ "NorthWest",
20
+ "South",
21
+ "SouthEast",
22
+ "SouthWest"
23
+ ]);
24
+ const checkChangesHeight = (edge) => HEIGHT_EDGES.has(edge);
25
+ // src/placement/checkIsSameCell.ts
26
+ const checkIsSameCell = (cell, other) => cell.h === other.h && cell.w === other.w && cell.x === other.x && cell.y === other.y;
27
+ // src/placement/clampCell.ts
28
+ const clampCell = (cell, columnNumber) => {
29
+ const h = Math.max(1, cell.h);
30
+ const w = Math.max(1, Math.min(cell.w, columnNumber));
31
+ const x = Math.min(Math.max(cell.x, 0), columnNumber - w);
32
+ return {
33
+ ...cell,
34
+ h,
35
+ w,
36
+ x,
37
+ y: Math.max(cell.y, 0)
38
+ };
39
+ };
40
+ // src/models/CompactType.ts
41
+ let CompactType = /* @__PURE__ */ function(CompactType) {
42
+ CompactType["Horizontal"] = "Horizontal";
43
+ CompactType["None"] = "None";
44
+ CompactType["Vertical"] = "Vertical";
45
+ return CompactType;
46
+ }({});
47
+ // src/placement/checkIsColliding.ts
48
+ 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;
49
+ // src/placement/compactLayout.ts
50
+ const compactLayout = (layout, columnNumber, compactType, solidIds = /* @__PURE__ */ new Set()) => {
51
+ const copies = layout.map((item) => ({ ...item }));
52
+ const isHorizontal = compactType === "Horizontal";
53
+ const axis = isHorizontal ? "x" : "y";
54
+ const crossAxis = isHorizontal ? "y" : "x";
55
+ const placedItems = [];
56
+ const checkIsBlocked = (candidate) => placedItems.some((placedItem) => checkIsColliding(candidate, placedItem));
57
+ const riseAlongAxis = (item) => {
58
+ while (item[axis] > 0 && !checkIsBlocked({
59
+ ...item,
60
+ [axis]: item[axis] - 1
61
+ })) item[axis] -= 1;
62
+ };
63
+ const pushAlongAxis = (item) => {
64
+ if (isHorizontal && item.x + item.w + 1 > columnNumber) {
65
+ item.x = columnNumber - item.w;
66
+ item.y += 1;
67
+ riseAlongAxis(item);
68
+ } else item[axis] += 1;
69
+ };
70
+ const sortedItems = copies.toSorted((firstItem, secondItem) => firstItem[axis] - secondItem[axis] || firstItem[crossAxis] - secondItem[crossAxis]);
71
+ const placementOrder = [...sortedItems.filter((item) => solidIds.has(item.id)), ...sortedItems.filter((item) => !solidIds.has(item.id))];
72
+ for (const item of placementOrder) {
73
+ if (compactType !== "None" && !solidIds.has(item.id)) riseAlongAxis(item);
74
+ while (checkIsBlocked(item)) pushAlongAxis(item);
75
+ placedItems.push(item);
76
+ }
77
+ return sortedItems;
78
+ };
79
+ // src/placement/fitLayout.ts
80
+ const fitLayout = (layout, columnNumber, compactType, solidIds) => compactLayout(layout.map((item) => clampCell(item, columnNumber)), columnNumber, compactType, solidIds);
81
+ // src/placement/getCellPosition.ts
82
+ const getCellPosition = (index, pitch, padding) => Math.round(index * pitch + padding);
83
+ // src/placement/getCellSize.ts
84
+ const getCellSize = (span, pitch, gap) => span * pitch - gap;
85
+ // src/placement/getCellSpan.ts
86
+ const getCellSpan = (size, pitch, gap) => Math.max(1, Math.ceil((size + gap) / pitch));
87
+ // src/placement/getColumnPitch.ts
88
+ const getColumnPitch = (width, columnNumber, { gap, padding }) => (width - 2 * padding + gap) / columnNumber;
89
+ // src/placement/getGestureCell.ts
90
+ const ResizeEdgeSidesMap = {
91
+ ["East"]: {
92
+ isEast: true,
93
+ isNorth: false,
94
+ isSouth: false,
95
+ isWest: false
96
+ },
97
+ ["North"]: {
98
+ isEast: false,
99
+ isNorth: true,
100
+ isSouth: false,
101
+ isWest: false
102
+ },
103
+ ["NorthEast"]: {
104
+ isEast: true,
105
+ isNorth: true,
106
+ isSouth: false,
107
+ isWest: false
108
+ },
109
+ ["NorthWest"]: {
110
+ isEast: false,
111
+ isNorth: true,
112
+ isSouth: false,
113
+ isWest: true
114
+ },
115
+ ["South"]: {
116
+ isEast: false,
117
+ isNorth: false,
118
+ isSouth: true,
119
+ isWest: false
120
+ },
121
+ ["SouthEast"]: {
122
+ isEast: true,
123
+ isNorth: false,
124
+ isSouth: true,
125
+ isWest: false
126
+ },
127
+ ["SouthWest"]: {
128
+ isEast: false,
129
+ isNorth: false,
130
+ isSouth: true,
131
+ isWest: true
132
+ },
133
+ ["West"]: {
134
+ isEast: false,
135
+ isNorth: false,
136
+ isSouth: false,
137
+ isWest: true
138
+ }
139
+ };
140
+ const getGestureCell = ({ columnNumber, columnPitch, edge, offset, origin, rowPitch }) => {
141
+ const columnDelta = Math.round(offset.x / columnPitch);
142
+ const rowDelta = Math.round(offset.y / rowPitch);
143
+ if (!edge) {
144
+ const x = Math.min(Math.max(origin.x + columnDelta, 0), columnNumber - origin.w);
145
+ const y = Math.max(0, origin.y + rowDelta);
146
+ return {
147
+ h: origin.h,
148
+ id: origin.id,
149
+ w: origin.w,
150
+ x,
151
+ y
152
+ };
153
+ }
154
+ const { isEast, isNorth, isSouth, isWest } = ResizeEdgeSidesMap[edge];
155
+ const right = origin.x + origin.w;
156
+ const bottom = origin.y + origin.h;
157
+ const leftX = Math.min(Math.max(origin.x + columnDelta, 0), right - 1);
158
+ const eastWidth = Math.min(Math.max(1, origin.w + columnDelta), columnNumber - origin.x);
159
+ const topY = Math.min(Math.max(origin.y + rowDelta, 0), bottom - 1);
160
+ const southHeight = Math.max(1, origin.h + rowDelta);
161
+ const x = isWest ? leftX : origin.x;
162
+ const widthFromRight = isEast ? eastWidth : origin.w;
163
+ const w = isWest ? right - leftX : widthFromRight;
164
+ const y = isNorth ? topY : origin.y;
165
+ const heightFromBottom = isSouth ? southHeight : origin.h;
166
+ return {
167
+ h: isNorth ? bottom - topY : heightFromBottom,
168
+ id: origin.id,
169
+ w,
170
+ x,
171
+ y
172
+ };
173
+ };
174
+ // src/placement/getLayoutBottom.ts
175
+ const getLayoutBottom = (layout) => Math.max(0, ...layout.map((item) => item.y + item.h));
176
+ // src/models/CollisionMode.ts
177
+ let CollisionMode = /* @__PURE__ */ function(CollisionMode) {
178
+ CollisionMode["Prevent"] = "Prevent";
179
+ CollisionMode["Push"] = "Push";
180
+ CollisionMode["Swap"] = "Swap";
181
+ return CollisionMode;
182
+ }({});
183
+ // 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
+ // src/placement/resolveMove.ts
204
+ const resolveMove = (layout, target, collision, columnNumber, solidIds = /* @__PURE__ */ new Set()) => {
205
+ const landedItems = layout.filter((item) => item.id !== target.id && checkIsColliding(target, item));
206
+ const [landedItem] = landedItems;
207
+ const movedItem = layout.find((item) => item.id === target.id);
208
+ if (collision === "Prevent" && landedItems.length > 0) return void 0;
209
+ if (landedItems.some((item) => solidIds.has(item.id))) return void 0;
210
+ const isMove = movedItem !== void 0 && movedItem.w === target.w && movedItem.h === target.h;
211
+ const resolvedLayout = collision === "Swap" && isMove && landedItems.length === 1 && landedItem && movedItem && movedItem.x + landedItem.w <= columnNumber && landedItem.x + movedItem.w <= columnNumber ? moveItem(layout.map((item) => item.id === landedItem.id ? {
212
+ ...item,
213
+ x: movedItem.x,
214
+ y: movedItem.y
215
+ } : { ...item }), {
216
+ ...target,
217
+ x: landedItem.x,
218
+ y: landedItem.y
219
+ }) : moveItem(layout, target);
220
+ return layout.some((item) => {
221
+ const resolvedItem = resolvedLayout.find(({ id }) => id === item.id);
222
+ return solidIds.has(item.id) && item.id !== target.id && resolvedItem !== void 0 && !checkIsSameCell(resolvedItem, item);
223
+ }) ? void 0 : resolvedLayout;
224
+ };
225
+ // src/placement/placeMove.ts
226
+ const placeMove = (layout, target, { collision, columnNumber, compactType }, solidIds) => {
227
+ const resolvedLayout = resolveMove(layout, target, collision, columnNumber, solidIds);
228
+ return resolvedLayout && compactLayout(resolvedLayout, columnNumber, compactType, solidIds);
229
+ };
4
230
  // src/services/mountedGrids.ts
5
231
  const mountedGrids = /* @__PURE__ */ new Map();
6
232
  // src/services/findGridRoot.ts
@@ -18,14 +244,20 @@ const findGridAt = (point, element) => {
18
244
  // src/composables/useGridLayout.ts
19
245
  const useGridLayout = (root, options) => {
20
246
  const getOptions = () => toValue(options);
247
+ const solidIds = computed(() => {
248
+ const { checkIsSolid, items } = getOptions();
249
+ return new Set(checkIsSolid ? items.filter((item) => checkIsSolid(item)).map(({ id }) => id) : []);
250
+ });
21
251
  const width = ref(0);
22
252
  const gesture = shallowRef();
23
- const preview = shallowRef();
253
+ const gesturePreview = shallowRef();
254
+ const externalPreview = shallowRef();
255
+ const activePreview = computed(() => gesturePreview.value ?? externalPreview.value);
24
256
  const columnPitch = computed(() => getColumnPitch(width.value, getOptions().columnNumber, getOptions().spacing));
25
257
  const rowPitch = computed(() => getOptions().rowHeight + getOptions().spacing.gap);
26
- const layout = computed(() => fitLayout(getOptions().items, getOptions().columnNumber, getOptions().compactType));
258
+ const layout = computed(() => fitLayout(getOptions().items, getOptions().columnNumber, getOptions().compactType, solidIds.value));
27
259
  const savedItemMap = computed(() => new Map(getOptions().items.map((item) => [item.id, item])));
28
- const previewItemMap = computed(() => new Map((preview.value ?? []).map((item) => [item.id, item])));
260
+ const previewItemMap = computed(() => new Map((activePreview.value?.layout ?? []).map((item) => [item.id, item])));
29
261
  const draggedGesture = computed(() => gesture.value?.isStarted && !gesture.value.edge ? gesture.value : void 0);
30
262
  const getGeometry = (cell) => {
31
263
  const { gap, padding } = getOptions().spacing;
@@ -53,23 +285,72 @@ const useGridLayout = (root, options) => {
53
285
  });
54
286
  });
55
287
  const placeholder = computed(() => {
56
- if (!gesture.value?.isStarted) return void 0;
57
- const target = previewItemMap.value.get(gesture.value.origin.id);
58
- return target ? getGeometry(target) : void 0;
288
+ if (!activePreview.value) return void 0;
289
+ const { cell, isRefused } = activePreview.value;
290
+ return {
291
+ geometry: getGeometry(cell),
292
+ isRefused
293
+ };
59
294
  });
60
295
  const gridHeight = computed(() => {
61
296
  const { gap, padding } = getOptions().spacing;
62
- const layoutBottom = getLayoutBottom(preview.value ?? layout.value);
297
+ const layoutBottom = getLayoutBottom(activePreview.value?.layout ?? layout.value);
63
298
  return Math.max(0, getCellSize(layoutBottom, rowPitch.value, gap)) + 2 * padding;
64
299
  });
300
+ const clearPreview = () => {
301
+ externalPreview.value = void 0;
302
+ };
65
303
  const resetGesture = () => {
304
+ gesture.value?.transfer?.clearPreview();
66
305
  gesture.value = void 0;
67
- preview.value = void 0;
306
+ gesturePreview.value = void 0;
307
+ clearPreview();
308
+ };
309
+ watch(layout, () => {
310
+ resetGesture();
311
+ });
312
+ const getPreview = (cell, landingLayout) => {
313
+ const landedLayout = placeMove(landingLayout, cell, getOptions(), solidIds.value);
314
+ if (!landedLayout) return {
315
+ cell,
316
+ isRefused: true,
317
+ layout: layout.value
318
+ };
319
+ return {
320
+ cell: landedLayout.find(({ id }) => id === cell.id) ?? cell,
321
+ isRefused: false,
322
+ layout: landedLayout
323
+ };
324
+ };
325
+ const getDropPreview = (item, point) => {
326
+ const element = root.value;
327
+ if (!element || !getOptions().isEditable || getOptions().checkIsRefused?.(item, element)) return void 0;
328
+ const { left, top } = element.getBoundingClientRect();
329
+ const { padding } = getOptions().spacing;
330
+ const offset = {
331
+ x: point.x - left - padding,
332
+ y: point.y - top - padding
333
+ };
334
+ const columnIndex = Math.floor(offset.x / columnPitch.value);
335
+ const rowIndex = Math.floor(offset.y / rowPitch.value);
336
+ const cell = clampCell({
337
+ ...item,
338
+ x: columnIndex,
339
+ y: rowIndex
340
+ }, getOptions().columnNumber);
341
+ return getPreview(cell, [...layout.value, cell]);
342
+ };
343
+ const checkIsTransferable = (activeGesture) => getOptions().onTransfer !== void 0 && !activeGesture.edge;
344
+ const findTransfer = (activeGesture, point) => {
345
+ if (!checkIsTransferable(activeGesture)) return void 0;
346
+ const grid = findGridAt(point, activeGesture.element);
347
+ return grid?.element === root.value ? void 0 : grid;
68
348
  };
69
- watch(layout, () => resetGesture());
70
- const startGesture = (event, item, edge) => {
349
+ const startGesture = (event, item, pressedEdge) => {
71
350
  if (event.button !== 0) return;
72
351
  if (!getOptions().isEditable || checkIsNestedControl(event)) return;
352
+ const edge = event.pointerType === "touch" ? void 0 : pressedEdge;
353
+ if (item.isAutoHeight && edge && checkChangesHeight(edge)) return;
73
354
  if (gesture.value && gesture.value.pointerId !== event.pointerId) return;
74
355
  if (!(event.currentTarget instanceof Element) || !(event.target instanceof Element)) return;
75
356
  if (findGridRoot(event.target) !== root.value) return;
@@ -110,14 +391,22 @@ const useGridLayout = (root, options) => {
110
391
  origin: activeGesture.origin,
111
392
  rowPitch: rowPitch.value
112
393
  });
394
+ const point = {
395
+ x: event.clientX,
396
+ y: event.clientY
397
+ };
113
398
  const isCellChanged = !activeGesture.cell || !checkIsSameCell(cell, activeGesture.cell);
399
+ const transfer = findTransfer(activeGesture, point);
400
+ if (activeGesture.transfer !== transfer) activeGesture.transfer?.clearPreview();
114
401
  gesture.value = {
115
402
  ...activeGesture,
116
403
  cell,
117
404
  isStarted: true,
118
- offset
405
+ offset,
406
+ transfer
119
407
  };
120
- if (isCellChanged) preview.value = placeMove(layout.value, cell, getOptions());
408
+ if (Boolean(transfer?.preview(activeGesture.origin, point))) gesturePreview.value = void 0;
409
+ else if (isCellChanged || !gesturePreview.value) gesturePreview.value = getPreview(cell, layout.value);
121
410
  };
122
411
  const getChangedCells = (changedLayout) => changedLayout.flatMap((item) => {
123
412
  const savedItem = savedItemMap.value.get(item.id);
@@ -130,34 +419,37 @@ const useGridLayout = (root, options) => {
130
419
  }] : [];
131
420
  });
132
421
  const accept = (item, point) => {
133
- const element = root.value;
134
- if (!element || !getOptions().isEditable) return false;
135
- const { left, top } = element.getBoundingClientRect();
136
- const { padding } = getOptions().spacing;
137
- const offset = {
138
- x: point.x - left - padding,
139
- y: point.y - top - padding
140
- };
141
- const columnIndex = Math.floor(offset.x / columnPitch.value);
142
- const rowIndex = Math.floor(offset.y / rowPitch.value);
143
- const y = Math.max(0, rowIndex);
144
- const cell = takeOne(correctBounds([{
145
- h: item.h,
146
- id: item.id,
147
- w: item.w,
148
- x: columnIndex,
422
+ const preview = getDropPreview(item, point);
423
+ if (!preview || preview.isRefused) return void 0;
424
+ const changes = getChangedCells(preview.layout).filter(({ id }) => id !== item.id);
425
+ if (changes.length > 0) getOptions().onCommit({ changes });
426
+ const { h, w, x, y } = preview.cell;
427
+ return {
428
+ h,
429
+ w,
430
+ x,
149
431
  y
150
- }], getOptions().columnNumber));
151
- if (getOptions().collision === "Prevent" && layout.value.some((other) => checkIsColliding(cell, other))) return false;
152
- const landedLayout = placeMove([...layout.value, cell], cell, getOptions());
153
- const changes = getChangedCells(landedLayout);
154
- getOptions().onCommit({
155
- changes,
156
- removed: []
157
- });
432
+ };
433
+ };
434
+ const stepItem = (id, step) => {
435
+ const item = layout.value.find((other) => other.id === id);
436
+ if (!getOptions().isEditable || !item) return false;
437
+ const { dh = 0, dw = 0, dx = 0, dy = 0 } = step;
438
+ if (item.isAutoHeight && dh !== 0) return false;
439
+ const target = {
440
+ ...item,
441
+ h: Math.max(1, item.h + dh),
442
+ w: Math.max(1, item.w + dw),
443
+ x: item.x + dx,
444
+ y: item.y + dy
445
+ };
446
+ if (target.x < 0 || target.y < 0 || target.x + target.w > getOptions().columnNumber || checkIsSameCell(target, item)) return false;
447
+ const preview = getPreview(target, layout.value);
448
+ const changes = preview.isRefused ? [] : getChangedCells(preview.layout);
449
+ if (changes.length === 0) return false;
450
+ getOptions().onCommit({ changes });
158
451
  return true;
159
452
  };
160
- const self = { accept };
161
453
  const handlePointerUp = (event) => {
162
454
  const activeGesture = gesture.value;
163
455
  if (!activeGesture || event.pointerId !== activeGesture.pointerId) return;
@@ -165,44 +457,44 @@ const useGridLayout = (root, options) => {
165
457
  x: event.clientX,
166
458
  y: event.clientY
167
459
  };
168
- const dropGrid = draggedGesture.value ? findGridAt(point, activeGesture.element) : self;
169
- const changes = getChangedCells(preview.value ?? []);
460
+ const preview = gesturePreview.value;
461
+ const changes = preview && !preview.isRefused ? getChangedCells(preview.layout) : [];
462
+ const transfer = findTransfer(activeGesture, point);
170
463
  resetGesture();
171
- if (dropGrid === self) {
172
- if (changes.length > 0) getOptions().onCommit({
173
- changes,
174
- removed: []
175
- });
464
+ if (!transfer) {
465
+ if (changes.length > 0) getOptions().onCommit({ changes });
176
466
  return;
177
467
  }
178
- if (!dropGrid) return;
179
- if (!dropGrid.accept(activeGesture.origin, point)) return;
180
- const remainingItems = layout.value.filter((item) => item.id !== activeGesture.origin.id);
181
- const remainingLayout = compactLayout(remainingItems, getOptions().columnNumber, getOptions().compactType);
182
- getOptions().onCommit({
183
- changes: getChangedCells(remainingLayout),
184
- removed: [activeGesture.origin.id]
468
+ const cell = transfer.accept(activeGesture.origin, point);
469
+ if (cell) getOptions().onTransfer?.({
470
+ cell,
471
+ item: activeGesture.origin,
472
+ target: transfer.element
185
473
  });
186
474
  };
187
475
  const reportContentHeight = (id, contentHeight) => {
188
476
  const savedItem = savedItemMap.value.get(id);
189
- if (!savedItem?.isAutoHeight) return;
477
+ if (!getOptions().isEditable || !savedItem?.isAutoHeight) return;
190
478
  const h = getCellSpan(contentHeight, rowPitch.value, getOptions().spacing.gap);
191
479
  if (h === savedItem.h) return;
192
480
  const otherItems = getOptions().items.filter((item) => item.id !== id);
193
481
  const changes = getChangedCells(fitLayout([...otherItems, {
194
482
  ...savedItem,
195
483
  h
196
- }], getOptions().columnNumber, getOptions().compactType));
197
- if (changes.length > 0) getOptions().onCommit({
198
- changes,
199
- removed: []
200
- });
484
+ }], getOptions().columnNumber, getOptions().compactType, solidIds.value));
485
+ if (changes.length > 0) getOptions().onCommit({ changes });
201
486
  };
202
487
  const handlePointerCancel = (event) => {
203
488
  if (event.pointerId === gesture.value?.pointerId) resetGesture();
204
489
  };
205
490
  const attached = shallowRef();
491
+ const previewDrop = (item, point) => {
492
+ const preview = getDropPreview(item, point);
493
+ const current = externalPreview.value;
494
+ if (preview && current?.cell.id === preview.cell.id && checkIsSameCell(preview.cell, current.cell)) return true;
495
+ externalPreview.value = preview;
496
+ return preview !== void 0;
497
+ };
206
498
  onMounted(() => {
207
499
  const element = root.value;
208
500
  if (!element) return;
@@ -215,7 +507,12 @@ const useGridLayout = (root, options) => {
215
507
  element.addEventListener("pointermove", handlePointerMove, { signal });
216
508
  element.addEventListener("pointerup", handlePointerUp, { signal });
217
509
  element.addEventListener("pointercancel", handlePointerCancel, { signal });
218
- mountedGrids.set(element, self);
510
+ mountedGrids.set(element, {
511
+ accept,
512
+ clearPreview,
513
+ element,
514
+ preview: previewDrop
515
+ });
219
516
  attached.value = {
220
517
  controller,
221
518
  element,
@@ -223,6 +520,7 @@ const useGridLayout = (root, options) => {
223
520
  };
224
521
  });
225
522
  onUnmounted(() => {
523
+ resetGesture();
226
524
  if (!attached.value) return;
227
525
  const { controller, element, observer } = attached.value;
228
526
  observer.disconnect();
@@ -234,29 +532,68 @@ const useGridLayout = (root, options) => {
234
532
  placeholder,
235
533
  reportContentHeight,
236
534
  startGesture,
535
+ stepItem,
237
536
  tiles
238
537
  };
239
538
  };
539
+ // src/placement/getFirstFreeCell.ts
540
+ const getFirstFreeCell = (layout, columnNumber, span) => {
541
+ const { h, w } = clampCell({
542
+ h: span.h,
543
+ w: span.w,
544
+ x: 0,
545
+ y: 0
546
+ }, columnNumber);
547
+ for (let y = 0;; y += 1) for (let x = 0; x <= columnNumber - w; x += 1) {
548
+ const cell = {
549
+ h,
550
+ w,
551
+ x,
552
+ y
553
+ };
554
+ if (!layout.some((item) => checkIsColliding(cell, item))) return cell;
555
+ }
556
+ };
240
557
  // src/services/startExternalDrag.ts
241
- const startExternalDrag = (event, item) => {
558
+ const startExternalDrag = (event, item, onDrop, checkIsRefused) => {
242
559
  if (event.button !== 0) return;
243
560
  if (!(event.currentTarget instanceof HTMLElement)) return;
244
561
  const element = event.currentTarget;
245
562
  const { pointerId } = event;
563
+ const checkIsGridRefused = (grid) => Boolean(checkIsRefused?.(item, grid.element));
246
564
  element.setPointerCapture(pointerId);
247
565
  const controller = new AbortController();
248
566
  const { signal } = controller;
567
+ let lastGrid;
568
+ element.addEventListener("pointermove", (moveEvent) => {
569
+ if (moveEvent.pointerId !== pointerId) return;
570
+ const point = {
571
+ x: moveEvent.clientX,
572
+ y: moveEvent.clientY
573
+ };
574
+ const grid = findGridAt(point, element);
575
+ if (grid !== lastGrid) {
576
+ lastGrid?.clearPreview();
577
+ lastGrid = grid;
578
+ }
579
+ if (grid && !checkIsGridRefused(grid)) grid.preview(item, point);
580
+ }, { signal });
249
581
  element.addEventListener("pointerup", (releaseEvent) => {
250
582
  if (releaseEvent.pointerId !== pointerId) return;
251
583
  controller.abort();
584
+ lastGrid?.clearPreview();
252
585
  const point = {
253
586
  x: releaseEvent.clientX,
254
587
  y: releaseEvent.clientY
255
588
  };
256
- findGridAt(point, element)?.accept(item, point);
589
+ const grid = findGridAt(point, element);
590
+ const cell = grid && !checkIsGridRefused(grid) ? grid.accept(item, point) : void 0;
591
+ if (grid && cell) onDrop(cell, grid.element);
257
592
  }, { signal });
258
593
  element.addEventListener("pointercancel", (cancelEvent) => {
259
- if (cancelEvent.pointerId === pointerId) controller.abort();
594
+ if (cancelEvent.pointerId !== pointerId) return;
595
+ controller.abort();
596
+ lastGrid?.clearPreview();
260
597
  }, { signal });
261
598
  };
262
- export { CollisionMode, CompactType, ResizeEdge, startExternalDrag, useGridLayout };
599
+ export { CollisionMode, CompactType, ResizeEdge, checkChangesHeight, getFirstFreeCell, startExternalDrag, useGridLayout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-grid-layout-headless",
3
- "version": "4.3.3",
3
+ "version": "4.5.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.3"
47
+ "@esposter/shared": "4.5.0"
49
48
  },
50
49
  "devDependencies": {
51
- "@esposter/configuration": "4.3.3",
50
+ "@esposter/configuration": "4.5.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.5"
55
54
  },
56
55
  "peerDependencies": {
57
56
  "vue": "^3.5.44"
58
57
  },
59
- "gitHead": "955bbc130ad9b28877fd4ae8efc73a045141ce01"
58
+ "gitHead": "e30c001f3b4b15693e75fb53092d62a97ade4d2b"
60
59
  }
@@ -1,50 +0,0 @@
1
- interface GridCell {
2
- h: number;
3
- w: number;
4
- x: number;
5
- y: number;
6
- }
7
- interface GridItem extends GridCell {
8
- id: string;
9
- isAutoHeight?: boolean;
10
- }
11
- declare enum CompactType {
12
- Horizontal = "Horizontal",
13
- None = "None",
14
- Vertical = "Vertical"
15
- }
16
- interface GridSpacing {
17
- gap: number;
18
- padding: number;
19
- }
20
- declare enum ResizeEdge {
21
- East = "East",
22
- North = "North",
23
- NorthEast = "NorthEast",
24
- NorthWest = "NorthWest",
25
- South = "South",
26
- SouthEast = "SouthEast",
27
- SouthWest = "SouthWest",
28
- West = "West"
29
- }
30
- declare enum CollisionMode {
31
- Prevent = "Prevent",
32
- Push = "Push",
33
- Swap = "Swap"
34
- }
35
- interface GridCommit {
36
- changes: GridItem[];
37
- removed: string[];
38
- }
39
- interface GridLayoutOptions<T extends GridItem = GridItem> {
40
- collision: CollisionMode;
41
- columnNumber: number;
42
- compactType: CompactType;
43
- dragStartDistance: number;
44
- isEditable: boolean;
45
- items: T[];
46
- onCommit: (commit: GridCommit) => void;
47
- rowHeight: number;
48
- spacing: GridSpacing;
49
- }
50
- 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 };
@@ -1,212 +0,0 @@
1
- // 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
- // src/placement/checkIsColliding.ts
9
- 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;
10
- // src/placement/checkIsSameCell.ts
11
- const checkIsSameCell = (cell, other) => cell.h === other.h && cell.w === other.w && cell.x === other.x && cell.y === other.y;
12
- // src/models/CompactType.ts
13
- let CompactType = /* @__PURE__ */ function(CompactType) {
14
- CompactType["Horizontal"] = "Horizontal";
15
- CompactType["None"] = "None";
16
- CompactType["Vertical"] = "Vertical";
17
- return CompactType;
18
- }({});
19
- // src/placement/compactLayout.ts
20
- const compactLayout = (layout, columnNumber, compactType) => {
21
- const copies = layout.map((item) => ({ ...item }));
22
- const isHorizontal = compactType === "Horizontal";
23
- const axis = isHorizontal ? "x" : "y";
24
- const crossAxis = isHorizontal ? "y" : "x";
25
- const placedItems = [];
26
- const checkIsBlocked = (candidate) => placedItems.some((placedItem) => checkIsColliding(candidate, placedItem));
27
- const riseAlongAxis = (item) => {
28
- while (item[axis] > 0 && !checkIsBlocked({
29
- ...item,
30
- [axis]: item[axis] - 1
31
- })) item[axis] -= 1;
32
- };
33
- const pushAlongAxis = (item) => {
34
- if (isHorizontal && item.x + item.w + 1 > columnNumber) {
35
- item.x = columnNumber - item.w;
36
- item.y += 1;
37
- riseAlongAxis(item);
38
- } else item[axis] += 1;
39
- };
40
- const sortedItems = copies.toSorted((firstItem, secondItem) => firstItem[axis] - secondItem[axis] || firstItem[crossAxis] - secondItem[crossAxis]);
41
- for (const item of sortedItems) {
42
- if (compactType !== "None") riseAlongAxis(item);
43
- while (checkIsBlocked(item)) pushAlongAxis(item);
44
- placedItems.push(item);
45
- }
46
- return placedItems;
47
- };
48
- // src/placement/correctBounds.ts
49
- const correctBounds = (layout, columnNumber) => layout.map((item) => {
50
- const h = Math.max(1, item.h);
51
- const w = Math.max(1, Math.min(item.w, columnNumber));
52
- const x = Math.min(Math.max(item.x, 0), columnNumber - w);
53
- return {
54
- ...item,
55
- h,
56
- w,
57
- x
58
- };
59
- });
60
- // src/placement/fitLayout.ts
61
- const fitLayout = (layout, columnNumber, compactType) => compactLayout(correctBounds(layout, columnNumber), columnNumber, compactType);
62
- // src/placement/getCellPosition.ts
63
- const getCellPosition = (index, pitch, padding) => Math.round(index * pitch + padding);
64
- // src/placement/getCellSize.ts
65
- const getCellSize = (span, pitch, gap) => span * pitch - gap;
66
- // src/placement/getCellSpan.ts
67
- const getCellSpan = (size, pitch, gap) => Math.max(1, Math.ceil((size + gap) / pitch));
68
- // src/placement/getColumnPitch.ts
69
- const getColumnPitch = (width, columnNumber, { gap, padding }) => (width - 2 * padding + gap) / columnNumber;
70
- // src/models/ResizeEdge.ts
71
- let ResizeEdge = /* @__PURE__ */ function(ResizeEdge) {
72
- ResizeEdge["East"] = "East";
73
- ResizeEdge["North"] = "North";
74
- ResizeEdge["NorthEast"] = "NorthEast";
75
- ResizeEdge["NorthWest"] = "NorthWest";
76
- ResizeEdge["South"] = "South";
77
- ResizeEdge["SouthEast"] = "SouthEast";
78
- ResizeEdge["SouthWest"] = "SouthWest";
79
- ResizeEdge["West"] = "West";
80
- return ResizeEdge;
81
- }({});
82
- // src/placement/getGestureCell.ts
83
- const ResizeEdgeSidesMap = {
84
- ["East"]: {
85
- isEast: true,
86
- isNorth: false,
87
- isSouth: false,
88
- isWest: false
89
- },
90
- ["North"]: {
91
- isEast: false,
92
- isNorth: true,
93
- isSouth: false,
94
- isWest: false
95
- },
96
- ["NorthEast"]: {
97
- isEast: true,
98
- isNorth: true,
99
- isSouth: false,
100
- isWest: false
101
- },
102
- ["NorthWest"]: {
103
- isEast: false,
104
- isNorth: true,
105
- isSouth: false,
106
- isWest: true
107
- },
108
- ["South"]: {
109
- isEast: false,
110
- isNorth: false,
111
- isSouth: true,
112
- isWest: false
113
- },
114
- ["SouthEast"]: {
115
- isEast: true,
116
- isNorth: false,
117
- isSouth: true,
118
- isWest: false
119
- },
120
- ["SouthWest"]: {
121
- isEast: false,
122
- isNorth: false,
123
- isSouth: true,
124
- isWest: true
125
- },
126
- ["West"]: {
127
- isEast: false,
128
- isNorth: false,
129
- isSouth: false,
130
- isWest: true
131
- }
132
- };
133
- const getGestureCell = ({ columnNumber, columnPitch, edge, offset, origin, rowPitch }) => {
134
- const columnDelta = Math.round(offset.x / columnPitch);
135
- const rowDelta = Math.round(offset.y / rowPitch);
136
- if (!edge) {
137
- const x = Math.min(Math.max(origin.x + columnDelta, 0), columnNumber - origin.w);
138
- const y = Math.max(0, origin.y + rowDelta);
139
- return {
140
- h: origin.h,
141
- id: origin.id,
142
- w: origin.w,
143
- x,
144
- y
145
- };
146
- }
147
- const { isEast, isNorth, isSouth, isWest } = ResizeEdgeSidesMap[edge];
148
- const right = origin.x + origin.w;
149
- const bottom = origin.y + origin.h;
150
- const leftX = Math.min(Math.max(origin.x + columnDelta, 0), right - 1);
151
- const eastWidth = Math.min(Math.max(1, origin.w + columnDelta), columnNumber - origin.x);
152
- const topY = Math.min(Math.max(origin.y + rowDelta, 0), bottom - 1);
153
- const southHeight = Math.max(1, origin.h + rowDelta);
154
- const x = isWest ? leftX : origin.x;
155
- const widthFromRight = isEast ? eastWidth : origin.w;
156
- const w = isWest ? right - leftX : widthFromRight;
157
- const y = isNorth ? topY : origin.y;
158
- const heightFromBottom = isSouth ? southHeight : origin.h;
159
- return {
160
- h: isNorth ? bottom - topY : heightFromBottom,
161
- id: origin.id,
162
- w,
163
- x,
164
- y
165
- };
166
- };
167
- // src/placement/getLayoutBottom.ts
168
- const getLayoutBottom = (layout) => Math.max(0, ...layout.map((item) => item.y + item.h));
169
- // src/placement/moveItem.ts
170
- const moveItem = (layout, target) => {
171
- const movedLayout = layout.map((item) => item.id === target.id ? {
172
- ...item,
173
- ...target
174
- } : { ...item });
175
- const pushedIds = /* @__PURE__ */ new Set([target.id]);
176
- const pushBelow = (pusher) => {
177
- for (const item of movedLayout) {
178
- if (pushedIds.has(item.id) || !checkIsColliding(pusher, item)) continue;
179
- if (item.y < pusher.y && pusher.y - item.y > item.h * .25) continue;
180
- pushedIds.add(item.id);
181
- item.y = pusher.y + pusher.h;
182
- pushBelow(item);
183
- }
184
- };
185
- const movedItem = movedLayout.find((item) => item.id === target.id);
186
- if (movedItem) pushBelow(movedItem);
187
- return movedLayout;
188
- };
189
- // src/placement/resolveMove.ts
190
- const resolveMove = (layout, target, collision) => {
191
- const landedItems = layout.filter((item) => item.id !== target.id && checkIsColliding(target, item));
192
- const [landedItem] = landedItems;
193
- const movedItem = layout.find((item) => item.id === target.id);
194
- if (collision === "Prevent" && landedItems.length > 0) return layout.map((item) => ({ ...item }));
195
- const isMove = movedItem !== void 0 && movedItem.w === target.w && movedItem.h === target.h;
196
- if (collision === "Swap" && isMove && landedItems.length === 1 && landedItem && movedItem) {
197
- const swappedLayout = layout.map((item) => item.id === landedItem.id ? {
198
- ...item,
199
- x: movedItem.x,
200
- y: movedItem.y
201
- } : { ...item });
202
- return moveItem(swappedLayout, {
203
- ...target,
204
- x: landedItem.x,
205
- y: landedItem.y
206
- });
207
- }
208
- return moveItem(layout, target);
209
- };
210
- // src/placement/placeMove.ts
211
- const placeMove = (layout, target, { collision, columnNumber, compactType }) => compactLayout(resolveMove(layout, target, collision), columnNumber, compactType);
212
- 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,25 +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-D0ot9xMz.js";
2
- export declare const checkIsColliding: (item: GridItem, other: GridItem) => boolean;
3
- export declare const checkIsSameCell: (cell: GridCell, other: GridCell) => boolean;
4
- export declare const compactLayout: <T extends GridItem>(layout: T[], columnNumber: number, compactType: CompactType) => T[];
5
- export declare const correctBounds: <T extends GridItem>(layout: T[], columnNumber: number) => T[];
6
- export declare const fitLayout: <T extends GridItem>(layout: T[], columnNumber: number, compactType: CompactType) => T[];
7
- export declare const getCellPosition: (index: number, pitch: number, padding: number) => number;
8
- export declare const getCellSize: (span: number, pitch: number, gap: number) => number;
9
- export declare const getCellSpan: (size: number, pitch: number, gap: number) => number;
10
- export declare const getColumnPitch: (width: number, columnNumber: number, { gap, padding }: GridSpacing) => number;
11
- export declare const getGestureCell: ({ columnNumber, columnPitch, edge, offset, origin, rowPitch }: {
12
- columnNumber: number;
13
- columnPitch: number;
14
- edge?: ResizeEdge;
15
- offset: {
16
- x: number;
17
- y: number;
18
- };
19
- origin: GridItem;
20
- rowPitch: number;
21
- }) => GridItem;
22
- export declare const getLayoutBottom: (layout: GridItem[]) => number;
23
- export declare const moveItem: <T extends GridItem>(layout: T[], target: GridItem) => T[];
24
- export declare const placeMove: <T extends GridItem>(layout: T[], target: GridItem, { collision, columnNumber, compactType }: Pick<GridLayoutOptions, "collision" | "columnNumber" | "compactType">) => T[];
25
- export declare const resolveMove: <T extends GridItem>(layout: T[], target: GridItem, collision: CollisionMode) => T[];
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-BPQPi70h.js";