vue-grid-layout-headless 4.6.0 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/composables/useGridLayout.d.ts +5 -0
  2. package/dist/composables/useGridLayout.js +312 -0
  3. package/dist/index.d.ts +23 -119
  4. package/dist/index.js +7 -598
  5. package/dist/models/CollisionMode.d.ts +5 -0
  6. package/dist/models/CollisionMode.js +8 -0
  7. package/dist/models/CompactType.d.ts +5 -0
  8. package/dist/models/CompactType.js +8 -0
  9. package/dist/models/GridBlock.d.ts +4 -0
  10. package/dist/models/GridCell.d.ts +6 -0
  11. package/dist/models/GridCommit.d.ts +4 -0
  12. package/dist/models/GridGeometry.d.ts +6 -0
  13. package/dist/models/GridGesture.d.ts +15 -0
  14. package/dist/models/GridItem.d.ts +4 -0
  15. package/dist/models/GridLayout.d.ts +14 -0
  16. package/dist/models/GridLayoutOptions.d.ts +21 -0
  17. package/dist/models/GridPlaceholder.d.ts +5 -0
  18. package/dist/models/GridPoint.d.ts +4 -0
  19. package/dist/models/GridRefusal.d.ts +2 -0
  20. package/dist/models/GridSpacing.d.ts +4 -0
  21. package/dist/models/GridStep.d.ts +6 -0
  22. package/dist/models/GridTile.d.ts +9 -0
  23. package/dist/models/GridTransfer.d.ts +7 -0
  24. package/dist/models/MountedGrid.d.ts +9 -0
  25. package/dist/models/ResizeEdge.d.ts +10 -0
  26. package/dist/models/ResizeEdge.js +13 -0
  27. package/dist/placement/checkChangesHeight.d.ts +2 -0
  28. package/dist/placement/checkChangesHeight.js +12 -0
  29. package/dist/placement/checkIsColliding.js +3 -0
  30. package/dist/placement/checkIsSameCell.js +3 -0
  31. package/dist/placement/clampCell.js +14 -0
  32. package/dist/placement/compactLayout.js +33 -0
  33. package/dist/placement/fitLayout.js +5 -0
  34. package/dist/placement/getCellPosition.js +3 -0
  35. package/dist/placement/getCellSize.js +3 -0
  36. package/dist/placement/getCellSpan.js +3 -0
  37. package/dist/placement/getColumnPitch.js +3 -0
  38. package/dist/placement/getFirstFreeCell.d.ts +6 -0
  39. package/dist/placement/getFirstFreeCell.js +21 -0
  40. package/dist/placement/getGestureCell.js +87 -0
  41. package/dist/placement/getLayoutBottom.js +3 -0
  42. package/dist/placement/moveItem.js +23 -0
  43. package/dist/placement/placeMove.js +8 -0
  44. package/dist/placement/resolveMove.js +27 -0
  45. package/dist/services/constants.js +3 -0
  46. package/dist/services/findGridAt.js +9 -0
  47. package/dist/services/findGridRoot.js +8 -0
  48. package/dist/services/mountedGrids.js +3 -0
  49. package/dist/services/startExternalDrag.d.ts +4 -0
  50. package/dist/services/startExternalDrag.js +44 -0
  51. package/package.json +4 -4
