Skip to content

Open source CapCut alternative.

TypeScript

fix(cut): ground the video track stack so the spine can never sit empty

David Le99b7c993183f
diff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.ts
index 358f05b2..aa5b7a12 100644
--- a/site/src/cut/lib/store.test.ts
+++ b/site/src/cut/lib/store.test.ts
@@ -101,7 +101,7 @@ describe("video placement", () => {
const a = asset(2);
useEditor.setState({
assets: [a],
- clips: [vclip({ track: 1, start: 1, out: 2 })],
+ clips: [vclip({ track: 0, start: 0, out: 2 }), vclip({ track: 1, start: 1, out: 2 })],
});
s().addVideoFromAsset(a.id, { kind: "track", track: 1 }, 1.5);
expectLaneSound(videoLane(1));
@@ -111,8 +111,9 @@ describe("video placement", () => {
test("adding onto a freshly inserted track keeps the requested start", () => {
const a = asset(2);
+ const spine = vclip({ track: 0, start: 0, out: 2 });
const resident = vclip({ track: 1, start: 1, out: 2 });
- useEditor.setState({ assets: [a], clips: [resident] });
+ useEditor.setState({ assets: [a], clips: [spine, resident] });
s().addVideoFromAsset(a.id, { kind: "insert", level: 1 }, 1.5);
const added = s().clips.find((c) => c.assetId === a.id)!;
expect(added.start).toBeCloseTo(1.5);
@@ -122,8 +123,9 @@ describe("video placement", () => {
test("dragging a track-0 clip up onto an occupied track slides it clear", () => {
const mover = vclip({ track: 0, start: 0, out: 2 });
+ const anchor = vclip({ track: 0, start: 2, out: 2 });
const resident = vclip({ track: 1, start: 1, out: 2 });
- useEditor.setState({ clips: [mover, resident] });
+ useEditor.setState({ clips: [mover, anchor, resident] });
s().dropVideoClip(mover.id, { kind: "track", track: 1 }, 1.2);
expect(clipById(mover.id).track).toBe(1);
expect(clipById(mover.id).start).toBeCloseTo(3);
@@ -313,3 +315,28 @@ describe("subtitle cues", () => {
expect(next.start).toBeCloseTo(0.5);
});
});
+
+describe("spine grounding", () => {
+ test("deleting the last track-0 clip grounds the layers above it", () => {
+ const spine = vclip({ track: 0, start: 0, out: 2 });
+ const layer = vclip({ track: 1, start: 2, out: 2 });
+ const upper = vclip({ track: 2, start: 2.5, out: 2 });
+ useEditor.setState({
+ clips: [spine, layer, upper],
+ selection: { kind: "clip", id: spine.id },
+ });
+ s().deleteSelection();
+ expect(clipById(layer.id).track).toBe(0);
+ expect(clipById(upper.id).track).toBe(1);
+ });
+
+ test("dragging the only track-0 clip up onto a layer grounds the stack", () => {
+ const mover = vclip({ track: 0, start: 0, out: 2 });
+ const resident = vclip({ track: 1, start: 1, out: 2 });
+ useEditor.setState({ clips: [mover, resident] });
+ s().dropVideoClip(mover.id, { kind: "track", track: 1 }, 1.2);
+ expect(clipById(mover.id).track).toBe(0);
+ expect(clipById(resident.id).track).toBe(0);
+ expectLaneSound(videoLane(0));
+ });
+});
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index da741ba5..4417f170 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -49,6 +49,15 @@ export const track0Clips = (clips: VideoClip[]) => clips.filter((c) => c.track =
/** Every video clip not on track 0 — the composited layers. */
export const overlayLayers = (clips: VideoClip[]) => clips.filter((c) => c.track !== 0);
+/** Ground a clip stack: the lowest occupied row becomes track 0. Deleting or
+ * dragging away the last track-0 clip re-grounds the rows above it, so the
+ * spine — the sequence that carries transitions, fades, and ripple — always
+ * exists while any clip does. */
+export function groundTracks(clips: VideoClip[]): VideoClip[] {
+ const lift = clips.length ? Math.min(...clips.map((c) => c.track)) : 0;
+ return lift > 0 ? clips.map((c) => ({ ...c, track: c.track - lift })) : clips;
+}
+
/** Open a slot at `level`, shifting the tracks at/above it up by one. `exclude`
* is the clip being placed (left untouched). Level 0 shifts the whole stack up:
* the placed clip becomes the new track 0 — the spine transplants to it. */
@@ -600,7 +609,20 @@ export async function runTranscription(projectId: string, spec: object): Promise
}
}
-export const useEditor = create<EditorState>((set, get) => {
+export const useEditor = create<EditorState>((baseSet, get) => {
+ // Every write that touches clips grounds the stack, so the spine invariant
+ // holds by construction — across deletes, drops, undo, and doc loads.
+ const set = (
+ partial:
+ | Partial<EditorState>
+ | ((s: EditorState) => Partial<EditorState>),
+ replace?: boolean
+ ) =>
+ baseSet((prev) => {
+ const next = typeof partial === "function" ? partial(prev) : partial;
+ return next.clips ? { ...next, clips: groundTracks(next.clips) } : next;
+ }, replace as false | undefined);
+
const snapshot = (): DocSnapshot => {
const { clips, audioClips, overlays, subtitles } = get();
return {