1 | {"version":3,"file":"useTileTable.js","sources":["../../../src/tile-layout/useTileTable.ts"],"sourcesContent":["import { useMemo, useState } from 'react';\nimport { useDrag } from '@use-gesture/react';\nimport {\n GridConfig,\n TileInfo,\n TilePositionInfo,\n RenderTileProps,\n} from './model';\nimport * as TilesTableUtils from './table-utils/tiles-table';\nimport moveStrategy from './strategies/move';\nimport reorderStrategy from './strategies/reorder';\nimport { DragState } from './model';\nimport { reduceRight } from 'lodash';\n\ninterface TileTableDNDProps<T> {\n strategy: 'reorder' | 'move';\n enabled: boolean;\n elementWidth: number;\n elementHeight: number;\n activeBorderSize: number;\n columns: number;\n currentTiles: TileInfo<T>[];\n canAcceptDrop: (\n draggingTile: TileInfo<T>,\n targetTile: TileInfo<T>\n ) => boolean;\n didDrop: (draggingTile: TileInfo<T>, targetTile: TileInfo<T>) => void;\n changeTilesOrder: (tiles: TileInfo<T>[]) => void;\n}\n\nexport const useTileTable = <T>({\n enabled,\n strategy,\n elementWidth,\n elementHeight,\n activeBorderSize,\n columns,\n currentTiles,\n canAcceptDrop,\n changeTilesOrder,\n didDrop,\n}: TileTableDNDProps<T>) => {\n const strategyImpl = strategy === 'reorder' ? reorderStrategy : moveStrategy;\n const [state, setState] = useState<DragState<T>>({\n dragging: false,\n droppable: false,\n });\n\n //the tile layout table\n const { tiles: draggingTiles, dragging, insertionPoint } = state;\n\n const effectiveTiles = (enabled && dragging && draggingTiles) || currentTiles;\n\n const table = useMemo(\n () => TilesTableUtils.tilesListToTable(effectiveTiles, columns),\n [effectiveTiles, columns]\n );\n\n const positionedTiles = useMemo(\n () => TilesTableUtils.tableToTilesList(table),\n [table]\n );\n\n const config: GridConfig = {\n elementHeight,\n elementWidth,\n activeBorderSize,\n columns,\n };\n\n const bind = useDrag<React.PointerEvent<HTMLDivElement>>(\n ({ args: [data], dragging, tap, xy, movement, event }) => {\n if (dragging) {\n if (!tap) {\n event.preventDefault();\n event.stopPropagation();\n }\n\n //if this is the first event, initialize the dragging\n if (!state.dragging) {\n const rect = event.currentTarget.getBoundingClientRect();\n\n //identify the hit tile\n const draggingTile = positionedTiles.find(tile => tile.data === data);\n if (!draggingTile) return;\n\n setState({\n dragging: true,\n droppable: false,\n tiles: positionedTiles,\n draggingTile,\n dragPosition: { x: xy[0] - rect.x, y: xy[1] - rect.y },\n offset: { x: movement[0], y: movement[1] },\n start: { col: draggingTile.col, row: draggingTile.row },\n });\n } else {\n const result =\n strategyImpl.onDragMove({\n offsetX: movement[0],\n offsetY: movement[1],\n canAcceptDrop,\n config,\n state,\n table,\n }) || {};\n setState(state => ({\n ...state,\n offset: { x: movement[0], y: movement[1] },\n dropTargetTile: undefined,\n droppable: false,\n insertionPoint: undefined,\n ...result,\n }));\n }\n } else {\n if (state.draggingTile && state.dropTargetTile && state.droppable) {\n didDrop(state.draggingTile, state.dropTargetTile);\n } else {\n const result =\n strategyImpl.onDragEnd({\n offsetX: movement[0],\n offsetY: movement[1],\n canAcceptDrop,\n config,\n state,\n table,\n }) || {};\n const finalState = { ...state, ...result };\n finalState.tiles && changeTilesOrder(finalState.tiles);\n }\n\n setState({\n dragging: false,\n droppable: false,\n });\n }\n },\n { filterTaps: true, enabled }\n );\n\n const renderTileProps = useMemo<\n (RenderTileProps<T> & {\n x: number;\n y: number;\n key: number;\n })[]\n >(() => {\n if (!enabled) {\n return positionedTiles\n .map(tile => ({\n ...tile,\n tileWidth: elementWidth,\n tileHeight: elementHeight,\n isDragging: false,\n isDropTarget: false,\n isDroppable: false,\n isDroppableAtInsertPosition: false,\n insertAtLeft: false,\n insertAtRight: false,\n x: tile.col * elementWidth,\n y: tile.row * elementHeight,\n }))\n .sort((a, b) => a.key - b.key);\n } else\n return positionedTiles\n .map(tile => {\n return {\n ...tile,\n tileWidth: elementWidth,\n tileHeight: elementHeight,\n isDragging: tile.data === state.draggingTile?.data,\n isDropTarget: tile.data === state.dropTargetTile?.data,\n insertAtLeft: tile.data === state.insertionPoint?.left?.data,\n insertAtRight: tile.data === state.insertionPoint?.right?.data,\n isDroppable:\n tile.data === state.draggingTile?.data && state.droppable,\n isDroppableAtInsertPosition: !!(\n tile.data === state.draggingTile?.data && state.insertionPoint\n ),\n x:\n tile.data === state.draggingTile?.data\n ? (state.start?.col || 0) * elementWidth +\n (state.offset?.x || 0)\n : tile.col * elementWidth,\n y:\n tile.data === state.draggingTile?.data\n ? (state.start?.row || 0) * elementHeight +\n (state.offset?.y || 0)\n : tile.row * elementHeight,\n };\n })\n .sort((a, b) => a.key - b.key);\n }, [state, positionedTiles, elementHeight, elementWidth, enabled]);\n\n const { draggingTile } = state;\n const insertIndicatorPosition = useMemo(() => {\n const { left, right } = insertionPoint || {};\n if (left)\n return {\n x: (left.col + left.colSpan) * elementWidth,\n y: left.row * elementHeight,\n };\n if (right)\n return {\n x: right.col * elementWidth,\n y: right.row * elementHeight,\n };\n if (draggingTile)\n return {\n x: draggingTile.col * elementWidth,\n y: draggingTile.row * elementHeight,\n };\n }, [insertionPoint, elementHeight, elementWidth, draggingTile]);\n\n return {\n table,\n tableHeight: table.length * elementHeight,\n tiles: positionedTiles,\n insertIndicatorPosition,\n bind,\n renderTileProps,\n };\n};\n"],"names":["reorderStrategy","moveStrategy","TilesTableUtils.tilesListToTable","TilesTableUtils.tableToTilesList"],"mappings":";;;;;;;IA8Ba,YAAY,GAAG,UAAI,EAWT;QAVrB,OAAO,aAAA,EACP,QAAQ,cAAA,EACR,YAAY,kBAAA,EACZ,aAAa,mBAAA,EACb,gBAAgB,sBAAA,EAChB,OAAO,aAAA,EACP,YAAY,kBAAA,EACZ,aAAa,mBAAA,EACb,gBAAgB,sBAAA,EAChB,OAAO,aAAA;IAEP,IAAM,YAAY,GAAG,QAAQ,KAAK,SAAS,GAAGA,OAAe,GAAGC,QAAY,CAAC;IACvE,IAAA,KAAA,OAAoB,QAAQ,CAAe;QAC/C,QAAQ,EAAE,KAAK;QACf,SAAS,EAAE,KAAK;KACjB,CAAC,IAAA,EAHK,KAAK,QAAA,EAAE,QAAQ,QAGpB,CA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