[d565449] | 1 | import { __read, __assign } from 'tslib';
|
---|
| 2 | import { useState, useMemo } from 'react';
|
---|
| 3 | import { useDrag } from '@use-gesture/react';
|
---|
| 4 | import { tilesListToTable, tableToTilesList } from './table-utils/tiles-table.js';
|
---|
| 5 | import dragMove from './strategies/move.js';
|
---|
| 6 | import reorder from './strategies/reorder.js';
|
---|
| 7 |
|
---|
| 8 | var useTileTable = function (_a) {
|
---|
| 9 | var enabled = _a.enabled, strategy = _a.strategy, elementWidth = _a.elementWidth, elementHeight = _a.elementHeight, activeBorderSize = _a.activeBorderSize, columns = _a.columns, currentTiles = _a.currentTiles, canAcceptDrop = _a.canAcceptDrop, changeTilesOrder = _a.changeTilesOrder, didDrop = _a.didDrop;
|
---|
| 10 | var strategyImpl = strategy === 'reorder' ? reorder : dragMove;
|
---|
| 11 | var _b = __read(useState({
|
---|
| 12 | dragging: false,
|
---|
| 13 | droppable: false,
|
---|
| 14 | }), 2), state = _b[0], setState = _b[1];
|
---|
| 15 | //the tile layout table
|
---|
| 16 | var draggingTiles = state.tiles, dragging = state.dragging, insertionPoint = state.insertionPoint;
|
---|
| 17 | var effectiveTiles = (enabled && dragging && draggingTiles) || currentTiles;
|
---|
| 18 | var table = useMemo(function () { return tilesListToTable(effectiveTiles, columns); }, [effectiveTiles, columns]);
|
---|
| 19 | var positionedTiles = useMemo(function () { return tableToTilesList(table); }, [table]);
|
---|
| 20 | var config = {
|
---|
| 21 | elementHeight: elementHeight,
|
---|
| 22 | elementWidth: elementWidth,
|
---|
| 23 | activeBorderSize: activeBorderSize,
|
---|
| 24 | columns: columns,
|
---|
| 25 | };
|
---|
| 26 | var bind = useDrag(function (_a) {
|
---|
| 27 | var _b = __read(_a.args, 1), data = _b[0], dragging = _a.dragging, tap = _a.tap, xy = _a.xy, movement = _a.movement, event = _a.event;
|
---|
| 28 | if (dragging) {
|
---|
| 29 | if (!tap) {
|
---|
| 30 | event.preventDefault();
|
---|
| 31 | event.stopPropagation();
|
---|
| 32 | }
|
---|
| 33 | //if this is the first event, initialize the dragging
|
---|
| 34 | if (!state.dragging) {
|
---|
| 35 | var rect = event.currentTarget.getBoundingClientRect();
|
---|
| 36 | //identify the hit tile
|
---|
| 37 | var draggingTile_1 = positionedTiles.find(function (tile) { return tile.data === data; });
|
---|
| 38 | if (!draggingTile_1)
|
---|
| 39 | return;
|
---|
| 40 | setState({
|
---|
| 41 | dragging: true,
|
---|
| 42 | droppable: false,
|
---|
| 43 | tiles: positionedTiles,
|
---|
| 44 | draggingTile: draggingTile_1,
|
---|
| 45 | dragPosition: { x: xy[0] - rect.x, y: xy[1] - rect.y },
|
---|
| 46 | offset: { x: movement[0], y: movement[1] },
|
---|
| 47 | start: { col: draggingTile_1.col, row: draggingTile_1.row },
|
---|
| 48 | });
|
---|
| 49 | }
|
---|
| 50 | else {
|
---|
| 51 | var result_1 = strategyImpl.onDragMove({
|
---|
| 52 | offsetX: movement[0],
|
---|
| 53 | offsetY: movement[1],
|
---|
| 54 | canAcceptDrop: canAcceptDrop,
|
---|
| 55 | config: config,
|
---|
| 56 | state: state,
|
---|
| 57 | table: table,
|
---|
| 58 | }) || {};
|
---|
| 59 | setState(function (state) { return (__assign(__assign(__assign({}, state), { offset: { x: movement[0], y: movement[1] }, dropTargetTile: undefined, droppable: false, insertionPoint: undefined }), result_1)); });
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | else {
|
---|
| 63 | if (state.draggingTile && state.dropTargetTile && state.droppable) {
|
---|
| 64 | didDrop(state.draggingTile, state.dropTargetTile);
|
---|
| 65 | }
|
---|
| 66 | else {
|
---|
| 67 | var result = strategyImpl.onDragEnd({
|
---|
| 68 | offsetX: movement[0],
|
---|
| 69 | offsetY: movement[1],
|
---|
| 70 | canAcceptDrop: canAcceptDrop,
|
---|
| 71 | config: config,
|
---|
| 72 | state: state,
|
---|
| 73 | table: table,
|
---|
| 74 | }) || {};
|
---|
| 75 | var finalState = __assign(__assign({}, state), result);
|
---|
| 76 | finalState.tiles && changeTilesOrder(finalState.tiles);
|
---|
| 77 | }
|
---|
| 78 | setState({
|
---|
| 79 | dragging: false,
|
---|
| 80 | droppable: false,
|
---|
| 81 | });
|
---|
| 82 | }
|
---|
| 83 | }, { filterTaps: true, enabled: enabled });
|
---|
| 84 | var renderTileProps = useMemo(function () {
|
---|
| 85 | if (!enabled) {
|
---|
| 86 | return positionedTiles
|
---|
| 87 | .map(function (tile) { return (__assign(__assign({}, tile), { tileWidth: elementWidth, tileHeight: elementHeight, isDragging: false, isDropTarget: false, isDroppable: false, isDroppableAtInsertPosition: false, insertAtLeft: false, insertAtRight: false, x: tile.col * elementWidth, y: tile.row * elementHeight })); })
|
---|
| 88 | .sort(function (a, b) { return a.key - b.key; });
|
---|
| 89 | }
|
---|
| 90 | else
|
---|
| 91 | return positionedTiles
|
---|
| 92 | .map(function (tile) {
|
---|
| 93 | var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
---|
| 94 | return __assign(__assign({}, tile), { tileWidth: elementWidth, tileHeight: elementHeight, isDragging: tile.data === ((_a = state.draggingTile) === null || _a === void 0 ? void 0 : _a.data), isDropTarget: tile.data === ((_b = state.dropTargetTile) === null || _b === void 0 ? void 0 : _b.data), insertAtLeft: tile.data === ((_d = (_c = state.insertionPoint) === null || _c === void 0 ? void 0 : _c.left) === null || _d === void 0 ? void 0 : _d.data), insertAtRight: tile.data === ((_f = (_e = state.insertionPoint) === null || _e === void 0 ? void 0 : _e.right) === null || _f === void 0 ? void 0 : _f.data), isDroppable: tile.data === ((_g = state.draggingTile) === null || _g === void 0 ? void 0 : _g.data) && state.droppable, isDroppableAtInsertPosition: !!(tile.data === ((_h = state.draggingTile) === null || _h === void 0 ? void 0 : _h.data) && state.insertionPoint), x: tile.data === ((_j = state.draggingTile) === null || _j === void 0 ? void 0 : _j.data)
|
---|
| 95 | ? (((_k = state.start) === null || _k === void 0 ? void 0 : _k.col) || 0) * elementWidth +
|
---|
| 96 | (((_l = state.offset) === null || _l === void 0 ? void 0 : _l.x) || 0)
|
---|
| 97 | : tile.col * elementWidth, y: tile.data === ((_m = state.draggingTile) === null || _m === void 0 ? void 0 : _m.data)
|
---|
| 98 | ? (((_o = state.start) === null || _o === void 0 ? void 0 : _o.row) || 0) * elementHeight +
|
---|
| 99 | (((_p = state.offset) === null || _p === void 0 ? void 0 : _p.y) || 0)
|
---|
| 100 | : tile.row * elementHeight });
|
---|
| 101 | })
|
---|
| 102 | .sort(function (a, b) { return a.key - b.key; });
|
---|
| 103 | }, [state, positionedTiles, elementHeight, elementWidth, enabled]);
|
---|
| 104 | var draggingTile = state.draggingTile;
|
---|
| 105 | var insertIndicatorPosition = useMemo(function () {
|
---|
| 106 | var _a = insertionPoint || {}, left = _a.left, right = _a.right;
|
---|
| 107 | if (left)
|
---|
| 108 | return {
|
---|
| 109 | x: (left.col + left.colSpan) * elementWidth,
|
---|
| 110 | y: left.row * elementHeight,
|
---|
| 111 | };
|
---|
| 112 | if (right)
|
---|
| 113 | return {
|
---|
| 114 | x: right.col * elementWidth,
|
---|
| 115 | y: right.row * elementHeight,
|
---|
| 116 | };
|
---|
| 117 | if (draggingTile)
|
---|
| 118 | return {
|
---|
| 119 | x: draggingTile.col * elementWidth,
|
---|
| 120 | y: draggingTile.row * elementHeight,
|
---|
| 121 | };
|
---|
| 122 | }, [insertionPoint, elementHeight, elementWidth, draggingTile]);
|
---|
| 123 | return {
|
---|
| 124 | table: table,
|
---|
| 125 | tableHeight: table.length * elementHeight,
|
---|
| 126 | tiles: positionedTiles,
|
---|
| 127 | insertIndicatorPosition: insertIndicatorPosition,
|
---|
| 128 | bind: bind,
|
---|
| 129 | renderTileProps: renderTileProps,
|
---|
| 130 | };
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | export { useTileTable };
|
---|
| 134 | //# sourceMappingURL=useTileTable.js.map
|
---|