fix(align): remove whole auto run between manual anchors on edit

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-30 06:30:48 +01:00
co-authored by Claude Haiku 5.5
parent 1ff29f94cd
commit ca5942956a
2 changed files with 40 additions and 18 deletions
+10 -9
View File
@@ -75,16 +75,17 @@ function checkSlot(left: Anchor | undefined, right: Anchor | undefined, a: Omit<
if (right && !(a.midiSec < right.midiSec && a.audioSec < right.audioSec)) throw new Error("anchor would cross its right neighbour");
}
// Inserts a manual anchor at index `at` of an already-sorted list, then drops the immediate
// neighbours of that anchor when they are auto. Never reaches past a manual anchor.
// Inserts a manual anchor at index `at` of an already-sorted list. Removes the whole contiguous run of
// auto anchors on each side, up to the nearest manual anchor (or the song start/end). Crossing checks
// apply only against those manual neighbours, since the autos in between are removed.
function insertManual(list: Anchor[], at: number, a: Omit<Anchor, "kind">): Anchor[] {
checkSlot(list[at - 1], list[at], a);
const out = [...list.slice(0, at), { midiSec: a.midiSec, audioSec: a.audioSec, kind: "manual" as const }, ...list.slice(at)];
const p = at;
const drop = new Set<number>();
if (p - 1 >= 0 && out[p - 1]!.kind === "auto") drop.add(p - 1);
if (p + 1 < out.length && out[p + 1]!.kind === "auto") drop.add(p + 1);
return sortedCopy(out.filter((_, i) => !drop.has(i)));
let l = at - 1;
while (l >= 0 && list[l]!.kind === "auto") l--;
let r = at;
while (r < list.length && list[r]!.kind === "auto") r++;
checkSlot(list[l], list[r], a);
const out = [...list.slice(0, l + 1), { midiSec: a.midiSec, audioSec: a.audioSec, kind: "manual" as const }, ...list.slice(r)];
return sortedCopy(out);
}
export function addManual(anchors: Anchor[], a: Omit<Anchor, "kind">): Anchor[] {