summaryrefslogtreecommitdiff
path: root/packages/web/src/components/grid/selection.ts
blob: 517f8aead60c36c61384358b17370d1ed7598548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { CellRef, cellRefEquals } from "../../types";
import { RenderedGrid } from "./renderGrid";

export type CellRange = [start: CellRef, end: CellRef];

export interface GridSelection {
  activeCellRef: CellRef;
  range?: CellRange;
}

export function extendSelection(
  selection: GridSelection | undefined,
  cellRef: CellRef,
): GridSelection {
  if (!selection || cellRefEquals(selection.activeCellRef, cellRef)) {
    return { activeCellRef: cellRef };
  }

  if (selection.range) {
    return { ...selection, range: [selection.range[0], cellRef] };
  }

  return { ...selection, range: [selection.activeCellRef, cellRef] };
}

export function getSelectionRange(selection: GridSelection): CellRange {
  return selection.range ?? [selection.activeCellRef, selection.activeCellRef];
}