Skip to content

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

refactor(timeline): delete the duplicate answer, route the rest through the one mapping

EtienneLescot023f414946ba
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index d34184954..fbf7a2a2e 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -666,7 +666,12 @@ export function NewEditorShell() {
void enqueueTimelineWrite(async () => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- const next = dropTrimPillsByIds(doc.timeline.trimRanges, doc.timeline.clips, trimIds);
+ const next = dropTrimPillsByIds(
+ doc.timeline.trimRanges,
+ doc.timeline.clips,
+ trimIds,
+ doc.timeline.insertRanges ?? [],
+ );
if (next.length === doc.timeline.trimRanges.length) return;
await saveDocument(
{ ...doc, timeline: { ...doc.timeline, trimRanges: next } },
@@ -1085,7 +1090,7 @@ export function NewEditorShell() {
// (properties kept, position taken from the playhead).
if (sel.kind === "trim") {
const { coalescedTrimGroups } = await import("@/lib/ai-edition/timeline/trim-mapping");
- const group = coalescedTrimGroups(tl.trimRanges, tl.clips).find((g) =>
+ const group = coalescedTrimGroups(tl.trimRanges, tl.clips, tl.insertRanges ?? []).find((g) =>
g.ids.includes(sel.id),
);
if (!group) return;
diff --git a/src/components/ai-edition/v4/FloatingInspector.tsx b/src/components/ai-edition/v4/FloatingInspector.tsx
index 254da35f2..2700087af 100644
--- a/src/components/ai-edition/v4/FloatingInspector.tsx
+++ b/src/components/ai-edition/v4/FloatingInspector.tsx
@@ -990,7 +990,7 @@ function SelectionPane({ tl, onClose }: { tl: TimelineApi; onClose: () => void }
// the group's, not the clicked row's. Deleting no longer needs the same expansion here:
// `removeRegion` drops the whole pill for every kind (`dropTrimPillsByIds`), which is
// what this pane used to have to arrange for itself.
- const trimGroup = coalescedTrimGroups(tl.trimRanges, tl.clips).find((g) =>
+ const trimGroup = coalescedTrimGroups(tl.trimRanges, tl.clips, tl.insertRanges ?? []).find((g) =>
g.ids.includes(selection.id),
);
if (!trimGroup) return null;
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index 21e8ce26c..1a3b97fab 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -770,7 +770,7 @@ export function V4Timeline({
// the clip through a reorder for free, with no ruler arithmetic of its own.
const insertedWordsByClip = useMemo(() => {
const out = new Map<string, InsertedWordMark[]>();
- for (const mark of insertedWordMarks(tl.transcripts, clips)) {
+ for (const mark of insertedWordMarks(tl.transcripts, clips, tl.insertRanges ?? [])) {
const list = out.get(mark.clipId);
if (list) list.push(mark);
else out.set(mark.clipId, [mark]);
@@ -783,7 +783,11 @@ export function V4Timeline({
// coalesced into one pill. This is what makes growing a trim across a
// junction look like one continuously-growing pill instead of visibly
// splitting, aligning trims with how zoom/speed/annotation already behave.
- const trimPills: LanePill[] = coalescedTrimGroups(tl.trimRanges, clips).map((g) => ({
+ const trimPills: LanePill[] = coalescedTrimGroups(
+ tl.trimRanges,
+ clips,
+ tl.insertRanges ?? [],
+ ).map((g) => ({
id: g.ids[0],
kind: "trim",
start: g.start,
@@ -1009,7 +1013,7 @@ export function V4Timeline({
let ranges = ventilateTimelineSpanToTrims(s, en, clips);
if (ranges.length === 0) {
// Span sits in a gap / past the end: fall back to the nearest clip.
- const resolved = resolveTimelineSpanToTrim(s, en, clips);
+ const resolved = resolveTimelineSpanToTrim(s, en, clips, tl.insertRanges ?? []);
if (!resolved) return;
ranges = [resolved];
}
diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.ts
index 1ba70d480..c52ae3b6f 100644
--- a/src/lib/ai-edition/document/timeline.ts
+++ b/src/lib/ai-edition/document/timeline.ts
@@ -1161,9 +1161,12 @@ export function removeRegion(document: AxcutDocument, kind: RegionKind, id: stri
...document,
timeline: {
...document.timeline,
- trimRanges: dropTrimPillsByIds(document.timeline.trimRanges, document.timeline.clips, [
- id,
- ]),
+ trimRanges: dropTrimPillsByIds(
+ document.timeline.trimRanges,
+ document.timeline.clips,
+ [id],
+ document.timeline.insertRanges ?? [],
+ ),
},
};
case "speed": {
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 025b7c4a4..79e4340f6 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -371,7 +371,12 @@ export function useTimeline() {
// the trim at the wrong source position.
const playhead = playheadSec();
const end = playhead + durationSec;
- const resolved = resolveTimelineSpanToTrim(playhead, end, document.timeline.clips);
+ const resolved = resolveTimelineSpanToTrim(
+ playhead,
+ end,
+ document.timeline.clips,
+ document.timeline.insertRanges ?? [],
+ );
const asset =
document.assets.find((a) => a.id === document.project.primaryAssetId) ?? document.assets[0];
if (!resolved && !asset) return;
@@ -952,6 +957,7 @@ export function useTimeline() {
document.timeline.trimRanges,
document.timeline.clips,
trimIds,
+ document.timeline.insertRanges ?? [],
),
},
legacyEditor:
diff --git a/src/lib/ai-edition/timeline/insert-mapping.test.ts b/src/lib/ai-edition/timeline/insert-mapping.test.ts
index 137c66332..8e75b2d11 100644
--- a/src/lib/ai-edition/timeline/insert-mapping.test.ts
+++ b/src/lib/ai-edition/timeline/insert-mapping.test.ts
@@ -110,15 +110,11 @@ function doc(inserts: AxcutInsertRange[], over: Partial<AxcutDocument> = {}): Ax
}
describe("resolveInsertPlacement", () => {
- it("gives a recording insert its raw moment, through the clip that plays it", () => {
+ it("leaves a recording insert to `rulerInserts`, the one place that places one", () => {
+ // It used to answer with a raw second of its own, from a plain shift the clip's own
+ // insertions made wrong — a second, contradictory answer that nothing read.
const row = insert({ id: "i1", atSec: 3 });
- expect(resolveInsertPlacement(row, doc([row]))).toEqual({ lane: "recording", atRawSec: 3 });
- });
-
- it("resolves through the SECOND clip when that is the one holding the source moment", () => {
- // Two clips over one media at different source positions: source 22 is raw 8.
- const row = insert({ id: "i1", atSec: 22 });
- expect(resolveInsertPlacement(row, doc([row]))).toEqual({ lane: "recording", atRawSec: 8 });
+ expect(resolveInsertPlacement(row, doc([row]))).toBeNull();
});
it("leaves a voiceover insert UNPROJECTED, naming the take and a source second", () => {
diff --git a/src/lib/ai-edition/timeline/insert-mapping.ts b/src/lib/ai-edition/timeline/insert-mapping.ts
index b21f2d31c..66f2fdf0c 100644
--- a/src/lib/ai-edition/timeline/insert-mapping.ts
+++ b/src/lib/ai-edition/timeline/insert-mapping.ts
@@ -23,14 +23,7 @@
import { trackGroupId } from "../document/audioTracks";
import type { AxcutDocument, AxcutInsertRange } from "../schema";
-/** A pause in the film: the clip holds a frame at this raw moment. */
-export interface RecordingInsertPlacement {
- lane: "recording";
- /** Raw ruler second — insertions occupy zero raw time, so this is a point. */
- atRawSec: number;
-}
-
-/** A silence inside a take: no picture, no ruler growth of its own. */
+/** A silence inside a take: no picture of its own. */
export interface VoiceoverInsertPlacement {
lane: "voiceover";
/** The user-visible take, not one of its stored fragments. */
@@ -39,11 +32,12 @@ export interface VoiceoverInsertPlacement {
atSourceSec: number;
}
-export type InsertPlacement = RecordingInsertPlacement | VoiceoverInsertPlacement;
+export type InsertPlacement = VoiceoverInsertPlacement;
/**
- * Where this insertion belongs, or null when nothing carries it any more — a clip whose
- * source range no longer contains the moment, or a take that has been deleted.
+ * Which TAKE carries this insertion, or null — a recording's insertion, or a take that has
+ * been deleted. Only takes need answering here: an insertion in the film is placed by
+ * `rulerInserts`, which is the one definition of where an insertion sits on the timeline.
*
* The lane is read from the ASSET, never from the row: `kind: "audio"` is the only thing
* that distinguishes a take's transcript from the film's, and it is already the
@@ -54,28 +48,16 @@ export function resolveInsertPlacement(
document: AxcutDocument,
): InsertPlacement | null {
const asset = document.assets.find((a) => a.id === insert.assetId);
- if (asset?.kind === "audio") {
- // The first take drawing on this asset whose source window contains the moment.
- // Inclusive at both edges, matching `rulerInserts`: a pause sits at the END of the
- // word it follows, which is routinely a window's own boundary.
- for (const track of document.audioTracks ?? []) {
- if (track.kind !== "voiceover" || track.assetId !== insert.assetId) continue;
- const startSec = track.offsetMs / 1000;
- const endSec = startSec + Math.max(0, track.endMs - track.startMs) / 1000;
- if (insert.atSec < startSec || insert.atSec > endSec) continue;
- return { lane: "voiceover", trackGroupId: trackGroupId(track), atSourceSec: insert.atSec };
- }
- return null;
- }
-
- for (const clip of document.timeline.clips) {
- if (clip.assetId !== insert.assetId) continue;
- const sourceEnd = clip.sourceEndSec ?? Number.POSITIVE_INFINITY;
- if (insert.atSec < clip.sourceStartSec || insert.atSec > sourceEnd) continue;
- return {
- lane: "recording",
- atRawSec: clip.timelineStartSec + (insert.atSec - clip.sourceStartSec),
- };
+ if (asset?.kind !== "audio") return null;
+ // The first take drawing on this asset whose source window contains the moment.
+ // Inclusive at both edges, matching `rulerInserts`: an insertion sits at the END of the
+ // word it follows, which is routinely a window's own boundary.
+ for (const track of document.audioTracks ?? []) {
+ if (track.kind !== "voiceover" || track.assetId !== insert.assetId) continue;
+ const startSec = track.offsetMs / 1000;
+ const endSec = startSec + Math.max(0, track.endMs - track.startMs) / 1000;
+ if (insert.atSec < startSec || insert.atSec > endSec) continue;
+ return { lane: "voiceover", trackGroupId: trackGroupId(track), atSourceSec: insert.atSec };
}
return null;
}
diff --git a/src/lib/ai-edition/timeline/inserted-time.test.ts b/src/lib/ai-edition/timeline/inserted-time.test.ts
index 7114afc6d..b1037f94c 100644
--- a/src/lib/ai-edition/timeline/inserted-time.test.ts
+++ b/src/lib/ai-edition/timeline/inserted-time.test.ts
@@ -131,7 +131,7 @@ describe("insertedWordMarks", () => {
];
it("paints a word on a split boundary exactly once", () => {
- const marks = insertedWordMarks([{ assetId: "a1", words: [synth("w_edge", 5)] }], split);
+ const marks = insertedWordMarks([{ assetId: "a1", words: [synth("w_edge", 5)] }], split, []);
expect(marks).toHaveLength(1);
expect(marks[0]).toMatchObject({ clipId: "c2", atRawSec: 5 });
});
@@ -140,6 +140,7 @@ describe("insertedWordMarks", () => {
const marks = insertedWordMarks(
[{ assetId: "a1", words: [synth("early", 2), synth("late", 7)] }],
split,
+ [],
);
expect(marks.map((m) => [m.clipId, m.atRawSec])).toEqual([
["c1", 2],
@@ -149,17 +150,19 @@ describe("insertedWordMarks", () => {
it("keeps a word at the very end of the last clip", () => {
// Half-open everywhere but the tail, or the final word of a project vanishes.
- const marks = insertedWordMarks([{ assetId: "a1", words: [synth("w_end", 10)] }], split);
+ const marks = insertedWordMarks([{ assetId: "a1", words: [synth("w_end", 10)] }], split, []);
expect(marks.map((m) => m.wordId)).toEqual(["w_end"]);
});
it("ignores words nobody added", () => {
const spoken = { id: "w1", segmentId: "s", text: "w1", startSec: 2, endSec: 3 } as AxcutWord;
- expect(insertedWordMarks([{ assetId: "a1", words: [spoken] }], split)).toEqual([]);
+ expect(insertedWordMarks([{ assetId: "a1", words: [spoken] }], split, [])).toEqual([]);
});
it("ignores a transcript no clip draws on", () => {
- expect(insertedWordMarks([{ assetId: "other", words: [synth("w1", 2)] }], split)).toEqual([]);
+ expect(insertedWordMarks([{ assetId: "other", words: [synth("w1", 2)] }], split, [])).toEqual(
+ [],
+ );
});
});
diff --git a/src/lib/ai-edition/timeline/inserted-time.ts b/src/lib/ai-edition/timeline/inserted-time.ts
index ea0cb9489..8b3e7950e 100644
--- a/src/lib/ai-edition/timeline/inserted-time.ts
+++ b/src/lib/ai-edition/timeline/inserted-time.ts
@@ -204,6 +204,7 @@ export interface InsertedWordMark {
export function insertedWordMarks(
transcripts: ReadonlyArray<{ assetId: string; words: ReadonlyArray<AxcutWord> }>,
clips: readonly AxcutClip[],
+ insertRanges: readonly AxcutInsertRange[],
): InsertedWordMark[] {
const byAsset = new Map<string, ReadonlyArray<AxcutWord>>();
for (const transcript of transcripts) {
@@ -228,7 +229,7 @@ export function insertedWordMarks(
clipId: clip.id,
wordId: word.id,
text: word.text,
- atRawSec: clip.timelineStartSec + (word.startSec - clip.sourceStartSec),
+ atRawSec: sourceToTimelineSec(clip, word.startSec, insertRanges),
});
}
});
diff --git a/src/lib/ai-edition/timeline/sharedMediaTrim.test.ts b/src/lib/ai-edition/timeline/sharedMediaTrim.test.ts
index 118495949..4edd43902 100644
--- a/src/lib/ai-edition/timeline/sharedMediaTrim.test.ts
+++ b/src/lib/ai-edition/timeline/sharedMediaTrim.test.ts
@@ -85,7 +85,7 @@ describe("repro: trim on clip 2 of two clips sharing one media", () => {
]);
// 2. Ruler — one pill, over clip 2 (timeline 11.8 + 8.4 = 20.2 … 22.2).
- const pills = coalescedTrimGroups(next.timeline.trimRanges, next.timeline.clips);
+ const pills = coalescedTrimGroups(next.timeline.trimRanges, next.timeline.clips, []);
expect(pills).toHaveLength(1);
expect(pills[0].start).toBeCloseTo(20.2, 6);
expect(pills[0].end).toBeCloseTo(22.2, 6);
diff --git a/src/lib/ai-edition/timeline/timelineMap.ts b/src/lib/ai-edition/timeline/timelineMap.ts
index b27103d6a..e344c2398 100644
--- a/src/lib/ai-edition/timeline/timelineMap.ts
+++ b/src/lib/ai-edition/timeline/timelineMap.ts
@@ -96,6 +96,11 @@ export function anchoredToRawSpanSec(
): { startSec: number; endSec: number } | null {
const clip = clips.find((c) => c.id === fragment.clipId);
if (!clip) return null;
+ // ponytail: plain shift, wrong by the clip's own insertions for a region anchored past
+ // one — the pill is drawn early while `projectRegionsToSource` exports it on the right
+ // frames. Route through `sourceToTimelineSec` when zoom/annotation pills need to agree
+ // with the effect; it means threading the ranges through `anchorRegionsWithDerivedMs`
+ // and its 15 callers, which is its own change.
return {
startSec: clip.timelineStartSec + (fragment.sourceStartSec - clip.sourceStartSec),
endSec: clip.timelineStartSec + (fragment.sourceEndSec - clip.sourceStartSec),
diff --git a/src/lib/ai-edition/timeline/trim-mapping.test.ts b/src/lib/ai-edition/timeline/trim-mapping.test.ts
index 3ac5cba00..0e003351e 100644
--- a/src/lib/ai-edition/timeline/trim-mapping.test.ts
+++ b/src/lib/ai-edition/timeline/trim-mapping.test.ts
@@ -38,7 +38,7 @@ describe("trimToTimelineSpan", () => {
timelineEndSec: 42,
}),
];
- expect(trimToTimelineSpan({ assetId: "a", startSec: 5, endSec: 7 }, clips)).toEqual({
+ expect(trimToTimelineSpan({ assetId: "a", startSec: 5, endSec: 7 }, clips, [])).toEqual({
start: 5,
end: 7,
});
@@ -65,7 +65,7 @@ describe("trimToTimelineSpan", () => {
}),
];
// A trim at source 20..22 lives in c2 → timeline 14 + (20-16) = 18..20.
- expect(trimToTimelineSpan({ assetId: "a", startSec: 20, endSec: 22 }, clips)).toEqual({
+ expect(trimToTimelineSpan({ assetId: "a", startSec: 20, endSec: 22 }, clips, [])).toEqual({
start: 18,
end: 20,
});
@@ -73,8 +73,8 @@ describe("trimToTimelineSpan", () => {
it("returns null when no clip carries the trim's source region", () => {
const clips = [clip({ id: "c1", assetId: "a", sourceStartSec: 0, sourceEndSec: 10 })];
- expect(trimToTimelineSpan({ assetId: "a", startSec: 40, endSec: 42 }, clips)).toBeNull();
- expect(trimToTimelineSpan({ assetId: "b", startSec: 2, endSec: 4 }, clips)).toBeNull();
+ expect(trimToTimelineSpan({ assetId: "a", startSec: 40, endSec: 42 }, clips, [])).toBeNull();
+ expect(trimToTimelineSpan({ assetId: "b", startSec: 2, endSec: 4 }, clips, [])).toBeNull();
});
});
@@ -99,7 +99,7 @@ describe("resolveTimelineSpanToTrim", () => {
}),
];
// Timeline 18..20 falls in c2 (asset b) → source 16 + (18-14)=20 .. 22.
- expect(resolveTimelineSpanToTrim(18, 20, clips)).toEqual({
+ expect(resolveTimelineSpanToTrim(18, 20, clips, [])).toEqual({
assetId: "b",
clipId: "c2",
sourceStartSec: 20,
@@ -127,8 +127,8 @@ describe("resolveTimelineSpanToTrim", () => {
}),
];
// Start in c1 → asset a, start in c2 → asset b.
- expect(resolveTimelineSpanToTrim(2, 4, clips)?.assetId).toBe("a");
- expect(resolveTimelineSpanToTrim(20, 22, clips)?.assetId).toBe("b");
+ expect(resolveTimelineSpanToTrim(2, 4, clips, [])?.assetId).toBe("a");
+ expect(resolveTimelineSpanToTrim(20, 22, clips, [])?.assetId).toBe("b");
});
it("clamps the span to the carrier clip's extent (no straddling)", () => {
@@ -151,7 +151,7 @@ describe("resolveTimelineSpanToTrim", () => {
}),
];
// Span 10..20 starts in c1; end clamps to c1's end (timeline 14 → source 14).
- expect(resolveTimelineSpanToTrim(10, 20, clips)).toEqual({
+ expect(resolveTimelineSpanToTrim(10, 20, clips, [])).toEqual({
assetId: "a",
clipId: "c1",
sourceStartSec: 10,
@@ -178,7 +178,7 @@ describe("resolveTimelineSpanToTrim", () => {
timelineEndSec: 28,
}),
];
- const resolved = resolveTimelineSpanToTrim(18, 21, clips);
+ const resolved = resolveTimelineSpanToTrim(18, 21, clips, []);
expect(resolved).not.toBeNull();
if (!resolved) return;
const back = trimToTimelineSpan(
@@ -188,12 +188,13 @@ describe("resolveTimelineSpanToTrim", () => {
endSec: resolved.sourceEndSec,
},
clips,
+ [],
);
expect(back).toEqual({ start: 18, end: 21 });
});
it("returns null with no clips", () => {
- expect(resolveTimelineSpanToTrim(1, 2, [])).toBeNull();
+ expect(resolveTimelineSpanToTrim(1, 2, [], [])).toBeNull();
});
});
@@ -270,7 +271,9 @@ describe("coalescedTrimGroups", () => {
trim({ id: "t1", assetId: "a", startSec: 8, endSec: 14 }), // -> timeline 8..14
trim({ id: "t2", assetId: "a", startSec: 16, endSec: 22 }), // -> timeline 14..20
];
- expect(coalescedTrimGroups(trims, clips)).toEqual([{ ids: ["t1", "t2"], start: 8, end: 20 }]);
+ expect(coalescedTrimGroups(trims, clips, [])).toEqual([
+ { ids: ["t1", "t2"], start: 8, end: 20 },
+ ]);
});
it("groups two independently-created trims snapped to touching clip boundaries", () => {
@@ -299,7 +302,9 @@ describe("coalescedTrimGroups", () => {
trim({ id: "t1", assetId: "a", startSec: 7, endSec: 10 }), // -> timeline 7..10
trim({ id: "t2", assetId: "b", startSec: 0, endSec: 2 }), // -> timeline 10..12
];
- expect(coalescedTrimGroups(trims, clips)).toEqual([{ ids: ["t1", "t2"], start: 7, end: 12 }]);
+ expect(coalescedTrimGroups(trims, clips, [])).toEqual([
+ { ids: ["t1", "t2"], start: 7, end: 12 },
+ ]);
});
it("keeps a trim separated by a real gap in its own group", () => {
@@ -317,7 +322,7 @@ describe("coalescedTrimGroups", () => {
trim({ id: "t1", assetId: "a", startSec: 2, endSec: 4 }),
trim({ id: "t2", assetId: "a", startSec: 10, endSec: 12 }),
];
- expect(coalescedTrimGroups(trims, clips)).toEqual([
+ expect(coalescedTrimGroups(trims, clips, [])).toEqual([
{ ids: ["t1"], start: 2, end: 4 },
{ ids: ["t2"], start: 10, end: 12 },
]);
@@ -338,7 +343,7 @@ describe("coalescedTrimGroups", () => {
trim({ id: "gone", assetId: "b", startSec: 0, endSec: 2 }), // no clip carries asset b
trim({ id: "t1", assetId: "a", startSec: 3, endSec: 5 }),
];
- expect(coalescedTrimGroups(trims, clips)).toEqual([{ ids: ["t1"], start: 3, end: 5 }]);
+ expect(coalescedTrimGroups(trims, clips, [])).toEqual([{ ids: ["t1"], start: 3, end: 5 }]);
});
});
@@ -370,15 +375,17 @@ describe("two clips sharing one asset over the same source window", () => {
// Without the anchor this returned {3,5} — the first clip — because the loop
// stopped at the first clip whose asset and source window matched.
expect(
- trimToTimelineSpan({ assetId: "a", clipId: "c2", startSec: 3, endSec: 5 }, sharedClips()),
+ trimToTimelineSpan({ assetId: "a", clipId: "c2", startSec: 3, endSec: 5 }, sharedClips(), []),
).toEqual({ start: 15, end: 17 });
});
it("keeps mapping an un-anchored trim to the first matching clip (pre-v7 behaviour)", () => {
- expect(trimToTimelineSpan({ assetId: "a", startSec: 3, endSec: 5 }, sharedClips())).toEqual({
- start: 3,
- end: 5,
- });
+ expect(trimToTimelineSpan({ assetId: "a", startSec: 3, endSec: 5 }, sharedClips(), [])).toEqual(
+ {
+ start: 3,
+ end: 5,
+ },
+ );
});
it("draws one pill per clip when each clip carries its own trim", () => {
@@ -387,7 +394,7 @@ describe("two clips sharing one asset over the same source window", () => {
trim({ id: "t2", assetId: "a", clipId: "c2", startSec: 3, endSec: 5 }),
];
// Two pills, 12s apart — not one merged pill, and not two stacked on c1.
- expect(coalescedTrimGroups(trims, sharedClips())).toEqual([
+ expect(coalescedTrimGroups(trims, sharedClips(), [])).toEqual([
{ ids: ["t1"], start: 3, end: 5 },
{ ids: ["t2"], start: 15, end: 17 },
]);
@@ -397,7 +404,7 @@ describe("two clips sharing one asset over the same source window", () => {
// The twin still uses the same asset over the same source range, so an asset-only
// match would resurrect the cut on it.
const trims = [trim({ id: "orphan", assetId: "a", clipId: "c2", startSec: 3, endSec: 5 })];
- expect(coalescedTrimGroups(trims, [sharedClips()[0]])).toEqual([]);
+ expect(coalescedTrimGroups(trims, [sharedClips()[0]], [])).toEqual([]);
});
it("still shows a pill when the anchor clip was re-cut past the trim's start", () => {
@@ -414,7 +421,7 @@ describe("two clips sharing one asset over the same source window", () => {
}),
];
expect(
- trimToTimelineSpan({ assetId: "a", clipId: "c2", startSec: 3, endSec: 5 }, clips),
+ trimToTimelineSpan({ assetId: "a", clipId: "c2", startSec: 3, endSec: 5 }, clips, []),
).toEqual({ start: 0, end: 1 });
});
});
diff --git a/src/lib/ai-edition/timeline/trim-mapping.ts b/src/lib/ai-edition/timeline/trim-mapping.ts
index 0dee56d7d..701b575d0 100644
--- a/src/lib/ai-edition/timeline/trim-mapping.ts
+++ b/src/lib/ai-edition/timeline/trim-mapping.ts
@@ -14,7 +14,8 @@
// clip) share a coordinate space: without the anchor, "which clip is this cut on?"
// had no answer and each caller invented its own. See `trimAppliesToClip`.
-import type { AxcutClip, AxcutTrimRange } from "../schema";
+import type { AxcutClip, AxcutInsertRange, AxcutTrimRange } from "../schema";
+import { sourceToTimelineSec, timelineToSourceSec } from "./inserted-time";
import { type CoalescedSpan, ventilateSpanAcrossClips } from "./region-ventilation";
import { coalesceByIdentity, regionIdentityKey } from "./timelineMap";
@@ -68,6 +69,9 @@ export function trimAppliesToClip(
export function trimToTimelineSpan(
trim: TrimAnchor,
clips: AxcutClip[],
+ /** REQUIRED: a clip carrying insertions is longer than its source window, so this is not
+ * a plain shift (issue #560). */
+ insertRanges: readonly AxcutInsertRange[],
): { start: number; end: number } | null {
for (const c of clips) {
if (!trimAppliesToClip(trim, c)) continue;
@@ -78,7 +82,7 @@ export function trimToTimelineSpan(
: trim.startSec >= c.sourceStartSec && trim.startSec <= srcEnd;
if (carries) {
const map = (s: number) =>
- c.timelineStartSec + (Math.min(Math.max(s, c.sourceStartSec), srcEnd) - c.sourceStartSec);
+ sourceToTimelineSec(c, Math.min(Math.max(s, c.sourceStartSec), srcEnd), insertRanges);
return { start: map(trim.startSec), end: map(trim.endSec) };
}
}
@@ -140,11 +144,12 @@ export function ventilateTimelineSpanToTrims(
export function coalescedTrimGroups(
trimRanges: AxcutTrimRange[],
clips: AxcutClip[],
+ insertRanges: readonly AxcutInsertRange[],
epsilonSec?: number,
): CoalescedSpan[] {
const spans = trimRanges
.map((t) => {
- const mapped = trimToTimelineSpan(t, clips);
+ const mapped = trimToTimelineSpan(t, clips, insertRanges);
return mapped
? {
id: t.id,
@@ -183,10 +188,12 @@ export function resolveTrimPillIds(
trimRanges: AxcutTrimRange[],
clips: AxcutClip[],
id: string,
+ insertRanges: readonly AxcutInsertRange[],
epsilonSec?: number,
): string[] {
return (
- coalescedTrimGroups(trimRanges, clips, epsilonSec).find((g) => g.ids.includes(id))?.ids ?? [id]
+ coalescedTrimGroups(trimRanges, clips, insertRanges, epsilonSec).find((g) => g.ids.includes(id))
+ ?.ids ?? [id]
);
}
@@ -201,11 +208,14 @@ export function dropTrimPillsByIds(
trimRanges: AxcutTrimRange[],
clips: AxcutClip[],
ids: Iterable<string>,
+ insertRanges: readonly AxcutInsertRange[],
epsilonSec?: number,
): AxcutTrimRange[] {
const under = new Set<string>();
for (const id of ids) {
- for (const member of resolveTrimPillIds(trimRanges, clips, id, epsilonSec)) under.add(member);
+ for (const member of resolveTrimPillIds(trimRanges, clips, id, insertRanges, epsilonSec)) {
+ under.add(member);
+ }
}
if (under.size === 0) return trimRanges;
return trimRanges.filter((t) => !under.has(t.id));
@@ -225,6 +235,9 @@ export function resolveTimelineSpanToTrim(
startSec: number,
endSec: number,
clips: AxcutClip[],
+ /** REQUIRED: a clip carrying insertions is longer than its source window, so this is not
+ * a plain shift (issue #560). */
+ insertRanges: readonly AxcutInsertRange[],
): TrimSourceRange | null {
if (clips.length === 0) return null;
const lo = Math.min(startSec, endSec);
@@ -246,7 +259,7 @@ export function resolveTimelineSpanToTrim(
Math.min(lo, carrier.timelineStartSec + srcLen),
);
const tEnd = Math.max(tStart, Math.min(hi, carrier.timelineStartSec + srcLen));
- const toSrc = (t: number) => carrier.sourceStartSec + (t - carrier.timelineStartSec);
+ const toSrc = (t: number) => timelineToSourceSec(carrier, t, insertRanges).sourceSec;
return {
assetId: carrier.assetId,
clipId: carrier.id,
diff --git a/src/lib/ai-edition/timeline/voiceoverCut.test.ts b/src/lib/ai-edition/timeline/voiceoverCut.test.ts
index 00892553c..701b403ef 100644
--- a/src/lib/ai-edition/timeline/voiceoverCut.test.ts
+++ b/src/lib/ai-edition/timeline/voiceoverCut.test.ts
@@ -101,7 +101,7 @@ describe("a cut authored from the voiceover lane", () => {
[5, 6],
[20, 21],
]);
- expect(coalescedTrimGroups(rows, CLIPS)).toHaveLength(1);
+ expect(coalescedTrimGroups(rows, CLIPS, [])).toHaveLength(1);
expect(filmSec(rows)).toBeCloseTo(10, 6);
});
@@ -109,7 +109,7 @@ describe("a cut authored from the voiceover lane", () => {
const rows = cut(5, 7);
// Restoring must not leave half the cut behind, with the word still gone and
// nothing on the ruler to click.
- expect(dropTrimPillsByIds(rows, CLIPS, [rows[0].id])).toEqual([]);
+ expect(dropTrimPillsByIds(rows, CLIPS, [rows[0].id], [])).toEqual([]);
});
it("writes nothing where there is no film", () => {