// Album shell: hash routes // #/ track list // #/ a composition (visitor: published defaults only) // #//edit the same composition with the author panel import "./style.css"; import { getAudio } from "./core/audio.ts"; import { createAuthorPanel } from "./core/authorPanel.ts"; import type { Composition, Piece } from "./core/composition.ts"; import { resolveParams } from "./core/params.ts"; import type { Params } from "./core/params.ts"; import { ALBUM_TITLE, COMPOSITIONS } from "./compositions/index.ts"; const app = document.getElementById("app")!; let teardown: (() => void) | null = null; function h(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; } function route() { teardown?.(); teardown = null; app.replaceChildren(); const parts = location.hash.replace(/^#\/?/, "").split("/").filter(Boolean); const comp = COMPOSITIONS.find((c) => c.id === parts[0]); if (!comp) return showAlbum(); teardown = showComposition(comp, parts[1] === "edit"); } function showAlbum() { document.title = ALBUM_TITLE; const list = h("ol", { className: "tracks" }); for (const c of COMPOSITIONS) list.append(h("li", {}, h("a", { href: `#/${c.id}`, textContent: c.title }))); app.append(h("main", { className: "album" }, h("h1", { textContent: ALBUM_TITLE }), list)); } const storageKey = (c: Composition) => `double-pendulum:author:${c.id}`; function loadEdits(c: Composition): Partial | null { try { const s = localStorage.getItem(storageKey(c)); return s ? JSON.parse(s) : null; } catch { return null; } } function saveEdits(c: Composition, p: Params) { try { localStorage.setItem(storageKey(c), JSON.stringify(p)); } catch { /* storage unavailable: edits last for this page view only */ } } function showComposition(comp: Composition, author: boolean): () => void { const index = COMPOSITIONS.indexOf(comp) + 1; document.title = `${comp.title} ยท ${ALBUM_TITLE}`; const published = () => resolveParams(comp.schema, comp.defaults); let params = author ? resolveParams(comp.schema, comp.defaults, loadEdits(comp)) : published(); const view = h("div", { className: "view" }); const playBtn = h("button", { className: "play", textContent: "play" }); const bar = h("div", { className: "bar" }); const progress = h("div", { className: "progress" }, bar); const editLink = h("a", { className: "edit", href: author ? `#/${comp.id}` : `#/${comp.id}/edit`, textContent: author ? "done" : "edit" }); const header = h( "header", {}, h("a", { href: "#/", textContent: ALBUM_TITLE }), h("span", { className: "sep", textContent: " / " }), h("span", { textContent: `${index}. ${comp.title}` }), ); const stage = h("main", { className: "stage" }, header, view, h("div", { className: "transport" }, playBtn, progress, editLink)); const page = h("div", { className: author ? "page with-author" : "page" }, stage); app.append(page); const piece: Piece = comp.create(view, () => params); const startAt = { value: 0 }; const play = async (offset: number) => { const audio = await getAudio(); await piece.start(audio, offset); }; playBtn.onclick = () => (piece.playing ? piece.stop() : play(author ? startAt.value : 0)); if (author) { // author can click the progress line to jump progress.classList.add("seekable"); progress.onclick = (ev) => { const r = progress.getBoundingClientRect(); const t = Math.max(0, ((ev.clientX - r.left) / r.width) * piece.length()); void play(t); }; } let panel: ReturnType | null = null; if (author) { panel = createAuthorPanel({ schema: comp.schema, get: () => params, set: (p) => { params = p; saveEdits(comp, p); }, defaults: published, readouts: () => piece.readouts(), startAt, }); page.append(panel.el); } const onKey = (e: KeyboardEvent) => { const tag = (e.target as HTMLElement).tagName; if (e.code === "Space" && tag !== "INPUT" && tag !== "SELECT" && tag !== "BUTTON") { e.preventDefault(); playBtn.click(); } }; window.addEventListener("keydown", onKey); let raf = 0; let lastReadout = 0; const frame = (ts: number) => { piece.draw(); playBtn.textContent = piece.playing ? "stop" : "play"; bar.style.width = `${Math.min(100, (100 * piece.now()) / piece.length())}%`; if (panel && ts - lastReadout > 200) { panel.updateReadouts(); lastReadout = ts; } raf = requestAnimationFrame(frame); }; raf = requestAnimationFrame(frame); return () => { cancelAnimationFrame(raf); window.removeEventListener("keydown", onKey); piece.dispose(); }; } window.addEventListener("hashchange", route); route();