|
1 | | -import { EventHandlerResult } from '../EventHandlerResult'; |
2 | | -import Grid from '../Grid'; |
3 | | -import GridMouseHandler, { GridMouseEvent } from '../GridMouseHandler'; |
4 | | -import GridRange from '../GridRange'; |
5 | | -import GridUtils, { GridPoint } from '../GridUtils'; |
| 1 | +import { type EventHandlerResult } from '../EventHandlerResult'; |
| 2 | +import type Grid from '../Grid'; |
| 3 | +import GridMouseHandler, { type GridMouseEvent } from '../GridMouseHandler'; |
| 4 | +import GridRange, { type GridRangeIndex } from '../GridRange'; |
| 5 | +import GridUtils, { type GridPoint } from '../GridUtils'; |
6 | 6 |
|
7 | 7 | const DEFAULT_INTERVAL_MS = 100; |
8 | 8 |
|
9 | 9 | class GridSelectionMouseHandler extends GridMouseHandler { |
| 10 | + /** |
| 11 | + * Returns the latest grid selection based on the current grid selection and where the user clicked |
| 12 | + * This code is dependent on the behavior of onContextMenu |
| 13 | + * @param originalSelection The selection from the current grid state which may be stale |
| 14 | + * @param columnIndex The column index where the user clicked |
| 15 | + * @param rowIndex The row index where the user clicked |
| 16 | + */ |
| 17 | + static getLatestSelection( |
| 18 | + originalSelection: readonly GridRange[], |
| 19 | + columnIndex: GridRangeIndex, |
| 20 | + rowIndex: GridRangeIndex |
| 21 | + ): readonly GridRange[] { |
| 22 | + const clickedInOriginalSelection = GridRange.containsCell( |
| 23 | + originalSelection, |
| 24 | + columnIndex, |
| 25 | + rowIndex |
| 26 | + ); |
| 27 | + |
| 28 | + // If the user clicked in a valid cell outside of the original selection, |
| 29 | + // the selection will be changed to just that cell. |
| 30 | + return clickedInOriginalSelection || columnIndex == null || rowIndex == null |
| 31 | + ? originalSelection |
| 32 | + : [GridRange.makeCell(columnIndex, rowIndex)]; |
| 33 | + } |
| 34 | + |
10 | 35 | private startPoint?: GridPoint; |
11 | 36 |
|
12 | 37 | private hasExtendedFloating = false; |
@@ -246,7 +271,7 @@ class GridSelectionMouseHandler extends GridMouseHandler { |
246 | 271 | ); |
247 | 272 |
|
248 | 273 | // only change the selected range if the selected cell is not in the selected range |
249 | | - if (!isInRange && gridPoint.row !== null) { |
| 274 | + if (!isInRange && gridPoint.row !== null && gridPoint.column !== null) { |
250 | 275 | this.startPoint = undefined; |
251 | 276 | this.stopTimer(); |
252 | 277 | grid.clearSelectedRanges(); |
|
0 commit comments