diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-10-29 01:54:37 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-10-29 01:54:37 +0200 |
| commit | 986e65f9ab7122995ae1d647df23d817cecf6816 (patch) | |
| tree | 39d4f77c6a6565b59522a5a77e2f550334472713 /web/src/components/app/index.ts | |
| parent | 95069f13d908bfd3c0f3b33f8fad7d8464fd192e (diff) | |
refactor(web): improve state management
Diffstat (limited to 'web/src/components/app/index.ts')
| -rw-r--r-- | web/src/components/app/index.ts | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/web/src/components/app/index.ts b/web/src/components/app/index.ts index ec40754..195011f 100644 --- a/web/src/components/app/index.ts +++ b/web/src/components/app/index.ts @@ -1,14 +1,44 @@ -import ntvGrid from "../grid"; +import defaultDoc from "../../defaultDoc"; +import { Selection } from "../../selection"; +import { Doc } from "../../types"; +import ntvGrid, { NotiveGridElement } from "../grid"; +import renderGrid from "../grid/renderGrid"; import ntvToolbar from "../toolbar"; import "./index.css"; -class NotiveAppElement extends HTMLElement { +export class NotiveAppElement extends HTMLElement { + doc: Doc = defaultDoc(); + #selection?: Selection; + + get selection() { + return this.#selection; + } + + set selection(selection: Selection | undefined) { + this.#selection = selection; + this.#updateGridSelections(); + } + + #updateGridSelections() { + this.querySelectorAll<NotiveGridElement>("ntv-grid").forEach((grid) => { + grid.selection = + this.#selection?.gridId === grid.grid?.id ? this.#selection : undefined; + }); + } + connectedCallback() { this.append( ntvToolbar(), - ...window.notive.doc.grids.map((grid) => { - return ntvGrid({ gridId: grid.id }); - }), + + ...this.doc.grids.map((grid) => + ntvGrid({ + grid: renderGrid(grid), + dataset: { gridId: grid.id }, + ongridselectionchange: (event) => { + this.selection = event.selection; + }, + }), + ), ); } } |
