summaryrefslogtreecommitdiff
path: root/web/src/components/toolbar/index.ts
blob: 666114b9075fee6a5ac621d66fa355f17c9904fd (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
import h, { CreateElement } from "../../html";
import { ActiveCellSelection, RangeSelection } from "../../selection";
import { RenderedGrid } from "../grid/renderGrid";
import "./index.css";

function getSelectedSubdivisionsCount(): number | undefined {
  const selection = window.notive.selection;

  if (!selection) return;

  if (selection instanceof ActiveCellSelection) {
    return 1;
  }

  if (!(selection instanceof RangeSelection)) return;

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

  if (!grid) return;

  const selectedCells = selection.getSelectedCells(grid);

  return Math.min(...selectedCells.map((cells) => cells.length));
}

class NotiveToolbarElement extends HTMLElement {
  #subdivisionsInputEl: HTMLInputElement = h.input({
    title: "Subdivisions",
    placeholder: "-",
    disabled: true,
  });

  connectedCallback() {
    this.#render();

    window.addEventListener("ntv:selectionchange", () => {
      if (window.notive.pendingSelection) {
        this.#subdivisionsInputEl.disabled = true;
        this.#subdivisionsInputEl.value = "";
        return;
      }

      const subdivisionsCount = getSelectedSubdivisionsCount();

      if (!subdivisionsCount) {
        this.#subdivisionsInputEl.disabled = true;
        this.#subdivisionsInputEl.value = "";
        return;
      }

      this.#subdivisionsInputEl.disabled = false;
      this.#subdivisionsInputEl.value = subdivisionsCount.toString();
    });

    this.#subdivisionsInputEl.addEventListener("change", () => {
      window.notive.subdivideSelection(
        parseInt(this.#subdivisionsInputEl.value),
      );
    });
  }

  #render() {
    this.append(
      h.section(
        h.button({ dataset: { variant: "menu" } }, "File"),
        h.button({ dataset: { variant: "menu" } }, "Edit"),
        h.button({ dataset: { variant: "menu" } }, "Format"),
      ),
      h.section(
        h.button(
          {
            dataset: { variant: "icon" },
            onclick: () => {
              const subdivisions = Math.max(
                1,
                parseInt(this.#subdivisionsInputEl.value) - 1,
              );

              this.#subdivisionsInputEl.value = subdivisions.toString();
              window.notive.subdivideSelection(subdivisions);
            },
          },
          "-",
        ),
        this.#subdivisionsInputEl,
        h.button(
          {
            dataset: { variant: "icon" },

            onclick: () => {
              const subdivisions =
                parseInt(this.#subdivisionsInputEl.value) + 1;

              this.#subdivisionsInputEl.value = subdivisions.toString();
              window.notive.subdivideSelection(subdivisions);
            },
          },
          "+",
        ),
      ),
    );
  }
}

customElements.define("ntv-toolbar", NotiveToolbarElement);

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