grid-cell-selection 1.0.6 → 1.0.7
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/dist/index.esm.js +0 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -62,7 +62,6 @@ var useMouseDragSelection = function (toggleCellSelection, resetSelection, optio
|
|
62
62
|
var handleMouseUp = function () {
|
63
63
|
setIsDragging(false);
|
64
64
|
if (selectionStartScrollY.current !== null && options.clearSelectionOnScroll) {
|
65
|
-
console.log("scrollY", window.scrollY, "selectionStartScrollY", selectionStartScrollY.current);
|
66
65
|
if (Math.abs(window.scrollY - selectionStartScrollY.current) > options.scrollThreshold) {
|
67
66
|
resetSelection();
|
68
67
|
}
|
@@ -122,7 +121,6 @@ var useGridCellSelection = function (_a) {
|
|
122
121
|
};
|
123
122
|
var handleTouchMove = useCallback(function (event) {
|
124
123
|
// Get the touch point
|
125
|
-
console.log("selectedCells", selectionState.selectedCells);
|
126
124
|
if (selectionState.selectedCells.size > 0) {
|
127
125
|
var touch = event.touches[0];
|
128
126
|
var target = document.elementFromPoint(touch.clientX, touch.clientY);
|
package/dist/index.esm.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void,\n resetSelection: () => void,\n options: SelectionOptions\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const selectionStartScrollY = useRef<number | null>(null);\n const initialCellRef = useRef<CellIdentifier | null>(null);\n\n const handleTouchStart = (cell: CellIdentifier, event: React.TouchEvent) => {\n setIsDragging(true);\n initialCellRef.current = cell;\n selectionStartScrollY.current = window.scrollY;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (isDragging) {\n // Check if the user is dragging vertically\n const isDraggingVertically = initialCellRef.current && cell.row !== initialCellRef.current.row;\n\n // If the user is dragging vertically, prevent the selection\n if (!options.allowYScrollSelection && isDraggingVertically) {\n return;\n }\n\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (!isDragging) {\n setIsDragging(true);\n initialCellRef.current = cell;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n if (selectionStartScrollY.current !== null && options.clearSelectionOnScroll) {\n console.log(\"scrollY\", window.scrollY, \"selectionStartScrollY\", selectionStartScrollY.current);\n if (Math.abs(window.scrollY - selectionStartScrollY.current) > options.scrollThreshold) {\n resetSelection();\n }\n }\n selectionStartScrollY.current = null;\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchStart,\n };\n};\n","import { useCallback, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = ({\n allCells,\n options = {\n allowYScrollSelection: true,\n clearSelectionOnScroll: false,\n scrollThreshold: 100,\n },\n}: {\n allCells?: CellIdentifier[];\n options?: SelectionOptions;\n}) => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const resetSelection = () => {\n setSelectionState({ selectedCells: new Map<string, CellIdentifier>() });\n };\n\n const handleTouchMove = useCallback(\n (event: React.TouchEvent) => {\n // Get the touch point\n console.log(\"selectedCells\", selectionState.selectedCells);\n if (selectionState.selectedCells.size > 0) {\n const touch = event.touches[0];\n const target = document.elementFromPoint(touch.clientX, touch.clientY);\n // Get closest cell\n const closest = target?.closest(\"[data-cell-id]\");\n const cellId = closest?.getAttribute(\"data-cell-id\");\n const cellRow = closest?.getAttribute(\"data-cell-row\");\n const cellCol = closest?.getAttribute(\"data-cell-col\");\n if (cellId && cellRow && cellCol && !selectionState.selectedCells.has(cellId)) {\n handleMouseEnter({ id: cellId, row: parseInt(cellRow), col: parseInt(cellCol) }, event);\n }\n }\n },\n [selectionState.selectedCells]\n );\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp, handleTouchStart } = useMouseDragSelection(\n toggleCellSelection,\n resetSelection,\n options\n );\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchMove,\n handleTouchStart,\n resetSelection,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier,\n allCells?: CellIdentifier[]\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n if (allCells) {\n const cell = allCells.find((c) => c.row === row && c.col === col);\n cell && selectedCells.set(cellKey, cell);\n } else {\n selectedCells.set(cellKey, { id: cellKey, row, col });\n }\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier\n) {\n if (selectedCells.has(cellKey)) {\n selectedCells.delete(cellKey);\n } else {\n selectedCells.set(cellKey, cell);\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAGa,qBAAqB,GAAG,UACnC,mBAA+G,EAC/G,cAA0B,EAC1B,OAAyB,EAAA;IAEnB,IAAA,EAAA,GAA8B,QAAQ,CAAC,KAAK,CAAC,EAA5C,UAAU,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,aAAa,GAAA,EAAA,CAAA,CAAA,CAAmB,CAAC;AAEpD,IAAA,IAAM,qBAAqB,GAAG,MAAM,CAAgB,IAAI,CAAC,CAAC;AAC1D,IAAA,IAAM,cAAc,GAAG,MAAM,CAAwB,IAAI,CAAC,CAAC;AAE3D,IAAA,IAAM,gBAAgB,GAAG,UAAC,IAAoB,EAAE,KAAuB,EAAA;QACrE,aAAa,CAAC,IAAI,CAAC,CAAC;AACpB,QAAA,cAAc,CAAC,OAAO,GAAG,IAAI,CAAC;AAC9B,QAAA,qBAAqB,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;AAC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G,EAAE,EAAA;gBAAlC,GAAG,CAAA,CAAA;AAcX,KAAA;AACH,CAAC;AAED,SAAS,+BAA+B,CACtC,aAA0C,EAC1C,OAAe,EACf,IAAoB,EAAA;AAEpB,IAAA,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;AAC9B,QAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,KAAA;AAAM,SAAA;AACL,QAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAClC,KAAA;AACH;;;;"}
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void,\n resetSelection: () => void,\n options: SelectionOptions\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const selectionStartScrollY = useRef<number | null>(null);\n const initialCellRef = useRef<CellIdentifier | null>(null);\n\n const handleTouchStart = (cell: CellIdentifier, event: React.TouchEvent) => {\n setIsDragging(true);\n initialCellRef.current = cell;\n selectionStartScrollY.current = window.scrollY;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (isDragging) {\n // Check if the user is dragging vertically\n const isDraggingVertically = initialCellRef.current && cell.row !== initialCellRef.current.row;\n\n // If the user is dragging vertically, prevent the selection\n if (!options.allowYScrollSelection && isDraggingVertically) {\n return;\n }\n\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (!isDragging) {\n setIsDragging(true);\n initialCellRef.current = cell;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n if (selectionStartScrollY.current !== null && options.clearSelectionOnScroll) {\n if (Math.abs(window.scrollY - selectionStartScrollY.current) > options.scrollThreshold) {\n resetSelection();\n }\n }\n selectionStartScrollY.current = null;\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchStart,\n };\n};\n","import { useCallback, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = ({\n allCells,\n options = {\n allowYScrollSelection: true,\n clearSelectionOnScroll: false,\n scrollThreshold: 100,\n },\n}: {\n allCells?: CellIdentifier[];\n options?: SelectionOptions;\n}) => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const resetSelection = () => {\n setSelectionState({ selectedCells: new Map<string, CellIdentifier>() });\n };\n\n const handleTouchMove = useCallback(\n (event: React.TouchEvent) => {\n // Get the touch point\n if (selectionState.selectedCells.size > 0) {\n const touch = event.touches[0];\n const target = document.elementFromPoint(touch.clientX, touch.clientY);\n // Get closest cell\n const closest = target?.closest(\"[data-cell-id]\");\n const cellId = closest?.getAttribute(\"data-cell-id\");\n const cellRow = closest?.getAttribute(\"data-cell-row\");\n const cellCol = closest?.getAttribute(\"data-cell-col\");\n if (cellId && cellRow && cellCol && !selectionState.selectedCells.has(cellId)) {\n handleMouseEnter({ id: cellId, row: parseInt(cellRow), col: parseInt(cellCol) }, event);\n }\n }\n },\n [selectionState.selectedCells]\n );\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp, handleTouchStart } = useMouseDragSelection(\n toggleCellSelection,\n resetSelection,\n options\n );\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchMove,\n handleTouchStart,\n resetSelection,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier,\n allCells?: CellIdentifier[]\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n if (allCells) {\n const cell = allCells.find((c) => c.row === row && c.col === col);\n cell && selectedCells.set(cellKey, cell);\n } else {\n selectedCells.set(cellKey, { id: cellKey, row, col });\n }\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier\n) {\n if (selectedCells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AAE,IAAI,CAAC,CAAC;AAC1C,iBAAA;AAAM,qBAAA;AACL,oBAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAA,GAAA,EAAE,GAAG,EAAA,GAAA,EAAE,CAAC,CAAC;AACvD,iBAAA;AACF,aAAA;AAAM,iBAAA;AACL,gBAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,aAAA;;QAXH,KAAK,IAAI,GAAG,GAAG,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,EAAE,EAAA;oBAAlC,GAAG,CAAA,CAAA;AAYX,SAAA;;IAbH,KAAK,IAAI,GAAG,GAAG,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,EAAE,EAAA;gBAAlC,GAAG,CAAA,CAAA;AAcX,KAAA;AACH,CAAC;AAED,SAAS,+BAA+B,CACtC,aAA0C,EAC1C,OAAe,EACf,IAAoB,EAAA;AAEpB,IAAA,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;AAC9B,QAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,KAAA;AAAM,SAAA;AACL,QAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAClC,KAAA;AACH;;;;"}
|
package/dist/index.js
CHANGED
@@ -66,7 +66,6 @@ var useMouseDragSelection = function (toggleCellSelection, resetSelection, optio
|
|
66
66
|
var handleMouseUp = function () {
|
67
67
|
setIsDragging(false);
|
68
68
|
if (selectionStartScrollY.current !== null && options.clearSelectionOnScroll) {
|
69
|
-
console.log("scrollY", window.scrollY, "selectionStartScrollY", selectionStartScrollY.current);
|
70
69
|
if (Math.abs(window.scrollY - selectionStartScrollY.current) > options.scrollThreshold) {
|
71
70
|
resetSelection();
|
72
71
|
}
|
@@ -126,7 +125,6 @@ var useGridCellSelection = function (_a) {
|
|
126
125
|
};
|
127
126
|
var handleTouchMove = react.useCallback(function (event) {
|
128
127
|
// Get the touch point
|
129
|
-
console.log("selectedCells", selectionState.selectedCells);
|
130
128
|
if (selectionState.selectedCells.size > 0) {
|
131
129
|
var touch = event.touches[0];
|
132
130
|
var target = document.elementFromPoint(touch.clientX, touch.clientY);
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void,\n resetSelection: () => void,\n options: SelectionOptions\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const selectionStartScrollY = useRef<number | null>(null);\n const initialCellRef = useRef<CellIdentifier | null>(null);\n\n const handleTouchStart = (cell: CellIdentifier, event: React.TouchEvent) => {\n setIsDragging(true);\n initialCellRef.current = cell;\n selectionStartScrollY.current = window.scrollY;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (isDragging) {\n // Check if the user is dragging vertically\n const isDraggingVertically = initialCellRef.current && cell.row !== initialCellRef.current.row;\n\n // If the user is dragging vertically, prevent the selection\n if (!options.allowYScrollSelection && isDraggingVertically) {\n return;\n }\n\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (!isDragging) {\n setIsDragging(true);\n initialCellRef.current = cell;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n if (selectionStartScrollY.current !== null && options.clearSelectionOnScroll) {\n console.log(\"scrollY\", window.scrollY, \"selectionStartScrollY\", selectionStartScrollY.current);\n if (Math.abs(window.scrollY - selectionStartScrollY.current) > options.scrollThreshold) {\n resetSelection();\n }\n }\n selectionStartScrollY.current = null;\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchStart,\n };\n};\n","import { useCallback, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = ({\n allCells,\n options = {\n allowYScrollSelection: true,\n clearSelectionOnScroll: false,\n scrollThreshold: 100,\n },\n}: {\n allCells?: CellIdentifier[];\n options?: SelectionOptions;\n}) => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const resetSelection = () => {\n setSelectionState({ selectedCells: new Map<string, CellIdentifier>() });\n };\n\n const handleTouchMove = useCallback(\n (event: React.TouchEvent) => {\n // Get the touch point\n console.log(\"selectedCells\", selectionState.selectedCells);\n if (selectionState.selectedCells.size > 0) {\n const touch = event.touches[0];\n const target = document.elementFromPoint(touch.clientX, touch.clientY);\n // Get closest cell\n const closest = target?.closest(\"[data-cell-id]\");\n const cellId = closest?.getAttribute(\"data-cell-id\");\n const cellRow = closest?.getAttribute(\"data-cell-row\");\n const cellCol = closest?.getAttribute(\"data-cell-col\");\n if (cellId && cellRow && cellCol && !selectionState.selectedCells.has(cellId)) {\n handleMouseEnter({ id: cellId, row: parseInt(cellRow), col: parseInt(cellCol) }, event);\n }\n }\n },\n [selectionState.selectedCells]\n );\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp, handleTouchStart } = useMouseDragSelection(\n toggleCellSelection,\n resetSelection,\n options\n );\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchMove,\n handleTouchStart,\n resetSelection,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier,\n allCells?: CellIdentifier[]\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n if (allCells) {\n const cell = allCells.find((c) => c.row === row && c.col === col);\n cell && selectedCells.set(cellKey, cell);\n } else {\n selectedCells.set(cellKey, { id: cellKey, row, col });\n }\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier\n) {\n if (selectedCells.has(cellKey)) {\n selectedCells.delete(cellKey);\n } else {\n selectedCells.set(c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AAC,CAAC,EAAK,EAAA,OAAA,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,CAAA,EAAA,CAAC,CAAC;oBAClE,IAAI,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAC1C,iBAAA;AAAM,qBAAA;AACL,oBAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAA,GAAA,EAAE,GAAG,EAAA,GAAA,EAAE,CAAC,CAAC;AACvD,iBAAA;AACF,aAAA;AAAM,iBAAA;AACL,gBAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,aAAA;;QAXH,KAAK,IAAI,GAAG,GAAG,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,EAAE,EAAA;oBAAlC,GAAG,CAAA,CAAA;AAYX,SAAA;;IAbH,KAAK,IAAI,GAAG,GAAG,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,EAAE,EAAA;gBAAlC,GAAG,CAAA,CAAA;AAcX,KAAA;AACH,CAAC;AAED,SAAS,+BAA+B,CACtC,aAA0C,EAC1C,OAAe,EACf,IAAoB,EAAA;AAEpB,IAAA,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;AAC9B,QAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,KAAA;AAAM,SAAA;AACL,QAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAClC,KAAA;AACH;;;;;"}
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void,\n resetSelection: () => void,\n options: SelectionOptions\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const selectionStartScrollY = useRef<number | null>(null);\n const initialCellRef = useRef<CellIdentifier | null>(null);\n\n const handleTouchStart = (cell: CellIdentifier, event: React.TouchEvent) => {\n setIsDragging(true);\n initialCellRef.current = cell;\n selectionStartScrollY.current = window.scrollY;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (isDragging) {\n // Check if the user is dragging vertically\n const isDraggingVertically = initialCellRef.current && cell.row !== initialCellRef.current.row;\n\n // If the user is dragging vertically, prevent the selection\n if (!options.allowYScrollSelection && isDraggingVertically) {\n return;\n }\n\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent | React.TouchEvent) => {\n if (!isDragging) {\n setIsDragging(true);\n initialCellRef.current = cell;\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n if (selectionStartScrollY.current !== null && options.clearSelectionOnScroll) {\n if (Math.abs(window.scrollY - selectionStartScrollY.current) > options.scrollThreshold) {\n resetSelection();\n }\n }\n selectionStartScrollY.current = null;\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchStart,\n };\n};\n","import { useCallback, useState } from \"react\";\nimport { CellIdentifier, SelectionOptions, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = ({\n allCells,\n options = {\n allowYScrollSelection: true,\n clearSelectionOnScroll: false,\n scrollThreshold: 100,\n },\n}: {\n allCells?: CellIdentifier[];\n options?: SelectionOptions;\n}) => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const resetSelection = () => {\n setSelectionState({ selectedCells: new Map<string, CellIdentifier>() });\n };\n\n const handleTouchMove = useCallback(\n (event: React.TouchEvent) => {\n // Get the touch point\n if (selectionState.selectedCells.size > 0) {\n const touch = event.touches[0];\n const target = document.elementFromPoint(touch.clientX, touch.clientY);\n // Get closest cell\n const closest = target?.closest(\"[data-cell-id]\");\n const cellId = closest?.getAttribute(\"data-cell-id\");\n const cellRow = closest?.getAttribute(\"data-cell-row\");\n const cellCol = closest?.getAttribute(\"data-cell-col\");\n if (cellId && cellRow && cellCol && !selectionState.selectedCells.has(cellId)) {\n handleMouseEnter({ id: cellId, row: parseInt(cellRow), col: parseInt(cellCol) }, event);\n }\n }\n },\n [selectionState.selectedCells]\n );\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp, handleTouchStart } = useMouseDragSelection(\n toggleCellSelection,\n resetSelection,\n options\n );\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n handleTouchMove,\n handleTouchStart,\n resetSelection,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier,\n allCells?: CellIdentifier[]\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n if (allCells) {\n const cell = allCells.find((c) => c.row === row && c.col === col);\n cell && selectedCells.set(cellKey, cell);\n } else {\n selectedCells.set(cellKey, { id: cellKey, row, col });\n }\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier\n) {\n if (selectedCells.has(cellKey)) {\n selectedCells.delete(cellKey);\n } else {\n selectedCells.set(cellKey, cell);\n }\n}\n"],"names":["useState","useRef","useCallback"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAGa,qBAAqB,GAAG,UACnC,mBAA+G,EAC/G,cAA0B,EAC1B,OAAyB,EAAA;IAEnB,IAAA,EAAA,GAA8BA,cAAQ,CAAC,KAAK,CAAC,EAA5C,UAAU,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,aAAa,GAAA,EAAA,CAAA,CAAA,CAAmB,CAAC;AAEpD,IAAA,IAAM,qBAAqB,GAAGC,YAAM,CAAgB,IAAI,CAAC,CAAC;AAC1D,IAAA,IAAM,cAAc,GAAGA,YAAM,CAAwB,IAAI,CAAC,CAAC;AAE3D,IAAA,IAAM,gBAAgB,GAAG,UAAC,IAAoB,EAAE,KAAuB,EAAA;QACrE,aAAa,CAAC,IAAI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