Files
JIANT/web/emu/pm.js
T
Claude 80d999b713 Emulator Project Manager redesigned as Studio's project library (#280): compact rows that open one at a time (a large slot number, the name, chips A–D, OPEN / GEN), inside them small-titled groups (NOW: Load / Save what's playing here; NAME + Save; FILE: Share / Download / Import; COPY TO and MOVE TO with numbered buttons; FM-1: Send / Get with Web MIDI; Delete apart in red), empty slots dimmed with Save and Import only; presets ticked into a bank with its own action bar, a bank's import / send / get with a per-preset target picker (Same slots, Into empty slots, Leave all out; two into one slot refused before Go)
- pm-ui.js: a black HUD card of its own (the firmware's thermal accents, DotGothic16 for numbers and names, IBM Plex Mono), every target 40 px or more, one column on a phone; no prompts: a replace or a delete asks inside the group it came from, with "download a copy first"; a bank write that replaces presets now asks too and keeps them first as one bank file (JIANT-FM-bank-before-*.jfmb); progress and messages shown where the action was; still device-neutral and loaded only when opened; EN / ES switch kept. A page restyles it with --pm-* tokens on `#box .pm-body`
- pm.js / pm-fm.js: list items may carry chips [{ label, on }] (JIANT FM: a project's parts A–D)
- devices.html: the panel's look through the tokens (the old button overrides removed)
- browser_pm_test.mjs: the new structure (rows, groups, numbered targets, the open project not a target, asks with the copy first for copy / delete / bank), the bank picker's quick choices, the language switch, a 390 px Android phone (no sideways scroll, 40 px targets) and an iPhone without Web MIDI in Spanish; screenshots at 390 px and on a desktop. pm_test.mjs unchanged, passing

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-28 23:59:49 +00:00

97 lines
6.6 KiB
JavaScript

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant)
// (pm) The Project Manager's device-neutral part: the bank file, file names, which kind a file is, names as a device
// takes them. Everything that knows a device (its protocol, its slots, its formats) is its adapter's (pm-fm.js for JIANT
// FM); the panel (pm-ui.js) only calls the adapter and this file, so another firmware (JIANT BYTE) brings its own
// adapter and reuses both. No DOM here: node tests, pm_test.mjs.
//
// The adapter (one object; every method async unless marked; `side` is the adapter's own handle of a device: the
// emulator, a connected hardware unit):
// id, product, device "jiant-fm", "JIANT FM", "FM-1": the file names, the panel's words
// kinds { project: K, preset: K, bank: { ext, format, of: "preset" } }, K = { count, ext, format,
// accept (the file picker's), nameMax }: what the panel lists, how many slots, the files
// emuSide(port) (sync) the emulator's side over a MessagePort the worklet answers ("edport")
// connect(access, opt) the hardware's side over Web MIDI: { side, version, name, bluetooth }
// caps(side) { [kind]: { save, load, rename, copy, move, remove, file, send } }: which actions it has
// list(side, kind) [{ n, used, name, detail, open, draft, chips? }] for every slot (n from 0): detail a short
// word under the name (an engine), chips [{ label, on }] small lit boxes instead (parts A-D),
// open the one playing (tag OPEN; draft: tag GEN, not saved yet)
// save(side, kind, n, name) what plays now into slot n; load(side, kind, n): slot n playing
// rename(side, kind, n, name), copy(side, kind, from, to), move(side, kind, from, to), remove(side, kind, n)
// exportItem(side, kind, n) a file's object (JSON); importItem(side, kind, n, file): a file's object into slot n
// readFile(kind, obj) (sync) obj checked as that kind's file (throws PMError "file"), returned normalised
// presets(side, slots) bank entries [{ slot, name, objects }]; putPresets(side, [{ entry, to }]): them written
// send(src, dst, kind, from, to) one item from a side to another (dst's slot `to`), verified
// firmware(side) (sync) the firmware string a file carries
// Errors: PMError(code, detail); the panel words a code (pm-ui.js T.err), the adapter may add its own codes.
export class PMError extends Error {
constructor(code, detail = "", extra = {}) { super(`${code}${detail ? ": " + detail : ""}`); this.code = code; this.detail = detail; Object.assign(this, extra); }
}
/* a name as the devices take it: printable ASCII, upper case (their screens' font), at most max */
export const cleanName = (s, max = 12) => String(s || "").toUpperCase().replace(/[^\x20-\x7E]/g, "").replace(/\s+/g, " ").trim().slice(0, max);
const slug = (s) => String(s || "").replace(/[^\w-]+/g, "_").replace(/^_+|_+$/g, "") || "untitled";
const two = (n) => String(n).padStart(2, "0");
export const stamp = (d = new Date()) => d.toISOString().slice(0, 10);
/* the file name of an item: JIANT-FM-project-1-NIGHT.jfm, JIANT-FM-preset-05-PAD.jfmp, JIANT-FM-bank-2026-11-28.jfmb */
export function fileName(adapter, kind, n, name, d = new Date()) {
const k = adapter.kinds[kind], who = slug(adapter.product).replace(/_/g, "-");
if (kind === "bank") return `${who}-bank-${name ? slug(name) + "-" : ""}${stamp(d)}.${k.ext}`;
const num = kind === "preset" ? two(n + 1) : String(n + 1);
return `${who}-${kind}-${num}${name ? "-" + slug(name) : ""}.${k.ext}`;
}
/* a parsed file's kind by its format: "project", "preset", "bank" or null */
export function kindOf(adapter, obj) {
if (!obj || typeof obj !== "object") return null;
for (const [kind, k] of Object.entries(adapter.kinds)) if (obj.format === k.format || (k.formats || []).includes(obj.format)) return kind;
return null;
}
/* a file's text as the given kind (or any kind, kind null): { kind, file } (the adapter's check), PMError "file" */
export function parseFile(adapter, text, kind = null) {
let obj;
try { obj = typeof text === "string" ? JSON.parse(text) : text; } catch (e) { throw new PMError("file", "not JSON"); }
const k = kindOf(adapter, obj);
if (!k) throw new PMError("file", `format ${obj && obj.format}`);
if (kind && k !== kind) throw new PMError("wrongKind", k, { kind: k });
return { kind: k, file: k === "bank" ? readBank(adapter, obj) : adapter.readFile(k, obj) };
}
// ---------------------------------------------------------------------------------------------- the preset bank
/* several presets in one file: { format, version: 1, firmware, created, name, presets: [{ slot, name, objects }] } (each
* entry its preset file's slot, name and objects: the adapter's) */
export function makeBank(adapter, firmware, name, entries, d = new Date()) {
const b = adapter.kinds.bank;
return { format: b.format, version: 1, firmware, created: d.toISOString(), name: cleanName(name, 32) || "BANK",
presets: entries.map((e) => ({ slot: e.slot, name: e.name, objects: e.objects })) };
}
export function readBank(adapter, obj) {
const b = adapter.kinds.bank, of = adapter.kinds[b.of];
if (!obj || obj.format !== b.format || obj.version !== 1 || !Array.isArray(obj.presets) || !obj.presets.length || obj.presets.length > of.count)
throw new PMError("file", "not a bank");
const seen = new Set();
const presets = obj.presets.map((e) => {
if (!e || !Number.isInteger(e.slot) || e.slot < 0 || e.slot >= of.count || seen.has(e.slot)) throw new PMError("file", "bank slot");
seen.add(e.slot);
const one = adapter.readFile(b.of, { format: of.format, version: 1, firmware: obj.firmware, name: e.name, slot: e.slot, objects: e.objects });
return { ...one, slot: e.slot };
});
return { ...obj, presets };
}
/* which presets of a bank go where: [{ entry, to }] from the person's choice { [entry slot]: target slot | -1 (left
* out) }; every entry to its own slot when there is no choice. Two entries into one slot: PMError "twice" */
export function bankPlan(bank, choice = null) {
const out = [], used = new Set();
for (const e of bank.presets) {
const to = choice ? (choice[e.slot] ?? -1) : e.slot;
if (to < 0) continue;
if (used.has(to)) throw new PMError("twice", String(to + 1));
used.add(to); out.push({ entry: e, to });
}
return out;
}
/* the slots a write replaces that hold something: [{ n, name }] (the question before writing) */
export const replaced = (list, slots) => slots.map((n) => list[n]).filter((it) => it && it.used).map((it) => ({ n: it.n, name: it.name }));