1// Author panel: controls generated from a composition's schema, plus copy/paste of the whole config.
2import type { NumberSpec, ParamSpec, ParamValue, Params } from "./params.ts";
3import { resolveParams, serializeParams } from "./params.ts";
5export interface AuthorPanelOptions {
6 schema: ParamSpec[];
7 get: () => Params;
8 set: (p: Params) => void;
9 defaults: () => Params;
10 readouts: () => Record<string, string>;
11 /** author-only start offset (s); not part of the config */
12 startAt: { value: number };
13}
15export interface AuthorPanel {
16 el: HTMLElement;
17 /** refresh all controls from the current params */
18 sync(): void;
19 updateReadouts(): void;
20}
22const SLIDER_STEPS = 1000;
24function toSlider(s: NumberSpec, v: number): number {
25 const u = s.log ? Math.log(v / s.min) / Math.log(s.max / s.min) : (v - s.min) / (s.max - s.min);
26 return Math.round(Math.max(0, Math.min(1, u)) * SLIDER_STEPS);
27}
29function fromSlider(s: NumberSpec, pos: number): number {
30 const u = pos / SLIDER_STEPS;
31 let v = s.log ? s.min * Math.pow(s.max / s.min, u) : s.min + u * (s.max - s.min);
32 if (s.step) v = Math.round(v / s.step) * s.step;
33 return Number(v.toPrecision(4));
34}
36function fmt(v: number): string {
37 return String(Number(v.toPrecision(4)));
38}
40function el<K extends keyof HTMLElementTagNameMap>(tag: K, props: Record<string, unknown> = {}, ...kids: (Node | string)[]): HTMLElementTagNameMap[K] {
41 const e = Object.assign(document.createElement(tag), props) as HTMLElementTagNameMap[K];
42 e.append(...kids);
43 return e;
44}
46export function createAuthorPanel(o: AuthorPanelOptions): AuthorPanel {
47 const root = el("aside", { className: "author" });
48 const syncers: (() => void)[] = [];
50 const update = (key: string, v: ParamValue) => {
51 o.set({ ...o.get(), [key]: v });
52 };
54 // ── actions ──
55 const status = el("span", { className: "status" });
56 const flash = (msg: string) => {
57 status.textContent = msg;
58 window.setTimeout(() => {
59 if (status.textContent === msg) status.textContent = "";
60 }, 2000);
61 };
62 const copyBtn = el("button", { textContent: "copy config" });
63 copyBtn.onclick = async () => {
64 const text = serializeParams(o.schema, o.get());
65 try {
66 await navigator.clipboard.writeText(text);
67 flash("copied");
68 } catch {
69 window.prompt("copy:", text);
70 }
71 };
72 const pasteBtn = el("button", { textContent: "paste config" });
73 pasteBtn.onclick = async () => {
74 let text: string | null = null;
75 try {
76 text = await navigator.clipboard.readText();
77 } catch {
78 text = window.prompt("paste config JSON:");
79 }
80 if (!text) return;
81 try {
82 o.set(resolveParams(o.schema, JSON.parse(text)));
83 sync();
84 flash("pasted");
85 } catch {
86 flash("not valid JSON");
87 }
88 };
89 const resetBtn = el("button", { textContent: "reset to defaults" });
90 resetBtn.onclick = () => {
91 if (!window.confirm("Discard your edits and return to the published defaults?")) return;
92 o.set(o.defaults());
93 sync();
94 };
95 root.append(el("div", { className: "actions" }, copyBtn, pasteBtn, resetBtn, status));
97 // ── author-only transport option ──
98 const startInput = el("input", { type: "number", min: "0", step: "1", value: String(o.startAt.value) });
99 startInput.oninput = () => {
100 o.startAt.value = Math.max(0, Number(startInput.value) || 0);
101 };
102 root.append(el("label", { className: "row" }, el("span", { textContent: "start at (s)" }), startInput));
104 const readoutBox = el("dl", { className: "readouts" });
105 root.append(readoutBox);
107 // ── params grouped ──
108 const groups = new Map<string, HTMLElement>();
109 for (const s of o.schema) {
110 let fs = groups.get(s.group);
111 if (!fs) {
112 fs = el("fieldset", {}, el("legend", { textContent: s.group }));
113 groups.set(s.group, fs);
114 root.append(fs);
115 }
116 const label = el("span", { textContent: s.label, title: s.hint ?? "" });
117 const row = el("label", { className: "row" }, label);
119 if (s.kind === "number") {
120 const slider = el("input", { type: "range", min: "0", max: String(SLIDER_STEPS), step: "1" });
121 const num = el("input", { type: "number", step: "any" });
122 const unit = el("span", { className: "unit", textContent: s.unit ?? "" });
123 slider.oninput = () => {
124 const v = fromSlider(s, Number(slider.value));
125 num.value = fmt(v);
126 update(s.key, v);
127 };
128 num.onchange = () => {
129 const v = Number(num.value);
130 if (!Number.isFinite(v)) return;
131 slider.value = String(toSlider(s, v));
132 update(s.key, v);
133 };
134 syncers.push(() => {
135 const v = o.get()[s.key] as number;
136 slider.value = String(toSlider(s, v));
137 num.value = fmt(v);
138 });
139 row.append(slider, num, unit);
140 } else if (s.kind === "select") {
141 const sel = el("select");
142 for (const opt of s.options) sel.append(el("option", { value: opt, textContent: opt }));
143 sel.onchange = () => update(s.key, sel.value);
144 syncers.push(() => (sel.value = o.get()[s.key] as string));
145 row.append(sel);
146 } else if (s.kind === "text") {
147 const ta = el("textarea", { rows: 3, spellcheck: false, placeholder: s.hint ?? "" });
148 ta.onchange = () => update(s.key, ta.value);
149 syncers.push(() => (ta.value = o.get()[s.key] as string));
150 row.classList.add("text");
151 row.append(ta);
152 } else {
153 const cb = el("input", { type: "checkbox" });
154 cb.onchange = () => update(s.key, cb.checked);
155 syncers.push(() => (cb.checked = o.get()[s.key] as boolean));
156 row.append(cb);
157 }
158 fs.append(row);
159 }
161 function sync() {
162 for (const f of syncers) f();
163 }
165 function updateReadouts() {
166 const r = o.readouts();
167 readoutBox.replaceChildren(...Object.entries(r).flatMap(([k, v]) => [el("dt", { textContent: k }), el("dd", { textContent: v })]));
168 }
170 sync();
171 updateReadouts();
172 return { el: root, sync, updateReadouts };
173}