From 986e65f9ab7122995ae1d647df23d817cecf6816 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Wed, 29 Oct 2025 01:54:37 +0200 Subject: refactor(web): improve state management --- web/src/components/app/index.ts | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'web/src/components/app/index.ts') 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("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; + }, + }), + ), ); } } -- cgit v1.2.3