@@ -0,0 +1,5 @@
1
+ import { GridItem } from "../models/GridItem.js";
2
+ import { GridLayout } from "../models/GridLayout.js";
3
+ import { GridLayoutOptions } from "../models/GridLayoutOptions.js";
4
+ import { MaybeRefOrGetter, ShallowRef } from "vue";
5
+ export declare const useGridLayout: <T extends GridItem>(root: Readonly<ShallowRef<HTMLElement | null>>, options: MaybeRefOrGetter<GridLayoutOptions<T>>) => GridLayout<T>;
@@ -0,0 +1,312 @@
1
+ import { checkChangesHeight } from "../placement/checkChangesHeight.js";
2
+ import { checkIsSameCell } from "../placement/checkIsSameCell.js";
3
+ import { clampCell } from "../placement/clampCell.js";
4
+ import { fitLayout } from "../placement/fitLayout.js";
5
+ import { getCellPosition } from "../placement/getCellPosition.js";
6
+ import { getCellSize } from "../placement/getCellSize.js";
7
+ import { getCellSpan } from "../placement/getCellSpan.js";
8
+ import { getColumnPitch } from "../placement/getColumnPitch.js";
9
+ import { getGestureCell } from "../placement/getGestureCell.js";
10
+ import { getLayoutBottom } from "../placement/getLayoutBottom.js";
11
+ import { placeMove } from "../placement/placeMove.js";
12
+ import { findGridAt } from "../services/findGridAt.js";
13
+ import { findGridRoot } from "../services/findGridRoot.js";
14
+ import { mountedGrids } from "../services/mountedGrids.js";
15
+ import { checkIsNestedControl } from "@esposter/shared";
16
+ import { computed, onMounted, onUnmounted, ref, shallowRef, toValue, watch } from "vue";
17
+ // src/composables/useGridLayout.ts
18
+ const useGridLayout = (root, options) => {
19
+ const getOptions = () => toValue(options);
20
+ const solidIds = computed(() => {
21
+ const { checkIsSolid, items } = getOptions();
22
+ return new Set(checkIsSolid ? items.filter((item) => checkIsSolid(item)).map(({ id }) => id) : []);
23
+ });
24
+ const width = ref(0);
25
+ const gesture = shallowRef();
26
+ const gesturePreview = shallowRef();
27
+ const externalPreview = shallowRef();
28
+ const activePreview = computed(() => gesturePreview.value ?? externalPreview.value);
29
+ const columnPitch = computed(() => getColumnPitch(width.value, getOptions().columnNumber, getOptions().spacing));
30
+ const rowPitch = computed(() => getOptions().rowHeight + getOptions().spacing.gap);
31
+ const layout = computed(() => fitLayout(getOptions().items, getOptions().columnNumber, getOptions().compactType, solidIds.value));
32
+ const savedItemMap = computed(() => new Map(getOptions().items.map((item) => [item.id, item])));
33
+ const previewItemMap = computed(() => new Map((activePreview.value?.layout ?? []).map((item) => [item.id, item])));
34
+ const draggedGesture = computed(() => gesture.value?.isStarted && !gesture.value.edge ? gesture.value : void 0);
35
+ const getGeometry = (cell) => {
36
+ const { gap, padding } = getOptions().spacing;
37
+ return {
38
+ height: getCellSize(cell.h, rowPitch.value, gap),
39
+ left: getCellPosition(cell.x, columnPitch.value, padding),
40
+ top: getCellPosition(cell.y, rowPitch.value, padding),
41
+ width: getCellSize(cell.w, columnPitch.value, gap)
42
+ };
43
+ };
44
+ const tiles = computed(() => {
45
+ if (width.value === 0) return [];
46
+ return layout.value.map((item) => {
47
+ const dragged = draggedGesture.value?.origin.id === item.id ? draggedGesture.value : void 0;
48
+ const cell = dragged ? item : previewItemMap.value.get(item.id) ?? item;
49
+ return {
50
+ geometry: getGeometry(cell),
51
+ isDragged: dragged !== void 0,
52
+ item,
53
+ offset: dragged?.offset ?? {
54
+ x: 0,
55
+ y: 0
56
+ }
57
+ };
58
+ });
59
+ });
60
+ const placeholder = computed(() => {
61
+ if (!activePreview.value) return void 0;
62
+ const { cell, isRefused } = activePreview.value;
63
+ return {
64
+ geometry: getGeometry(cell),
65
+ isRefused
66
+ };
67
+ });
68
+ const gridHeight = computed(() => {
69
+ const { gap, padding } = getOptions().spacing;
70
+ const layoutBottom = getLayoutBottom(activePreview.value?.layout ?? layout.value);
71
+ return Math.max(0, getCellSize(layoutBottom, rowPitch.value, gap)) + 2 * padding;
72
+ });
73
+ const clearPreview = () => {
74
+ externalPreview.value = void 0;
75
+ };
76
+ const resetGesture = () => {
77
+ gesture.value?.transfer?.clearPreview();
78
+ gesture.value = void 0;
79
+ gesturePreview.value = void 0;
80
+ clearPreview();
81
+ };
82
+ watch(layout, () => {
83
+ resetGesture();
84
+ });
85
+ const getPreview = (cell, landingLayout) => {
86
+ const landedLayout = placeMove(landingLayout, cell, getOptions(), solidIds.value);
87
+ if (!landedLayout) return {
88
+ cell,
89
+ isRefused: true,
90
+ layout: layout.value
91
+ };
92
+ return {
93
+ cell: landedLayout.find(({ id }) => id === cell.id) ?? cell,
94
+ isRefused: false,
95
+ layout: landedLayout
96
+ };
97
+ };
98
+ const getDropPreview = (item, point) => {
99
+ const element = root.value;
100
+ if (!element || !getOptions().isEditable || getOptions().checkIsRefused?.(item, element)) return void 0;
101
+ const { left, top } = element.getBoundingClientRect();
102
+ const { padding } = getOptions().spacing;
103
+ const offset = {
104
+ x: point.x - left - padding,
105
+ y: point.y - top - padding
106
+ };
107
+ const columnIndex = Math.floor(offset.x / columnPitch.value);
108
+ const rowIndex = Math.floor(offset.y / rowPitch.value);
109
+ const cell = clampCell({
110
+ ...item,
111
+ x: columnIndex,
112
+ y: rowIndex
113
+ }, getOptions().columnNumber);
114
+ return getPreview(cell, [...layout.value, cell]);
115
+ };
116
+ const checkIsTransferable = (activeGesture) => getOptions().onTransfer !== void 0 && !activeGesture.edge;
117
+ const findTransfer = (activeGesture, point) => {
118
+ if (!checkIsTransferable(activeGesture)) return void 0;
119
+ const grid = findGridAt(point, activeGesture.element);
120
+ return grid?.element === root.value ? void 0 : grid;
121
+ };
122
+ const startGesture = (event, item, pressedEdge) => {
123
+ if (event.button !== 0) return;
124
+ if (!getOptions().isEditable || checkIsNestedControl(event)) return;
125
+ const edge = event.pointerType === "touch" ? void 0 : pressedEdge;
126
+ if (item.isAutoHeight && edge && checkChangesHeight(edge)) return;
127
+ if (gesture.value && gesture.value.pointerId !== event.pointerId) return;
128
+ if (!(event.currentTarget instanceof Element) || !(event.target instanceof Element)) return;
129
+ if (findGridRoot(event.target) !== root.value) return;
130
+ gesture.value = {
131
+ edge,
132
+ element: event.currentTarget,
133
+ isStarted: false,
134
+ offset: {
135
+ x: 0,
136
+ y: 0
137
+ },
138
+ origin: item,
139
+ pointerId: event.pointerId,
140
+ start: {
141
+ x: event.clientX,
142
+ y: event.clientY
143
+ }
144
+ };
145
+ };
146
+ const handlePointerMove = (event) => {
147
+ const activeGesture = gesture.value;
148
+ if (!activeGesture || event.pointerId !== activeGesture.pointerId) return;
149
+ if (!activeGesture.isStarted && event.buttons === 0) {
150
+ resetGesture();
151
+ return;
152
+ }
153
+ const offset = {
154
+ x: event.clientX - activeGesture.start.x,
155
+ y: event.clientY - activeGesture.start.y
156
+ };
157
+ if (!activeGesture.isStarted && Math.hypot(offset.x, offset.y) < getOptions().dragStartDistance) return;
158
+ if (!activeGesture.isStarted) activeGesture.element.setPointerCapture(event.pointerId);
159
+ const cell = getGestureCell({
160
+ columnNumber: getOptions().columnNumber,
161
+ columnPitch: columnPitch.value,
162
+ edge: activeGesture.edge,
163
+ offset,
164
+ origin: activeGesture.origin,
165
+ rowPitch: rowPitch.value
166
+ });
167
+ const point = {
168
+ x: event.clientX,
169
+ y: event.clientY
170
+ };
171
+ const isCellChanged = !activeGesture.cell || !checkIsSameCell(cell, activeGesture.cell);
172
+ const transfer = findTransfer(activeGesture, point);
173
+ if (activeGesture.transfer !== transfer) activeGesture.transfer?.clearPreview();
174
+ gesture.value = {
175
+ ...activeGesture,
176
+ cell,
177
+ isStarted: true,
178
+ offset,
179
+ transfer
180
+ };
181
+ if (Boolean(transfer?.preview(activeGesture.origin, point))) gesturePreview.value = void 0;
182
+ else if (isCellChanged || !gesturePreview.value) gesturePreview.value = getPreview(cell, layout.value);
183
+ };
184
+ const getChangedCells = (changedLayout) => changedLayout.flatMap((item) => {
185
+ const savedItem = savedItemMap.value.get(item.id);
186
+ return !savedItem || !checkIsSameCell(item, savedItem) ? [{
187
+ h: item.h,
188
+ id: item.id,
189
+ w: item.w,
190
+ x: item.x,
191
+ y: item.y
192
+ }] : [];
193
+ });
194
+ const accept = (item, point) => {
195
+ const preview = getDropPreview(item, point);
196
+ if (!preview || preview.isRefused) return void 0;
197
+ const changes = getChangedCells(preview.layout).filter(({ id }) => id !== item.id);
198
+ if (changes.length > 0) getOptions().onCommit({ changes });
199
+ const { h, w, x, y } = preview.cell;
200
+ return {
201
+ h,
202
+ w,
203
+ x,
204
+ y
205
+ };
206
+ };
207
+ const stepItem = (id, step) => {
208
+ const item = layout.value.find((other) => other.id === id);
209
+ if (!getOptions().isEditable || !item) return false;
210
+ const { dh = 0, dw = 0, dx = 0, dy = 0 } = step;
211
+ if (item.isAutoHeight && dh !== 0) return false;
212
+ const target = {
213
+ ...item,
214
+ h: Math.max(1, item.h + dh),
215
+ w: Math.max(1, item.w + dw),
216
+ x: item.x + dx,
217
+ y: item.y + dy
218
+ };
219
+ if (target.x < 0 || target.y < 0 || target.x + target.w > getOptions().columnNumber || checkIsSameCell(target, item)) return false;
220
+ const preview = getPreview(target, layout.value);
221
+ const changes = preview.isRefused ? [] : getChangedCells(preview.layout);
222
+ if (changes.length === 0) return false;
223
+ getOptions().onCommit({ changes });
224
+ return true;
225
+ };
226
+ const handlePointerUp = (event) => {
227
+ const activeGesture = gesture.value;
228
+ if (!activeGesture || event.pointerId !== activeGesture.pointerId) return;
229
+ const point = {
230
+ x: event.clientX,
231
+ y: event.clientY
232
+ };
233
+ const preview = gesturePreview.value;
234
+ const changes = preview && !preview.isRefused ? getChangedCells(preview.layout) : [];
235
+ const transfer = findTransfer(activeGesture, point);
236
+ resetGesture();
237
+ if (!transfer) {
238
+ if (changes.length > 0) getOptions().onCommit({ changes });
239
+ return;
240
+ }
241
+ const cell = transfer.accept(activeGesture.origin, point);
242
+ if (cell) getOptions().onTransfer?.({
243
+ cell,
244
+ item: activeGesture.origin,
245
+ target: transfer.element
246
+ });
247
+ };
248
+ const reportContentHeight = (id, contentHeight) => {
249
+ const savedItem = savedItemMap.value.get(id);
250
+ if (!getOptions().isEditable || !savedItem?.isAutoHeight) return;
251
+ const h = getCellSpan(contentHeight, rowPitch.value, getOptions().spacing.gap);
252
+ if (h === savedItem.h) return;
253
+ const otherItems = getOptions().items.filter((item) => item.id !== id);
254
+ const changes = getChangedCells(fitLayout([...otherItems, {
255
+ ...savedItem,
256
+ h
257
+ }], getOptions().columnNumber, getOptions().compactType, solidIds.value));
258
+ if (changes.length > 0) getOptions().onCommit({ changes });
259
+ };
260
+ const handlePointerCancel = (event) => {
261
+ if (event.pointerId === gesture.value?.pointerId) resetGesture();
262
+ };
263
+ const attached = shallowRef();
264
+ const previewDrop = (item, point) => {
265
+ const preview = getDropPreview(item, point);
266
+ const current = externalPreview.value;
267
+ if (preview && current?.cell.id === preview.cell.id && checkIsSameCell(preview.cell, current.cell)) return true;
268
+ externalPreview.value = preview;
269
+ return preview !== void 0;
270
+ };
271
+ onMounted(() => {
272
+ const element = root.value;
273
+ if (!element) return;
274
+ const controller = new AbortController();
275
+ const { signal } = controller;
276
+ const observer = new ResizeObserver(([entry]) => {
277
+ width.value = entry?.contentRect.width ?? 0;
278
+ });
279
+ observer.observe(element);
280
+ element.addEventListener("pointermove", handlePointerMove, { signal });
281
+ element.addEventListener("pointerup", handlePointerUp, { signal });
282
+ element.addEventListener("pointercancel", handlePointerCancel, { signal });
283
+ mountedGrids.set(element, {
284
+ accept,
285
+ clearPreview,
286
+ element,
287
+ preview: previewDrop
288
+ });
289
+ attached.value = {
290
+ controller,
291
+ element,
292
+ observer
293
+ };
294
+ });
295
+ onUnmounted(() => {
296
+ resetGesture();
297
+ if (!attached.value) return;
298
+ const { controller, element, observer } = attached.value;
299
+ observer.disconnect();
300
+ mountedGrids.delete(element);
301
+ controller.abort();
302
+ });
303
+ return {
304
+ gridHeight,
305
+ placeholder,
306
+ reportContentHeight,
307
+ startGesture,
308
+ stepItem,
309
+ tiles
310
+ };
311
+ };
312
+ export { useGridLayout };
package/dist/index.d.ts CHANGED
@@ -1,119 +1,23 @@
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
- }
14
- interface GridGeometry {
15
- height: number;
16
- left: number;
17
- top: number;
18
- width: number;
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
- }
30
- interface GridPoint {
31
- x: number;
32
- y: number;
33
- }
34
- interface GridTile<T extends GridItem = GridItem> {
35
- geometry: GridGeometry;
36
- isDragged: boolean;
37
- item: T;
38
- offset: GridPoint;
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
- }
50
- interface GridLayout<T extends GridItem = GridItem> {
51
- gridHeight: ComputedRef<number>;
52
- placeholder: ComputedRef<GridPlaceholder | undefined>;
53
- reportContentHeight: (id: string, contentHeight: number) => void;
54
- startGesture: (event: PointerEvent, item: T, edge?: ResizeEdge) => void;
55
- stepItem: (id: string, step: GridStep) => boolean;
56
- tiles: ComputedRef<GridTile<T>[]>;
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
- }
95
- export declare const useGridLayout: <T extends GridItem>(root: Readonly<ShallowRef<HTMLElement | null>>, options: MaybeRefOrGetter<GridLayoutOptions<T>>) => GridLayout<T>;
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;
104
- edge?: ResizeEdge;
105
- element: Element;
106
- isStarted: boolean;
107
- offset: GridPoint;
108
- origin: T;
109
- pointerId: number;
110
- start: GridPoint;
111
- transfer?: MountedGrid;
112
- }
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 };
1
+ import { useGridLayout } from "./composables/useGridLayout.js";
2
+ import { CollisionMode } from "./models/CollisionMode.js";
3
+ import { CompactType } from "./models/CompactType.js";
4
+ import { GridBlock } from "./models/GridBlock.js";
5
+ import { GridCell } from "./models/GridCell.js";
6
+ import { GridCommit } from "./models/GridCommit.js";
7
+ import { GridGeometry } from "./models/GridGeometry.js";
8
+ import { GridGesture } from "./models/GridGesture.js";
9
+ import { GridItem } from "./models/GridItem.js";
10
+ import { GridLayout } from "./models/GridLayout.js";
11
+ import { GridLayoutOptions } from "./models/GridLayoutOptions.js";
12
+ import { GridPlaceholder } from "./models/GridPlaceholder.js";
13
+ import { GridPoint } from "./models/GridPoint.js";
14
+ import { GridRefusal } from "./models/GridRefusal.js";
15
+ import { GridSpacing } from "./models/GridSpacing.js";
16
+ import { GridStep } from "./models/GridStep.js";
17
+ import { GridTile } from "./models/GridTile.js";
18
+ import { GridTransfer } from "./models/GridTransfer.js";
19
+ import { ResizeEdge } from "./models/ResizeEdge.js";
20
+ import { checkChangesHeight } from "./placement/checkChangesHeight.js";
21
+ import { getFirstFreeCell } from "./placement/getFirstFreeCell.js";
22
+ import { startExternalDrag } from "./services/startExternalDrag.js";
23
+ export { CollisionMode, CompactType, type GridBlock, type GridCell, type GridCommit, type GridGeometry, type GridGesture, type GridItem, type GridLayout, type GridLayoutOptions, type GridPlaceholder, type GridPoint, type GridRefusal, type GridSpacing, type GridStep, type GridTile, type GridTransfer, ResizeEdge, checkChangesHeight, getFirstFreeCell, startExternalDrag, useGridLayout };