// Author panel: controls generated from a composition's schema, plus copy/paste of the whole config. import type { NumberSpec, ParamSpec, ParamValue, Params } from "./params.ts"; import { resolveParams, serializeParams } from "./params.ts"; export interface AuthorPanelOptions { schema: ParamSpec[]; get: () => Params; set: (p: Params) => void; defaults: () => Params; readouts: () => Record; /** author-only start offset (s); not part of the config */ startAt: { value: number }; } export interface AuthorPanel { el: HTMLElement; /** refresh all controls from the current params */ sync(): void; updateReadouts(): void; } const SLIDER_STEPS = 1000; function toSlider(s: NumberSpec, v: number): number { const u = s.log ? Math.log(v / s.min) / Math.log(s.max / s.min) : (v - s.min) / (s.max - s.min); return Math.round(Math.max(0, Math.min(1, u)) * SLIDER_STEPS); } function fromSlider(s: NumberSpec, pos: number): number { const u = pos / SLIDER_STEPS; let v = s.log ? s.min * Math.pow(s.max / s.min, u) : s.min + u * (s.max - s.min); if (s.step) v = Math.round(v / s.step) * s.step; return Number(v.toPrecision(4)); } function fmt(v: number): string { return String(Number(v.toPrecision(4))); } function el(tag: K, props: Record = {}, ...kids: (Node | string)[]): HTMLElementTagNameMap[K] { const e = Object.assign(document.createElement(tag), props) as HTMLElementTagNameMap[K]; e.append(...kids); return e; } export function createAuthorPanel(o: AuthorPanelOptions): AuthorPanel { const root = el("aside", { className: "author" }); const syncers: (() => void)[] = []; const update = (key: string, v: ParamValue) => { o.set({ ...o.get(), [key]: v }); }; // ── actions ── const status = el("span", { className: "status" }); const flash = (msg: string) => { status.textContent = msg; window.setTimeout(() => { if (status.textContent === msg) status.textContent = ""; }, 2000); }; const copyBtn = el("button", { textContent: "copy config" }); copyBtn.onclick = async () => { const text = serializeParams(o.schema, o.get()); try { await navigator.clipboard.writeText(text); flash("copied"); } catch { window.prompt("copy:", text); } }; const pasteBtn = el("button", { textContent: "paste config" }); pasteBtn.onclick = async () => { let text: string | null = null; try { text = await navigator.clipboard.readText(); } catch { text = window.prompt("paste config JSON:"); } if (!text) return; try { o.set(resolveParams(o.schema, JSON.parse(text))); sync(); flash("pasted"); } catch { flash("not valid JSON"); } }; const resetBtn = el("button", { textContent: "reset to defaults" }); resetBtn.onclick = () => { if (!window.confirm("Discard your edits and return to the published defaults?")) return; o.set(o.defaults()); sync(); }; root.append(el("div", { className: "actions" }, copyBtn, pasteBtn, resetBtn, status)); // ── author-only transport option ── const startInput = el("input", { type: "number", min: "0", step: "1", value: String(o.startAt.value) }); startInput.oninput = () => { o.startAt.value = Math.max(0, Number(startInput.value) || 0); }; root.append(el("label", { className: "row" }, el("span", { textContent: "start at (s)" }), startInput)); const readoutBox = el("dl", { className: "readouts" }); root.append(readoutBox); // ── params grouped ── const groups = new Map(); for (const s of o.schema) { let fs = groups.get(s.group); if (!fs) { fs = el("fieldset", {}, el("legend", { textContent: s.group })); groups.set(s.group, fs); root.append(fs); } const label = el("span", { textContent: s.label, title: s.hint ?? "" }); const row = el("label", { className: "row" }, label); if (s.kind === "number") { const slider = el("input", { type: "range", min: "0", max: String(SLIDER_STEPS), step: "1" }); const num = el("input", { type: "number", step: "any" }); const unit = el("span", { className: "unit", textContent: s.unit ?? "" }); slider.oninput = () => { const v = fromSlider(s, Number(slider.value)); num.value = fmt(v); update(s.key, v); }; num.onchange = () => { const v = Number(num.value); if (!Number.isFinite(v)) return; slider.value = String(toSlider(s, v)); update(s.key, v); }; syncers.push(() => { const v = o.get()[s.key] as number; slider.value = String(toSlider(s, v)); num.value = fmt(v); }); row.append(slider, num, unit); } else if (s.kind === "select") { const sel = el("select"); for (const opt of s.options) sel.append(el("option", { value: opt, textContent: opt })); sel.onchange = () => update(s.key, sel.value); syncers.push(() => (sel.value = o.get()[s.key] as string)); row.append(sel); } else if (s.kind === "text") { const ta = el("textarea", { rows: 3, spellcheck: false, placeholder: s.hint ?? "" }); ta.onchange = () => update(s.key, ta.value); syncers.push(() => (ta.value = o.get()[s.key] as string)); row.classList.add("text"); row.append(ta); } else { const cb = el("input", { type: "checkbox" }); cb.onchange = () => update(s.key, cb.checked); syncers.push(() => (cb.checked = o.get()[s.key] as boolean)); row.append(cb); } fs.append(row); } function sync() { for (const f of syncers) f(); } function updateReadouts() { const r = o.readouts(); readoutBox.replaceChildren(...Object.entries(r).flatMap(([k, v]) => [el("dt", { textContent: k }), el("dd", { textContent: v })])); } sync(); updateReadouts(); return { el: root, sync, updateReadouts }; }