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): give "is this anchored?" a single answer

EtienneLescot9309e926080e
diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.ts
index 68bf7d43c..27eaf0f0e 100644
--- a/src/lib/ai-edition/document/timeline.ts
+++ b/src/lib/ai-edition/document/timeline.ts
@@ -8,6 +8,7 @@ import {
anchoredToRawSpanSec,
anchorRegionsWithDerivedMs,
dropPillById,
+ hasCompleteClipAnchor,
} from "../timeline/timelineMap";
import { dropTrimPillsByIds, trimAppliesToClip } from "../timeline/trim-mapping";
import { createId } from "./ids";
@@ -27,17 +28,6 @@ export function byStart(a: { startSec: number }, b: { startSec: number }): numbe
return a.startSec - b.startSec;
}
-/** A region is anchored once it states WHERE IN THE SOURCE it lives. Anything missing
- * a part of `{clipId, sourceStartSec, sourceEndSec}` still relies on its RAW ms.
- * One definition, so "is this anchored?" can never be asked two different ways. */
-function isAnchored<T extends { clipId?: string; sourceStartSec?: number; sourceEndSec?: number }>(
- region: T,
-): region is T & { clipId: string; sourceStartSec: number; sourceEndSec: number } {
- return (
- !!region.clipId && region.sourceStartSec !== undefined && region.sourceEndSec !== undefined
- );
-}
-
export interface Interval {
startSec: number;
endSec: number;
@@ -299,7 +289,7 @@ export function rederiveRegionMs(document: AxcutDocument, clips: AxcutClip[]): A
const clipById = new Map(clips.map((c) => [c.id, c]));
return mapAllRegionCollections(document, (regions) =>
regions.flatMap((region) => {
- if (!isAnchored(region)) {
+ if (!hasCompleteClipAnchor(region)) {
return [region];
}
const clip = clipById.get(region.clipId);
@@ -426,7 +416,7 @@ export function applyProbedDuration(
// and so an already-correct anchor is never re-minted.
return mapAllRegionCollections(refreshed, (regions, prefix) =>
regions.flatMap((region) =>
- isAnchored(region)
+ hasCompleteClipAnchor(region)
? [region]
: (anchorRegionsWithDerivedMs([region], nextClips, () =>
createId(prefix),
@@ -507,7 +497,7 @@ function anchoredRegionsOf(document: AxcutDocument): Array<{ id: string; clipId:
];
return collections
.flat()
- .filter(isAnchored)
+ .filter(hasCompleteClipAnchor)
.map((region) => ({ id: region.id, clipId: region.clipId }));
}
@@ -725,16 +715,18 @@ function reconcileRegionsAfterReplace(
const clipById = new Map(clips.map((clip) => [clip.id, clip]));
return mapAllRegionCollections(document, (regions, prefix) =>
regions.flatMap((region) => {
- if (isAnchored(region) && surviving.has(region.clipId)) {
+ if (hasCompleteClipAnchor(region) && surviving.has(region.clipId)) {
const clip = clipById.get(region.clipId);
if (clip) return rederiveAnchoredRegion(region, clip, clips);
}
const reventilated = anchorRegionsWithDerivedMs([region], clips, () =>
createId(prefix),
) as StoredRegion[];
- const placed = reventilated.some((next) => isAnchored(next) && surviving.has(next.clipId));
+ const placed = reventilated.some(
+ (next) => hasCompleteClipAnchor(next) && surviving.has(next.clipId),
+ );
if (placed) return reventilated;
- return isAnchored(region) ? [] : reventilated;
+ return hasCompleteClipAnchor(region) ? [] : reventilated;
}),
);
}
@@ -935,12 +927,37 @@ export function removeClip(document: AxcutDocument, clipId: string): AxcutDocume
trimRanges: document.timeline.trimRanges.filter((t) => t.clipId !== clipId),
},
};
- const withoutRemovedRegions = mapAllRegionCollections(next, (regions) =>
- regions.filter((region) => !(isAnchored(region) && region.clipId === clipId)),
- );
- return newClips.length > 0
- ? rederiveRegionMs(withoutRemovedRegions, newClips)
- : withoutRemovedRegions;
+ // The asymmetry with the trim filter three lines up is deliberate, and the obvious
+ // "cleanup" that makes the two match reintroduces #249.
+ //
+ // A trim's complete anchor IS a bare `clipId` -- it carries its own `startSec`/
+ // `endSec` in source time (`trimAppliesToClip`), so the clip going away takes the
+ // trim with it. A region carrying only a `clipId` and no source range is NOT
+ // anchored (`hasCompleteClipAnchor`): it is still placed by its RAW ms, so the clip
+ // does not own it and deleting the clip must not delete it.
+ //
+ // The cost of keeping it, stated so it is a decision and not an accident: that
+ // region is now unreachable but immortal. With clips [0-10s] and [10-20s] and a bare
+ // `clipId` zoom at raw 12000-14000ms, deleting the second clip leaves the zoom off
+ // the end of a 10s ruler -- no pill to click, dropped by `projectRegionsToSource`,
+ // and re-emitted by every rederive. Hitting "Restore full timeline" then re-anchors
+ // it from those stale raw ms onto whatever footage now sits at 12-14s. That is the
+ // same treatment fully-unanchored legacy regions already get, and losing the user's
+ // region outright is the worse of the two.
+ //
+ // Only the last-clip case needs the filter spelled out. With survivors,
+ // `rederiveRegionMs` already drops every anchored region whose `clipId` is absent
+ // from the new clips -- a strict superset of "anchored to the one just removed" --
+ // so running both walked all four region collections twice and spread the document
+ // twice per delete. `rederiveRegionMs` bails on an empty clip list (a guard against
+ // a transient wipe deleting everything), which is why the empty case is handled
+ // here rather than left to it.
+ if (newClips.length === 0) {
+ return mapAllRegionCollections(next, (regions) =>
+ regions.filter((region) => !(hasCompleteClipAnchor(region) && region.clipId === clipId)),
+ );
+ }
+ return rederiveRegionMs(next, newClips);
}
export function restoreFullTimeline(document: AxcutDocument): AxcutDocument {
diff --git a/src/lib/ai-edition/timeline/timelineMap.ts b/src/lib/ai-edition/timeline/timelineMap.ts
index 2a00573a6..b2b0edd2e 100644
--- a/src/lib/ai-edition/timeline/timelineMap.ts
+++ b/src/lib/ai-edition/timeline/timelineMap.ts
@@ -416,8 +416,19 @@ interface RegionClipAnchor {
}
/** True when the anchor is usable on its own (all three parts present), i.e. the
- * region can be placed without consulting raw-virtual time at all. */
-function hasCompleteClipAnchor<T extends RegionClipAnchor>(
+ * region can be placed without consulting raw-virtual time at all. Anything missing
+ * a part of `{clipId, sourceStartSec, sourceEndSec}` still relies on its RAW ms.
+ *
+ * THE definition, so "is this anchored?" can never be asked two different ways. It
+ * used to be asked twice: the document layer had its own copy testing
+ * `sourceStartSec !== undefined`, which called a region carrying `null` anchored
+ * while this one called it unanchored. `null` survives any in-memory mutation that
+ * does not round-trip through zod, and the two answers sent the same region down
+ * two different paths -- `rederiveAnchoredRegion` rewrote it to `Math.max(null, ...)`
+ * i.e. the clip start, while the exporter went on using its raw ms. Preview and
+ * export disagreed. The `typeof` tests below are what make that unreachable, so
+ * keep them: `!== undefined` is not the same question. */
+export function hasCompleteClipAnchor<T extends RegionClipAnchor>(
region: T,
): region is T & Required<RegionClipAnchor> {
return (