/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. * * The canvas controller (#815) — ONE framework-free store for everything a * Plan canvas remembers between renders: the UI state machine, the paged * source's residency, the key search, the open element overlay, the scroll * anchor and the scroll target. * * # Actions run their own effects * * Every action is a synchronous method: it makes the state transition, then * runs the effects that transition returned (slice writes, the author's * callbacks, page requests) before it returns. There is no effect batch held * in state for a later drain, so two effectful actions in one handler both * run — the drain this replaces saw only the last batch. * * # One notification per action * * An action — or a window landing, which arrives as a channel firing — changes * any number of parts and notifies ONCE, at its end, and only when the * snapshot changed. Subscribers select the part they read (`usePlanSelector`), * so a row re-renders only when its own slice of the state moves. * * # Reconcile * * A host data commit reconciles the UI state against the new rows (#610). The * canvas's first render with a new value already draws the reconciled VIEW * ({@link reconciledUi}, the same pure transition) — no flash of a vanished * row's focus — and `setValue`, the layout-effect sync, commits it so the * pruning is real (a row that comes back does not bring its old selection). * That commit notifies nobody: it moves nothing a reader shows — the view was * drawn from it, and what it prunes belongs to rows that are gone — so the * value renders once. The one exception is a changed DECLARED grain, which * clears the selection and focus of rows that are still there. * * # A bound `ui` state (#824) * * A root may bind the canvas's interaction state to the host (`ui`, a * `State.bind` handle). The controller reads it before the first render, then * again whenever the state store says something was written and on every * `setValue`: a state it has not seen is the host's write, and replaces the * selection, the rows folded or opened against their declaration, and the * expanded charts. The user's own actions are written back, once per action * and only when they moved it. Bound, the canvas persists no toggles of its * own under its storage key — the host holds them — though its scroll anchor * still is. * * `focus` in the state is a REQUEST — bring this row into view — which the * canvas spends at once (it writes `focus: none` back) and then serves: a row * on the canvas has its folded ancestors opened and is scrolled to the top * and made the tab stop; a paged row seen before has its window opened * first; one never seen is sought by its element's key, when the source can * seek. A request that cannot be served is dropped. * * # A new result keeps the row at the top (#1344, #1354) * * A new narrowing of the paged rows — the `rows` source's — is a new * result: the view the last one drew * stands in until its first window lands (the paging driver's `replacing`). * Once it has, a bounded frame puts the row at the top of its view back where * it was, by its key and the px into it, where the new rows draw it, and else * starts at the top — as the Sheet does over a narrowed source's own rows — * never seeking the row at the window it was in, which another narrowing's * positions do not hold. Where the view rests until the row is placed is the * last result's place: the driver is told of it only where nothing scrolls the * view, so no window of the new result is read for it. * * # A place over rows not read is kept by its element (#1398) * * A view that rests over rows not read — a run, or a window whose read failed * — is saved by the source element under its top and the px into that * element's window (the paging driver's `unreadPlace`), never by the run's key, * which names only its side: the run a remount draws on that side stands for * other rows. A remount opens that window where it is (`openAt`), as it opens * the window of a row it has not read, and puts the view back as many px into * the window once it is in — its rows, or its failure's band. * * @packageDocumentation */ import { type ValueTypeOf } from "@elaraai/east"; import { Plan } from "@elaraai/e3-ui/internal"; import type { PlanElementRefValue, PlanElementResolver } from "../context.js"; import { type PlanBodyItem, type PlanRootValue, type PlanRowValue } from "../model.js"; import { type PlanEvent, type PlanGrain, type PlanStore, type PlanUiState, type RowKey } from "../plan-state.js"; import { type ResidencyOptions } from "@elaraai/east-ui-components/internal"; import { type PlanViewport } from "../use-plan-paging.js"; import type { PlanSearch } from "../use-seek.js"; import { type PlanAnchor, type PlanPersisted } from "../persisted.js"; import { type PlanPagingSnapshot, type PlanRowPlace } from "./paging.js"; import { type PlanSeekSnapshot } from "./seek.js"; import type { PlanWindowHold } from "../root/window.js"; import { type PlanWords } from "../words.js"; /** A resolved overlay body (a resolver's some-value). */ export type PlanOverlayBody = Extract, { type: "some"; }>["value"]; /** A decoded bound `ui` handle — the root's `ui` some-value (#824). */ export type PlanUiBindValue = ValueTypeOf; /** A decoded bound `ui` state. */ export type PlanUiStateValue = ValueTypeOf; /** One open element overlay — the element it belongs to, and its body. */ export interface PlanOverlay { ref: PlanElementRefValue; body: PlanOverlayBody; } /** An open tooltip — a labelled mark's own text (a port, a cell marker). */ export interface PlanTooltip { /** Which mark it belongs to — its row and the mark's own attribute. */ key: string; text: string; } /** The canvas's open surfaces — at most one of each kind (#816: one overlay * layer serves every element). */ export interface PlanOverlays { popover: PlanOverlay | null; hover: PlanOverlay | null; tooltip: PlanTooltip | null; } /** Where the persisted scroll anchor is in its restore (#813). */ export interface PlanAnchorState { /** The anchor the last session left, if any — or, while it is `keeping`, the row at the top of the view. */ saved: PlanAnchor | null; /** `pending` until the body can place it, `seeking` while a paged canvas * loads its window, `settled` once restored (or found gone). */ phase: "pending" | "seeking" | "settled"; /** * Whether it is the row at the top of the view a new result of the paged * rows keeps (#1344, #1354) — a new narrowing's, once its rows land: put * back by its key and the px into it where they draw it, and else the view * starts at the top; never sought at the window it was in, which another * narrowing's positions do not hold. */ keeping: boolean; /** Where the frame scrolls to restore it — set once, when it settles. */ restore: { index: number; offset: number; } | undefined; } /** How a keyboard move brings its item into view (#819) — the least scroll * that shows it, or one of its edges at the viewport's. */ export type PlanNavAlign = "auto" | "start" | "end"; /** Who owns the scroll position the frame is asked for (#811). */ export interface PlanScrollTarget { /** A key search's target, the first skipped row the chip seeks, or the * item keyboard navigation moved to (#819). */ owner: "search" | "skipped" | "nav"; /** Bumped per chip click, so the chip scrolls there again after the user moved. */ skippedSeq: number; /** Bumped each time a key search's request is served — its target row * resolved — so the frame brings it in once per search (#574). */ searchSeq: number; /** The key search's target row — the first LOADED row at-or-after the * sought key, resolved ONCE per search, when the window holding the * matched element has landed. */ targetKey: string | undefined; /** The keyboard's target item (`bodyItemKey`), how to bring it into view, * and a nonce per move — so moving back onto a row the user has since * scrolled away from scrolls to it again. `row` names the row itself * when a selection made outside the canvas asks for it (#1198): the * narrow layout, which has no grid to scroll, brings its card into view. */ nav: { key: string; align: PlanNavAlign; seq: number; row?: RowKey | undefined; } | undefined; } /** * Where keyboard focus rests in the canvas (#819) — the roving tab stop, and a * move of DOM focus the keyboard asked for. */ export interface PlanNav { /** The body item (`bodyItemKey`) holding the canvas's ONE tab stop — the * last one focused. `null` until something in the grid takes focus: the * grid itself is the tab stop until then, and whenever the active item * is not mounted. */ active: string | null; /** A requested move of DOM focus: the item focuses itself once it is * mounted (it may first have to scroll into view), then reports * {@link PlanController.focusDone} — so a later remount never takes * focus back. */ request: { key: string; seq: number; } | null; } /** The live region's latest message (#819). `seq` counts up, so the same * words said twice are two messages. */ export interface PlanAnnouncement { text: string; seq: number; } /** Everything the canvas renders from — replaced whole, parts kept by identity. */ export interface PlanSnapshot { store: PlanStore; /** Parent rows available to the fold control, including the first inline render. */ groups: readonly RowKey[]; paging: PlanPagingSnapshot; seek: PlanSeekSnapshot; overlay: PlanOverlays; anchor: PlanAnchorState; scroll: PlanScrollTarget; nav: PlanNav; announce: PlanAnnouncement | null; } /** Options for {@link createPlanController}. */ export interface PlanControllerOptions { /** The declared grain at mount. */ grain: PlanGrain; /** The declared-collapsed row keys at mount. */ collapsed: Iterable; /** What the last session persisted under the canvas's `storageKey` (#813). */ restored?: PlanPersisted | undefined; /** Write the persisted state — the canvas passes its storage setter. */ persist?: ((next: PlanPersisted) => void) | undefined; /** Residency policy for a paged source (tests tune it). */ policy?: ResidencyOptions | undefined; /** The root's bound `ui` handle at mount (#824) — read before the first * render, so a bound canvas draws the host's state from its first frame. */ ui?: PlanUiBindValue | undefined; /** Listen for writes to the state a bound `ui` handle reads. The canvas * passes the state store's own subscribe: a handle names no key to * listen to, so every write is looked at, and one that did not move the * canvas's state does nothing. */ subscribeUi?: ((listener: () => void) => () => void) | undefined; /** The canvas's window hold (#1344): the window last shown while no source of its slice holds one — what a * pan or the now rung moves, as the reader sees it, and what the controller keeps under the canvas's * storage key as each gesture moves it (#813). */ windowHold?: PlanWindowHold | undefined; } /** The canvas controller's handle. */ export interface PlanController { getSnapshot(): PlanSnapshot; subscribe(listener: () => void): () => void; /** Props sync — the latest root, and its data-stable twin (a new `data` * identity is a data change: the UI state reconciles). */ setValue(value: PlanRootValue, data: PlanRootValue): void; /** The canvas's words (#820) — the message table and locale its live * region speaks in. The canvas root hands them over whenever either * changes; English in `en-US` until then. */ setWords(words: PlanWords): void; /** An interaction — the transition, then its effects. */ dispatch(e: PlanEvent): void; /** Parent rows in the current canvas, including nested parents. */ groupKeys(): readonly RowKey[]; /** Fold all parents in one action, retaining offscreen overrides. */ setGroupsCollapsed(collapsed: boolean): void; /** * Select events from outside the canvas (#1198) — the overlaps chip's * first pair, a peer the inspector's banner names: the selection replaced * by them, on `row`, the row that draws them, which is brought into view * as a host's `focus` request brings one — its folded ancestors and the * group grain opened, scrolled to and made the tab stop, DOM focus left * where it is. With no row (the viewer hid the series that draws them), * the events are selected and nothing scrolls. */ selectEvents(keys: readonly string[], row: RowKey | null): void; /** * Open a row's folded ancestors — and the group grain, where it folds the * top group the row sits under — as the user would to see it: a * conflict's Go to does, before it selects the row and shows it. * * @param key - The row */ revealRow(key: RowKey): void; /** An element click — reported to the root's `onElementClick` (#824). */ elementClick(ref: PlanElementRefValue): void; /** An element's popover / hover card wants to open or close. Opening runs * the root's resolver first, and only a `some` body opens; an element whose * surface is already open is not resolved again. A popover takes the * surface from a hover card. */ overlayIntent(kind: "popover" | "hover", ref: PlanElementRefValue, open: boolean): void; /** A labelled mark's tooltip opens, or closes (`null`). */ tooltipIntent(tip: PlanTooltip | null): void; /** The paged source's content moved under the same source — a drafted * canvas's windows, derived with new drafts (#880): read its windows * again at the revision it names now, the rows it has standing in until * they land (#821). */ refreshSource(): void; /** Where the viewport is — the paged source's demand (#577). */ reportViewport(at: PlanViewport, scrolling: boolean): void; /** * Home or End past a run not read (#1398, CL10): the source's first or last window read where it is — pinned as a * key search's jump is, so the view's reports leave it be — and the view the key left standing in, retained, until * it lands (the paging snapshot's `standing`); the keyboard's intent lands on its row. The focus moving to any item * but `from` meanwhile puts that view back and reads nothing for the jump. A source that has not counted itself has * no last window to ask for. */ jumpToEdge(edge: "first" | "last", from: string): void; /** The canvas committed a render of `paging` (the paging driver's * `committed`). Call it after every commit: a jump hands the viewport back * only once the canvas has shown — and scrolled to — its target (#812). */ committed(paging: PlanPagingSnapshot): void; /** Ask a failed window again (#811) — or, with none named, every failed * window and the source's own failed reads, at once (#1398: the footer * line's Retry). */ retry(w?: number): void; /** * The stored entry the window the canvas draws at a source position read with the entry of an id (#1599, the paging * driver's `entryOf`): what a gesture on a row the canvas shows begins from while its window is read again. */ entryOf(id: string, at: number): Uint8Array | undefined; /** Whether the window the canvas draws at a source position has landed at the revision it reads at, never standing in from before (#1599, the paging driver's `landedAt`). */ landedAt(at: number): boolean; /** Where a paged row the body does not hold sits — its block's band, and * how far down it (#823: a link into an evicted window). */ placeOf(key: RowKey): PlanRowPlace | undefined; /** The key search, for the toolbar (its `find` / `jump` / `clear` are this * controller's). Mount it only where the source declares `seek`; its * `requery` is the seek snapshot's `again` (#821, #1199), and its * `resetKey` the source's id. */ readonly search: Omit; /** * Go to a row of the `rows` source Search Records found (#1513): its window opened as a key search's jump opens it, * once the source reads the narrowing the row was placed in, and the view brought to the row as it lands. * * @param key - The row's element key * @param row - Its place among the narrowing's rows * @param narrowing - The narrowing, as the source names it */ goToRow(key: string, row: number, narrowing: string): void; /** Drop what {@link PlanController.goToRow} asked: the box cleared. */ leaveRow(): void; /** The diagnostics chip asked for the first skipped row (#811). */ seekSkipped(): void; /** * Keyboard navigation (#819): make `key` (a `bodyItemKey`) the canvas's * tab stop and move DOM focus onto it — scrolled into view first when an * `align` is given (a move onto an item already beside the focused one * needs none). */ focusItem(key: string, align?: PlanNavAlign): void; /** * Scroll an item into view, the focus left where it is: for a cell of the * item to take the focus as it draws, as a conflict's Go to focuses a * table row's cell. * * @param key - The item's key * @param align - Where the view puts it */ showItem(key: string, align: PlanNavAlign): void; /** DOM focus landed in an item (a click, a Tab) — it holds the tab stop. */ itemFocused(key: string): void; /** The item a focus request named has taken focus. */ focusDone(seq: number): void; /** Place the persisted anchor against the body the canvas rendered (#813). * A no-op once it has settled. */ placeAnchor(items: readonly PlanBodyItem[], bounded: boolean): void; /** The frame's scroll settled here — persisted once the anchor has settled. */ anchorChanged(at: { index: number; offset: number; }, items: readonly PlanBodyItem[]): void; /** * Listen to the source's channels while the canvas is mounted — call it * from an effect and return what it returns. The controller listens from * its first read, so the first `connect` only hands back the * disconnect; a `connect` after a disconnect (StrictMode rehearsing an * unmount, a canvas remounting the same controller) re-reads the source * and any pending search, which subscribes their channels afresh. State * outlives a disconnect. * * @returns Stop listening */ connect(): () => void; } /** * The declared-collapsed row keys among `rows`. * * @param rows - Canvas rows * @returns The keys of the rows that declare `collapsed: true` — any row with * children may (#822) */ export declare function declaredCollapsedOf(rows: readonly PlanRowValue[]): ReadonlySet; /** The declared grain of a root. */ export declare function declaredGrainOf(value: PlanRootValue): PlanGrain; /** Whether a root declares the compact density. */ export declare function denseOf(value: PlanRootValue): boolean; /** What a reconcile is judged against — the rows the canvas renders now. */ export interface PlanReconcileModel { /** Every row key the canvas holds. */ alive: ReadonlySet; /** Whether `alive` is every row the canvas has (inline), or only the resident ones (paged). */ complete: boolean; /** The declared-collapsed row keys among them. */ declaredCollapsed: ReadonlySet; /** The declared grain. */ declaredGrain: PlanGrain; } /** * A paged canvas's rows that its source's elements placed — its paged blocks' * — which are what a key search looks among. A fixed block's rows are no * element's (a section's header, `rows`, the event kinds' rows, #1192), though * their ids may read like one. * * @param p - The paging snapshot * @returns Its paged blocks' rows, in order — the same array when no block is fixed */ export declare function elementRowsOf(p: PlanPagingSnapshot): readonly PlanRowValue[]; /** * The UI state as a reconcile against `model` would leave it — what the canvas * renders its first frame of a new value with, before `setValue` commits the * same transition. * * @param store - The current store * @param model - The rows the canvas renders now * @returns The reconciled UI state */ export declare function reconciledUi(store: PlanStore, model: PlanReconcileModel): PlanUiState; /** * Create a canvas controller. * * @param options - See {@link PlanControllerOptions} * @returns The controller */ export declare function createPlanController(options: PlanControllerOptions): PlanController; export type { PlanPagingSnapshot } from "./paging.js"; export type { PlanSeekSnapshot } from "./seek.js"; //# sourceMappingURL=index.d.ts.map