summaryrefslogtreecommitdiff
path: root/web/src/selection.ts
blob: 294db5238b7cf166a25c32b2d8e69ad84302467b (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {
  getRenderedCell,
  RenderedCell,
  RenderedGrid,
} from "./components/grid/renderGrid";
import { CellRef, cellRefEquals } from "./types";

export abstract class Selection {
  readonly gridId: string;
  readonly activeCellRef: CellRef;

  constructor(gridId: string, activeCellRef: CellRef) {
    this.gridId = gridId;
    this.activeCellRef = activeCellRef;
  }

  abstract extend(cellRef: CellRef): Selection;

  abstract getSelectedCells(grid: RenderedGrid): RenderedCell[][];
}

export class ActiveCellSelection extends Selection {
  extend(cellRef: CellRef): Selection {
    if (cellRefEquals(cellRef, this.activeCellRef)) {
      return this;
    }

    return new RangeSelection(this.gridId, this.activeCellRef, [
      this.activeCellRef,
      cellRef,
    ]);
  }

  getSelectedCells(grid: RenderedGrid): RenderedCell[][] {
    return [[getRenderedCell(grid, this.activeCellRef)!]];
  }
}

export type CellRange = [CellRef, CellRef];

export class RangeSelection extends Selection {
  #range: CellRange;

  get range() {
    return this.#range;
  }

  constructor(gridId: string, activeCellRef: CellRef, range: CellRange) {
    super(gridId, activeCellRef);
    this.#range = range;
  }

  extend(cellRef: CellRef): Selection {
    if (cellRefEquals(cellRef, this.activeCellRef)) {
      return new ActiveCellSelection(this.gridId, cellRef);
    }

    return new RangeSelection(this.gridId, this.activeCellRef, [
      this.#range[0],
      cellRef,
    ]);
  }

  getSelectedCells(grid: RenderedGrid): RenderedCell[][] {
    const startCell = getRenderedCell(grid, this.range[0]);
    const endCell = getRenderedCell(grid, this.range[1]);

    if (!startCell || !endCell) return [];

    const firstRowIndex = Math.min(
      startCell.renderedRowIndex,
      endCell.renderedRowIndex,
    );

    const lastRowIndex = Math.max(
      startCell.renderedRowIndex,
      endCell.renderedRowIndex,
    );

    const startRatio =
      startCell.startRatio.compare(endCell.startRatio) <= 0
        ? startCell.startRatio
        : endCell.startRatio;

    const endRatio =
      startCell.endRatio.compare(endCell.endRatio) >= 0
        ? startCell.endRatio
        : endCell.endRatio;

    return grid.renderedRows
      .slice(firstRowIndex, lastRowIndex + 1)
      .map((row) => {
        const firstCellIndex = row.renderedCells.findIndex(
          (cell) => cell.startRatio.compare(startRatio) >= 0,
        );

        const lastCellIndex = row.renderedCells.findLastIndex(
          (cell) => cell.endRatio.compare(endRatio) <= 0,
        );

        return row.renderedCells.slice(firstCellIndex, lastCellIndex + 1);
      });
  }
}