/** DOM-access helpers + initial-value sync for the controller. Three typed accessors ({@link el}, {@link elInput}, {@link elSelect}) narrow `document.getElementById` results via `instanceof` checks so downstream call sites don't need `as` casts; {@link syncDomFromState} pushes every value from an {@link AppState} into the corresponding control element, used after wholesale state swaps (reset button, URL-hash restore). Browser-only: relies on `document` / `window` / `HTMLInputElement`. @example ```ts import { elInput, syncDomFromState } from './controller-dom.ts'; elInput('search').value = 'etag'; syncDomFromState({ state: nextState }); ``` */ import type { ChannelKey, ToggleKey, } from './filter.ts'; import { type AppState, TOGGLE_KEYS, } from './state.ts'; //region Constants /** Channel keys, fixed order. */ const CHANNEL_KEYS: readonly ChannelKey[] = [ 'x', 'y', 'z', 'color', 'shape', 'size', ]; //endregion Constants //region Typed accessors /** Resolves an element by id, throwing if absent. The control panel's IDs are generated statically by `../render-controls.ts`, so a missing element is a programming error and must surface loudly. @param id - Element id. @returns Element typed as `HTMLElement`. @throws When the element is absent from the DOM. @example ```ts el('dim-x').classList.add('active'); ``` */ export function el(id: string,): HTMLElement { /** Raw `querySelector` result, validated below so the caller receives a non-null element. */ const node = document.querySelector(`#${id}`,); if (node === null) throw new Error(`Control element #${id} missing from DOM`,); return node; } /** Resolves an `` element by id, narrowing via `instanceof`. @param id - Element id. @returns Element typed as `HTMLInputElement`. @throws When the element is absent or not an ``. @example ```ts elInput('search').value = 'etag'; ``` */ export function elInput(id: string,): HTMLInputElement { /** Element resolved by {@link el}, narrowed below to `HTMLInputElement` via `instanceof`. */ const node = el(id,); if (!(node instanceof HTMLInputElement)) throw new Error(`Control element #${id} is not an `,); return node; } /** Resolves a ``. @example ```ts elSelect('dim-x').value = 'tsRatio'; ``` */ export function elSelect(id: string,): HTMLSelectElement { /** Element resolved by {@link el}, narrowed below to `HTMLSelectElement` via `instanceof`. */ const node = el(id,); if (!(node instanceof HTMLSelectElement)) throw new Error(`Control element #${id} is not a