feat: sequence editor undo/redo, collapsible browser, playhead easing

Sequence editor: undo and redo, a collapsible browser with chips, search
over sequences too, name in the header row, Beats/Seconds dropdown, Delete
beside Save. Playhead eases between poll results. Lanes keep their phase
when a step ends late, so they no longer drift. The rig's default room is
two columns of bars facing each other, and the simulator has a read-only
debug hook for the playback checks.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-17 00:28:59 +13:00
1 parent a1c8ce853b
commit 02440be55d
7 files changed
+252 -109

No files matched your search

+158 -28
View File
@@ -25,7 +25,9 @@ const seqTl = {
loopEndSet: false, // the user moved the end marker
brush: '', // the browser item double-click adds
lastLen: TL_BAR,
head: null, // playhead {pos, at, rate, max}
head: null, // playhead {pos, at, rate, max, wrap}
headErr: 0, // the playhead's easing offset after a status poll: glides to the server's position instead of jumping
headErrT: 0,
pollTimer: null,
raf: 0,
drag: null,
@@ -364,6 +366,9 @@ function seqTlOnOpen(doc) {
: '',
);
seqTl.on = true;
tlHist.undo = [];
tlHist.redo = [];
tlHist.cur = null; // the first render records the opened state
tlRender();
tlStartPoll();
}
@@ -415,6 +420,7 @@ function tlSelect(sel) {
function tlRender() {
const canvas = tlEl('tl-canvas');
if (!canvas || !seqTl.on) return;
tlRecord(); // every edit ends in a render: an edit since the last one becomes an undo step
if (seqTl.snapAuto) seqTl.snap = tlAutoSnap(); // Auto follows the zoom, so drags land on the ticks shown
const ppb = seqTl.ppb;
const timed = tlTimed();
@@ -426,14 +432,9 @@ function tlRender() {
canvas.style.setProperty('--ppb', String(ppb));
canvas.innerHTML = '';
// Units toggle and BPM readout
document.querySelectorAll('#tl-units button').forEach((b) => {
b.classList.toggle('on', (b.dataset.v === 'time') === timed);
// A one-shot is always in seconds: Beats is greyed out while Loop is off
b.disabled = !loop && b.dataset.v !== 'time';
});
const bpm = tlEl('tl-bpm');
if (bpm) bpm.textContent = `${Math.round(tlBpm())} BPM`;
// A one-shot is always in seconds: Beats is greyed out while Loop is off
const beatsOpt = document.querySelector('#sequence-editor-mode option[value="beats"]');
if (beatsOpt) beatsOpt.disabled = !loop;
// Ruler
const rrow = document.createElement('div');
@@ -721,11 +722,7 @@ function tlRenderInspector() {
const track = sel && sel.track ? tlTrack(sel.track) : null;
if (clip) return tlInspectClip(host, clip);
if (track) return tlInspectTrack(host, track);
const hint = document.createElement('div');
hint.className = 'muted-text';
hint.textContent =
'Drag a preset from the browser onto a track. Drag clips to move them and their edges to resize. Double-click a track to add the preset picked in the browser. Click a track name for its groups. Delete removes the selected clip.';
host.appendChild(hint);
// Nothing selected: the inspector stays empty (and hidden by the stylesheet)
}
function tlInspectClip(host, c) {
@@ -797,7 +794,6 @@ function tlInspectClip(host, c) {
const btns = document.createElement('div');
btns.className = 'tl-insp-btns';
btns.appendChild(tlBtn('Edit', 'Edit the preset or sequence', () => tlEditPlay(c.play)));
btns.appendChild(tlBtn('Duplicate', 'Copy it to the next free space (Ctrl+D)', () => tlDuplicate(c)));
btns.appendChild(tlBtn('Delete', 'Remove the clip (Delete)', () => tlDelete(c), 'btn btn-danger btn-small'));
host.appendChild(btns);
}
@@ -888,6 +884,71 @@ function tlInspectTrack(host, t) {
host.appendChild(btns);
}
/* --------------------------------------------------------------- undo/redo */
const TL_HISTORY_MAX = 100;
const tlHist = { undo: [], redo: [], cur: null }; // states as JSON; cur is the one on screen
/** What undo brings back: clips, tracks, the window and units. Not the selection, zoom or scroll. */
function tlSnapshot() {
return JSON.stringify({
clips: seqTl.clips,
targets: seqEd.targets,
loopEnd: seqTl.loopEnd,
loopEndSet: seqTl.loopEndSet,
mode: (tlEl('sequence-editor-mode') || {}).value || '',
loop: tlLoop(),
});
}
/** Called on each render: if the timeline changed since the last one, the old state is an undo step. */
function tlRecord() {
const s = tlSnapshot();
if (tlHist.cur !== null && s !== tlHist.cur) {
tlHist.undo.push(tlHist.cur);
if (tlHist.undo.length > TL_HISTORY_MAX) tlHist.undo.shift();
tlHist.redo = [];
}
tlHist.cur = s;
tlUndoButtons();
}
function tlUndoButtons() {
const u = tlEl('tl-undo');
const r = tlEl('tl-redo');
if (u) u.disabled = !tlHist.undo.length;
if (r) r.disabled = !tlHist.redo.length;
}
function tlRestore(s) {
const st = JSON.parse(s);
seqTl.clips = st.clips;
seqEd.targets = st.targets;
seqTl.loopEnd = st.loopEnd;
seqTl.loopEndSet = st.loopEndSet;
const mode = tlEl('sequence-editor-mode');
if (mode && st.mode) mode.value = st.mode;
const loop = tlEl('sequence-editor-loop');
if (loop) loop.checked = st.loop;
const sel = seqTl.sel;
if (sel && ((sel.clip && !tlClip(sel.clip)) || (sel.track && !tlTrack(sel.track)))) seqTl.sel = null;
tlHist.cur = s; // so the render below doesn't record the restore as a new edit
tlBuildSnap();
tlRender();
}
function tlUndo() {
if (!tlHist.undo.length || seqTl.drag) return;
tlHist.redo.push(tlHist.cur);
tlRestore(tlHist.undo.pop());
}
function tlRedo() {
if (!tlHist.redo.length || seqTl.drag) return;
tlHist.undo.push(tlHist.cur);
tlRestore(tlHist.redo.pop());
}
/* ------------------------------------------------------------------- edits */
function tlAddAt(uid, start, play) {
@@ -967,7 +1028,6 @@ function tlSetUnits(timed) {
seqTl.loopEnd = q(seqTl.loopEnd);
seqTl.lastLen = timed ? 4 : TL_BAR;
mode.value = timed ? 'time' : 'beats';
mode.dispatchEvent(new Event('change'));
tlBuildSnap();
tlRender();
}
@@ -1221,7 +1281,6 @@ async function tlPoll() {
let lanes = null;
if (st && st.overlay && st.overlay.active && String(st.overlay.sequence_id) === sequenceEditorId) lanes = st.overlay.lanes;
else if (st && st.active && String(st.sequence_id) === sequenceEditorId) lanes = st.lanes;
seqTl.head = null;
if (Array.isArray(lanes)) {
const { map } = tlToLanes();
const li = lanes.findIndex((l, i) => l && l.started && !l.done && map[i]);
@@ -1231,11 +1290,27 @@ async function tlPoll() {
const timed = l.mode === 'time';
const s = m.starts[l.step] != null ? m.starts[l.step] : 0;
const into = timed ? (l.elapsed_ms || 0) / 1000 : l.beat_in_step || 0;
seqTl.head = { pos: s + into, at: performance.now(), rate: timed ? 1 : tlBpm() / 60, max: m.ends[l.step] != null ? m.ends[l.step] : s + into };
}
}
const looping = !!(st && st.active && String(st.sequence_id) === sequenceEditorId);
const now = performance.now();
const next = {
pos: s + into,
at: now,
rate: timed ? 1 : tlBpm() / 60,
max: m.ends[l.step] != null ? m.ends[l.step] : s + into,
wrap: looping ? tlLoopLen() : 0, // a looping sequence wraps at its window; a one-shot stops at its lane's end
};
// Keep the head where it was on screen and ease it onto the server's position, so a poll never jumps it
const old = seqTl.head;
if (old) {
const d = tlHeadDiff(tlHeadAt(old, now) + seqTl.headErr, tlHeadAt(next, now), next.wrap);
seqTl.headErr = Math.abs(d) < 0.5 ? d : 0;
} else seqTl.headErr = 0;
seqTl.headErrT = now;
seqTl.head = next;
} else seqTl.head = null;
} else seqTl.head = null;
} catch (_) {
/* try again */
/* try again: the head stays where it is */
}
} else {
seqTl.head = null;
@@ -1243,12 +1318,31 @@ async function tlPoll() {
seqTl.pollTimer = setTimeout(tlPoll, 250);
}
/** Where the head is at time now, extrapolated from its last poll. */
function tlHeadAt(h, now) {
const x = h.pos + ((now - h.at) / 1000) * h.rate;
return h.wrap > 0 ? ((x % h.wrap) + h.wrap) % h.wrap : Math.min(h.max, x);
}
/** a - b, the short way round a loop of length wrap (no wrap: plain difference). */
function tlHeadDiff(a, b, wrap) {
const d = a - b;
return wrap > 0 ? ((((d + wrap / 2) % wrap) + wrap) % wrap) - wrap / 2 : d;
}
function tlDrawHead() {
const ph = tlEl('tl-playhead');
const lcd = tlEl('tl-pos');
const h = seqTl.head;
const now = performance.now();
let pos = 0;
if (h) pos = Math.min(h.max, h.pos + ((performance.now() - h.at) / 1000) * h.rate);
if (h) {
pos = tlHeadAt(h, now) + seqTl.headErr;
if (h.wrap > 0) pos = ((pos % h.wrap) + h.wrap) % h.wrap;
else pos = Math.min(h.max, pos);
seqTl.headErr *= Math.exp(-(now - seqTl.headErrT) / 150); // ease out the correction
seqTl.headErrT = now;
} else seqTl.headErr = 0;
if (ph) {
ph.hidden = !h;
if (h) ph.style.left = `calc(var(--tl-head-w) + ${pos * seqTl.ppb}px)`;
@@ -1279,12 +1373,36 @@ document.addEventListener('DOMContentLoaded', () => {
if (el) el.addEventListener(ev, fn);
};
on('tl-add-track', 'change', (e) => tlAddTrack(e.target.value));
on('tl-per-driver', 'click', () => {
addTargetsPerDriver();
if (seqEd.targets.length > TL_TRACKS_MAX) alert(`Only the first ${TL_TRACKS_MAX} tracks can have clips.`);
tlRender();
});
on('tl-undo', 'click', tlUndo);
on('tl-redo', 'click', tlRedo);
on('tl-browser-search', 'input', tlRenderBrowser);
// The browser panel collapses to a strip with its arrow, giving the tracks the width; the choice is remembered
const setBrowser = (show) => {
const panel = document.querySelector('#seq-tl .tl-browser');
const btn = tlEl('tl-browser-toggle');
if (panel) panel.classList.toggle('collapsed', !show);
if (btn) {
btn.innerHTML = show ? '&#8250;' : '&#8249;';
btn.title = show ? 'Hide the browser' : 'Show the browser';
btn.setAttribute('aria-expanded', String(show));
}
};
let browserShown = true;
try {
browserShown = localStorage.getItem('seqTlBrowser') !== '0';
} catch (_) {
/* private mode */
}
setBrowser(browserShown);
on('tl-browser-toggle', 'click', () => {
browserShown = !browserShown;
setBrowser(browserShown);
try {
localStorage.setItem('seqTlBrowser', browserShown ? '1' : '0');
} catch (_) {
/* private mode */
}
});
on('tl-browser-refresh', 'click', async () => {
try {
const r = await fetch('/presets', { headers: { Accept: 'application/json' } });
@@ -1309,7 +1427,12 @@ document.addEventListener('DOMContentLoaded', () => {
});
}
on('tl-zoom-out', 'click', () => tlZoom(1 / 1.4, seqTl.hoverX));
document.querySelectorAll('#tl-units button').forEach((b) => b.addEventListener('click', () => tlSetUnits(b.dataset.v === 'time')));
// Units dropdown: put the old value back first, so tlSetUnits sees a change and converts the clips
on('sequence-editor-mode', 'change', (e) => {
const timed = e.target.value === 'time';
e.target.value = timed ? 'beats' : 'time';
tlSetUnits(timed);
});
on('tl-play', 'click', async () => {
const ok = await saveSequenceEditor();
if (ok === false || !sequenceEditorId) return;
@@ -1340,6 +1463,13 @@ document.addEventListener('DOMContentLoaded', () => {
if (!seqTl.on || !modal || !modal.classList.contains('active')) return;
if (e.target && e.target.closest && e.target.closest('input, select, textarea, [contenteditable]')) return;
if (document.querySelector('.modal.active:not(#sequence-editor-modal)')) return;
const k = e.key.toLowerCase();
if ((e.ctrlKey || e.metaKey) && (k === 'z' || k === 'y')) {
e.preventDefault();
if (k === 'y' || e.shiftKey) tlRedo();
else tlUndo();
return;
}
const c = seqTl.sel && seqTl.sel.clip ? tlClip(seqTl.sel.clip) : null;
if (!c) return;
if (e.key === 'Delete' || e.key === 'Backspace') {