mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 10:12:34 +00:00
Implement fit-mode rendering contract with container sizing and pane re-fit
This commit is contained in:
+57
-8
@@ -8,21 +8,69 @@
|
||||
* never mutated by rendering:
|
||||
*
|
||||
* 1. Dataset reference resolution — arrives in M3 (no-op here).
|
||||
* 2. Fit-mode sizing — arrives in M2 (no-op here).
|
||||
* 2. Fit-mode sizing — implemented in M2.
|
||||
*
|
||||
* In M1 it is an identity transform over a copy: it establishes the
|
||||
* copy-not-mutate invariant and the call site the renderer depends on, so M2/M3
|
||||
* can fill in the steps without the preview pipeline changing shape.
|
||||
* The copy-not-mutate invariant and the call site the renderer depends on are
|
||||
* fixed; M3 fills in step 1 without the preview pipeline changing shape.
|
||||
*/
|
||||
|
||||
/** Preview sizing modes (spec §04 → Fit / Sizing Modes). `default` = Original. */
|
||||
export type FitMode = 'default' | 'width' | 'height' | 'full';
|
||||
|
||||
export interface PrepareOptions {
|
||||
/** Active fit mode. Applied in M2; ignored in M1. */
|
||||
/** Active fit mode. Defaults to `'default'` (Original — spec sizing untouched). */
|
||||
fitMode?: FitMode;
|
||||
}
|
||||
|
||||
/** The container/sub-spec keys the rendering contract recurses into (spec §04). */
|
||||
const CHILD_ARRAY_KEYS = ['layer', 'concat', 'hconcat', 'vconcat'] as const;
|
||||
|
||||
/** A spec node we might rewrite sizing on; loose by design (any Vega-Lite spec). */
|
||||
type SpecNode = Record<string, unknown>;
|
||||
|
||||
function isSpecNode(value: unknown): value is SpecNode {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite one node's sizing to the fit mode (spec §04 → Rendering Contract,
|
||||
* step 2). `'container'` is Vega-Lite's responsive keyword; the unconstrained
|
||||
* dimension is removed so it recomputes naturally.
|
||||
*/
|
||||
function applyFitToNode(node: SpecNode, mode: FitMode): void {
|
||||
switch (mode) {
|
||||
case 'width':
|
||||
node.width = 'container';
|
||||
delete node.height;
|
||||
break;
|
||||
case 'height':
|
||||
node.height = 'container';
|
||||
delete node.width;
|
||||
break;
|
||||
case 'full':
|
||||
node.width = 'container';
|
||||
node.height = 'container';
|
||||
break;
|
||||
// 'default' (Original) leaves sizing untouched and never reaches here.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the fit mode to a spec and every nested sub-spec it recurses into —
|
||||
* layered (`layer`) and concatenated (`concat`/`hconcat`/`vconcat`) children,
|
||||
* and a parent spec's single child `spec` (facet/repeat). Mutates in place; the
|
||||
* caller (`prepareSpecForRender`) already works on a copy.
|
||||
*/
|
||||
function applyFitMode(node: unknown, mode: FitMode): void {
|
||||
if (!isSpecNode(node)) return;
|
||||
applyFitToNode(node, mode);
|
||||
for (const key of CHILD_ARRAY_KEYS) {
|
||||
const children = node[key];
|
||||
if (Array.isArray(children)) for (const child of children) applyFitMode(child, mode);
|
||||
}
|
||||
if (isSpecNode(node.spec)) applyFitMode(node.spec, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape `.`/`[`/`]` so Vega-Lite treats a string as a literal field name rather
|
||||
* than a nested-property accessor (docs/architecture/05 §4). Used wherever
|
||||
@@ -37,11 +85,12 @@ export function escapeVegaField(name: string): string {
|
||||
* Transform the authored spec into the spec to embed. Operates on a deep copy
|
||||
* and returns it; the input is never mutated.
|
||||
*/
|
||||
export function prepareSpecForRender<T>(spec: T, _options: PrepareOptions = {}): T {
|
||||
export function prepareSpecForRender<T>(spec: T, options: PrepareOptions = {}): T {
|
||||
const copy = structuredClone(spec);
|
||||
|
||||
// M3: resolveDatasetRefs(copy, datasets)
|
||||
// M2: applyFitMode(copy, options.fitMode)
|
||||
// 1. M3: resolveDatasetRefs(copy, datasets)
|
||||
// 2. Fit-mode sizing.
|
||||
applyFitMode(copy, options.fitMode ?? 'default');
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user