summaryrefslogtreecommitdiff
path: root/web/src/components/grid/index.ts
blob: 5301afb48ac63605dd38cb4042f91d5b545e98e0 (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
105
106
107
108
109
110
111
112
113
114
115
import h, { type CreateElement } from "../../html";
import { CellRef } from "../../types";
import cellAtCoord from "./cellAtCoord";
import drawGrid, { getGridColors } from "./drawGrid";
import "./index.css";

class NotiveGridElement extends HTMLElement {
  #gridId!: string;

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

  set gridId(val: string) {
    this.#gridId = val;
    this.setAttribute("grid-id", val);
  }

  get renderedGrid() {
    return window.notive.getGrid(this.#gridId)!;
  }

  canvasEl: HTMLCanvasElement = h.canvas();

  connectedCallback() {
    if (!this.gridId) {
      throw new Error("ntv-grid requries gridId attribute");
    }

    window.addEventListener("ntv:selectionchange", () => this.draw());

    this.canvasEl.addEventListener(
      "mousedown",
      this.#canvasMouseDownCallback.bind(this),
    );

    this.append(this.canvasEl);
    this.draw();
  }

  draw() {
    const ctx = this.canvasEl.getContext("2d");

    if (!ctx) throw new Error("Unable to get canvas context");

    const grid = window.notive.getGrid(this.gridId);

    if (!grid) return;

    this.canvasEl.setAttribute("width", grid.rect.width + "px");
    this.canvasEl.setAttribute("height", grid.rect.height + "px");

    const colors = getGridColors(this);

    drawGrid(
      ctx,
      colors,
      grid,
      window.notive.selection,
      window.notive.pendingSelection,
    );
  }

  #mouseEventCellRef(
    this: NotiveGridElement,
    event: MouseEvent,
  ): CellRef | undefined {
    const clientRect = this.canvasEl.getBoundingClientRect();
    const x = event.x - clientRect.x;
    const y = event.y - clientRect.y;
    return cellAtCoord(this.renderedGrid, x, y);
  }

  #canvasMouseDownCallback(this: NotiveGridElement, event: MouseEvent) {
    const cellRef = this.#mouseEventCellRef(event);

    if (!cellRef) return;

    window.notive.startSelecting(this.gridId, cellRef);

    this.#selectionAbortController = new AbortController();
    const { signal } = this.#selectionAbortController;

    window.addEventListener(
      "mousemove",
      this.#selectionMouseMoveCallback.bind(this),
      { signal },
    );

    window.addEventListener(
      "mouseup",
      this.#selectionMouseUpCallback.bind(this),
      { signal },
    );
  }

  #selectionAbortController?: AbortController;

  #selectionMouseMoveCallback(this: NotiveGridElement, event: MouseEvent) {
    const cellRef = this.#mouseEventCellRef(event);
    if (!cellRef) return;
    window.notive.extendSelection(cellRef);
  }

  #selectionMouseUpCallback(this: NotiveGridElement, event: MouseEvent) {
    this.#selectionAbortController?.abort();
    this.#selectionAbortController = undefined;
    window.notive.finishSelecting();
  }
}

customElements.define("ntv-grid", NotiveGridElement);

export default ((...args: any[]): NotiveGridElement =>
  (h as any)["ntv-grid"](...args)) as CreateElement<NotiveGridElement>;