summaryrefslogtreecommitdiff
path: root/web/src/selection.ts
blob: 3d18417dfbd871cea2f3f040881c29e996130135 (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
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;
}

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,
    ]);
  }
}

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,
    ]);
  }
}