1 | import { pointToLocation, tableSize } from '../table-utils/table.js';
|
---|
2 | import { getTileTouchPoint } from '../table-utils/tiles.js';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Calculates the new table configuration while the tiles are moving
|
---|
6 | * @param offsetX
|
---|
7 | * @param offsetY
|
---|
8 | * @returns
|
---|
9 | */
|
---|
10 | var reorder = {
|
---|
11 | onDragMove: function (_a) {
|
---|
12 | var _b;
|
---|
13 | var offsetX = _a.offsetX, offsetY = _a.offsetY, config = _a.config, state = _a.state, table = _a.table, canAcceptDrop = _a.canAcceptDrop;
|
---|
14 | var elementHeight = config.elementHeight, elementWidth = config.elementWidth;
|
---|
15 | var draggingTile = state.draggingTile, dragPosition = state.dragPosition, dropTargetTile = state.dropTargetTile, droppable = state.droppable, start = state.start, tiles = state.tiles;
|
---|
16 | console.log('DRAG MOVE');
|
---|
17 | if (!draggingTile || !dragPosition || !start || !tiles)
|
---|
18 | return;
|
---|
19 | var getInsertionPoint = function (tiles, touchPoint, touchedTile) {
|
---|
20 | var tilePosition = tiles.findIndex(function (tile) { return tile.data === (touchedTile === null || touchedTile === void 0 ? void 0 : touchedTile.data); });
|
---|
21 | if (touchPoint === 'left') {
|
---|
22 | var leftTile = tilePosition - 1 >= 0 ? tiles[tilePosition - 1] : undefined;
|
---|
23 | if (leftTile === draggingTile)
|
---|
24 | return undefined;
|
---|
25 | return {
|
---|
26 | insertionPoint: {
|
---|
27 | right: touchedTile,
|
---|
28 | left: (leftTile === null || leftTile === void 0 ? void 0 : leftTile.row) === touchedTile.row ? leftTile : undefined,
|
---|
29 | },
|
---|
30 | };
|
---|
31 | }
|
---|
32 | else {
|
---|
33 | var rightTile = tilePosition + 1 < tiles.length ? tiles[tilePosition + 1] : undefined;
|
---|
34 | if (rightTile === draggingTile)
|
---|
35 | return undefined;
|
---|
36 | return {
|
---|
37 | insertionPoint: {
|
---|
38 | left: touchedTile,
|
---|
39 | right: (rightTile === null || rightTile === void 0 ? void 0 : rightTile.row) === touchedTile.row ? rightTile : undefined,
|
---|
40 | },
|
---|
41 | };
|
---|
42 | }
|
---|
43 | };
|
---|
44 | var x = start.col * elementWidth + offsetX + dragPosition.x;
|
---|
45 | var y = start.row * elementHeight + offsetY + dragPosition.y;
|
---|
46 | //find the position of the tile in the grid
|
---|
47 | var cell = pointToLocation(table, x, y, config);
|
---|
48 | var tsize = tableSize(table);
|
---|
49 | //console.log('data:', { x, y, cell, start, draggingTile });
|
---|
50 | //try to see if we're touching a hot point
|
---|
51 | //find the touched tile within the move
|
---|
52 | var touchedTile = table[cell.row][cell.col];
|
---|
53 | if (!touchedTile || touchedTile.data === draggingTile.data)
|
---|
54 | return;
|
---|
55 | //get the touchpoint of the touched tile
|
---|
56 | var touchPoint = getTileTouchPoint(touchedTile, touchedTile, {
|
---|
57 | x: x,
|
---|
58 | y: y,
|
---|
59 | }, config);
|
---|
60 | if (!touchPoint)
|
---|
61 | return;
|
---|
62 | //console.log('HIT TOUCHPOINT', { table, tiles });
|
---|
63 | console.log('touch point:', touchPoint);
|
---|
64 | if (touchPoint === 'center') {
|
---|
65 | if (touchedTile === dropTargetTile)
|
---|
66 | return {
|
---|
67 | dropTargetTile: dropTargetTile,
|
---|
68 | droppable: droppable,
|
---|
69 | };
|
---|
70 | if (canAcceptDrop(draggingTile, touchedTile))
|
---|
71 | return {
|
---|
72 | dropTargetTile: touchedTile,
|
---|
73 | droppable: true,
|
---|
74 | };
|
---|
75 | return;
|
---|
76 | }
|
---|
77 | //apply calculation only if touching left or right areas
|
---|
78 | if (touchPoint !== 'right' && touchPoint !== 'left')
|
---|
79 | return;
|
---|
80 | //if the touch point is in the bottom of the cell, pick the adjacent cell
|
---|
81 | if (touchedTile.row !== cell.row) {
|
---|
82 | var adjacentCol = touchPoint === 'left' ? cell.col - 1 : cell.col + touchedTile.colSpan;
|
---|
83 | if (adjacentCol < 0 || adjacentCol > tsize.cols)
|
---|
84 | return;
|
---|
85 | var adjacentTile = table[cell.row][adjacentCol];
|
---|
86 | if (!adjacentTile)
|
---|
87 | return;
|
---|
88 | var shiftedInsertionPoint = (_b = getInsertionPoint(tiles, touchPoint === 'left' ? 'right' : 'left', adjacentTile)) === null || _b === void 0 ? void 0 : _b.insertionPoint;
|
---|
89 | console.log('SHIFTING: ', { shiftedInsertionPoint: shiftedInsertionPoint, touchPoint: touchPoint });
|
---|
90 | return {
|
---|
91 | insertionPoint: {
|
---|
92 | left: touchPoint === 'left' ? shiftedInsertionPoint === null || shiftedInsertionPoint === void 0 ? void 0 : shiftedInsertionPoint.right : undefined,
|
---|
93 | right: touchPoint === 'right' ? shiftedInsertionPoint === null || shiftedInsertionPoint === void 0 ? void 0 : shiftedInsertionPoint.left : undefined,
|
---|
94 | },
|
---|
95 | };
|
---|
96 | }
|
---|
97 | return getInsertionPoint(tiles, touchPoint, touchedTile);
|
---|
98 | },
|
---|
99 | onDragEnd: function (_a) {
|
---|
100 | var state = _a.state;
|
---|
101 | var insertionPoint = state.insertionPoint, draggingTile = state.draggingTile, tiles = state.tiles;
|
---|
102 | console.log('Drag end start:', {
|
---|
103 | insertionPoint: insertionPoint,
|
---|
104 | draggingTile: draggingTile,
|
---|
105 | tiles: tiles,
|
---|
106 | });
|
---|
107 | if (!tiles || !draggingTile || !insertionPoint)
|
---|
108 | return;
|
---|
109 | var newTiles = tiles.filter(function (tile) { return tile.data !== draggingTile.data; });
|
---|
110 | var left = insertionPoint.left, right = insertionPoint.right;
|
---|
111 | var insertionLeftIdx = left
|
---|
112 | ? newTiles.findIndex(function (tile) { return tile.data === left.data; }) + 1
|
---|
113 | : -1;
|
---|
114 | var insertionRightIdx = right
|
---|
115 | ? newTiles.findIndex(function (tile) { return tile.data === right.data; })
|
---|
116 | : -1;
|
---|
117 | var insertionIndex = insertionLeftIdx > 0 ? insertionLeftIdx : insertionRightIdx;
|
---|
118 | if (insertionIndex < 0)
|
---|
119 | newTiles.push(draggingTile);
|
---|
120 | else {
|
---|
121 | newTiles.splice(insertionIndex, 0, draggingTile);
|
---|
122 | }
|
---|
123 | console.log('Drag end:', {
|
---|
124 | insertionPoint: insertionPoint,
|
---|
125 | insertionLeftIdx: insertionLeftIdx,
|
---|
126 | insertionRightIdx: insertionRightIdx,
|
---|
127 | draggingTile: draggingTile,
|
---|
128 | tiles: tiles,
|
---|
129 | newTiles: newTiles,
|
---|
130 | });
|
---|
131 | return { tiles: newTiles };
|
---|
132 | },
|
---|
133 | };
|
---|
134 |
|
---|
135 | export default reorder;
|
---|
136 | //# sourceMappingURL=reorder.js.map
|
---|