/** * How far each divider was dragged, kept in this browser: a panel made wider stays wider after a * reload, in every project. Best effort, like the folds – a browser that refuses `localStorage` * forgets with the page. */ import { clamp } from '../ui/math'; const STORAGE_KEY = 'feuerwerk.panel-sizes'; let sizes: Map | null = null; function storage(): Pick | null { try { return typeof window === 'undefined' ? null : window.localStorage; } catch { return null; } } function read(): Map { try { const stored: unknown = JSON.parse(storage()?.getItem(STORAGE_KEY) ?? '{}'); if (typeof stored !== 'object' || stored === null || Array.isArray(stored)) return new Map(); return new Map( Object.entries(stored).filter((entry): entry is [string, number] => Number.isFinite(entry[1])), ); } catch { return new Map(); } } /** * The size a panel starts with: what was kept under `name`, held to the bounds its divider keeps * today, or `fallback` where nothing was kept. */ export function panelSize(name: string, fallback: number, min: number, max: number): number { sizes ??= read(); const kept = sizes.get(name); return kept === undefined ? fallback : clamp(kept, min, max); } export function keepPanelSize(name: string, size: number): void { sizes ??= read(); sizes.set(name, size); try { storage()?.setItem(STORAGE_KEY, JSON.stringify(Object.fromEntries([...sizes].sort()))); } catch { // Kept for this page only. } } /** Forgets what was read, so the next question asks the browser again (tests). */ export function resetPanelSizes(): void { sizes = null; }