/** * The knob ENVELOPE: the inputs to the knob stack, serialized. * * MPO-191 AC-3 asks for the real `useResolvedKnobs` with its Preset / Surface / * Intent context stack, not the pure `resolveKnobs` function. React context does * not cross a WebView, so what crosses is the INPUTS to that stack, never the * resolved outputs — the DOM side rebuilds the stack out of the theme package's * own components and resolves for itself. One code path: the web host is the * degenerate case where the envelope never leaves the process. * * Design: docs/design/node-graph-editor.md section 6.1. */ import type { Animation, Knobs, SurfaceSize } from "@multiplatform.one/theme"; /** * `unset` is part of the surface vocabulary, and dropping it changes what * resolves: `SurfaceContext`'s default is `{ size: "unset" }` and * `useResolvedKnobs` treats an `unset` ceiling as none. Coercing a host that * sits under no `` to a token would clamp the rehydrated tree where * the host it copied was not clamped. So the envelope carries the value as is. */ export type GraphSurfaceSize = SurfaceSize; /** A face the DOM side has to register itself, because WKWebView cannot see an expo-font registration. */ export interface GraphFontRef { readonly family: string; /** Asset URL or `data:` URI the WebView can fetch. */ readonly src: string; readonly weight?: number; readonly style?: "normal" | "italic"; } /** Bumped when the envelope's shape changes in a way the DOM side must refuse. */ export const knobEnvelopeSchemaVersion = 2; export interface KnobEnvelope { readonly schemaVersion: typeof knobEnvelopeSchemaVersion; /** Fully merged host knobs: preset.knobs + every Preset override in the chain + props.knobs. */ readonly knobs: Knobs; /** * REQUIRED when the host is under a NAMED preset. Without it the DOM side * loses that preset's `intents` map: `useResolvedKnobs.ts:139` reads * `presetCtx?.preset.intents[intent] ?? defaultIntents[intent]`, so a * rehydrated tree with no name falls back to the defaults and a * preset-specific intent override vanishes inside the WebView. * * It is a PROP, not a context read, because `PresetContextValue` * (PresetContext.ts:7-10) carries `{ preset, overrides }` and no name — the * name genuinely cannot be recovered from the context. The host passes the * name it mounted `` with, or `useTheme()`'s active preset. */ readonly presetName?: string; readonly intent: string | null; readonly surface: { readonly size: GraphSurfaceSize }; readonly scheme: "light" | "dark"; readonly themeName: string; /** * Resolved HOST-side. `usePrefersReducedMotion` (useResolvedKnobs.ts:22-41) * reads `window.matchMedia`, which inside a WKWebView does not see the iOS * setting. */ readonly prefersReducedMotion: boolean; /** The `--xy-*` / `--mpo-graph-*` layer, built host-side by `graphCssVars`. */ readonly vars: Readonly>; readonly fonts: readonly GraphFontRef[]; readonly pixelRatio: number; /** Bumped on any change above, so the DOM side memoises cheaply. */ readonly revision: number; } /** Everything except `revision` — two envelopes with the same identity are the same envelope. */ export type KnobEnvelopeBody = Omit; /** * Deterministic JSON. `JSON.stringify` preserves INSERTION order, and the host * knobs are built by spreading, so `{...defaults, ...overrides}` and * `{...overrides, ...defaults}` stringify differently while being the same * object. A revision that bumps on key order alone would re-emit the whole var * sheet on every unrelated render. */ export function stableStringify(value: unknown): string { if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null"; if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`; const entries = Object.entries(value as Record) .filter(([, item]) => item !== undefined) .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)) .map(([key, item]) => `${JSON.stringify(key)}:${stableStringify(item)}`); return `{${entries.join(",")}}`; } /** Stable identity of an envelope's inputs, ignoring `revision`. */ export function knobEnvelopeIdentity(body: KnobEnvelopeBody): string { return stableStringify(body); } export function serializeKnobEnvelope(envelope: KnobEnvelope): string { return JSON.stringify(envelope); } /** * Parse an envelope that crossed the bridge. * * A version mismatch throws rather than rendering: a DOM bundle older than its * host would otherwise resolve against a knob set it does not understand and * paint something plausible but wrong, which is the failure that takes longest * to find. */ export function parseKnobEnvelope(text: string): KnobEnvelope { const parsed = JSON.parse(text) as KnobEnvelope; if (parsed?.schemaVersion !== knobEnvelopeSchemaVersion) { throw new Error( `knob envelope schemaVersion ${String(parsed?.schemaVersion)} is not ${knobEnvelopeSchemaVersion} — ` + "the DOM bundle and its native host are out of step; rebuild the app rather than rendering against a knob set it cannot read.", ); } return parsed; } /** `animation: "none"` forced in, idempotent against the resolver's own reduced-motion branch. */ export function knobsForEnvelope(envelope: KnobEnvelope): Knobs { if (!envelope.prefersReducedMotion) return envelope.knobs; return { ...envelope.knobs, animation: "none" as Animation }; }