Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): stop a region dragged into an identical one flush against it

EtienneLescot412908282cdc
diff --git a/src/lib/ai-edition/document/audioTracks.ts b/src/lib/ai-edition/document/audioTracks.ts
index 8f1f5ad9a..027f330da 100644
--- a/src/lib/ai-edition/document/audioTracks.ts
+++ b/src/lib/ai-edition/document/audioTracks.ts
@@ -512,13 +512,7 @@ export function placeAudioTrackInDocument(
const before = lane.find((other) => trackGroupId(other) === groupId) ?? pill;
const clamped = clampSpanAgainstNeighbours(
{ start: pill.startMs, end: pill.endMs },
- `lane:${pill.kind}:${groupId}`,
- others.map((other) => ({
- id: other.id,
- identity: `lane:${other.kind}:${trackGroupId(other)}`,
- start: other.startMs,
- end: other.endMs,
- })),
+ others.map((other) => ({ start: other.startMs, end: other.endMs })),
{ start: before.startMs, end: before.endMs },
);
startMs = clamped.start;
diff --git a/src/lib/ai-edition/timeline/timelineMap.test.ts b/src/lib/ai-edition/timeline/timelineMap.test.ts
index 18c6fd142..11aa30c50 100644
--- a/src/lib/ai-edition/timeline/timelineMap.test.ts
+++ b/src/lib/ai-edition/timeline/timelineMap.test.ts
@@ -652,59 +652,38 @@ describe("coalesceByIdentity (rule 1 — merge)", () => {
});
describe("clampSpanAgainstNeighbours (rule 2 — repel)", () => {
- const other = (id: string, start: number, end: number, identity: string) => ({
- id,
- start,
- end,
- identity,
- });
+ const other = (start: number, end: number) => ({ start, end });
- it("stops at a different-identity neighbour on the right", () => {
+ it("stops at a neighbour on the right", () => {
// The right edge dragged from 4 to 8.
- const out = clampSpanAgainstNeighbours(
- { start: 0, end: 8 },
- "fast",
- [other("n", 5, 12, "slow")],
- { start: 0, end: 4 },
- );
+ const out = clampSpanAgainstNeighbours({ start: 0, end: 8 }, [other(5, 12)], {
+ start: 0,
+ end: 4,
+ });
expect(out).toEqual({ start: 0, end: 5 });
});
- it("stops at a different-identity neighbour on the left", () => {
+ it("stops at a neighbour on the left", () => {
// The left edge dragged from 6 to 3.
- const out = clampSpanAgainstNeighbours(
- { start: 3, end: 10 },
- "fast",
- [other("n", 0, 5, "slow")],
- { start: 6, end: 10 },
- );
+ const out = clampSpanAgainstNeighbours({ start: 3, end: 10 }, [other(0, 5)], {
+ start: 6,
+ end: 10,
+ });
expect(out).toEqual({ start: 5, end: 10 });
});
- it("treats a same-identity neighbour as no obstacle (they simply merge)", () => {
- const out = clampSpanAgainstNeighbours(
- { start: 0, end: 8 },
- "fast",
- [other("n", 5, 12, "fast")],
- { start: 0, end: 4 },
- );
- expect(out).toEqual({ start: 0, end: 8 });
- });
-
it("is squeezed by blockers on both sides", () => {
// Stretched both ways, from 4–6: each edge stops at its own blocker.
- const out = clampSpanAgainstNeighbours(
- { start: 1, end: 10 },
- "fast",
- [other("l", 0, 3, "slow"), other("r", 7, 12, "other")],
- { start: 4, end: 6 },
- );
+ const out = clampSpanAgainstNeighbours({ start: 1, end: 10 }, [other(0, 3), other(7, 12)], {
+ start: 4,
+ end: 6,
+ });
expect(out).toEqual({ start: 3, end: 7 });
});
it("leaves a span that overlaps nothing untouched", () => {
expect(
- clampSpanAgainstNeighbours({ start: 2, end: 4 }, "fast", [other("n", 8, 9, "slow")], {
+ clampSpanAgainstNeighbours({ start: 2, end: 4 }, [other(8, 9)], {
start: 0,
end: 2,
}),
@@ -712,21 +691,23 @@ describe("clampSpanAgainstNeighbours (rule 2 — repel)", () => {
});
it("keeps a moved span whole, flush against the neighbour it ran into", () => {
- const n = [other("n", 6, 12, "slow")];
+ const n = [other(6, 12)];
// From the left: it stops with its end on the neighbour's start…
- expect(
- clampSpanAgainstNeighbours({ start: 5, end: 9 }, "fast", n, { start: 0, end: 4 }),
- ).toEqual({ start: 2, end: 6 });
+ expect(clampSpanAgainstNeighbours({ start: 5, end: 9 }, n, { start: 0, end: 4 })).toEqual({
+ start: 2,
+ end: 6,
+ });
// …and from the right, with its start on the neighbour's end.
- expect(
- clampSpanAgainstNeighbours({ start: 9, end: 13 }, "fast", n, { start: 20, end: 24 }),
- ).toEqual({ start: 12, end: 16 });
+ expect(clampSpanAgainstNeighbours({ start: 9, end: 13 }, n, { start: 20, end: 24 })).toEqual({
+ start: 12,
+ end: 16,
+ });
});
it("never throws a moved span to the far side of a neighbour it overlaps", () => {
// Dropped with its start past the neighbour's start, which decided the side before.
expect(
- clampSpanAgainstNeighbours({ start: 7, end: 11 }, "fast", [other("n", 6, 10, "slow")], {
+ clampSpanAgainstNeighbours({ start: 7, end: 11 }, [other(6, 10)], {
start: 0,
end: 4,
}),
@@ -735,7 +716,7 @@ describe("clampSpanAgainstNeighbours (rule 2 — repel)", () => {
it("keeps a drop that overlaps nothing, even past a neighbour", () => {
expect(
- clampSpanAgainstNeighbours({ start: 12, end: 16 }, "fast", [other("n", 6, 10, "slow")], {
+ clampSpanAgainstNeighbours({ start: 12, end: 16 }, [other(6, 10)], {
start: 0,
end: 4,
}),
@@ -744,7 +725,7 @@ describe("clampSpanAgainstNeighbours (rule 2 — repel)", () => {
it("stops a left edge dragged past a whole neighbour at that neighbour", () => {
expect(
- clampSpanAgainstNeighbours({ start: 30, end: 77 }, "fast", [other("n", 40, 50, "slow")], {
+ clampSpanAgainstNeighbours({ start: 30, end: 77 }, [other(40, 50)], {
start: 64,
end: 77,
}),
@@ -754,19 +735,17 @@ describe("clampSpanAgainstNeighbours (rule 2 — repel)", () => {
it("leaves a move with no room where it was", () => {
// Neighbours at 0–3 and 7–12, and the span already flush against the second one.
expect(
- clampSpanAgainstNeighbours(
- { start: 2, end: 7 },
- "fast",
- [other("l", 0, 3, "slow"), other("r", 7, 12, "other")],
- { start: 12, end: 17 },
- ),
+ clampSpanAgainstNeighbours({ start: 2, end: 7 }, [other(0, 3), other(7, 12)], {
+ start: 12,
+ end: 17,
+ }),
).toEqual({ start: 12, end: 17 });
});
it("ignores a neighbour the span already overlapped before the edit", () => {
// An add does not clamp, so two regions can overlap; no position on the way clears that.
expect(
- clampSpanAgainstNeighbours({ start: 0, end: 5 }, "fast", [other("n", 4, 9, "slow")], {
+ clampSpanAgainstNeighbours({ start: 0, end: 5 }, [other(4, 9)], {
start: 1,
end: 6,
}),
@@ -921,6 +900,32 @@ describe("pills wired to the universal rules", () => {
expect(slow?.startMs).toBe(10000);
expect(slow?.endMs).toBe(15000);
});
+
+ it.each([
+ ["speed", { speed: 1.5 }],
+ ["zoom", { depth: 2 }],
+ ["annotation", { type: "text", content: "Hi" }],
+ ["full-camera", {}],
+ ])("stops a %s region flush against an identical one instead of overlapping it (#1133)", (_kind, payload) => {
+ // The report's layout, shifted into clip A: 13.711–16.202 and 16.307–18.798, the second
+ // dragged 1.375 s left. It used to land on 14.932–17.423, stored over the first.
+ const regions = anchorRegionsWithDerivedMs(
+ [
+ { id: "a", startMs: 13_711, endMs: 16_202, ...payload },
+ { id: "b", startMs: 16_307, endMs: 18_798, ...payload },
+ ],
+ clips,
+ ids(),
+ );
+ const out = replacePillSpan(regions, "b", 14_932, 17_423, clips, ids());
+ expect(out.map((r) => [r.id, r.startMs, r.endMs])).toEqual([
+ ["a", 13_711, 16_202],
+ ["b", 16_202, 18_693],
+ ]);
+ // Flush, they show as one pill, and an edit of either half reaches only that half.
+ expect(coalesceRegionsForRuler(out)).toHaveLength(1);
+ expect(resolveRegionIds(out, "b")).toEqual(["b"]);
+ });
});
describe("a dragged pill stays whole against its neighbour (#1008)", () => {
diff --git a/src/lib/ai-edition/timeline/timelineMap.ts b/src/lib/ai-edition/timeline/timelineMap.ts
index 8a7f763fe..451987ce9 100644
--- a/src/lib/ai-edition/timeline/timelineMap.ts
+++ b/src/lib/ai-edition/timeline/timelineMap.ts
@@ -128,9 +128,10 @@ export function anchoredToRawSpanSec(
// came to be adjacent (authored side by side, split by a reorder then
// rejoined, …) is irrelevant: identity is what a region IS, never where
// it came from or how it got there.
-// 2. REPEL — two regions of the same kind with DIFFERENT identities may not
-// overlap. An edit clamps to the neighbour's edge; the neighbour never
-// moves (no cascade).
+// 2. REPEL — a moved or resized pill may not overlap another pill of its kind, of
+// any identity. An edit clamps to the neighbour's edge; the neighbour
+// never moves (no cascade). Once flush, a same-identity neighbour merges
+// with it on display (rule 1).
//
// A kind with no properties (trim, full-camera) collapses to a constant identity, so
// its regions always merge — the long-standing trim behaviour, now *derived* from the
@@ -232,8 +233,8 @@ export function coalesceByIdentity(spans: IdentifiedSpan[], epsilonSec = 0.001):
}
/**
- * Rule 2 — clamp an edited span so it cannot overlap a same-kind span of a DIFFERENT
- * identity. Blocking neighbours act as walls, and `from`, the span before the edit, decides
+ * Rule 2 — clamp an edited span so it cannot overlap another span of its kind. Neighbours
+ * act as walls, and `from`, the span before the edit, decides
* which side of a wall the span stays on: the edit backs off toward it until it clears every
* wall. The edge that ran into a wall stops on it, and the other edge backs off with it only
* if it moved the same way. So a move keeps its length and stops flush against the neighbour
@@ -241,21 +242,20 @@ export function coalesceByIdentity(spans: IdentifiedSpan[], epsilonSec = 0.001):
* drop that overlaps nothing stands, past a neighbour or not. Clamping each edge on its own,
* with the side guessed from the desired span, cut a moved region short or threw it past the
* neighbour (#1008). The neighbour is never modified or displaced, so an edit can never
- * cascade into regions the user did not touch. Same-identity spans are not obstacles —
- * overlapping them is harmless, they simply merge (rule 1). Neither is a neighbour that `from`
- * already overlaps: an add does not clamp, and no point on the way back would clear it.
+ * cascade into regions the user did not touch. A same-identity neighbour is a wall too: let
+ * through, it stayed stored under the moved region, the pair showed as one pill, and an edit
+ * of either half reached both (#1133). Stopped flush, it merges on display (rule 1) and an
+ * edit still splits it (#1017). A neighbour that `from` already overlaps is no wall: an add
+ * does not clamp, and no point on the way back would clear it.
*/
export function clampSpanAgainstNeighbours(
desired: { start: number; end: number },
- identity: string,
- others: IdentifiedSpan[],
+ others: Array<{ start: number; end: number }>,
from: { start: number; end: number },
): { start: number; end: number } {
let start = Math.min(desired.start, desired.end);
let end = Math.max(desired.start, desired.end);
- const walls = others.filter(
- (o) => o.identity !== identity && (o.end <= from.start || o.start >= from.end),
- );
+ const walls = others.filter((o) => o.end <= from.start || o.start >= from.end);
for (;;) {
const wall = walls.find((o) => o.start < end && o.end > start);
if (!wall) return { start, end };
@@ -385,7 +385,7 @@ export function dropPillsByIds<T extends { id: string; startMs: number; endMs: n
/**
* Move/resize the pill containing `id`, obeying both rules: the requested span is first
- * CLAMPED against pills of a different identity (rule 2 — they act as walls and never
+ * CLAMPED against every other pill (rule 2 — they act as walls and never
* move), on the ruler and from where the pill sits now, so a moved pill keeps its length;
* then the pill's regions are replaced by fragments re-anchored to the clamped
* span, carrying the pill's payload. Crossing a clip boundary re-splits into one fragment
@@ -410,10 +410,7 @@ export function replacePillSpan<T extends { id: string; startMs: number; endMs:
const clamped = clampSpanAgainstNeighbours(
{ start: Math.min(startMs, endMs) / 1000, end: Math.max(startMs, endMs) / 1000 },
- pill.identity,
- pills
- .filter((p) => p !== pill)
- .map((p) => ({ id: p.ids[0], start: p.start, end: p.end, identity: p.identity })),
+ pills.filter((p) => p !== pill),
pill,
);
diff --git a/technical-documentation/architecture/timeline-model.md b/technical-documentation/architecture/timeline-model.md
index f77a37f7b..f8f293eb0 100644
--- a/technical-documentation/architecture/timeline-model.md
+++ b/technical-documentation/architecture/timeline-model.md
@@ -61,17 +61,17 @@ Every public export of
| Function | What it converts | Direction |
|---|---|---|
| `anchorRawRegionsToClips` (`:65`) | v4 RAW-virtual-ms region → one anchored fragment per covered clip (drops zero-length / off-timeline regions, and the sub-ms sliver a whole-ms edge leaves past a clip boundary) | RAW-virtual → clip-anchored |
-| `anchorRegionsWithDerivedMs` (`:459`) | Same as above but never drops user data: emits `{…fragment, startMs, endMs}` for anchored regions and passes un-anchorable regions through with their original ms | RAW-virtual → v5 stored shape |
+| `anchorRegionsWithDerivedMs` (`:456`) | Same as above but never drops user data: emits `{…fragment, startMs, endMs}` for anchored regions and passes un-anchorable regions through with their original ms | RAW-virtual → v5 stored shape |
| `anchoredToRawSpanSec` (`:110`) | One anchored fragment → its current RAW-virtual span on the ruler | clip-anchored → RAW-virtual |
| `regionIdentityKey` (`:181`) | A region → canonical identity key (properties minus position/provenance); equal keys = "same kind, same look" | region → identity string |
| `coalesceByIdentity` (`:211`) | Set of identified spans → merged runs that touch and share an identity | spans → pills |
-| `clampSpanAgainstNeighbours` (`:248`) | A desired span clamped against different-identity neighbours, backing off toward the span before the edit (a move keeps its length; no cascade) | desired + span before the edit → clamped span |
+| `clampSpanAgainstNeighbours` (`:251`) | A desired span clamped against every neighbour, of any identity, backing off toward the span before the edit (a move keeps its length; no cascade) | desired + span before the edit → clamped span |
| `coalesceRegionsForRuler` (`:290`) | Region array → ruler pills (one entry per merged run, payload carried by `member`) | regions → pills |
| `resolvePillIds` (`:314`) | Region id → every region id under its pill (recomputed, not stored) | id → ids |
| `resolveRegionIds` (`:337`) | Region id → the ids of the one region it belongs to: its fragments across clip junctions, not a look-alike that touches it on the same clip (see below) | id → ids |
| `dropPillById` / `dropPillsByIds` (`:368` / `:377`) | Delete every region under a pill (resolved from the merge rule) | regions → regions |
-| `replacePillSpan` (`:398`) | Move/resize a pill: clamp against different-identity neighbours, then re-anchor to the clamped span, keeping the id of the member it was called with | pill + clip layout → re-anchored fragments |
-| `segmentRawSpanSec` (`:484`) | One kept playback segment → its RAW-virtual extent | segment → RAW span |
+| `replacePillSpan` (`:398`) | Move/resize a pill: clamp against every other pill, then re-anchor to the clamped span, keeping the id of the member it was called with | pill + clip layout → re-anchored fragments |
+| `segmentRawSpanSec` (`:481`) | One kept playback segment → its RAW-virtual extent | segment → RAW span |
| `projectRegionsToSource` (`:641`) | Region array → source-ms entries with `clipIndex` for native (anchored path uses anchor; unanchored path falls back to RAW mapping through each segment's own raw extent — never drops an un-anchorable region onto an unrelated clip). A region wholly under a trim is emitted once, marked `underTrim`, addressed by the segment the cut interrupts. A region a cut splits marks each piece's cut edge `continuesBefore` / `continuesAfter`, so Full Camera ramps only at its own edges and stays full across the cut | RAW/anchored → source + `clipIndex` |
| `resolveNativePosition` (`:759`) | RAW-virtual playhead → `{clip, clipIndex, sourceTimeSec}` for the active native decoder + paired camera (over a trimmed-out stretch it presents the removed frames themselves, borrowing the same segment index the modifiers under that cut borrow) | RAW-virtual → source + `clipIndex` |
@@ -82,11 +82,13 @@ file rather than re-derived per kind:
identity that touch are one pill. How they became adjacent (authored side by side,
split by a reorder then rejoined, …) is irrelevant; identity is what a region *is*,
not where it came from.
-2. **Repel** (`clampSpanAgainstNeighbours`) — two regions of the same kind with
- different identities may not overlap. An edit clamps to the neighbour's edge; the
+2. **Repel** (`clampSpanAgainstNeighbours`) — a moved or resized region may not
+ overlap another region of its kind, whatever its identity. An edit clamps to the neighbour's edge; the
neighbour never moves (no cascade). Which edge is decided by where the region was
before the edit, so a moved region keeps its length and stops flush against the
- neighbour on the side it came from.
+ neighbour on the side it came from. A same-identity neighbour is a wall too: let
+ through, it stayed stored under the moved region and an edit of either half reached
+ both (#1133). Stopped flush, the two merge on display (rule 1).
A kind with no properties (trim, full-camera) collapses to a constant identity, so its
regions always merge — the long-standing trim behaviour, now derived from the general
@@ -253,10 +255,14 @@ the contract a reviewer can grade against. Each is asserted in
to one region, with every fragment of it (#1017)"; end to end through the store in
`useTimeline.test.ts`, "changes the region picked and its fragments, not its
look-alike, and the pill splits".)
-- **Repel never cascades.** A different-identity neighbour acts as a wall — the
- edited span stops at its edge and the neighbour never moves. (`clampSpanAgainst
- Neighbours` "stops at a different-identity neighbour on the right" / "left".)
-- **Repel never shortens a move.** A region dragged into a different-identity neighbour
+- **Repel never cascades.** A neighbour acts as a wall — the edited span stops at its
+ edge and the neighbour never moves. (`clampSpanAgainstNeighbours` "stops at a
+ neighbour on the right" / "left".)
+- **Repel ignores identity.** An identical region dragged into another stops flush
+ instead of overlapping it; the pair then shows as one pill and an edit of either half
+ splits it. (`replacePillSpan` "stops a speed / zoom / annotation / full-camera region
+ flush against an identical one instead of overlapping it (#1133)".)
+- **Repel never shortens a move.** A region dragged into a neighbour
keeps its length and stops against it on the side it came from, clip boundary or not;
it is never cut short or thrown past it (#1008). (`replacePillSpan` "stops a zoom
against a neighbour that ends on a clip boundary, keeping its length".)