Open source CapCut alternative.
TypeScript
feat(site): unify timeline selection and grouped operations [rebuild]
David Le2bfe96c1a927
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.mdindex 1546d48b..c6ad0b16 100644--- a/docs/guides/cut/README.md+++ b/docs/guides/cut/README.md@@ -28,7 +28,7 @@ AI is hosted whatever the home: image, video, and voiceover generation and the aThe preview scrubs and plays from per-clip decoders held in memory. It shows a frame the moment it arrives and refines behind it, so the timeline always answers and a jump lands on a picture. Every cache is bounded by one memory budget sized to the machine. The performance guide holds the frame budget and how a change is measured.-Timeline and preview share selection: ⌘/Ctrl-click toggles items, and dragging moves them together with one undo step.+Timeline and preview share selection: ⌘/Ctrl-click or Shift-click toggles items, and dragging moves them together with one undo step. Items move independently until grouped, and groups save with the project.Guides draw over the preview from the button beside the timeline zoom: thirds, center, safe margins, the short-form keep-out zone where TikTok, Reels and Shorts draw their own UI or crop the sides of a 9:16 frame, and custom lines dragged into place on the preview. They save with the project, snap a dragged element to their edges, never export, and the assistant reads the safe area they leave when it places graphics.diff --git a/site/package.json b/site/package.jsonindex 90fcc502..751a8b26 100644--- a/site/package.json+++ b/site/package.json@@ -33,6 +33,9 @@"eval:cut-sync": "bun run scripts/eval-cut-sync.ts","eval:cut-edges": "bun run scripts/eval-cut-edges.ts","eval:cut-keys": "bun run scripts/eval-cut-keys.ts",+ "eval:cut-preview-selection": "bun run scripts/eval-cut-preview-selection.ts",+ "eval:cut-timeline-operations": "bun run scripts/eval-cut-timeline-operations.ts",+ "eval:cut-timeline-rows": "bun run scripts/eval-cut-timeline-rows.ts","eval:cut-export": "bun run scripts/eval-cut-export.ts"},"dependencies": {diff --git a/site/packages/effects-kit/src/types.ts b/site/packages/effects-kit/src/types.tsindex 44aa9a6f..58756c84 100644--- a/site/packages/effects-kit/src/types.ts+++ b/site/packages/effects-kit/src/types.ts@@ -33,11 +33,6 @@ export interface OverlayBase {hidden?: boolean;/** Group membership (select/move as one); assigned by the host's grouping. */groupId?: string;- /** The video clip whose frames this element rides: the editor keeps it over- * the same footage when that clip moves, retimes or splits, and leaves it- * where it is when the clip goes. Absent = the editor picks the clip under- * the element whenever it is placed; null = the element stays free. */- hostClipId?: string | null;/** Preset In / Out / Loop animation (see anim.ts); absent = static. */anim?: OverlayAnim;/** Keyframed pose track, seconds from the element's start (see keys.ts).@@ -210,7 +205,7 @@ export function stripDefaultOverlayKinds<T extends Overlay>(overlays: T[]): T[]* empty for an ungrouped element. The host supplies `newId` so id format* stays its business.*/-export function groupRemap(newId: () => string): (o: Overlay) => { groupId?: string } {+export function groupRemap(newId: () => string): (o: { groupId?: string }) => { groupId?: string } {const seen = new Map<string, string>();return (o) => {if (!o.groupId) return {};diff --git a/site/scripts/eval-cut-preview-selection.ts b/site/scripts/eval-cut-preview-selection.tsnew file mode 100644index 00000000..3cacd7fa--- /dev/null+++ b/site/scripts/eval-cut-preview-selection.ts@@ -0,0 +1,100 @@+#!/usr/bin/env bun+/** Real pointer gestures on the Preview component, with an in-memory project. */+import { strict as assert } from "node:assert";+import type { Page } from "playwright";+import { componentFixture } from "./lib/cut-eval/componentFixture";+import type { EditorState } from "../src/cut/lib/store";+import type { TextOverlay } from "../src/cut/lib/types";++declare global {+ interface Window {+ __cutDev: { useEditor: { getState(): EditorState; setState(state: Partial<EditorState>): void } };+ }+}++const { browser, page } = await componentFixture("previewSelectionFixture.tsx");++async function selected(page: Page) {+ return page.evaluate(() => window.__cutDev.useEditor.getState().multiSelection.map((s) => `${s?.kind}:${s?.id}`).sort());+}++type Modifier = "Meta" | "Control" | "Shift";+async function click(x: number, y: number, keys: Modifier[]) {+ if (process.platform === "darwin" && keys.includes("Control")) {+ // macOS translates native Ctrl-click to a context menu. Send the primary+ // pointer/click sequence Windows delivers, through the real DOM hit target.+ await page.evaluate(({ x, y, shiftKey }) => {+ const target = document.elementFromPoint(x, y)!;+ const init = { bubbles: true, cancelable: true, clientX: x, clientY: y, button: 0, ctrlKey: true, shiftKey };+ target.dispatchEvent(new PointerEvent("pointerdown", { ...init, buttons: 1, pointerId: 1, pointerType: "mouse" }));+ const released = document.elementFromPoint(x, y)!;+ released.dispatchEvent(new PointerEvent("pointerup", { ...init, pointerId: 1, pointerType: "mouse" }));+ released.dispatchEvent(new MouseEvent("click", init));+ }, { x, y, shiftKey: keys.includes("Shift") });+ } else {+ for (const key of keys) await page.keyboard.down(key);+ await page.mouse.click(x, y);+ for (const key of [...keys].reverse()) await page.keyboard.up(key);+ }+}++try {+ await page.goto("http://preview.localhost", { waitUntil: "domcontentloaded" });+ await page.waitForFunction(() => !!window.__cutDev);+ const modifiers: Modifier[][] = [["Meta"], ["Shift"], ["Meta", "Shift"], ["Control"], ["Control", "Shift"]];+ for (const keys of modifiers) {+ await page.evaluate(() => {+ const store = window.__cutDev.useEditor;+ const title = (id: string, x: number, y: number): TextOverlay => ({ id, text: id, start: 0, end: 4, x, y, size: 60, font: "sf", weight: 700, color: "#fff", shadow: false, plate: false });+ store.setState({+ playing: false, selection: null, multiSelection: [], assets: [], audioClips: [], transitions: [],+ overlays: [title("a", 0.25, 0.25), title("b", 0.75, 0.25)],+ clips: [{ id: "video", assetId: "fixture", track: 0, start: 0, in: 0, out: 4, muted: true }],+ subtitles: { ...store.getState().subtitles, cues: [{ id: "caption", start: 0, end: 4, text: "Caption" }], tracks: [{ x: 0.5, y: 0.75 }] },+ });+ store.getState().seek(1);+ });+ const a = page.locator('.overlay-item[data-preview-id="a"]');+ const b = page.locator('.overlay-item[data-preview-id="b"]');+ const caption = page.locator('.sub-caption[data-preview-id="caption"]');+ const clickAt = async (locator: typeof a, mods: typeof keys) => {+ const box = await locator.boundingBox();+ assert(box);+ await click(box.x + box.width / 2, box.y + box.height / 2, mods);+ };+ await a.waitFor({ state: "visible" });+ await clickAt(a, []);+ assert.deepEqual(await selected(page), ["overlay:a"]);+ await clickAt(b, keys);+ assert.deepEqual(await selected(page), ["overlay:a", "overlay:b"]);+ await clickAt(caption, keys);+ assert.deepEqual(await selected(page), ["cue:caption", "overlay:a", "overlay:b"]);+ const stage = await page.locator(".stage").boundingBox();+ assert(stage);+ await click(stage.x + 15, stage.y + 15, keys);+ assert.deepEqual(await selected(page), ["clip:video", "cue:caption", "overlay:a", "overlay:b"]);+ // Full-frame clip chrome covers titles; picking must still reach the title.+ await clickAt(b, keys);+ assert.deepEqual(await selected(page), ["clip:video", "cue:caption", "overlay:a"]);+ await clickAt(b, keys);+ assert.deepEqual(await selected(page), ["clip:video", "cue:caption", "overlay:a", "overlay:b"]);+ // Deselecting a clip removes its hit box before the generated click arrives.+ await click(stage.x + 15, stage.y + 15, keys);+ assert.deepEqual(await selected(page), ["cue:caption", "overlay:a", "overlay:b"]);+ const box = await a.boundingBox();+ assert(box);+ const before = await page.evaluate(() => window.__cutDev.useEditor.getState().overlays.map((o) => ({ x: o.x, y: o.y })));+ await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);+ await page.mouse.down();+ await page.mouse.move(box.x + box.width / 2 + 30, box.y + box.height / 2 + 20, { steps: 5 });+ await page.mouse.up();+ const after = await page.evaluate(() => window.__cutDev.useEditor.getState().overlays.map((o) => ({ x: o.x, y: o.y })));+ assert(after[0].x > before[0].x);+ assert(Math.abs((after[1].x - before[1].x) - (after[0].x - before[0].x)) < 1e-6);+ await page.evaluate(() => window.__cutDev.useEditor.getState().undo());+ assert.deepEqual(await page.evaluate(() => window.__cutDev.useEditor.getState().overlays.map((o) => ({ x: o.x, y: o.y }))), before);+ console.log(`PASS ${keys.join("+")}${process.platform === "darwin" && keys.includes("Control") ? " (Windows event sequence)" : ""}: add/remove titles, caption, full-frame video, overlapping chrome, group drag and undo`);+ }+} finally {+ await browser.close();+}diff --git a/site/scripts/eval-cut-timeline-operations.ts b/site/scripts/eval-cut-timeline-operations.tsnew file mode 100644index 00000000..d5ad32ec--- /dev/null+++ b/site/scripts/eval-cut-timeline-operations.ts@@ -0,0 +1,122 @@+#!/usr/bin/env bun+import { strict as assert } from "node:assert";+import { componentFixture } from "./lib/cut-eval/componentFixture";++const { browser, page } = await componentFixture("timelineOperationsFixture.tsx");+try {+ await page.context().grantPermissions(["clipboard-read", "clipboard-write"]);+ await page.goto("http://preview.localhost");+ await page.waitForFunction(() => !!window.__cutDev);+ await page.evaluate(() => {+ window.__cutDev.useEditor.setState({+ projectId: "operations-fixture", readOnly: false, playing: false, loaded: true,+ timelineH: 700, pxPerSec: 60, selection: null, multiSelection: [],+ assets: [{ id: "media", name: "still", type: "image", duration: 30, width: 64, height: 64, url: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64"><rect width="64" height="64" fill="pink"/></svg>' }],+ clips: [{ id: "video", assetId: "media", track: 0, start: 0, in: 0, out: 20, muted: true }],+ audioClips: [],+ overlays: [+ { id: "effect", kind: "effect", effect: "blur", amount: 0.7, start: 1, end: 3, lane: 0, x: 0.5, y: 0.5 },+ { id: "text", kind: "text", text: "Title", start: 1, end: 3, lane: 1, x: 0.5, y: 0.5, size: 48, color: "#fff", shadow: false, plate: false },+ { id: "rect", kind: "shape", shape: "rect", fill: "#aaff33", start: 1, end: 3, lane: 2, x: 0.5, y: 0.5, w: 0.3, h: 0.3 },+ { id: "sticker", kind: "sticker", assetId: "media", start: 1, end: 3, lane: 3, x: 0.5, y: 0.5, w: 0.3 },+ ],+ transitions: [{ id: "transition", style: "crossfade", start: 1, seconds: 0.8 }],+ });+ });+ for (const id of ["effect", "text", "rect", "sticker", "transition"]) {+ const kind = id === "transition" ? "transition" : "overlay";+ const item = page.locator(`[data-tl-sel="${kind}:${id}"]`);+ await item.click({ position: { x: 20, y: 10 } });+ await page.keyboard.press("Meta+c");+ // Actual browser clipboard flavor, followed by the shared paste handler.+ let html = "";+ for (let attempt = 0; attempt < 50 && !html; attempt++) {+ html = await page.evaluate(async (id) => {+ try {+ const items = await navigator.clipboard.read();+ const value = await (await items[0].getType("text/html")).text();+ const encoded = value.match(/data-donkeycut="([^"]+)"/)?.[1];+ return encoded && JSON.parse(atob(encoded)).items.some((item: { item: { id: string } }) => item.item.id === id) ? value : "";+ } catch { return ""; }+ }, id);+ if (!html) await new Promise((resolve) => setTimeout(resolve, 50));+ }+ assert(html.includes("data-donkeycut"));+ await page.mouse.move(800, 100);+ await page.evaluate(() => window.__cutDev.useEditor.getState().seek(10));+ await page.evaluate((html) => {+ const data = new DataTransfer(); data.setData("text/html", html);+ window.dispatchEvent(new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true }));+ }, html);+ await page.waitForFunction((id) => window.__cutDev.useEditor.getState().selection?.id !== id, id);+ const selection = await page.evaluate(() => window.__cutDev.useEditor.getState().selection!);+ assert.equal(selection.kind, kind);+ await page.locator(`[data-tl-sel="${kind}:${selection.id}"]`).waitFor();+ const pasted = await page.evaluate((sel) => {+ const s = window.__cutDev.useEditor.getState();+ return (sel.kind === "transition" ? s.transitions : s.overlays).find((x) => x.id === sel.id)!;+ }, selection);+ assert.equal(pasted.start, 10);+ if (id === "transition") {+ const split = page.getByRole("button", { name: "Split", exact: true });+ assert(await split.isDisabled());+ await page.keyboard.press("s");+ assert.equal(await page.evaluate(() => window.__cutDev.useEditor.getState().clips.length), 1);+ assert.equal(await page.evaluate(() => window.__cutDev.useEditor.getState().transitions.length), 2);+ }+ await page.evaluate(() => window.__cutDev.useEditor.getState().undo());+ assert.equal(await page.locator(`[data-tl-sel="${kind}:${selection.id}"]`).count(), 0);+ console.log(`PASS ${id}: click, system copy, paste, visible bar and undo`);+ }+ const bar = page.locator('[data-tl-sel="transition:transition"]');+ const box = await bar.boundingBox(); assert(box);+ await page.mouse.move(box.x + 20, box.y + 10);+ await page.mouse.down();+ await page.mouse.move(box.x + 200, box.y + 10, { steps: 8 });+ await page.mouse.up();+ const moved = await page.evaluate(() => window.__cutDev.useEditor.getState().transitions[0]);+ assert(Math.abs(moved.start - 4) < 0.02);+ await page.evaluate(() => window.__cutDev.useEditor.getState().undo());+ assert.equal(await page.evaluate(() => window.__cutDev.useEditor.getState().transitions[0].start), 1);+ console.log("PASS transition: shared pointer movement and undo");+ for (const side of ["l", "r"]) {+ const handle = bar.locator(`.tl-trim-${side}`);+ const rect = await handle.boundingBox(); assert(rect);+ await page.mouse.move(rect.x + rect.width / 2, rect.y + rect.height / 2);+ await page.mouse.down();+ await page.mouse.move(rect.x + rect.width / 2 + 12, rect.y + rect.height / 2, { steps: 6 });+ await page.mouse.up();+ const trimmed = await page.evaluate(() => window.__cutDev.useEditor.getState().transitions[0]);+ assert(Math.abs(trimmed.seconds - (side === "l" ? 0.6 : 1)) < 0.02);+ assert.equal(await page.evaluate(() => window.__cutDev.useEditor.getState().clips[0].start), 0);+ await page.evaluate(() => window.__cutDev.useEditor.getState().undo());+ console.log(`PASS transition: shared ${side} trim, independent video and undo`);+ }+ await page.evaluate(() => {+ const s = window.__cutDev.useEditor.getState();+ s.setMultiSelection([{ kind: "overlay", id: "effect" }, { kind: "transition", id: "transition" }]);+ });+ const group = page.getByRole("button", { name: "Group", exact: true });+ const ungroup = page.getByRole("button", { name: "Ungroup", exact: true });+ assert(await group.isEnabled());+ assert(await ungroup.isDisabled());+ await group.click();+ await page.waitForFunction(() => !!window.__cutDev.useEditor.getState().transitions[0].groupId);+ assert(await ungroup.isEnabled());+ await ungroup.click();+ await page.waitForFunction(() => !window.__cutDev.useEditor.getState().transitions[0].groupId);+ assert(await ungroup.isDisabled());+ await group.click();+ console.log("PASS group toolbar follows grouping and ungrouping a mixed selection");+ const grouped = await bar.boundingBox(); assert(grouped);+ await page.mouse.move(grouped.x + 20, grouped.y + 10);+ await page.mouse.down();+ await page.mouse.move(grouped.x + 80, grouped.y + 10, { steps: 8 });+ await page.mouse.up();+ const state = await page.evaluate(() => {+ const s = window.__cutDev.useEditor.getState();+ return { effect: s.overlays.find((o) => o.id === "effect")!.start, transition: s.transitions[0].start, video: s.clips[0].start };+ });+ assert.deepEqual(state, { effect: 2, transition: 2, video: 0 });+ console.log("PASS mixed effect/transition group moves together and leaves video in place");+} finally { await browser.close(); }diff --git a/site/scripts/eval-cut-timeline-rows.ts b/site/scripts/eval-cut-timeline-rows.tsnew file mode 100644index 00000000..cba0f736--- /dev/null+++ b/site/scripts/eval-cut-timeline-rows.ts@@ -0,0 +1,82 @@+#!/usr/bin/env bun+import { strict as assert } from "node:assert";+import { componentFixture } from "./lib/cut-eval/componentFixture";+import { NEW_ROW_PX } from "../src/cut/lib/laneTracks";++const { browser, page } = await componentFixture("timelineRowsFixture.tsx");+try {+ await page.goto("http://preview.localhost");+ await page.waitForFunction(() => !!window.__cutDev);+ for (const kind of ["sticker", "shape", "effect"]) for (const side of ["above", "below", "existing", "empty"] as const) {+ await page.evaluate((empty) => {+ const store = window.__cutDev.useEditor;+ store.setState({+ projectId: "row-fixture", playing: false,+ clips: [{ id: "video", assetId: "sticker-asset", track: 0, start: 0, in: 0, out: 20, muted: true }],+ audioClips: [], overlays: [], transitions: [], selection: null, multiSelection: [], timelineH: 500, pxPerSec: 60,+ assets: [{ id: "sticker-asset", name: "sticker", type: "image", origin: "sticker", duration: 3, width: 64, height: 64, url: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64"><rect width="64" height="64" fill="pink"/></svg>' }],+ });+ if (!empty) {+ store.getState().addOverlay({ at: 0, lane: 0 });+ store.getState().addShape("rect", { at: 0, lane: 1 });+ }+ store.getState().select(null);+ }, side === "empty");+ await page.evaluate(() => new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))));+ const before = await page.evaluate(() => window.__cutDev.useEditor.getState().overlays);+ const band = side === "empty" ? null : await page.locator("[data-tl-trows]").boundingBox();+ const video = await page.locator("[data-tl-vrow]").first().boundingBox();+ assert(video);+ const x = video.x + 400;+ const y = side === "empty" ? video.y + 30 : side === "above" ? band!.y - NEW_ROW_PX - 2 : side === "below" ? band!.y + band!.height + NEW_ROW_PX + 2 : band!.y + band!.height / 4;+ const data = await page.evaluateHandle(() => new DataTransfer());+ await page.locator(`[data-source="${kind}"]`).dispatchEvent("dragstart", { dataTransfer: data });+ const hover = async () => {+ await page.evaluate(({ x, y, data }) => document.elementFromPoint(x, y)!.dispatchEvent(new DragEvent("dragover", { bubbles: true, cancelable: true, clientX: x, clientY: y, dataTransfer: data })), { x, y, data });+ await page.evaluate(() => new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))));+ };+ await hover();+ await hover(); // The row that opens must retain the same landing.+ const rows = await page.locator("[data-tl-trows]").boundingBox();+ assert(rows);+ if (side === "above" || side === "below") assert(rows.height > band!.height);+ await page.evaluate(({ x, y, data }) => document.elementFromPoint(x, y)!.dispatchEvent(new DragEvent("drop", { bubbles: true, cancelable: true, clientX: x, clientY: y, dataTransfer: data })), { x, y, data });+ await page.waitForFunction((count) => window.__cutDev.useEditor.getState().overlays.length === count, before.length + 1);+ const after = await page.evaluate(() => window.__cutDev.useEditor.getState().overlays);+ const added = after.find((o) => !before.some((b) => b.id === o.id))!;+ assert.equal(added.kind, kind);+ assert.equal(added.lane, side === "below" ? 2 : 0);+ for (const original of before) assert.equal(after.find((o) => o.id === original.id)?.lane, (original.lane ?? 0) + (side === "above" ? 1 : 0));+ await page.evaluate(() => window.__cutDev.useEditor.getState().undo());+ assert.deepEqual(await page.evaluate(() => window.__cutDev.useEditor.getState().overlays), before);+ await data.dispose();+ console.log(`PASS ${kind}: ${side} row, stable preview, drop and undo`);+ }+ for (const side of ["above", "below"]) {+ const id = await page.evaluate(() => {+ const store = window.__cutDev.useEditor;+ store.setState({ overlays: [], selection: null, multiSelection: [] });+ store.getState().addOverlay({ at: 0, lane: 0 });+ store.getState().addSticker({ assetId: "sticker-asset", at: 0, lane: 1 });+ store.getState().addSticker({ assetId: "sticker-asset", at: 5, lane: 1 });+ return store.getState().selection!.id;+ });+ const bar = await page.locator(`[data-tl-sel="overlay:${id}"]`).boundingBox();+ const band = await page.locator("[data-tl-trows]").boundingBox();+ assert(bar && band);+ const before = await page.evaluate(() => window.__cutDev.useEditor.getState().overlays);+ const x = bar.x + bar.width / 2;+ const y = side === "above" ? band.y - NEW_ROW_PX - 2 : band.y + band.height + NEW_ROW_PX + 2;+ await page.mouse.move(x, bar.y + bar.height / 2);+ await page.mouse.down();+ await page.mouse.move(x, y, { steps: 8 });+ await page.mouse.up();+ const after = await page.evaluate(() => window.__cutDev.useEditor.getState().overlays);+ assert.equal(after.find((o) => o.id === id)?.lane, side === "above" ? 0 : 2);+ await page.evaluate(() => window.__cutDev.useEditor.getState().undo());+ assert.deepEqual(await page.evaluate(() => window.__cutDev.useEditor.getState().overlays), before);+ console.log(`PASS existing sticker: drag to new row ${side} and undo`);+ }+} finally {+ await browser.close();+}diff --git a/site/scripts/lib/cut-eval/componentFixture.ts b/site/scripts/lib/cut-eval/componentFixture.tsnew file mode 100644index 00000000..f5ee1cc0--- /dev/null+++ b/site/scripts/lib/cut-eval/componentFixture.ts@@ -0,0 +1,29 @@+import { strict as assert } from "node:assert";+import { readFile } from "node:fs/promises";+import path from "node:path";+import postcss from "postcss";+import tailwind from "@tailwindcss/postcss";+import { chromium } from "playwright";++/** Mount real editor components and styles with an in-memory document. */+export async function componentFixture(entrypoint: string) {+ const site = path.resolve(import.meta.dir, "../../..");+ const build = await Bun.build({+ entrypoints: [path.join(import.meta.dir, entrypoint)],+ target: "browser", define: { "process.env.NODE_ENV": JSON.stringify("development") },+ });+ assert(build.success, build.logs.join("\n"));+ const js = await build.outputs.find((o) => o.path.endsWith(".js"))!.text();+ const from = path.join(site, "src/app/globals.css");+ const css = await postcss([tailwind({ base: site })]).process(await readFile(from, "utf8"), { from });+ const browser = await chromium.launch({ channel: "chrome", headless: true });+ const page = await browser.newPage({ viewport: { width: 1600, height: 1000 } });+ page.on("pageerror", (error) => console.error(error));+ await page.route("http://preview.localhost/**", (route) => {+ const resource = new URL(route.request().url()).pathname;+ if (resource === "/fixture.js") return route.fulfill({ contentType: "text/javascript", body: js });+ if (resource === "/fixture.css") return route.fulfill({ contentType: "text/css", body: css.css });+ return route.fulfill({ contentType: "text/html", body: '<html><head><link rel="stylesheet" href="/fixture.css"><style>#root{height:900px;display:grid}</style></head><body><div id="root"></div><script type="module" src="/fixture.js"></script></body></html>' });+ });+ return { browser, page };+}diff --git a/site/scripts/lib/cut-eval/previewSelectionFixture.tsx b/site/scripts/lib/cut-eval/previewSelectionFixture.tsxnew file mode 100644index 00000000..882fdb32--- /dev/null+++ b/site/scripts/lib/cut-eval/previewSelectionFixture.tsx@@ -0,0 +1,6 @@+import { createRoot } from "react-dom/client";+import { Preview } from "@/cut/components/Preview";+import { useEditor } from "@/cut/lib/store";++window.__cutDev = { useEditor };+createRoot(document.getElementById("root")!).render(<Preview />);diff --git a/site/scripts/lib/cut-eval/timelineOperationsFixture.tsx b/site/scripts/lib/cut-eval/timelineOperationsFixture.tsxnew file mode 100644index 00000000..e905e599--- /dev/null+++ b/site/scripts/lib/cut-eval/timelineOperationsFixture.tsx@@ -0,0 +1,24 @@+import { createRoot } from "react-dom/client";+import { Timeline } from "@/cut/components/Timeline";+import { useEditor } from "@/cut/lib/store";+import { copyTimelineSelection, pasteCutPayload, payloadFromHtml } from "@/cut/lib/cutClipboard";+import { EMPTY_LIBRARY } from "@/cut/lib/assetRef";+import { previewAt } from "@/cut/lib/playhead";++window.__cutDev = { useEditor };+window.addEventListener("keydown", (event) => {+ const s = useEditor.getState();+ if ((event.metaKey || event.ctrlKey) && event.key === "c") {+ if (copyTimelineSelection()) event.preventDefault();+ } else if (event.key === "s") s.splitAtPlayhead();+});+window.addEventListener("paste", (event) => {+ const s = useEditor.getState();+ const payload = payloadFromHtml(event.clipboardData?.getData("text/html"));+ event.preventDefault();+ if (payload && s.projectId) void pasteCutPayload(payload, { projectId: s.projectId, at: previewAt(), library: EMPTY_LIBRARY });+ else s.paste();+});+createRoot(document.getElementById("root")!).render(+ <div className="flex h-full flex-col justify-end"><Timeline /></div>+);diff --git a/site/scripts/lib/cut-eval/timelineRowsFixture.tsx b/site/scripts/lib/cut-eval/timelineRowsFixture.tsxnew file mode 100644index 00000000..5f111548--- /dev/null+++ b/site/scripts/lib/cut-eval/timelineRowsFixture.tsx@@ -0,0 +1,16 @@+import { createRoot } from "react-dom/client";+import { Timeline } from "@/cut/components/Timeline";+import { useEditor } from "@/cut/lib/store";+import { setAssetDragData, setElementDragData } from "@/cut/lib/assetDrag";++window.__cutDev = { useEditor };+createRoot(document.getElementById("root")!).render(+ <div className="flex h-full flex-col justify-end">+ <div className="flex h-24 gap-8">+ <div data-source="sticker" draggable onDragStart={(e) => setAssetDragData(e, "sticker-asset")}>Sticker</div>+ <div data-source="shape" draggable onDragStart={(e) => setElementDragData(e, { kind: "shape", shape: "rect" })}>Shape</div>+ <div data-source="effect" draggable onDragStart={(e) => setElementDragData(e, { kind: "effect", effect: "blur" })}>Effect</div>+ </div>+ <Timeline />+ </div>+);diff --git a/site/src/cut/components/Editor.tools.ts b/site/src/cut/components/Editor.tools.tsindex 056b43cc..26d6dded 100644--- a/site/src/cut/components/Editor.tools.ts+++ b/site/src/cut/components/Editor.tools.ts@@ -5,9 +5,19 @@* handlers on `EditorToolName`.*/-import { obj, type AiToolDef } from "@/cut/lib/aiToolDef";+import { num, obj, type AiToolDef } from "@/cut/lib/aiToolDef";export const EDITOR_TOOLS = [+ {+ name: "copy_selection",+ description: "Copy selected timeline items and their groups, including effects, transitions, captions, video, audio and overlay elements. Preserve all properties and relative timing.",+ inputSchema: obj({}),+ },+ {+ name: "paste_selection",+ description: "Paste copies of the timeline clipboard at the playhead or at seconds. Adds fresh items, preserving properties, grouping and relative timing. An occupied row moves the set to the next free position.",+ inputSchema: obj({ at: num("Timeline start seconds; defaults to the playhead") }),+ },{name: "undo",description: "Undo the last edit (unlimited).",diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsxindex b8a98aea..ffde466d 100644--- a/site/src/cut/components/Editor.tsx+++ b/site/src/cut/components/Editor.tsx@@ -21,15 +21,15 @@ import {renderPreviewProxy,type ExportDoc,} from "@/cut/lib/exportClient";-import { EMPTY_LIBRARY, fileZoneAt, hasRefDrag, parseMentions, refCandidatesOf, selectionRefTokens } from "@/cut/lib/assetRef";+import { EMPTY_LIBRARY, fileZoneAt, hasRefDrag, parseMentions, refCandidatesOf } from "@/cut/lib/assetRef";import { placeRefAtPlayhead } from "@/cut/lib/refPlace";-import { pasteCutPayload, payloadAssets, payloadFromHtml, writeCutClipboard } from "@/cut/lib/cutClipboard";+import { copyTimelineSelection, pasteCutPayload, payloadFromHtml } from "@/cut/lib/cutClipboard";import { useLibrary } from "@/cut/lib/queries";import { copyableRefs } from "@/cut/lib/refCopy";import { startUpload } from "@/cut/lib/importQueue";import { enrichAsset, importFileToProject, isFontFile, isMediaFile, prepareImport } from "@/cut/lib/media";import { uploadLibraryFont } from "@/cut/lib/linkedLibrary";-import { clearCopiedFrame, copiedFrameFile, hasCopiedFrame } from "@/cut/lib/stageFrame";+import { copiedFrameFile, hasCopiedFrame } from "@/cut/lib/stageFrame";// Side-effect import: registers the brief-to-video resume subscription, so a// persisted run resumes on project load even when the AI panel never mounts.import "@/cut/lib/genScene";@@ -1155,39 +1155,12 @@ export function Editor({// same keystroke. The timeline clipboard keeps its last timeline// copy, so ⌘V still pastes what was copied for the timeline.if (copyableRefs().length > 0) return;- const token = selectionRefTokens(s);- // The newer copy owns the clipboard: a copied preview frame steps- // aside for this one, and so does a file copied from another app —- // the system clipboard takes the token, or nothing at all, so a- // paste finds the timeline copy first.- const copied = s.copySelection();- if (copied) clearCopiedFrame();- if (copied || token) {- // The copy leaves the tab whole: the items and the assets they- // play, so ⌘V in another tab or another project lands them.- const items = copied ? s.copiedItems() : [];- const payload =- copied && s.projectId- ? { v: 1 as const, projectId: s.projectId, items, assets: payloadAssets(items, s.assets) }- : null;- void writeCutClipboard(token ?? "", payload).catch(() => {});- e.preventDefault();- }+ if (copyTimelineSelection()) e.preventDefault();}} else if (mod && e.key.toLowerCase() === "g") {- // ⌘G groups the multi-selected elements, ⇧⌘G dissolves the primary's- // group — the panel's Group button is out of reach while a- // multi-selection keeps the inspector closed.e.preventDefault();- if (e.shiftKey) {- const gid =- s.selection?.kind === "overlay"- ? s.overlays.find((o) => o.id === s.selection!.id)?.groupId- : undefined;- if (gid) s.ungroupOverlays(gid);- } else {- s.groupSelectedOverlays();- }+ if (e.shiftKey) s.ungroupSelection();+ else s.groupSelection();} else if (mod && e.key.toLowerCase() === "j") {e.preventDefault();s.setAiOpen(!s.aiOpen);diff --git a/site/src/cut/components/Inspector.tools.ts b/site/src/cut/components/Inspector.tools.tsindex 804d57e2..df26cff8 100644--- a/site/src/cut/components/Inspector.tools.ts+++ b/site/src/cut/components/Inspector.tools.ts@@ -74,9 +74,6 @@ export const INSPECTOR_TOOLS = [id: str("Overlay element id"),name: str("What the person calls this element — shown on its timeline chip and inspector title (empty string clears it, back to what it is)"),text: str("New text (titles)"),- follows_clip: bool(- "Whether the element rides the clip under it (on by default): true homes it to that clip, false frees it where it is"- ),start: num("Start s"),end: num("End s"),x: num("Center x 0..1"),diff --git a/site/src/cut/components/Inspector.tsx b/site/src/cut/components/Inspector.tsxindex 93d311f6..f3d7017b 100644--- a/site/src/cut/components/Inspector.tsx+++ b/site/src/cut/components/Inspector.tsx@@ -2214,8 +2214,6 @@ function TextPanel({ overlay: o }: { overlay: TextOverlay }) {</Section><OverlayMaskSection overlay={o} /><TransformRows overlay={o} />- <FollowRow overlay={o} />- <GroupRow overlay={o} /></div></>);@@ -2721,52 +2719,6 @@ function AnimationToolbar({ overlay: o, slot }: { overlay: Overlay; slot: AnimSl);}-/** Group / Ungroup, shown when the selection can form one or the element is- * in one. Grouping is shallow by design: select-one-selects-all, and gestures- * apply member-relative deltas. */-/** Whether the element rides the clip under it. On, the store homes it to- * that clip and carries it with the clip's frames; off, it stays free. */-function FollowRow({ overlay: o }: { overlay: Overlay }) {- return (- <Row- label="Follows clip"- info="The element moves and retimes with the frames of the video clip under it. Off keeps it where it is whatever the clip does."- >- <Switch- checked={o.hostClipId !== null}- onCheckedChange={(v) => useEditor.getState().setOverlayFollows(o.id, v)}- />- </Row>- );-}--function GroupRow({ overlay: o }: { overlay: Overlay }) {- const selectedOverlays = useEditor(- (s) => s.multiSelection.filter((x) => x?.kind === "overlay").length- );- if (o.groupId) {- return (- <Row label="Group">- <Button- size="sm"- variant="outline"- onClick={() => useEditor.getState().ungroupOverlays(o.groupId!)}- >- Ungroup- </Button>- </Row>- );- }- if (selectedOverlays < 2) return null;- return (- <Row label="Group">- <Button size="sm" variant="outline" onClick={() => useEditor.getState().groupSelectedOverlays()}>- Group {selectedOverlays} elements- </Button>- </Row>- );-}-/** Rotation and opacity, shared by every overlay kind. Neutral values store* as absence so untouched elements carry nothing. *//** The element twin of the clip panels' Hidden row: parked on the timeline,@@ -3895,8 +3847,6 @@ function ShapePanel({ overlay: o }: { overlay: ShapeOverlay }) {)}<OverlayMaskSection overlay={o} /><TransformRows overlay={o} />- <FollowRow overlay={o} />- <GroupRow overlay={o} /></div></>);@@ -3938,8 +3888,6 @@ function EffectPanel({ overlay: o }: { overlay: EffectOverlay }) {</Row>)}<HiddenRow overlay={o} />- <FollowRow overlay={o} />- <GroupRow overlay={o} /></div></>);@@ -4117,8 +4065,6 @@ function StickerPanel({ overlay: o }: { overlay: StickerOverlay }) {</Row><OverlayMaskSection overlay={o} /><TransformRows overlay={o} />- <FollowRow overlay={o} />- <GroupRow overlay={o} /></div></>);diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsxindex 26a15dc9..d1b0987c 100644--- a/site/src/cut/components/OverlayLayer.tsx+++ b/site/src/cut/components/OverlayLayer.tsx@@ -5,7 +5,7 @@ import { createContext, Fragment, useCallback, useContext, useEffect, useLayoutEimport { createPortal } from "react-dom";import { releaseAnimRest, useAnimPreview } from "@/cut/lib/animPreview";import { startDrag } from "@/cut/lib/drag";-import { startSelectionDrag } from "@/cut/components/previewSelectionDrag";+import { startSelectionDrag, togglePreviewSelection } from "@/cut/components/previewSelectionDrag";import { useSkim, usePreviewTime } from "@/cut/lib/playhead";import { useEditor } from "@/cut/lib/store";import {@@ -488,12 +488,7 @@ function SubtitleCaption({onPointerDown={(e) => {if (e.button !== 0) return;const s = useEditor.getState();- if (e.metaKey || e.ctrlKey) {- e.preventDefault();- e.stopPropagation();- s.toggleSelect({ kind: "cue", id: cue.id });- return;- }+ if (togglePreviewSelection(e, { kind: "cue", id: cue.id })) return;if (armed && startSelectionDrag(e, stageWidth, stageHeight)) return;if (!armed && stagePress) {stagePress(e, () => s.select({ kind: "cue", id: cue.id }));@@ -683,19 +678,17 @@ function OverlayItem({const liftChrome = !!chromeHost && selected && !editing;const [chromeSize, setChromeSize] = useState<{ w: number; h: number } | null>(null);useLayoutEffect(() => {- if (!liftChrome) {- // Cleared so the next selection starts from the in-box chrome and the- // portal twin never paints at a stale size.- setChromeSize(null);- return;- }+ if (!liftChrome) return;const el = boxRef.current;if (!el) return;const read = () => setChromeSize({ w: el.offsetWidth, h: el.offsetHeight });read();const ro = new ResizeObserver(read);ro.observe(el);- return () => ro.disconnect();+ return () => {+ ro.disconnect();+ setChromeSize(null);+ };}, [liftChrome]);// The box carries position, rotation, and opacity; the content wrapper@@ -1036,12 +1029,7 @@ function OverlayItem({const beginMove = (e: React.PointerEvent) => {if (editing || e.button !== 0) return;const s = useEditor.getState();- if (e.metaKey || e.ctrlKey) {- e.preventDefault();- e.stopPropagation();- s.toggleSelect({ kind: "overlay", id: o.id });- return;- }+ if (togglePreviewSelection(e, { kind: "overlay", id: o.id })) return;if (armed && startSelectionDrag(e, stageWidth, stageHeight)) return;// Nothing moves until it is the selection: an unselected element is part// of the picture, so the stage pans under it and a stationary press picksdiff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsxindex 5c68d967..52e842ea 100644--- a/site/src/cut/components/Preview.tsx+++ b/site/src/cut/components/Preview.tsx@@ -4,7 +4,7 @@ import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, typimport { Check, Copy, Loader2, PanelTopClose, ZoomIn, ZoomOut } from "lucide-react";import { usePlayback } from "@/cut/hooks/usePlayback";import { startDrag } from "@/cut/lib/drag";-import { startSelectionDrag } from "@/cut/components/previewSelectionDrag";+import { isPreviewSelectionModifier, startSelectionDrag, togglePreviewSelection } from "@/cut/components/previewSelectionDrag";import { useBrushUi } from "@/cut/lib/removal/brushUi";import {playheadAt,@@ -506,7 +506,7 @@ export function Preview() {for (const c of s.clips) {if (c.hidden) continue;const r = rectOf(c);- if (isFullRect(r) && !e.metaKey && !e.ctrlKey) continue;+ if (isFullRect(r) && !isPreviewSelectionModifier(e)) continue;const len = clipLen(c);if (t < c.start || t >= c.start + len) continue;const pose = clipKeyed(c) ? clipPoseAt(c, t - c.start) : null;@@ -533,8 +533,8 @@ export function Preview() {onPointerDownCapture={(e) => {// Selection chrome can cover another item. Modifier picking follows// the visible elements inside the stage.- if (e.button === 0 && (e.metaKey || e.ctrlKey) &&- (e.target as HTMLElement).closest("[data-preview-clip], [data-preview-kind]") &&+ if (e.button === 0 && isPreviewSelectionModifier(e) &&+ (e.target as HTMLElement).closest(".stage, [data-preview-clip], [data-preview-kind]") &&!(e.target as HTMLElement).closest("[title], [contenteditable=true]")) {const element = document.elementsFromPoint(e.clientX, e.clientY).map((node) => node.closest<HTMLElement>(".overlay-item[data-preview-kind], .sub-caption[data-preview-kind]"))@@ -543,9 +543,7 @@ export function Preview() {const id = element?.dataset.previewId;const clipId = !id ? clipAtPoint(e) : null;if ((id && (kind === "overlay" || kind === "cue")) || clipId) {- e.preventDefault();- e.stopPropagation();- useEditor.getState().toggleSelect(id && (kind === "overlay" || kind === "cue")+ togglePreviewSelection(e, id && (kind === "overlay" || kind === "cue")? { kind, id } : { kind: "clip", id: clipId! });return;}@@ -624,8 +622,7 @@ export function Preview() {if (hit) {const s = useEditor.getState();const item = { kind: "clip" as const, id: hit };- if (e.metaKey || e.ctrlKey) s.toggleSelect(item);- else s.select(item);+ s.select(item);}}}}@@ -1006,12 +1003,7 @@ function ClipTransformGizmo({ stage, selectedClip }: { stage: Stage; selectedCli// partly off screen — as long as a sliver stays inside to grab.const onMoveBox = (e: React.PointerEvent) => {if (e.button !== 0) return;- if (e.metaKey || e.ctrlKey) {- e.preventDefault();- e.stopPropagation();- st().toggleSelect({ kind: "clip", id: clip.id });- return;- }+ if (togglePreviewSelection(e, { kind: "clip", id: clip.id })) return;if (startSelectionDrag(e, stage.w, stage.h)) return;surfaceDrag(e,diff --git a/site/src/cut/components/Timeline.tools.ts b/site/src/cut/components/Timeline.tools.tsindex e3863caa..13810cf4 100644--- a/site/src/cut/components/Timeline.tools.ts+++ b/site/src/cut/components/Timeline.tools.ts@@ -7,7 +7,8 @@*/import { bool, num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";-import { SELECTABLE_ITEM_KINDS } from "@/cut/lib/previewSelection";+import { ITEM_KIND_IDS, canSplitItem } from "@/cut/lib/itemKinds";+import { TIMELINE_ITEM_KINDS } from "@/cut/lib/timelineGroups";import {TEXT_EMPHASIS_IDS,TEXT_LAYOUT_IDS,@@ -21,23 +22,38 @@ export const TIMELINE_TOOLS = [{name: "select",description:- "Select a video clip (on any track), soundtrack clip, or overlay element — title, shape, or sticker (or clear the selection). Selection appears in the timeline and preview. Use additive:true to toggle an item in the selection; move_selection moves the selected visual items together.",+ "Select any timeline item: video, audio, an overlay element, caption cue, or transition (or clear the selection). Selection appears in the timeline and preview. Use additive:true to toggle an item in the selection; move_selection moves the selected visual items together.",inputSchema: obj({- kind: { type: "string", enum: [...SELECTABLE_ITEM_KINDS, "none"], description: "What to select — 'clip' is any video clip, whatever track; 'overlay' is any title-lane element" },+ kind: { type: "string", enum: [...TIMELINE_ITEM_KINDS, "none"], description: "What to select — 'clip' is any video clip, whatever track; 'overlay' is any title-lane element" },id: str("The item id (omit for kind=none)"),additive: bool("Toggle this item in the existing selection"),}, ["kind"]),},+ {+ name: "group_items",+ description: "Group the selected timeline items, including video, audio, text, shapes, captions and transitions. Use select with additive:true to build the selection first. New items start ungrouped. Selecting any group member selects the whole group; drag a member to move the group.",+ inputSchema: obj({}),+ },+ {+ name: "ungroup_items",+ description: "Ungroup every group represented in the current selection. Items keep their timing and become independently selectable and movable.",+ inputSchema: obj({}),+ },+ {+ name: "move_timeline_selection",+ description: "Move selected timeline items and their group members together by delta seconds, preserving relative timing and caption word timings. Negative moves stop at the timeline start; occupied rows move the set to the next free position. Use this for a grouped selection, including captions and transitions.",+ inputSchema: obj({ delta: num("Timeline shift in seconds") }, ["delta"]),+ },{name: "split_at",description:- "Split the video (or a selected soundtrack/overlay clip) at a time, like pressing S. Omit t to split at the playhead. Splits don't move times, so issue every planned split together in one round. The result carries the updated track-0 and soundtrack rows (ids, starts, lengths) — read the new ids from there.",+ `Split selected items and group members at a time, like pressing S. Supported kinds: ${ITEM_KIND_IDS.filter(canSplitItem).join(", ")}. Transitions remain whole. With no selection, split the video on track 0. Omit t for the playhead. Times stay fixed; the result includes the new selected ids.`,inputSchema: obj({ t: num("Timeline seconds to cut at (optional)") }),},{name: "move_clip",description:- "Reorder a track-0 video clip to a new index: it lifts out (its old spot becomes a gap) and clips from the landing index shift right to make room — nothing else moves, so sound and titles stay synced. To move one clip in time, use place_clip.",+ "Reorder a track-0 video clip to a new index: its old spot becomes a gap and clips from the landing index shift right to make room. The moved clip and displaced clips carry their group members with relative timing preserved. Occupied rows can move a group farther right. To set a timeline start, use place_clip.",inputSchema: obj({ clipId: str("Video clip id"), toIndex: num("Target index, 0-based") }, ["clipId", "toIndex"]),},{@@ -60,7 +76,7 @@ export const TIMELINE_TOOLS = [{name: "trim_clip",description:- "Set a video clip's trim points inside its source media (seconds). Changing `in` hides the leading part; `out` the trailing part. While track 0 is the only video track, a track-0 resize ripples: everything past the clip's tail — clips, titles, captions, soundtrack — rides the moved edge in both directions, and every existing gap keeps its width. With overlay video tracks present, growth pushes the track-0 run and a shrink leaves a gap.",+ "Set a video clip's source trim points in seconds. Growth pushes overlapping neighbors on its own video track; shrinking leaves a gap. Other rows keep their timing.",inputSchema: obj({ clipId: str("Video clip id"), in: num("New in point (optional)"), out: num("New out point (optional)") }, ["clipId"]),},{@@ -134,9 +150,9 @@ export const TIMELINE_TOOLS = [{name: "delete_item",description:- "Delete a video clip (any track), soundtrack clip, or overlay element (title/shape/sticker) by id. Only the listed item leaves the project — anything else laid over it survives at full length. While track 0 is the only video track, deleting a track-0 clip ripples: its footprint closes and everything after it — clips, titles, captions, soundtrack — slides left in sync, while items standing over the footprint fall back to where it started. With overlay video tracks present the delete leaves its gap in place (remove_gap closes it). Deletes on other tracks remove just that item.",+ "Delete a timeline item and its explicit group by id. Other items keep their timing. The deleted footprint becomes a gap; remove_gap closes it.",inputSchema: obj({- kind: { type: "string", enum: ["clip", "audio", "overlay"], description: "Item kind — 'clip' is any video clip, whatever track; 'overlay' is any title-lane element" },+ kind: { type: "string", enum: TIMELINE_ITEM_KINDS, description: "Item kind — 'clip' is any video clip, whatever track; 'overlay' is any title-lane element" },id: str("Item id"),}, ["kind", "id"]),},diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsxindex 530554b9..b34726a0 100644--- a/site/src/cut/components/Timeline.tsx+++ b/site/src/cut/components/Timeline.tsx@@ -1,4 +1,5 @@"use client";+import { TimelineGroupActions } from "@/cut/components/TimelineGroupActions";import { Fragment, useCallback, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, type ReactNode } from "react";import { ArrowDownToLine, AudioLines, Check, Clapperboard, Copy, Diamond, Download, EllipsisVertical, Eye, EyeOff, FolderOpen, FolderPlus, Fullscreen, Loader2, MoreHorizontal, PanelBottomClose, Pause, Play, Scissors, SkipBack, Sticker, Trash2, Type, Volume2, VolumeX } from "lucide-react";@@ -51,6 +52,7 @@ import { reportSwallowed } from "@/cut/lib/report";import { downloadMedia, dropEdgeFrames, ensurePeaks, importImage, importStockAudio, importStockVideo, peekEdgeFrame, requestEdgeFrame, revealMedia, stripFailedFor, subscribeStripStatus } from "@/cut/lib/media";import { planFilmstrip, type FilmTile } from "@/cut/lib/filmstrip";import { waveGain } from "@/cut/lib/waveform";+import { canSplitItem } from "@/cut/lib/itemKinds";import { track0Clips, laneGapAt, sameLane, type LaneRef, clipLen, clipSpeed, getClipSpans, maxClipFade, overlayLaneOrder, overlayLayers, projectDuration, resolveTransitions, rippleInsert, useEditor } from "@/cut/lib/store";import type { VideoTrackPlacement } from "@/cut/lib/store";import { playheadAt, setSkim, skimAt, subscribePlayhead, usePlayhead, useSkim } from "@/cut/lib/playhead";@@ -59,7 +61,7 @@ import { useMatteBakes } from "@/cut/lib/removal/bakeJobs";import { laneHidden, subtitleLaneCount } from "@/cut/lib/subtitles";import { formatTime, formatTimecode } from "@/cut/lib/time";import { EFFECT_LABELS, hasSpeedCurve, headSrc, retimeOf, SPEED_CURVE_MAX, SPEED_CURVE_MIN, tailSrc, type EffectId, type Retime, type SpeedNode } from "@donkeycut/effects-kit";-import { assetIsSilent, emptySubtitles, fontStack, IMAGE_CLIP_SECONDS, isAudioTransition, clipName, isTextOverlay, overlayName, SHAPE_LABELS, TRANSITION_MAX, TRANSITION_STYLE_LABELS, transitionBarStart, transitionDefaultSeconds, XBAR_MAGNET_PX, type ShapeKind } from "@/cut/lib/types";+import { assetIsSilent, emptySubtitles, fontStack, IMAGE_CLIP_SECONDS, isAudioTransition, clipName, isTextOverlay, overlayName, SHAPE_LABELS, TRANSITION_STYLE_LABELS, transitionBarStart, transitionDefaultSeconds, XBAR_MAGNET_PX, type ShapeKind } from "@/cut/lib/types";import type { AudioClip, ClipSpan, ColorGrade, MediaAsset, Overlay, Selection, StickerOverlay, SubtitleCue, TimelineTransition, TransitionBoundaryKind, TransitionStyle, VideoClip } from "@/cut/lib/types";import { isLottieAsset } from "@/cut/lib/lottieAssets";import { gradeCssApprox } from "@donkeycut/effects-kit";@@ -735,9 +737,9 @@ export function Timeline() {// doesn't resize under the pointer; it commits on release.const dragging = useSyncExternalStore(subscribeDragActive, isDragActive, () => false);const liveContentW = Math.max(total * pps + PAD_END, viewportW - PAD_SIDE * 2, 600);- const heldContentW = useRef(liveContentW);- if (!dragging) heldContentW.current = liveContentW;- const contentW = heldContentW.current;+ const [widthHold, setWidthHold] = useState({ dragging, width: liveContentW });+ if (widthHold.dragging !== dragging) setWidthHold({ dragging, width: liveContentW });+ const contentW = dragging ? widthHold.width : liveContentW;// Drop preview while dragging a media asset onto video track 0: where the// clip would land, how long it runs, and what the source looks like, so the@@ -1157,22 +1159,6 @@ export function Timeline() {[audioRowBoxes, audioDrop]);- // The element row under the pointer, or undefined for a point outside the- // band — a drop there takes the element's home row.- const overlayRowAt = useCallback(- (clientY: number): number | undefined => {- const el = overlayRef.current;- if (!el || overlayLanes.count === 0) return undefined;- const r = el.getBoundingClientRect();- if (clientY < r.top || clientY > r.bottom) return undefined;- return Math.min(overlayLanes.count - 1, Math.max(0, Math.floor((clientY - r.top) / TEXT_H)));- },- [overlayLanes]- );- const overlayLaneAt = (clientY: number): number | undefined => {- const row = overlayRowAt(clientY);- return row === undefined ? undefined : overlayLanes.used[row];- };// Every place on every video track a transition can sit: the cuts, where one// clip hands over to the next, and the open edges — a clip's head with// nothing before it, its tail with nothing after — where it arrives from or@@ -1304,7 +1290,7 @@ export function Timeline() {st.endHistoryBatch();st.select({ kind: "transition", id });};- // A pasted transition lands through the same closure a dropped one does.+ // A transition copied from a catalog tile uses the panel drop placement.const dropTransitionRef = useRef(dropTransitionAt);useEffect(() => {dropTransitionRef.current = dropTransitionAt;@@ -1314,80 +1300,15 @@ export function Timeline() {[]);- // Drag a bar anywhere along the row. An anchor within reach pulls the drop- // onto itself and lights the room it takes; released anywhere else the bar- // stays exactly there — parked, playing nothing until a cut or a clip edge- // lines up with it. A press that never moved selects the bar.const moveTransition = (e: React.PointerEvent, x: XBar) => {- if (e.button !== 0) return;- e.stopPropagation();- const s = useEditor.getState();- if (s.readOnly) return;- // ⌘/Ctrl/⇧-click folds the bar into the multi-selection, like any lane bar.- const additive = additiveClick(e);- let landing: { start: number; anchor: Anchor | null } | null = null;- startDrag(e, {- onMove: (dx) => {- const free = Math.max(-x.t.seconds + 0.1, x.t.start + dx / pps);- const near = anchors.reduce<{ a: Anchor; gap: number } | null>((best, a) => {- const gap = Math.abs(anchorBarStart(a, x.t.seconds, x.t.style) - free);- return !best || gap < best.gap ? { a, gap } : best;- }, null);- const snapped = near && near.gap <= XBAR_MAGNET_PX / pps ? near.a : null;- landing = snapped- ? { start: anchorBarStart(snapped, x.t.seconds, x.t.style), anchor: snapped }- : { start: free, anchor: null };- setSnapX(snapped ? snapped.at * pps : null);- setJointDrop(snapped ? { ...snapped, len: x.t.seconds } : null);- setTransitionDrag({ ...x, t: { ...x.t, start: landing.start } });- },- onUp: (_dx, _dy, moved) => {- setSnapX(null);- setJointDrop(null);- setTransitionDrag(null);- const st = useEditor.getState();- if (!moved || !landing) {- if (additive) return st.toggleSelect({ kind: "transition", id: x.t.id });- return st.select({ kind: "transition", id: x.t.id });- }- st.beginHistoryBatch();- const incumbent = landing.anchor ? barAt(landing.anchor) : null;- if (incumbent && incumbent.id !== x.t.id) st.removeTransition(incumbent.id);- st.updateTransition(x.t.id, { start: landing.start });- st.endHistoryBatch();- },+ startLaneMove(e, "transition", x.t.id, {+ pps, rows: () => [], homeRow: 0, onDrag: setLaneDrag, onSnap: setSnapX,});};- // Drag the loose edge to retime. A playing bar keeps the boundary end still —- // an entrance grows forward from its start, everything else backward from- // its end — so retiming never un-aligns it.- const trimTransition = (e: React.PointerEvent, x: XBar) => {- if (e.button !== 0) return;- e.stopPropagation();- const st0 = useEditor.getState();- if (st0.readOnly) return;- const growsForward = x.role?.kind === "in";- // A cross dissolve sits across its cut, so retiming holds the middle and- // both halves grow together; every other bar holds the boundary end.- const straddles = isAudioTransition(x.t.style);- const fixed = growsForward- ? x.t.start- : straddles- ? x.t.start + x.t.seconds / 2- : x.t.start + x.t.seconds;- st0.beginHistoryBatch();- startDrag(e, {- onMove: (dx) => {- const loose = (growsForward ? x.t.start + x.t.seconds : x.t.start) + dx / pps;- const reach = Math.abs(loose - fixed) * (straddles ? 2 : 1);- const seconds = Math.max(0.1, Math.min(TRANSITION_MAX, reach));- useEditor.getState().updateTransitionTransient(x.t.id, {- seconds,- start: growsForward ? fixed : fixed - (straddles ? seconds / 2 : seconds),- });- },- onUp: () => useEditor.getState().endHistoryBatch(),+ const trimTransition = (e: React.PointerEvent, x: XBar, side: "l" | "r") => {+ startLaneTrim(e, "transition", x.t.id, side, {+ pps, onSnap: setSnapX,});};@@ -1400,6 +1321,8 @@ export function Timeline() {t: number;y: number;} | null>(null);+ const overlayRowAt = (clientY: number): number =>+ resolveRow(overlayRowBoxes(), clientY, elementDrop?.row ?? null, { top: true, bottom: true });// The place a dragged transition would land on, marked with the footprint it// would take while it is in flight.const [jointDrop, setJointDrop] = useDropPreview<Anchor | null>(null);@@ -1433,6 +1356,8 @@ export function Timeline() {previewNext.current = null;}, []);useEffect(() => cancelPreview, [cancelPreview]);+ // An empty timeline starts incoming footage at zero.+ const dropTimeAt = (clientX: number) => (total <= 0 ? 0 : Math.max(0, timeAt(clientX)));// Every drag ends here, wherever it was released: the landing previews and// the placement hold clear on the browser's own `dragend`, which fires for// a drop the timeline took, a release elsewhere, and an escape alike. An@@ -1483,18 +1408,13 @@ export function Timeline() {[resolveDropTrack, audioRowAt]);// A transition bar mid-drag, drawn where the pointer has it.- const [transitionDrag, setTransitionDrag] = useState<XBar | null>(null);// A drag past the top edge opens a row there, pushing the stack down by one// for as long as it is aimed that way.- const topRowShift = laneDrag?.kind === "overlay" && laneDrag.targetRow < 0 ? TEXT_H : 0;+ const topRowShift =+ (laneDrag?.kind === "overlay" && laneDrag.targetRow < 0) || elementDrop?.row === -1 ? TEXT_H : 0;const audioTopShift =(laneDrag?.kind === "audio" && laneDrag.targetRow < 0) || audioDrop?.row === -1 ? AUDIO_H : 0;- // Where a dropped asset should land. An empty timeline has no arrangement to- // read a position against, so the drop starts the film at 0 no matter where- // the cursor released.- const dropTimeAt = (clientX: number) => (total <= 0 ? 0 : Math.max(0, timeAt(clientX)));-// Scrub with auto-scroll when the pointer nears the viewport edges.//// Both boxes are measured once, at the press. Reading a rect forces the@@ -1703,18 +1623,17 @@ export function Timeline() {t: number,audioRow = 0,place: TrackTarget = TRACK_ZERO,- /** The element row the pointer came down on, when it was over the band. */- elementLane?: number+ elementRow = 0) => {const s = useEditor.getState();const sticker = stickerOf(s.assets.find((a) => a.id === assetId));if (sticker) {- s.addSticker({+ landOnRow("overlay", elementRow, (lane) => useEditor.getState().addSticker({assetId,...(isLottieAsset(sticker) ? { lottie: true } : {}),at: t,- ...(elementLane !== undefined ? { lane: elementLane } : {}),- });+ lane,+ }));return;}if (isClipMedia(type)) {@@ -1758,7 +1677,7 @@ export function Timeline() {atElement: number,audioRow = 0,place: TrackTarget = TRACK_ZERO,- elementLane?: number,+ elementRow = 0,only?: (asset: MediaAsset) => boolean) => {const next = runCursor(t, atElement);@@ -1771,7 +1690,7 @@ export function Timeline() {if (asset.type === "font") continue;if (only && !only(asset)) continue;const sticker = !!stickerOf(asset);- placeAssetAt(asset.id, asset.type, next(asset, sticker), audioRow, place, elementLane);+ placeAssetAt(asset.id, asset.type, next(asset, sticker), audioRow, place, elementRow);} catch (err) {reportSwallowed(`[cut] drop failed for ${item.name}`, err);}@@ -1820,7 +1739,7 @@ export function Timeline() {// and OS files belong to the timeline's own drop, which stashes their// landing for the editor's import and clears every preview; both pass// through here untouched.- if (draggedSticker() || draggedFiles(e)) return;+ if (hasElementDrag(e) || draggedSticker() || draggedFiles(e)) return;e.preventDefault();e.stopPropagation();cancelPreview();@@ -2004,10 +1923,9 @@ export function Timeline() {setDropType(null);// An empty band has no rows to hit; the drag opens the first one,// where the drop will land.- const hit = overlayRowAt(y);const row =!element || element.kind !== "transition"- ? hit ?? (overlayLanes.count === 0 ? 0 : null)+ ? overlayRowAt(y): null;// The pointer's own time, not `dropTimeAt`: an empty timeline pins// clip drops to 0, but an element rides wherever it is held.@@ -2098,7 +2016,7 @@ export function Timeline() {}// Resolve the hovered rows before the previews (and their rows) clear.const audioRow = audioRowAt(e.clientY);- const elementLane = overlayLaneAt(e.clientY);+ const elementRow = overlayRowAt(e.clientY);setElementDrop(null);setJointDrop(null);setXTileDrag(null);@@ -2123,13 +2041,12 @@ export function Timeline() {const atElement = Math.max(0, timeAt(e.clientX));if (element) {e.preventDefault();- const aim = {- at: atElement,- ...(elementLane !== undefined ? { lane: elementLane } : {}),- };- if (element.kind === "shape") useEditor.getState().addShape(element.shape, aim);- else if (element.kind === "effect") useEditor.getState().addEffect(element.effect, aim);- else dropTransitionAt(t, element.style);+ if (element.kind === "transition") dropTransitionAt(t, element.style);+ else landOnRow("overlay", elementRow, (lane) => {+ const aim = { at: atElement, lane };+ if (element.kind === "shape") useEditor.getState().addShape(element.shape, aim);+ else useEditor.getState().addEffect(element.effect, aim);+ });return;}if (tpl && projectId) {@@ -2143,7 +2060,7 @@ export function Timeline() {}if (carried.length > 0 && projectId) {e.preventDefault();- void placeCarriedAt(projectId, carried, t, atElement, audioRow, videoPlace, elementLane);+ void placeCarriedAt(projectId, carried, t, atElement, audioRow, videoPlace, elementRow);}}}>@@ -2352,7 +2269,7 @@ export function Timeline() {{Array.from({length:- overlayLanes.count ++ Math.max(overlayLanes.count, (elementDrop?.row ?? -1) + 1) +(topRowShift ? 1 : 0) +(laneDrag?.kind === "overlay" && laneDrag.targetRow === overlayLanes.count ? 1 : 0),},@@ -2409,7 +2326,7 @@ export function Timeline() {"pointer-events-none absolute inset-x-0 rounded-[3px] border border-dashed",FAMILY_STYLE[family].row)}- style={{ top: elementDrop.row * TEXT_H + 2, height: TEXT_H - 6 }}+ style={{ top: elementDrop.row * TEXT_H + 2 + topRowShift, height: TEXT_H - 6 }}/>{/* The slot the drop takes on its row... */}<div@@ -2418,7 +2335,7 @@ export function Timeline() {FAMILY_STYLE[family].slot)}style={{- top: elementDrop.row * TEXT_H + 2,+ top: elementDrop.row * TEXT_H + 2 + topRowShift,height: TEXT_H - 6,left: elementDrop.t * pps,width: Math.max(14, ELEMENT_DROP_SECONDS * pps - CLIP_GAP),@@ -2494,14 +2411,14 @@ export function Timeline() {onPointerDown={deselectIfSelf}>{laneRail(TEXT_H - 4, "xrail")}- {(transitionDrag !== null || xTileDrag) &&+ {xTileDrag &&// While a transition is in flight, every place it could play// lights up in the timeline's usual drop-zone dress: one slot// per cut and open edge, each the size the bar would take// there.anchors.map((a) => {- const style = transitionDrag?.t.style ?? xTileDrag ?? "crossfade";- const len = transitionDrag ? transitionDrag.t.seconds : anchorLen(a, style);+ const style = xTileDrag ?? "crossfade";+ const len = anchorLen(a, style);return (<divkey={`xzone-${a.kind}-${a.clipId}`}@@ -2517,7 +2434,7 @@ export function Timeline() {{jointDrop &&(() => {// The zone the drop is aimed at burns brighter than the rest.- const style = transitionDrag?.t.style ?? xTileDrag ?? "crossfade";+ const style = xTileDrag ?? "crossfade";const len = anchorLen(jointDrop, style);return (<div@@ -2534,7 +2451,8 @@ export function Timeline() {);})()}{transitions.map((live) => {- const x = transitionDrag?.t.id === live.t.id ? transitionDrag : live;+ const x = live;+ const drag = laneDragFor(laneDrag, "transition", x.t.id);const Icon = TRANSITION_ICONS[x.t.style];const playing = !!x.role;return (@@ -2552,10 +2470,10 @@ export function Timeline() {selKeys.has(`transition:${x.t.id}`) && SELECTED_SHADOW,// Mid-drag it rides over the zone it would land on, so// the marked room stays readable underneath.- x === transitionDrag && "opacity-75"+ drag && "opacity-75")}style={{- ...barBox(x.t, x.role?.kind ?? "cut"),+ ...barBox(drag ? { ...x.t, start: drag.ghostX / pps } : x.t, x.role?.kind ?? "cut"),top: 2,height: TEXT_H - 6,}}@@ -2578,17 +2496,14 @@ export function Timeline() {.updateTransition(x.t.id, { hidden: !x.t.hidden || undefined })}/>- {/* Retiming pulls the loose end: an entrance runs forward- from the clip's head, everything else backward from the- boundary it sits on. */}- <span- title="Drag to retime"- className={cn(- trimHandle,- x.role?.kind === "in" ? "tl-trim-r right-0" : "tl-trim-l left-0"- )}- onPointerDown={(e) => trimTransition(e, x)}- />+ {(["l", "r"] as const).map((side) => (+ <span+ key={side}+ title="Drag to trim"+ className={cn(trimHandle, side === "l" ? "tl-trim-l left-0" : "tl-trim-r right-0")}+ onPointerDown={(e) => trimTransition(e, x, side)}+ />+ ))}</div>);})}@@ -3311,6 +3226,13 @@ function HoverLine({*//** The timeline's editing tools. Dropping `labels` leaves the icons on their* own — the step between a full toolbar and folding the lot into the menu. */+function useSplitEnabled() {+ return useEditor((s) => {+ const selected = s.multiSelection.length ? s.multiSelection : s.selection ? [s.selection] : [];+ return !s.readOnly && (!selected.length || selected.some((item) => item && canSplitItem(item.kind)));+ });+}+function TimelineTools({labels,split,@@ -3324,6 +3246,7 @@ function TimelineTools({deleteSelection: () => void;selectionCount: number;}) {+ const splitEnabled = useSplitEnabled();const size = labels ? "sm" : "icon-sm";return (<>@@ -3332,6 +3255,7 @@ function TimelineTools({size={size}title="Split at pointer, or at playhead (⌘B or S)"onClick={split}+ disabled={!splitEnabled}><Scissors />{labels && <span>Split</span>}@@ -3350,6 +3274,7 @@ function TimelineTools({<Trash2 />{labels && <span>{selectionCount > 1 ? `Delete ${selectionCount}` : "Delete"}</span>}</Button>+ <TimelineGroupActions labels={labels} /><SaveSelectionButton labels={labels} /></>);@@ -3377,6 +3302,7 @@ function TimelineToolsMenu({deleteSelection: () => void;selectionCount: number;}) {+ const splitEnabled = useSplitEnabled();const save = useSaveSelection();// Held open, because the zoom row is not a pick: dragging the slider has to// leave the menu standing, while Fit beside it closes as any command would.@@ -3391,7 +3317,7 @@ function TimelineToolsMenu({<MoreHorizontal /></DropdownMenuTrigger><DropdownMenuContent align="end" className="w-56">- <DropdownMenuItem onClick={split}>+ <DropdownMenuItem disabled={!splitEnabled} onClick={split}><Scissors /> Split</DropdownMenuItem><DropdownMenuItem onClick={addText}>@@ -3400,6 +3326,7 @@ function TimelineToolsMenu({<DropdownMenuItem disabled={selectionCount === 0} onClick={deleteSelection}><Trash2 /> {selectionCount > 1 ? `Delete ${selectionCount}` : "Delete"}</DropdownMenuItem>+ <TimelineGroupActions menu />{save.available && (<DropdownMenuItem disabled={save.state === "saving"} onClick={save.save}>{save.state === "done" ? <Check /> : <FolderPlus />}diff --git a/site/src/cut/components/TimelineGroupActions.tsx b/site/src/cut/components/TimelineGroupActions.tsxnew file mode 100644index 00000000..8d3af0b4--- /dev/null+++ b/site/src/cut/components/TimelineGroupActions.tsx@@ -0,0 +1,29 @@+"use client";++import { Group, Ungroup } from "lucide-react";+import { useState } from "react";+import { Button } from "@/components/ui/button";+import { DropdownMenuItem } from "@/components/ui/dropdown-menu";+import { useEditor } from "@/cut/lib/store";+import { createSelectedGroupSelector } from "@/cut/lib/timelineGroups";++export function TimelineGroupActions({ labels = true, menu = false }: { labels?: boolean; menu?: boolean }) {+ const count = useEditor((s) => s.multiSelection.length);+ const [selectGrouped] = useState(createSelectedGroupSelector);+ const grouped = useEditor(selectGrouped);+ const readOnly = useEditor((s) => s.readOnly);+ const group = () => useEditor.getState().groupSelection();+ const ungroup = () => useEditor.getState().ungroupSelection();+ if (menu) return <>+ <DropdownMenuItem disabled={readOnly || count < 2} onClick={group}><Group /> Group</DropdownMenuItem>+ <DropdownMenuItem disabled={readOnly || !grouped} onClick={ungroup}><Ungroup /> Ungroup</DropdownMenuItem>+ </>;+ return <>+ <Button variant="ghost" size={labels ? "sm" : "icon-sm"} title="Group (⌘/Ctrl+G)" disabled={readOnly || count < 2} onClick={group}>+ <Group />{labels && <span>Group</span>}+ </Button>+ <Button variant="ghost" size={labels ? "sm" : "icon-sm"} title="Ungroup (⇧⌘/Ctrl+G)" disabled={readOnly || !grouped} onClick={ungroup}>+ <Ungroup />{labels && <span>Ungroup</span>}+ </Button>+ </>;+}diff --git a/site/src/cut/components/previewSelectionDrag.ts b/site/src/cut/components/previewSelectionDrag.tsindex e672ef71..593af083 100644--- a/site/src/cut/components/previewSelectionDrag.ts+++ b/site/src/cut/components/previewSelectionDrag.ts@@ -5,6 +5,19 @@ import { startDrag } from "@/cut/lib/drag";import { previewAt } from "@/cut/lib/playhead";import { useEditor } from "@/cut/lib/store";import { movePreviewSelection, previewSelectionSnapshot } from "@/cut/lib/previewSelection";+import type { Selection } from "@/cut/lib/types";++export function isPreviewSelectionModifier(e: Pick<PointerEvent, "metaKey" | "ctrlKey" | "shiftKey">): boolean {+ return e.metaKey || e.ctrlKey || e.shiftKey;+}++/** Own the whole press so its generated click cannot select a second time. */+export function togglePreviewSelection(e: PointerEvent, item: NonNullable<Selection>): boolean {+ if (e.button !== 0 || !isPreviewSelectionModifier(e)) return false;+ startDrag(e, { onMove: () => {} });+ useEditor.getState().toggleSelect(item);+ return true;+}/** Selected items share one gesture and one undo checkpoint. */export function startSelectionDrag(e: PointerEvent, width: number, height: number): boolean {diff --git a/site/src/cut/lib/aiContext.ts b/site/src/cut/lib/aiContext.tsindex 094c51dc..0388cf65 100644--- a/site/src/cut/lib/aiContext.ts+++ b/site/src/cut/lib/aiContext.ts@@ -465,6 +465,7 @@ function describeState(videoTrack: spans.map((sp, index) => ({index,id: sp.clip.id,+ ...(sp.clip.groupId ? { groupId: sp.clip.groupId } : {}),asset: sp.asset.name,...(sp.clip.name ? { name: sp.clip.name } : {}),start: r(sp.start),@@ -521,6 +522,7 @@ function describeState(const trackSpans = getClipSpans(s.clips, s.assets, track);return trackSpans.map((sp, i) => ({id: sp.clip.id,+ ...(sp.clip.groupId ? { groupId: sp.clip.groupId } : {}),...describeOverlayClip(sp.clip, assetById),...(() => {const t = transitionToNext(sp, i, trackSpans);@@ -543,6 +545,7 @@ function describeState(const plays = roles.get(t.id) ?? [];return {id: t.id,+ ...(t.groupId ? { groupId: t.groupId } : {}),start: r(t.start),seconds: r(t.seconds),style: t.style,@@ -592,6 +595,7 @@ function describeState(// for the whole transcript (e.g. "clean up all the captions").cues: s.subtitles.cues.slice(0, cueCap).map((c) => ({id: c.id,+ ...(c.groupId ? { groupId: c.groupId } : {}),start: r(c.start),end: r(c.end),text: c.text,@@ -617,12 +621,13 @@ function describeRate(c: { speed?: number; speedCurve?: SpeedNode[]; reverse?: b}function describeAudio(- a: { assetId: string; name?: string; start: number; in: number; out: number; volume: number; fadeIn?: number; fadeOut?: number; speed?: number; speedCurve?: SpeedNode[]; reverse?: boolean; sound?: ClipSound; duck?: number; lane?: number; hidden?: boolean },+ a: { groupId?: string; assetId: string; name?: string; start: number; in: number; out: number; volume: number; fadeIn?: number; fadeOut?: number; speed?: number; speedCurve?: SpeedNode[]; reverse?: boolean; sound?: ClipSound; duck?: number; lane?: number; hidden?: boolean },assets: Map<string, { name: string }>) {const rt = retimeOf(a);const speed = rt.rate;return {+ ...(a.groupId ? { groupId: a.groupId } : {}),asset: assets.get(a.assetId)?.name ?? a.assetId,...(a.name ? { name: a.name } : {}),start: r(a.start),@@ -681,6 +686,7 @@ function describeOverlayClip(c: VideoClip, assets: Map<string, { name: string }>function describeOverlay(o: Overlay) {const base = {kind: o.kind ?? "text",+ ...(o.groupId ? { groupId: o.groupId } : {}),name: overlayName(o),start: r(o.start),end: r(o.end),@@ -695,7 +701,6 @@ function describeOverlay(o: Overlay) {...(o.mask ? { mask: o.mask } : {}),...(o.lane ? { lane: o.lane } : {}),...(o.hidden ? { hidden: true } : {}),- ...(o.hostClipId ? { host: o.hostClipId } : o.hostClipId === null ? { free: true } : {}),};if (o.kind === "shape") {return {@@ -731,7 +736,6 @@ function describeOverlay(o: Overlay) {color: o.color,shadow: o.shadow,plate: o.plate,- ...(o.groupId ? { groupId: o.groupId } : {}),...(o.plateRadius !== undefined && { plateRadius: r(o.plateRadius) }),...(o.stretchX !== undefined ? { stretchX: r(o.stretchX) } : {}),...(o.stretchY !== undefined ? { stretchY: r(o.stretchY) } : {}),diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex c018785e..2027168a 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -1,5 +1,7 @@"use client";+import { ITEM_KIND_IDS, ITEM_KINDS, type ItemKind } from "./itemKinds";+import { timelineItem } from "./timelineItems";import { movePreviewSelection, previewSelectionSnapshot } from "@/cut/lib/previewSelection";import { libraryShareTargetSchema, shareSettingsSchema, librarySharePath } from "@/cut/lib/librarySharing";@@ -138,7 +140,7 @@ import { landReferenceAssets as landReferenceAssetsInto, openReference, referencimport { clampLayersToAssets, mediaTypeFits, templateFromDoc } from "./projectTemplate";import { isSoundPresetTemplate, listSoundPresets, saveSoundPreset } from "./soundPresets";import { isStylePresetTemplate } from "./stylePresets";-import { applyOverlayPatchSettled, clipLen, track0Clips, laneGapAt, getClipSpans, nextFreeStart, overlayLaneOrder, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor } from "./store";+import { applyOverlayPatchSettled, clipLen, track0Clips, laneGapAt, getClipSpans, overlayLaneOrder, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor } from "./store";import { playheadAt } from "./playhead";import { renderProjectFrame } from "./exportRender";import { renderStageFrame, storeStageStill } from "./stageFrame";@@ -1255,20 +1257,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return { selection: null };}const id = String(input.id ?? "");- // A video clip on any track selects as "clip"; "overlayClip" is accepted- // as a legacy alias for a layer clip and "text" for an overlay element.- const pool =- kind === "clip" || kind === "overlayClip"- ? s.clips- : kind === "audio"- ? s.audioClips- : kind === "overlay" || kind === "text"- ? s.overlays- : kind === "cue" ? s.subtitles.cues : null;- if (!pool) throw new ToolError(`Unknown kind: ${kind}`);- if (!pool.some((x) => x.id === id)) throw new ToolError(`No ${kind} with id ${id}.`);- const selKind =- kind === "overlayClip" ? "clip" : kind === "text" ? "overlay" : (kind as "clip" | "audio" | "overlay" | "cue");+ if (!ITEM_KIND_IDS.includes(kind as ItemKind)) throw new ToolError(`Unknown kind: ${kind}`);+ const selKind = kind as ItemKind;+ if (!timelineItem(s, { kind: selKind, id })) throw new ToolError(`No ${kind} with id ${id}.`);if (input.additive === true) s.toggleSelect({ kind: selKind, id });else s.select({ kind: selKind, id });return { selection: useEditor.getState().selection, multiSelection: useEditor.getState().multiSelection };@@ -1282,13 +1273,27 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return { panel };},+ group_items: (s) => {+ const groupId = s.groupSelection();+ if (!groupId) throw new ToolError("Select at least two timeline items to group.");+ return { groupId, selection: useEditor.getState().multiSelection };+ },+ ungroup_items: (s) => {+ s.ungroupSelection();+ return { selection: useEditor.getState().multiSelection };+ },+ move_timeline_selection: (s, input) => {+ if (!isNum(input.delta)) throw new ToolError("delta is required.");+ return { delta: s.moveTimelineSelection(input.delta), selection: useEditor.getState().multiSelection };+ },+split_at: (s, input) => {- const before = s.clips.length + s.audioClips.length;+ const before = ITEM_KIND_IDS.reduce((n, kind) => n + ITEM_KINDS[kind].list(s).length, 0);s.splitAtPlayhead(isNum(input.t) ? input.t : undefined);const after = useEditor.getState();- const made = after.clips.length + after.audioClips.length - before;+ const made = ITEM_KIND_IDS.reduce((n, kind) => n + ITEM_KINDS[kind].list(after).length, 0) - before;if (made === 0) throw new ToolError("Nothing to split at that time.");- return { split: true, ...tracksAfter() };+ return { split: made, selection: after.multiSelection, ...tracksAfter() };},move_clip: (s, input) => {@@ -1308,18 +1313,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {place_clip: (s, input) => {const clip = requireItem(s.clips, input.clipId, "video clip");if (!isNum(input.start)) throw new ToolError("start (seconds) is required.");- const len = clipLen(clip);- const taken = s.clips- .filter((c) => c.id !== clip.id && c.track === clip.track)- .map((c) => ({ start: c.start, end: c.start + clipLen(c) }));- const at = nextFreeStart(taken, Math.max(0, input.start), len);- // Closing a gap is this tool's main job, so the clip's blends travel- // with it: a bar playing its head or its cut lands on the edge's new- // time instead of staying behind on the row.- const before = s.clips;- s.updateClip(clip.id, { start: at });- useEditor.getState().sortClips();- useEditor.getState().reanchorBars(before);+ s.select({ kind: "clip", id: clip.id });+ const delta = s.moveTimelineSelection(Math.max(0, input.start) - clip.start);+ const at = clip.start + delta;return {id: clip.id,start: round2(at),@@ -1814,23 +1810,14 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {},delete_item: (s, input) => {- const kind = String(input.kind) as "clip" | "overlayClip" | "audio" | "overlay" | "text";- const id = String(input.id ?? "");- const pool =- kind === "clip" || kind === "overlayClip"- ? s.clips- : kind === "audio"- ? s.audioClips- : s.overlays;- if (!pool.some((x) => x.id === id)) throw new ToolError(`No ${kind} with id ${id}.`);- const selKind =- kind === "overlayClip" ? "clip" : kind === "text" ? "overlay" : (kind as "clip" | "audio" | "overlay");- s.select({ kind: selKind, id });- s.deleteSelection();- return {- deleted: { kind: selKind, id },- ...(selKind === "overlay" ? {} : tracksAfter()),- };+ const kind = String(input.kind) as ItemKind;+ const id = String(input.id ?? "");+ if (!ITEM_KIND_IDS.includes(kind)) throw new ToolError(`Unknown kind: ${kind}`);+ if (!timelineItem(s, { kind, id })) throw new ToolError(`No ${kind} with id ${id}.`);+ s.select({ kind, id });+ const deleted = useEditor.getState().multiSelection;+ s.deleteSelection();+ return { deleted, ...tracksAfter() };},remove_gap: (s, input) => {@@ -2198,7 +2185,6 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {update_overlay: (s, input) => {const o = requireItem(s.overlays, input.id, "overlay element");s.updateOverlay(o.id, overlayPatch(input, o.kind ?? "text"));- if (typeof input.follows_clip === "boolean") s.setOverlayFollows(o.id, input.follows_clip);const next = useEditor.getState().overlays.find((x) => x.id === o.id)!;return {id: next.id,@@ -2206,7 +2192,6 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {...(isTextOverlay(next) ? { text: next.text, color: next.color, size: next.size } : {}),start: round2(next.start),end: round2(next.end),- ...(next.hostClipId ? { host: next.hostClipId } : {}),};},@@ -3916,6 +3901,17 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return out;},+ copy_selection: (s) => {+ if (!s.copySelection()) throw new ToolError("Select timeline items to copy.");+ return { copied: s.copiedItems().length };+ },++ paste_selection: (s, input) => {+ if (input.at !== undefined && !isNum(input.at)) throw new ToolError("at must be finite timeline seconds.");+ if (!s.paste(isNum(input.at) ? input.at : undefined)) throw new ToolError("Copy timeline items before pasting.");+ return { selection: useEditor.getState().multiSelection };+ },+undo: (s) => {s.undo();return { ok: true };diff --git a/site/src/cut/lib/cutClipboard.test.ts b/site/src/cut/lib/cutClipboard.test.tsindex a0ae24d8..41677f4a 100644--- a/site/src/cut/lib/cutClipboard.test.ts+++ b/site/src/cut/lib/cutClipboard.test.ts@@ -71,6 +71,7 @@ describe("pasting into another project", () => {copies.length = 0;useEditor.setState({projectId: "p2",+ readOnly: false,clips: [],transitions: [],audioClips: [],@@ -83,6 +84,25 @@ describe("pasting into another project", () => {setPlayhead(2);});+ test("a read-only destination rejects paste before transferring media", async () => {+ useEditor.setState({ readOnly: true });+ expect(await pasteCutPayload(payloadOf(["c0"]), { projectId: "p2", at: 2, library: EMPTY_LIBRARY })).toBe(false);+ expect(copies).toEqual([]);+ expect(useEditor.getState().assets).toEqual([]);+ });++ test("asset-free effects and transitions paste without opening the source project", async () => {+ const payload: CutClipboardPayload = { v: 1, projectId: "unavailable-project", assets: [], items: [+ { kind: "overlay", item: { id: "fx", kind: "effect", effect: "blur", start: 1, end: 3, x: 0.5, y: 0.5 } },+ { kind: "transition", item: { id: "tr", style: "crossfade", start: 1.5, seconds: 0.4 } },+ ] };+ setPlayhead(99);+ expect(await pasteCutPayload(payload, { projectId: "p2", at: 5, library: EMPTY_LIBRARY })).toBe(true);+ expect(useEditor.getState().overlays[0]).toMatchObject({ start: 5, end: 7, effect: "blur" });+ expect(useEditor.getState().transitions[0]).toMatchObject({ start: 5.5, seconds: 0.4, style: "crossfade" });+ expect(copies).toEqual([]);+ });+test("brings the media across once and lands the items on it", async () => {const payload = payloadOf(["c0", "a0", "s1", "t1", "q0"]);expect(await pasteCutPayload(payload, { projectId: "p2", at: 2, library: EMPTY_LIBRARY })).toBe(true);@@ -102,7 +122,7 @@ describe("pasting into another project", () => {const title = s.overlays.find(isTextOverlay)!;expect(title.font).toBe(`asset:${s.assets.find((a) => a.type === "font")!.id}`);expect(s.subtitles.cues).toHaveLength(1);- expect(s.subtitles.cues[0]).toMatchObject({ start: 2, text: "every feature" });+ expect(s.subtitles.cues[0]).toMatchObject({ start: 2.3, text: "every feature" });// Pasting the same copy again finds the media already here.copies.length = 0;diff --git a/site/src/cut/lib/cutClipboard.ts b/site/src/cut/lib/cutClipboard.tsindex 2bb66731..b016648f 100644--- a/site/src/cut/lib/cutClipboard.ts+++ b/site/src/cut/lib/cutClipboard.ts@@ -10,7 +10,9 @@// engine, the cloud), remembers where each copy came from so the next paste// finds it, and then lands the items on the copies.-import { refFromAsset } from "./assetRef";+import { clearCopiedFrame } from "./stageFrame";+import { reportSwallowed } from "./report";+import { refFromAsset, selectionRefTokens } from "./assetRef";import { trackEditorTool } from "./editorWork";import { clipboardItemAcross, clipboardItemAssetIds, type TimelineClipboardItem } from "./itemKinds";import type { LibraryData } from "./library";@@ -92,6 +94,21 @@ export async function writeCutClipboard(text: string, payload: CutClipboardPayloawait clip.writeText(text);}+/** Copy timeline selection to both the editor clipboard and the system clipboard. */+export function copyTimelineSelection(): boolean {+ const s = useEditor.getState();+ const copied = s.copySelection();+ const token = selectionRefTokens(s);+ if (!copied && !token) return false;+ if (copied) clearCopiedFrame();+ const items = copied ? s.copiedItems() : [];+ const payload = copied && s.projectId+ ? { v: 1 as const, projectId: s.projectId, items, assets: payloadAssets(items, s.assets) }+ : null;+ void writeCutClipboard(token ?? "", payload).catch((error) => reportSwallowed("timeline clipboard write", error));+ return true;+}+/** The items on this project's assets: what crosses, on the copies' ids. */const remapItems = (items: TimelineClipboardItem[], idMap: Map<string, string>): TimelineClipboardItem[] =>items.map((cb) => clipboardItemAcross(cb, (id) => idMap.get(id) ?? id));@@ -110,16 +127,16 @@ export async function pasteCutPayload(): Promise<boolean> {const { projectId, at, library } = ctx;const s = useEditor.getState();- if (s.projectId !== projectId) return false;+ if (s.projectId !== projectId || s.readOnly) return false;if (payload.projectId === projectId) {if (payload.items.length === 0) return false;s.setClipboard(payload.items);- return s.paste();+ return s.paste(at);}// The editor holds this project open until the copies have landed, the way// it waits for a chat tool; a switch that beats the hold leaves the// landing refused with the project it was for.- const landed = await trackEditorTool(async () => {+ const landed = payload.assets.length === 0 ? [] : await trackEditorTool(async () => {const ref = await openReference({ kind: "project", id: payload.projectId });return landReferenceAssets(ref, payload.assets, projectId);});@@ -135,5 +152,5 @@ export async function pasteCutPayload(const idMap = new Map(landed.map((l) => [l.sourceId, l.asset.id]));const st = useEditor.getState();st.setClipboard(remapItems(payload.items, idMap));- return st.paste();+ return st.paste(at);}diff --git a/site/src/cut/lib/fixtures/richDoc.ts b/site/src/cut/lib/fixtures/richDoc.tsindex 65325875..6059cebf 100644--- a/site/src/cut/lib/fixtures/richDoc.ts+++ b/site/src/cut/lib/fixtures/richDoc.ts@@ -85,7 +85,7 @@ export function richDoc(): ProjectDoc {overlays: [{id: "e0", kind: "effect", effect: "vintage", amount: 0.6,- start: 0, end: c0Len, x: 0.5, y: 0.5, lane: 2, hostClipId: "c0",+ start: 0, end: c0Len, x: 0.5, y: 0.5, lane: 2,},{id: "t0", kind: "text", text: "Everything test",diff --git a/site/src/cut/lib/itemKinds.test.ts b/site/src/cut/lib/itemKinds.test.tsindex 9445bd75..bf645261 100644--- a/site/src/cut/lib/itemKinds.test.ts+++ b/site/src/cut/lib/itemKinds.test.ts@@ -108,7 +108,7 @@ describe("copy and paste, kind by kind", () => {expect(s.paste()).toBe(true);const after = useEditor.getState();const list = ITEM_KINDS[kind].list(after) as { id: string }[];- expect(list).toHaveLength(before + 1);+ expect(list).toHaveLength(before + s.copiedItems().filter((item) => item.kind === kind).length);const fresh = after.selection!;expect(fresh.kind).toBe(kind);expect(fresh.id).not.toBe(SAMPLES[kind].id);diff --git a/site/src/cut/lib/itemKinds.ts b/site/src/cut/lib/itemKinds.tsindex 5cab74d9..5d4338c5 100644--- a/site/src/cut/lib/itemKinds.ts+++ b/site/src/cut/lib/itemKinds.ts@@ -13,6 +13,7 @@// compile until it has an entry, and `itemKinds.test.ts` walks the table so// every entry proves it copies and pastes.+import { retimeOf } from "@donkeycut/effects-kit";import {fontAssetId,isStickerOverlay,@@ -53,6 +54,15 @@ export interface ItemLists {export interface ItemKindDef<K extends ItemKind> {/** The list this kind lives in. */list(s: ItemLists): ItemOf[K][];+ /** Store the list while preserving the document's other collections. */+ withList(s: ItemLists, items: ItemOf[K][]): ItemLists;+ /** Timeline duration and row, independent of the item's source representation. */+ duration(item: ItemOf[K]): number;+ lane(item: ItemOf[K]): number;+ /** Move to a timeline start, preserving all item properties and local timing. */+ at(item: ItemOf[K], start: number): ItemOf[K];+ /** Split at an interior timeline time; the coordinator assigns the new id. */+ split: ((item: ItemOf[K], at: number) => [ItemOf[K], ItemOf[K]]) | null;/** A copy that shares nothing with the original. */clone(item: ItemOf[K]): ItemOf[K];/** Every asset id on the item: the media it plays, the font it is set in,@@ -67,9 +77,24 @@ export interface ItemKindDef<K extends ItemKind> {const deep = <T>(v: T): T => (v === undefined ? v : (JSON.parse(JSON.stringify(v)) as T));+function splitMedia<T extends VideoClip | AudioClip>(item: T, at: number): [T, T] {+ const cut = retimeOf(item).srcAt(at - item.start);+ return item.reverse+ ? [{ ...item, in: cut }, { ...item, start: at, out: cut }]+ : [{ ...item, out: cut }, { ...item, start: at, in: cut }];+}+export const ITEM_KINDS: { [K in ItemKind]: ItemKindDef<K> } = {clip: {list: (s) => s.clips,+ withList: (s, clips) => ({ ...s, clips }),+ duration: (c) => retimeOf(c).len,+ lane: (c) => c.track,+ at: (c, start) => ({ ...c, start }),+ split: (c, at) => {+ const [left, right] = splitMedia(c, at);+ return [{ ...left, transition: undefined, transitionStyle: undefined, animOut: undefined }, { ...right, animIn: undefined }];+ },clone: deep,assetIds: (c) =>[c.assetId, c.removal?.matte?.assetId, c.removal?.backdrop?.assetId].filter(@@ -101,6 +126,11 @@ export const ITEM_KINDS: { [K in ItemKind]: ItemKindDef<K> } = {},audio: {list: (s) => s.audioClips,+ withList: (s, audioClips) => ({ ...s, audioClips }),+ duration: (a) => retimeOf(a).len,+ lane: (a) => a.lane ?? 0,+ at: (a, start) => ({ ...a, start }),+ split: splitMedia,clone: deep,assetIds: (a) => (a.assetId ? [a.assetId] : []),remapAssets: (a, to) => ({ ...a, assetId: to(a.assetId) }),@@ -108,6 +138,11 @@ export const ITEM_KINDS: { [K in ItemKind]: ItemKindDef<K> } = {},overlay: {list: (s) => s.overlays,+ withList: (s, overlays) => ({ ...s, overlays }),+ duration: (o) => o.end - o.start,+ lane: (o) => o.lane ?? 0,+ at: (o, start) => ({ ...o, start, end: start + o.end - o.start }),+ split: (o, at) => [{ ...o, end: at }, { ...o, start: at }],clone: deep,assetIds: (o) => {if (isStickerOverlay(o)) return o.assetId ? [o.assetId] : [];@@ -129,6 +164,21 @@ export const ITEM_KINDS: { [K in ItemKind]: ItemKindDef<K> } = {},cue: {list: (s) => s.subtitles.cues,+ withList: (s, cues) => ({ ...s, subtitles: { ...s.subtitles, cues } }),+ duration: (c) => c.end - c.start,+ lane: (c) => c.lane ?? 0,+ at: (c, start) => ({ ...c, start, end: start + c.end - c.start,+ ...(c.words ? { words: c.words.map((w) => ({ ...w, t0: w.t0 + start - c.start, t1: w.t1 + start - c.start })) } : {}),+ }),+ split: (c, at) => {+ const left = c.words?.filter((w) => w.t0 < at);+ const right = c.words?.filter((w) => w.t0 >= at);+ const offset = Math.round(c.text.length * ((at - c.start) / (c.end - c.start)));+ return [+ { ...c, end: at, text: left?.length ? left.map((w) => w.w).join(" ") : c.text.slice(0, offset).trim() || c.text, words: left?.length ? left : undefined },+ { ...c, start: at, text: right?.length ? right.map((w) => w.w).join(" ") : c.text.slice(offset).trim() || c.text, words: right?.length ? right : undefined },+ ];+ },clone: deep,assetIds: () => [],remapAssets: (c) => c,@@ -136,6 +186,11 @@ export const ITEM_KINDS: { [K in ItemKind]: ItemKindDef<K> } = {},transition: {list: (s) => s.transitions,+ withList: (s, transitions) => ({ ...s, transitions }),+ duration: (t) => t.seconds,+ lane: () => 0,+ at: (t, start) => ({ ...t, start }),+ split: null,clone: deep,assetIds: () => [],remapAssets: (t) => t,@@ -178,3 +233,6 @@ export function listedAssetIds(s: ItemLists): Set<string> {export const assertNever = (x: never): never => {throw new Error(`Unhandled item kind ${String(x)}`);};++/** Explicit editing capability, shared by controls and commands. */+export const canSplitItem = (kind: ItemKind) => ITEM_KINDS[kind].split !== null;diff --git a/site/src/cut/lib/laneTracks.test.ts b/site/src/cut/lib/laneTracks.test.tsindex 978030d9..4e4c4ee4 100644--- a/site/src/cut/lib/laneTracks.test.ts+++ b/site/src/cut/lib/laneTracks.test.ts@@ -232,6 +232,49 @@ describe("commitRow on the audio lanes", () => {});});+describe("element rows use the shared drop and move placement", () => {+ const add = {+ text: (lane: number) => s().addOverlay({ at: 4, lane }),+ shape: (lane: number) => s().addShape("rect", { at: 4, lane }),+ sticker: (lane: number) => s().addSticker({ assetId: "sticker", at: 4, lane }),+ effect: (lane: number) => s().addEffect("blur", { at: 4, lane }),+ };+ for (const [kind, place] of Object.entries(add)) {+ for (const row of [-1, 0, 1, 2]) {+ test(`${kind} drops on row ${row} and undoes as one edit`, () => {+ s().addOverlay({ at: 0, lane: 0 });+ s().addShape("rect", { at: 0, lane: 1 });+ const before = s().overlays;+ landOnRow("overlay", row, place);+ const landed = s().overlays.find((o) => o.id === s().selection?.id)!;+ expect(landed.lane).toBe(Math.max(0, row));+ expect(landed.start).toBe(4);+ expect(s().overlays.find((o) => o.id === before[0].id)?.lane).toBe(row < 0 ? 1 : 0);+ expect(s().overlays.find((o) => o.id === before[1].id)?.lane).toBe(row < 0 ? 2 : 1);+ s().undo();+ expect(s().overlays).toEqual(before);+ });+ }+ test(`${kind} opens the first row of an empty band`, () => {+ landOnRow("overlay", 0, place);+ expect(s().overlays).toHaveLength(1);+ expect(s().overlays[0].lane).toBe(0);+ });+ test(`${kind} can move to a fresh row at either end`, () => {+ s().addOverlay({ at: 0, lane: 0 });+ place(0);+ const id = s().selection!.id;+ s().pushHistory();+ commitRow("overlay", id, -1);+ expect(s().overlays.find((o) => o.id === id)?.lane).toBe(0);+ expect(s().overlays.find((o) => o.id !== id)?.lane).toBe(1);+ commitRow("overlay", id, 2);+ expect(s().overlays.find((o) => o.id === id)?.lane).toBe(1);+ expect(s().overlays.find((o) => o.id !== id)?.lane).toBe(0);+ });+ }+});+describe("a cross-row move keeps the lane sound", () => {test("landing among residents parts them and the commit takes the row", () => {const mover = vclip({ track: 0, start: 0, out: 2 });diff --git a/site/src/cut/lib/laneTracks.ts b/site/src/cut/lib/laneTracks.tsindex 272a92a5..e8df4081 100644--- a/site/src/cut/lib/laneTracks.ts+++ b/site/src/cut/lib/laneTracks.ts@@ -26,18 +26,19 @@ import { retimeOf, type Retimable, type SpeedNode } from "@donkeycut/effects-kit* lane — the store's `nextFreeStart` is that one primitive.* - Cut: the store's `splitAtPlayhead` slices whichever kind is selected.*- * An adapter can vary exactly three things: `closesGap` (a video clip's- * slot heals behind it; a sound bed or title keeps its absolute time),- * `rowsDescend` (the video stack draws track 0 at the bottom), and- * `multiLane` (cues never leave their language row).+ * Adapters describe source bounds, row orientation and whether items can+ * change rows. Selection, editing and history belong to this coordinator.*/import type React from "react";import { refFromAsset, startPointerRefDrag } from "./assetRef";import { startDrag } from "./drag";import { additiveClick, snapHeldOff } from "./hostKeys";-import { clipLen, getClipSpans, moveOverlayGroup, nextFreeStart, overlayLaneOrder, projectDuration, reanchorTransitions, startTrimRipple, useEditor } from "./store";+import { getClipSpans, moveOverlayGroup, nextFreeStart, overlayLaneOrder, projectDuration, useEditor } from "./store";+import { ITEM_KINDS, type ItemKind, type ItemOf } from "./itemKinds";+import { expandTimelineGroups } from "./timelineGroups";import { playheadAt } from "./playhead";+import { TRANSITION_MAX } from "./types";import type {AudioClip,MediaAsset,@@ -45,6 +46,7 @@ import type {Selection,SubtitleCue,VideoClip,+ TimelineTransition,} from "./types";type S = ReturnType<typeof useEditor.getState>;@@ -52,7 +54,7 @@ type S = ReturnType<typeof useEditor.getState>;// A drag lane: one per doc structure. "video" is every video track (a// clip's lane is its track number); "overlay" is the title lanes, where every// overlay element kind rides one adapter.-export type LaneKind = "video" | "audio" | "overlay" | "cue";+export type LaneKind = Exclude<ItemKind, "clip"> | "video";/** The Selection kind a lane maps to: a video clip selects as `"clip"`. */const laneSelectionKind = (kind: LaneKind): NonNullable<Selection>["kind"] =>@@ -175,13 +177,7 @@ interface LaneAdapter<T> {/** The asset's beat grid mapped through this item onto the timeline —* snap targets, so an edge lands on the music. Media kinds only. */beatTimes?(s: S, raw: T): number[];- /** A lifted item's slot closes behind it: while one drags, same-lane items- * past its old spot rest slid left by its length, so the run heals the- * moment the item leaves. Video tracks set this; free-form lanes (audio,- * titles, cues) hold every resting spot. */- closesGap?: boolean;- /** After a committed move: keep the list sorted, re-seat what rides the- * cuts. `before` is the state the gesture started from. */+ /** Keep the item list sorted after a committed move or trim. */onMoved?(before: S): void;/** After a committed move, shift companions that ride along — a grouped* overlay's peers keep their relative timing. Same undo step. */@@ -236,9 +232,9 @@ const videoAdapter: LaneAdapter<VideoClip> = {multiLane: true,rowsDescend: true,raws: (s) => s.clips,- view: (c) => ({ id: c.id, start: c.start, len: clipLen(c), lane: c.track }),+ view: (c) => ({ id: c.id, start: c.start, len: ITEM_KINDS.clip.duration(c), lane: ITEM_KINDS.clip.lane(c) }),apply: (patches) => useEditor.getState().updateClipsTransient(patches),- movePatch: (c, start) => ({ id: c.id, patch: { start } }),+ movePatch: (c, start) => ({ id: c.id, patch: ITEM_KINDS.clip.at(c, start) }),trimLeftPatch: (c, newStart) => ({id: c.id,patch: { start: newStart, ...headTrim(c, newStart, c.start) },@@ -254,25 +250,18 @@ const videoAdapter: LaneAdapter<VideoClip> = {leftFloor: (s, c) => headFloor(c, s.assets.find((x) => x.id === c.assetId)?.duration),maxLen: videoMaxLen,lanePatch: (c, lane) => ({ id: c.id, patch: { track: lane } }),- closesGap: true,assetOf: (s, c) => s.assets.find((x) => x.id === c.assetId),beatTimes: mediaBeatTimes,- // Clips stay sorted by start, and the transition bars re-seat onto the- // cuts the move carried — the same settle every placement does.- onMoved: (before) =>- useEditor.setState((st) => {- const clips = [...st.clips].sort((a, b) => a.start - b.start);- return { clips, transitions: reanchorTransitions(before.clips, clips, st.transitions) };- }),+ onMoved: () => useEditor.getState().sortClips(),};const audioAdapter: LaneAdapter<AudioClip> = {minLen: 0.15,multiLane: true,raws: (s) => s.audioClips,- view: (a) => ({ id: a.id, start: a.start, len: clipLen(a), lane: a.lane ?? 0 }),+ view: (a) => ({ id: a.id, start: a.start, len: ITEM_KINDS.audio.duration(a), lane: ITEM_KINDS.audio.lane(a) }),apply: (patches) => useEditor.getState().updateAudiosTransient(patches),- movePatch: (a, start) => ({ id: a.id, patch: { start } }),+ movePatch: (a, start) => ({ id: a.id, patch: ITEM_KINDS.audio.at(a, start) }),trimLeftPatch: (a, newStart) => ({id: a.id,patch: { start: newStart, ...headTrim(a, newStart, a.start) },@@ -296,9 +285,9 @@ const textAdapter: LaneAdapter<Overlay> = {minLen: 0.2,multiLane: true,raws: (s) => s.overlays,- view: (o) => ({ id: o.id, start: o.start, len: o.end - o.start, lane: o.lane ?? 0 }),+ view: (o) => ({ id: o.id, start: o.start, len: ITEM_KINDS.overlay.duration(o), lane: ITEM_KINDS.overlay.lane(o) }),apply: (patches) => useEditor.getState().updateOverlaysTransient(patches),- movePatch: (o, start) => ({ id: o.id, patch: { start, end: start + (o.end - o.start) } }),+ movePatch: (o, start) => ({ id: o.id, patch: ITEM_KINDS.overlay.at(o, start) }),trimLeftPatch: (o, newStart) => ({ id: o.id, patch: { start: newStart } }),trimRightPatch: (o, newEnd) => ({ id: o.id, patch: { end: newEnd } }),leftFloor: () => 0,@@ -313,18 +302,10 @@ const cueAdapter: LaneAdapter<SubtitleCue> = {// its language, and tracks are managed in the panel (capped at three).multiLane: false,raws: (s) => s.subtitles.cues,- view: (c) => ({ id: c.id, start: c.start, len: c.end - c.start, lane: c.lane ?? 0 }),+ view: (c) => ({ id: c.id, start: c.start, len: ITEM_KINDS.cue.duration(c), lane: ITEM_KINDS.cue.lane(c) }),apply: (patches) => useEditor.getState().updateCuesTransient(patches),- // Retiming detaches a cue from its word timings; an unmoved patch restores- // the originals, so parted neighbors that flow back keep theirs.- movePatch: (c, start) => ({- id: c.id,- patch: {- start,- end: start + (c.end - c.start),- words: Math.abs(start - c.start) < 1e-6 ? c.words : undefined,- },- }),+ // A moved cue keeps its words at the same offsets within the cue.+ movePatch: (c, start) => ({ id: c.id, patch: ITEM_KINDS.cue.at(c, start) }),trimLeftPatch: (c, newStart) => ({ id: c.id, patch: { start: newStart, words: undefined } }),trimRightPatch: (c, newEnd) => ({ id: c.id, patch: { end: newEnd, words: undefined } }),leftFloor: () => 0,@@ -332,7 +313,23 @@ const cueAdapter: LaneAdapter<SubtitleCue> = {onMoved: () => useEditor.getState().sortCues(),};-type LaneRaw = VideoClip | AudioClip | Overlay | SubtitleCue;+const transitionAdapter: LaneAdapter<TimelineTransition> = {+ minLen: 0.1,+ multiLane: false,+ raws: (s) => s.transitions,+ view: (t) => ({ id: t.id, start: t.start, len: ITEM_KINDS.transition.duration(t), lane: ITEM_KINDS.transition.lane(t) }),+ apply: (patches) => {+ const byId = new Map(patches.map((p) => [p.id, p.patch]));+ useEditor.setState((s) => ({ transitions: s.transitions.map((t) => byId.has(t.id) ? { ...t, ...byId.get(t.id) } : t) }));+ },+ movePatch: (t, start) => ({ id: t.id, patch: ITEM_KINDS.transition.at(t, start) }),+ trimLeftPatch: (t, start) => ({ id: t.id, patch: { start, seconds: t.start + t.seconds - start } }),+ trimRightPatch: (t, end) => ({ id: t.id, patch: { seconds: end - t.start } }),+ leftFloor: (_s, t) => Math.max(0, t.start + t.seconds - TRANSITION_MAX),+ maxLen: () => TRANSITION_MAX,+};++type LaneRaw = ItemOf[ItemKind];// The generic parameter is erased at the registry boundary; each gesture only// feeds an adapter values that came out of that same adapter, so this is safe.const ADAPTERS: Record<LaneKind, LaneAdapter<LaneRaw>> = {@@ -340,6 +337,7 @@ const ADAPTERS: Record<LaneKind, LaneAdapter<LaneRaw>> = {audio: audioAdapter as unknown as LaneAdapter<LaneRaw>,overlay: textAdapter as unknown as LaneAdapter<LaneRaw>,cue: cueAdapter as unknown as LaneAdapter<LaneRaw>,+ transition: transitionAdapter as unknown as LaneAdapter<LaneRaw>,};/** Logical times an edge can snap to: the timeline start, video track 0's@@ -541,40 +539,25 @@ interface GroupMember {}const memberOf = (s: S, sel: NonNullable<Selection>): GroupMember | null => {- if (sel.kind === "clip") {- const c = s.clips.find((x) => x.id === sel.id);- return c ? { kind: "video", raw: c, id: c.id, start: c.start, len: clipLen(c), lane: c.track } : null;- }- if (sel.kind === "audio") {- const a = s.audioClips.find((x) => x.id === sel.id);- return a ? { kind: "audio", raw: a, id: a.id, start: a.start, len: clipLen(a), lane: a.lane ?? 0 } : null;- }- if (sel.kind === "overlay") {- const o = s.overlays.find((x) => x.id === sel.id);- return o ? { kind: "overlay", raw: o, id: o.id, start: o.start, len: o.end - o.start, lane: o.lane ?? 0 } : null;- }- if (sel.kind === "cue") {- const c = s.subtitles.cues.find((x) => x.id === sel.id);- return c ? { kind: "cue", raw: c, id: c.id, start: c.start, len: c.end - c.start, lane: c.lane ?? 0 } : null;- }- return null;+ const kind: LaneKind = sel.kind === "clip" ? "video" : sel.kind;+ const adapter = ADAPTERS[kind];+ const raw = adapter.raws(s).find((item) => item.id === sel.id);+ return raw ? { kind, raw, ...adapter.view(raw) } : null;};/** The set a grab on `id` carries when the item sits in a multi-selection:- * every selected lane item, and the unselected peers of any grouped overlay- * among them, so a group never tears apart. Null when the grab is on its- * own. */-function selectedMembers(+ * every selected item and every member of its explicit timeline group.+ * Null when the grab is on its own. */+export function selectedMembers(s: S,kind: LaneKind,id: string): { grabbed: GroupMember; members: GroupMember[] } | null {const selKind = laneSelectionKind(kind);- if (- s.multiSelection.length < 2 ||- !s.multiSelection.some((m) => m?.kind === selKind && m.id === id)- )- return null;+ const selected = s.multiSelection.some((m) => m?.kind === selKind && m.id === id)+ ? s.multiSelection : [{ kind: selKind, id }];+ const selection = expandTimelineGroups(s, selected);+ if (selection.length < 2) return null;const seen = new Set<string>();const members: GroupMember[] = [];const admit = (m: GroupMember | null) => {@@ -584,13 +567,7 @@ function selectedMembers(seen.add(key);members.push(m);};- for (const sel of s.multiSelection) if (sel) admit(memberOf(s, sel));- for (const m of [...members]) {- const gid = m.kind === "overlay" ? (m.raw as Overlay).groupId : undefined;- if (!gid) continue;- for (const peer of s.overlays.filter((o) => o.groupId === gid))- admit(memberOf(s, { kind: "overlay", id: peer.id }));- }+ for (const sel of selection) admit(memberOf(s, sel));const grabbed = members.find((m) => m.kind === kind && m.id === id) ?? members.find((m) => m.id === id);return grabbed && members.length > 1 ? { grabbed, members } : null;}@@ -617,6 +594,7 @@ function startGroupMove(useEditor.setState({selection: { kind: laneSelectionKind(grabbed.kind), id: grabbed.id },selectedKey: null,+ multiSelection: members.map((m) => ({ kind: laneSelectionKind(m.kind), id: m.id })),});if (s.playing) s.setPlaying(false);const grabTime =@@ -643,7 +621,7 @@ function startGroupMove(.map((raw) => ({ kind, raw, view: ADAPTERS[kind].view(raw) })).filter((x) => !memberKeys.has(`${kind}:${x.view.id}`));const rest = (- [...restOf("video"), ...restOf("audio"), ...restOf("overlay"), ...restOf("cue")] as {+ (Object.keys(ADAPTERS) as LaneKind[]).flatMap(restOf) as {kind: LaneKind;raw: LaneRaw;view: LaneItem;@@ -914,20 +892,7 @@ export function startLaneMove(e: React.PointerEvent, kind: LaneKind, id: string,.raws(s).filter((r) => ad.view(r).id !== id).map((r) => ({ raw: r, view: ad.view(r) }));- // Where a neighbor rests while the drag is live. On a gap-closing lane the- // lifted clip's slot heals under it: same-lane neighbors past its old spot- // rest slid left by its length, and the parting below lays the lane out- // from these closed spots.- const restAt = (x: (typeof rest)[number]) =>- ad.closesGap && x.view.lane === self.lane && x.view.start > start0 + 1e-9- ? x.view.start - len- : x.view.start;- // The one spot on the healed home lane that overlaps nothing: past the end- // of the resting run. The lifted clip parks there while it hovers other- // tracks, so the closed gap never puts two clips on the same span.- const parked = rest- .filter((x) => x.view.lane === self.lane)- .reduce((m, x) => Math.max(m, restAt(x) + x.view.len), 0);+ const restAt = (x: (typeof rest)[number]) => x.view.start;const usedLanes = laneOrder(kind, s, [...rest.map((x) => x.view.lane), self.lane]);// The edges this drag can open a new row past. From an outermost row an// item alone there has nothing to open on its side: the row it left@@ -963,16 +928,9 @@ export function startLaneMove(e: React.PointerEvent, kind: LaneKind, id: string,// restores of previously shifted ones): dragging one cue must not rebuild// hundreds of unmoved neighbors on every mousemove.//- // On a gap-closing lane the lifted item rides along too (`selfStart`): the- // store mirrors the previewed layout every frame — the landing slot while- // home, the parked spot while hovering other tracks — so the doc stays- // overlap-free at every instant a mid-drag autosave could catch it. The- // ghost is what the user sees, so the transient self-moves never show.- let selfAt = start0;const shifted = new Map<string, number>();const applyMoves = (- startFor: (x: (typeof rest)[number]) => number,- selfStart?: number+ startFor: (x: (typeof rest)[number]) => number) => {const patches: Patch<LaneRaw>[] = [];for (const x of rest) {@@ -984,13 +942,9 @@ export function startLaneMove(e: React.PointerEvent, kind: LaneKind, id: string,else shifted.delete(x.view.id);}}- if (ad.closesGap && selfStart !== undefined && Math.abs(selfStart - selfAt) > 1e-9) {- patches.push(ad.movePatch(raw0, selfStart));- selfAt = selfStart;- }if (patches.length) ad.apply(patches);};- const restRestore = () => applyMoves((x) => x.view.start, start0);+ const restRestore = () => applyMoves((x) => x.view.start);startDrag(e, {onMove: (dx, dy, ev) => {@@ -1042,7 +996,7 @@ export function startLaneMove(e: React.PointerEvent, kind: LaneKind, id: string,// The aimed row's neighbors part around the cursor. Order comes from// the original midpoints, so a lifted item keeps its spot until the// pointer truly crosses a neighbor's middle; the runs themselves sit at- // their resting spots (closed on gap-closing lanes).+ // their resting spots.const part = partAround(rest.filter((x) => x.view.lane === lane),pointerTime,@@ -1053,10 +1007,7 @@ export function startLaneMove(e: React.PointerEvent, kind: LaneKind, id: string,if (part.clamped) guide = null;slotStart = part.slotStart;ui.onSnap(guide);- // On a gap-closing lane the lifted item rides its slot while home and- // parks past the healed run while it aims at another row, so the doc- // never holds two items on one span.- applyMoves(part.at, targetRow === ui.homeRow ? slotStart : parked);+ applyMoves(part.at);ui.onDrag({kind,id,@@ -1217,34 +1168,7 @@ export function startLaneTrim(.raws(s).map((r) => ({ raw: r, view: ad.view(r) })).filter((x) => x.view.id !== id && x.view.lane === self.lane);- // While track 0 is the only video track, a spine trim ripples: everything- // past the clip's tail — clips, titles, captions, soundtrack — rides the- // moved edge in both directions, every gap keeping its width. With overlay- // video tracks present the engine is null (the delete gate) and the trim- // keeps its own track's push rules.- const ripple =- kind === "video" && self.lane === 0- ? (startTrimRipple(s, id, self.start + self.len) as {- move: (shift: number, clipPatches: Patch<LaneRaw>[]) => void;- settle: (close?: { at: number; shift: number }) => void;- } | null)- : null;- // The clip layout the gesture started from, so a trim with no ripple engine- // behind it can still map the bars onto where it left the cuts.- const clips0 = kind === "video" && self.lane === 0 ? s.clips : null;- /** Close the gesture: the ripple settles the document when there is one,- * and either way the transition bars re-seat onto the cuts the trim moved.- * A dissolve follows its cut whether or not the project has overlay tracks. */- const settle = (close?: { at: number; shift: number }) => {- if (ripple) {- ripple.settle(close);- return;- }- if (!clips0) return;- const st = useEditor.getState();- if (st.transitions.length)- useEditor.setState({ transitions: reanchorTransitions(clips0, st.clips, st.transitions) });- };+ const settle = () => ad.onMoved?.(s);if (side === "l") {const start0 = self.start;@@ -1337,16 +1261,12 @@ export function startLaneTrim(const end = ad.revealLeftPatch? Math.max(start, floor) + len0 + (start0 - reveal): start0 + len0;- if (ripple) {- ripple.move(end - (start0 + len0), patches);- } else {- const delta = Math.max(0, end - nextStart);- if (delta !== lastDelta) {- patches.push(...followers.map((f) => ad.movePatch(f.raw, f.view.start + delta)));- lastDelta = delta;- }- ad.apply(patches);+ const delta = Math.max(0, end - nextStart);+ if (delta !== lastDelta) {+ patches.push(...followers.map((f) => ad.movePatch(f.raw, f.view.start + delta)));+ lastDelta = delta;}+ ad.apply(patches);},onUp: () => {ui.onSnap(null);@@ -1354,10 +1274,7 @@ export function startLaneTrim(const cur = ad.raws(useEditor.getState()).find((r) => ad.view(r).id === id);const from = cur ? ad.view(cur).start : floor;if (from >= floor - 1e-4) {- // Settled within the room. A head trimmed to the right left its- // trimmed footage as a gap; the ripple closes it, pulling the clip- // and everything after back onto the footage that survives.- settle(from > start0 + 1e-4 ? { at: start0, shift: start0 - from } : undefined);+ settle();return;}// Elastic spring back to the floor. `finish` lands the floor exactly,@@ -1418,18 +1335,10 @@ export function startLaneTrim(// Followers respond only to travel up to the ceiling, so the overshoot// gives visually without shoving the run — and springing back needs no// re-lay, just as packed leaders hold at the floor on the left edge.- if (ripple) {- // The document rides the edge both ways, every gap keeping its width.- ripple.move(Math.min(end, ceil) - end0, [ad.trimRightPatch(raw0, end)]);- } else {- const delta = Math.max(0, Math.min(end, ceil) - nextStart);- const run =- delta === lastDelta- ? []- : followers.map((f) => ad.movePatch(f.raw, f.view.start + delta));- lastDelta = delta;- ad.apply([ad.trimRightPatch(raw0, end), ...run]);- }+ const delta = Math.max(0, Math.min(end, ceil) - nextStart);+ const run = delta === lastDelta ? [] : followers.map((f) => ad.movePatch(f.raw, f.view.start + delta));+ lastDelta = delta;+ ad.apply([ad.trimRightPatch(raw0, end), ...run]);},onUp: () => {ui.onSnap(null);@@ -1498,6 +1407,7 @@ function startGroupTrim(useEditor.setState({selection: { kind: laneSelectionKind(grabbed.kind), id: grabbed.id },selectedKey: null,+ multiSelection: members.map((m) => ({ kind: laneSelectionKind(m.kind), id: m.id })),});if (s.playing) s.setPlaying(false);s.pushHistory();@@ -1527,7 +1437,6 @@ function startGroupTrim(return { at: end, lo: m.start + ad.minLen, hi: Math.min(nextStart, m.start + ad.maxLen(s, m.raw)) };});const gi = members.indexOf(grabbed);- const clips0 = members.some((m) => m.kind === "video") ? s.clips : null;const apply = (at: number[]) => {const buckets = new Map<LaneKind, Patch<LaneRaw>[]>();members.forEach((m, i) => {@@ -1552,11 +1461,7 @@ function startGroupTrim(},onUp: () => {ui.onSnap(null);- // Transition bars re-seat onto the cuts the trim moved.- if (!clips0) return;- const st = useEditor.getState();- if (st.transitions.length)- useEditor.setState({ transitions: reanchorTransitions(clips0, st.clips, st.transitions) });+ for (const k of new Set(members.map((m) => m.kind))) ADAPTERS[k].onMoved?.(s);},});}diff --git a/site/src/cut/lib/overlayHosts.test.ts b/site/src/cut/lib/overlayHosts.test.tsdeleted file mode 100644index 99bbafab..00000000--- a/site/src/cut/lib/overlayHosts.test.ts+++ /dev/null@@ -1,88 +0,0 @@-import { describe, expect, test } from "bun:test";-import type { Overlay } from "@donkeycut/effects-kit";-import type { VideoClip } from "./types";-import { adoptOverlayHosts, hostClipFor, settleOverlayHosts } from "./overlayHosts";--const clip = (p: Partial<VideoClip> & { id: string }): VideoClip =>- ({ track: 0, assetId: "a", start: 0, in: 0, out: 10, muted: false, ...p }) as VideoClip;-const title = (p: Partial<Overlay> & { id: string }): Overlay =>- ({ kind: "text", text: "hi", x: 0.5, y: 0.5, start: 2, end: 4, ...p }) as Overlay;--const c = clip({ id: "c1", start: 10, in: 0, out: 10 });-const t = title({ id: "t1", start: 12, end: 14, hostClipId: "c1" });--describe("element hosts", () => {- test("a placed element homes to the clip under its middle, highest track first", () => {- const low = clip({ id: "lo", start: 0, out: 20, track: 0 });- const high = clip({ id: "hi", start: 5, out: 10, track: 1 });- expect(hostClipFor([low, high], { start: 6, end: 8 })?.id).toBe("hi");- expect(hostClipFor([low, high], { start: 1, end: 3 })?.id).toBe("lo");- expect(hostClipFor([low, high], { start: 30, end: 32 })).toBeUndefined();- });-- test("a moved host carries its element by the same distance", () => {- const moved = { ...c, start: 40 };- const r = settleOverlayHosts([c], [moved], [t], [t])!;- expect(r.overlays[0].start).toBe(42);- expect(r.overlays[0].end).toBe(44);- expect(r.carried.has("t1")).toBe(true);- });-- test("a retimed host stretches the element with the picture, keys included", () => {- const keyed = { ...t, kf: [{ t: 1, x: 0.2, y: 0.2 }] } as Overlay;- const slow = { ...c, speed: 0.5 };- const r = settleOverlayHosts([c], [slow], [keyed], [keyed])!;- expect(r.overlays[0].start).toBeCloseTo(14, 6);- expect(r.overlays[0].end).toBeCloseTo(18, 6);- expect(r.overlays[0].kf![0].t).toBeCloseTo(2, 6);- });-- test("a head trim leaves the element on the frames it covered", () => {- const trimmed = { ...c, start: 11, in: 1 };- expect(settleOverlayHosts([c], [trimmed], [t], [t])).toBeNull();- });-- test("a split hands the element to the half that plays its frames", () => {- const left = { ...c, out: 1.5 };- const right = clip({ id: "c2", start: 11.5, in: 1.5, out: 10 });- const r = settleOverlayHosts([c], [left, right], [t], [t])!;- expect(r.overlays[0].start).toBe(12);- expect(r.overlays[0].hostClipId).toBe("c2");- });-- test("a ripple that already moved the element with its footage changes nothing", () => {- const moved = { ...c, start: 15 };- const rippled = { ...t, start: 17, end: 19 };- expect(settleOverlayHosts([c], [moved], [t], [rippled])).toBeNull();- });-- test("an element dragged by hand homes where it lands", () => {- const other = clip({ id: "c2", start: 30, out: 10 });- const dragged = { ...t, start: 33, end: 35 };- const r = settleOverlayHosts([c, other], [c, other], [t], [dragged])!;- expect(r.overlays[0].hostClipId).toBe("c2");- });-- test("a deleted host leaves the element in place, homed to what is there now", () => {- const next = clip({ id: "c2", start: 10, out: 10 });- const r = settleOverlayHosts([c], [next], [t], [t])!;- expect(r.overlays[0].start).toBe(12);- expect(r.overlays[0].hostClipId).toBe("c2");- const gone = settleOverlayHosts([c], [], [t], [t])!;- expect(gone.overlays[0].hostClipId).toBeUndefined();- });-- test("a freed element stays free through every write", () => {- const free = { ...t, hostClipId: null } as Overlay;- expect(settleOverlayHosts([c], [{ ...c, start: 40 }], [free], [free])).toBeNull();- const dragged = { ...free, start: 20, end: 22 };- expect(settleOverlayHosts([c], [c], [free], [dragged])).toBeNull();- });-- test("an older document's elements adopt the clips under them on load", () => {- const bare = title({ id: "t2", start: 12, end: 14 });- expect(adoptOverlayHosts([c], [bare])[0].hostClipId).toBe("c1");- const off = title({ id: "t3", start: 50, end: 52 });- expect(adoptOverlayHosts([c], [off])[0].hostClipId).toBeUndefined();- });-});diff --git a/site/src/cut/lib/overlayHosts.ts b/site/src/cut/lib/overlayHosts.tsdeleted file mode 100644index c2a7c5a5..00000000--- a/site/src/cut/lib/overlayHosts.ts+++ /dev/null@@ -1,143 +0,0 @@-import { retimeOf, type Overlay } from "@donkeycut/effects-kit";-import type { VideoClip } from "./types";--// An element rides the frames of the clip under it. Its start and end are-// remembered as source seconds of that clip, so wherever those frames go —-// the clip moved, retimed, trimmed, split — the element goes over the same-// footage: a move carries it, a speed change stretches it with the picture,-// a trim leaves it on the frames it covered, and a split re-homes it to the-// half that now plays them. The store's write wrapper runs this on every-// write, transient drag frames included, so no edit path can leave a hosted-// element behind.--const EPS = 1e-6;-const NEAR = 1e-3;-const MIN_LEN = 0.1;--const spanEnd = (c: VideoClip) => c.start + retimeOf(c).len;--/** The clip whose frames sit under the element's middle; the highest track- * wins, since that is the picture the element is drawn over. */-export function hostClipFor(- clips: readonly VideoClip[],- o: Pick<Overlay, "start" | "end">-): VideoClip | undefined {- const mid = (o.start + o.end) / 2;- let best: VideoClip | undefined;- for (const c of clips) {- if (mid < c.start - EPS || mid >= spanEnd(c) + EPS) continue;- if (!best || c.track > best.track) best = c;- }- return best;-}--const spanChanged = (a: VideoClip, b: VideoClip) =>- a.start !== b.start ||- a.in !== b.in ||- a.out !== b.out ||- a.speed !== b.speed ||- a.speedCurve !== b.speedCurve ||- a.reverse !== b.reverse;--/** The element over the same source seconds of `host` that it covered of- * `was`. The curve maps linearly past either end, so an element leading into- * or trailing out of the clip keeps that lead. A stretched element scales its- * keyed poses with it, so a key stays on the frame it was set on. */-function ride(o: Overlay, was: VideoClip, host: VideoClip): Overlay {- const from = retimeOf(was);- const to = retimeOf(host);- let start = host.start + to.tAt(from.srcAt(o.start - was.start));- let end = host.start + to.tAt(from.srcAt(o.end - was.start));- // A clip turned around plays those source seconds the other way, so the- // element's window lands mirrored.- if (end < start) [start, end] = [end, start];- if (end - start < MIN_LEN) end = start + MIN_LEN;- if (start < 0) {- end -= start;- start = 0;- }- const scale = (end - start) / Math.max(EPS, o.end - o.start);- const next: Overlay = { ...o, start, end };- if (Math.abs(scale - 1) > EPS) {- if (o.kf?.length) next.kf = o.kf.map((k) => ({ ...k, t: k.t * scale }));- if (o.mask?.kf?.length)- next.mask = { ...o.mask, kf: o.mask.kf.map((k) => ({ ...k, t: k.t * scale })) };- }- return next;-}--const near = (a: Pick<Overlay, "start" | "end">, b: Pick<Overlay, "start" | "end">) =>- Math.abs(a.start - b.start) < NEAR && Math.abs(a.end - b.end) < NEAR;--/** Every element after a write: hosted ones carried with their host's frames,- * placed ones and ones whose host went re-homed to the clip under them.- * Null when the write changes nothing here. `carried` names the elements- * this moved, for the caller to part the others clear of. */-export function settleOverlayHosts(- prevClips: readonly VideoClip[],- nextClips: readonly VideoClip[],- prevOverlays: readonly Overlay[],- nextOverlays: readonly Overlay[]-): { overlays: Overlay[]; carried: Set<string> } | null {- if (prevClips === nextClips && prevOverlays === nextOverlays) return null;- const prevClip = new Map(prevClips.map((c) => [c.id, c]));- const nextClip = new Map(nextClips.map((c) => [c.id, c]));- const prevOv = prevOverlays === nextOverlays ? null : new Map(prevOverlays.map((o) => [o.id, o]));- const carried = new Set<string>();- let changed = false;- const overlays = nextOverlays.map((o) => {- if (o.hostClipId === null) return o;- const before = prevOv ? prevOv.get(o.id) : o;- // The write positioned this element itself: it was created, or its span moved.- const placed = !before || before.start !== o.start || before.end !== o.end;- const host = o.hostClipId ? nextClip.get(o.hostClipId) : undefined;- if (host && before) {- const was = prevClip.get(host.id);- if (was && spanChanged(was, host)) {- const expected = ride(before, was, host);- if (!placed) {- // Its frames stayed put through a trim: nothing to carry.- const landed = near(expected, before) ? o : expected;- if (landed !== o) {- carried.add(o.id);- changed = true;- }- const mid = (landed.start + landed.end) / 2;- if (mid >= host.start - EPS && mid < spanEnd(host) + EPS) return landed;- // Its frames left the host — cut away, or split into another clip —- // so it homes to the clip that plays them now.- const id = hostClipFor(nextClips, landed)?.id;- if (id === landed.hostClipId) return landed;- changed = true;- return { ...landed, hostClipId: id };- }- // A ripple moved the element with its footage already.- if (near(expected, o)) return o;- // Placed by hand while its host changed: it homes where it landed.- } else if (!placed) {- return o;- }- } else if (!placed && !o.hostClipId) {- return o;- }- const id = hostClipFor(nextClips, o)?.id;- if (id === o.hostClipId || (id === undefined && o.hostClipId === undefined)) return o;- changed = true;- return { ...o, hostClipId: id };- });- return changed ? { overlays, carried } : null;-}--/** A loaded document's elements homed to the clips under them, for docs from- * before elements had hosts. Elements that already say stay as they say. */-export function adoptOverlayHosts(clips: readonly VideoClip[], overlays: readonly Overlay[]): Overlay[] {- let changed = false;- const next = overlays.map((o) => {- if (o.hostClipId !== undefined) return o;- const id = hostClipFor(clips, o)?.id;- if (!id) return o;- changed = true;- return { ...o, hostClipId: id };- });- return changed ? next : (overlays as Overlay[]);-}diff --git a/site/src/cut/lib/projectTemplate.ts b/site/src/cut/lib/projectTemplate.tsindex 2de481bc..e17bdbd8 100644--- a/site/src/cut/lib/projectTemplate.ts+++ b/site/src/cut/lib/projectTemplate.ts@@ -111,7 +111,7 @@ export function templateFromDoc(doc: ProjectDoc, opts: { items?: string[] } = {}for (const c of audioClips) {const mi = mediaFor(c.assetId);if (mi == null) continue;- audio.push({ media: mi, start: c.start - start0, in: c.in, out: c.out, volume: c.volume, fadeIn: c.fadeIn, fadeOut: c.fadeOut, speed: c.speed, speedCurve: c.speedCurve, reverse: c.reverse, sound: c.sound, duck: c.duck, lane: c.lane });+ audio.push({ groupId: c.groupId, media: mi, start: c.start - start0, in: c.in, out: c.out, volume: c.volume, fadeIn: c.fadeIn, fadeOut: c.fadeOut, speed: c.speed, speedCurve: c.speedCurve, reverse: c.reverse, sound: c.sound, duck: c.duck, lane: c.lane });itemByAudio.push(c.id);}const texts: TemplateSaveInput["texts"] = [];diff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.tsindex 1ac25754..ca50b209 100644--- a/site/src/cut/lib/store.test.ts+++ b/site/src/cut/lib/store.test.ts@@ -1,9 +1,15 @@import { beforeEach, describe, expect, test } from "bun:test";import { groupRemap } from "@donkeycut/effects-kit";-import { adoptTransitionFields, assetIdsInUse, clipLen, closeMicroGaps, cutTranscribeSpec, deriveTransitionFields, docOverlays, getClipSpans, liftClipLooks, moveOverlayGroup, overlayLaneOrder, normalizeElementLanes, parkedTransitions, placeInRun, projectDuration, rippleInsert, separateOverlaps, serializeDoc, startTrimRipple, useEditor } from "./store";+import { adoptTransitionFields, assetIdsInUse, clipLen, closeMicroGaps, cutTranscribeSpec, deriveTransitionFields, docOverlays, getClipSpans, liftClipLooks, moveOverlayGroup, overlayLaneOrder, normalizeElementLanes, parkedTransitions, placeInRun, projectDuration, rippleInsert, separateOverlaps, serializeDoc, useEditor } from "./store";+import type React from "react";+import { runAiTool } from "./aiTools";+import { selectedMembers, startLaneMove } from "./laneTracks";+import { expandTimelineGroups } from "./timelineGroups";+import { timelineItem } from "./timelineItems";+import { normalizeDocState } from "./store";import { playheadAt, setPlayhead, setSkim } from "./playhead";import { emptySubtitles, uploadedFontId } from "./types";-import type { AudioClip, MediaAsset, SubtitleCue, TextOverlay, VideoClip } from "./types";+import type { Selection, AudioClip, MediaAsset, SubtitleCue, TextOverlay, VideoClip } from "./types";/*** The lane invariant: segments never overlap, with no exceptions. Every@@ -767,8 +773,8 @@ describe("subtitle cues", () => {});});-describe("delete ripple and gaps", () => {- test("single-track delete ripples: later items on every track slide left", () => {+describe("independent deletion and gaps", () => {+ test("single-track delete keeps all other items in place", () => {const a = vclip({ track: 0, start: 0, out: 2 });const b = vclip({ track: 0, start: 2, out: 2 });const au = aclip({ start: 3, out: 2 });@@ -780,12 +786,12 @@ describe("delete ripple and gaps", () => {selection: { kind: "clip", id: a.id },});s().deleteSelection();- expect(clipById(b.id).start).toBeCloseTo(0);- expect(audioById(au.id).start).toBeCloseTo(1);- expect(s().overlays[0].start).toBeCloseTo(1);+ expect(clipById(b.id).start).toBeCloseTo(2);+ expect(audioById(au.id).start).toBeCloseTo(3);+ expect(s().overlays[0].start).toBeCloseTo(3);});- test("a ripple delete carries the transitions and drops the deleted clip's own", () => {+ test("deleting a clip leaves independent transitions in place", () => {const av = asset(2);const a = vclip({ track: 0, start: 0, out: 2, assetId: av.id });const b = vclip({ track: 0, start: 2, out: 2, assetId: av.id });@@ -795,14 +801,13 @@ describe("delete ripple and gaps", () => {s().setClipTransition(b.id, 0.5);s().select({ kind: "clip", id: a.id });s().deleteSelection();- // a's own blend goes with it; b's rides the slide and keeps playing.- expect(s().transitions.length).toBe(1);- expect(clipById(b.id).start).toBeCloseTo(0);+ expect(s().transitions.length).toBe(2);+ expect(clipById(b.id).start).toBeCloseTo(2);expect(clipById(b.id).transition).toBeCloseTo(0.5);expect(s().transitions[0].start).toBeCloseTo(1.5);});- test("a trim that pushes the run takes the blends along", () => {+ test("a trim pushes its row and leaves independent transitions in place", () => {const av = asset(8);const a = vclip({ track: 0, start: 0, in: 0, out: 2, assetId: av.id });const b = vclip({ track: 0, start: 2, in: 0, out: 2, assetId: av.id });@@ -812,8 +817,8 @@ describe("delete ripple and gaps", () => {s().setClipTransition(b.id, 0.5);s().updateClip(a.id, { out: 3 }); // a grows by 1s, pushing b and c rightexpect(clipById(b.id).start).toBeCloseTo(3);- expect(clipById(a.id).transition).toBeCloseTo(0.5);- expect(clipById(b.id).transition).toBeCloseTo(0.5);+ expect(s().transitions.map((t) => t.start)).toEqual([1.5, 3.5]);+ expect(parkedTransitions(s().clips, s().transitions)).toHaveLength(2);});test("clearing track 0 leaves the titles and captions standing", () => {@@ -864,11 +869,9 @@ describe("delete ripple and gaps", () => {selection: { kind: "clip", id: b.id },});s().deleteSelection();- // The clip goes; the title keeps its full second and falls back to where- // the footage under it used to start.expect(s().overlays.map((o) => o.id)).toEqual([t.id]);- expect(s().overlays[0].start).toBeCloseTo(2);- expect(s().overlays[0].end).toBeCloseTo(3);+ expect(s().overlays[0].start).toBeCloseTo(2.5);+ expect(s().overlays[0].end).toBeCloseTo(3.5);});test("a soundtrack spanning the deleted clip keeps every second of it", () => {@@ -885,10 +888,10 @@ describe("delete ripple and gaps", () => {expect(s().audioClips.map((x) => x.id)).toEqual([bed.id]);expect(audioById(bed.id).start).toBeCloseTo(0);expect(clipLen(audioById(bed.id))).toBeCloseTo(6);- expect(clipById(c.id).start).toBeCloseTo(2);+ expect(clipById(c.id).start).toBeCloseTo(4);});- test("captions over the deleted footage stand, packed and in order", () => {+ test("captions over deleted footage keep their times and words", () => {const a = vclip({ track: 0, start: 0, out: 2 });const b = vclip({ track: 0, start: 2, out: 4 });const q1 = cue({ start: 2.5, end: 3, words: [{ t0: 2.5, t1: 3, w: "one" }] });@@ -901,14 +904,14 @@ describe("delete ripple and gaps", () => {s().deleteSelection();const cues = s().subtitles.cues;expect(cues.map((c) => c.id)).toEqual([q1.id, q2.id]);- expect(cues[0].start).toBeCloseTo(2);- expect(cues[0].end).toBeCloseTo(2.5);- expect(cues[0].words![0].t0).toBeCloseTo(2);- expect(cues[1].start).toBeCloseTo(2.5);+ expect(cues[0].start).toBeCloseTo(2.5);+ expect(cues[0].end).toBeCloseTo(3);+ expect(cues[0].words![0].t0).toBeCloseTo(2.5);+ expect(cues[1].start).toBeCloseTo(3.5);expectLaneSound(cues);});- test("two titles on one row pulled back by a ripple stay apart", () => {+ test("two titles on one row keep their times after video deletion", () => {const a = vclip({ track: 0, start: 0, out: 2 });const b = vclip({ track: 0, start: 2, out: 4 });const t1 = title({ start: 2.5, end: 3 });@@ -921,10 +924,10 @@ describe("delete ripple and gaps", () => {s().deleteSelection();expect(s().overlays.map((o) => o.id)).toEqual([t1.id, t2.id]);expectLaneSound(s().overlays);- expect(s().overlays.find((o) => o.id === t2.id)!.start).toBeCloseTo(2.5);+ expect(s().overlays.find((o) => o.id === t2.id)!.start).toBeCloseTo(3.5);});- test("a grouped set crossing rows stays rigid through the ripple", () => {+ test("an unrelated group keeps its times after video deletion", () => {const a = vclip({ track: 0, start: 0, out: 2 });const b = vclip({ track: 0, start: 2, out: 6 });// Lane 0 is crowded right where the group wants to land, so its member@@ -1035,8 +1038,8 @@ describe("delete ripple and gaps", () => {});});-describe("resize ripple", () => {- test("shrinking a clip pulls everything after it along, gaps kept", () => {+describe("independent resize", () => {+ test("shrinking a clip leaves other items in place", () => {const a = vclip({ track: 0, start: 0, out: 4 });const b = vclip({ track: 0, start: 4, out: 2 });const t = title({ start: 4.5, end: 5.5 });@@ -1049,22 +1052,22 @@ describe("resize ripple", () => {subtitles: { ...emptySubtitles(), cues: [q] },});s().setClipTrim(a.id, 0, 3);- expect(clipById(b.id).start).toBeCloseTo(3);- expect(s().overlays[0].start).toBeCloseTo(3.5);- expect(audioById(au.id).start).toBeCloseTo(4);- expect(s().subtitles.cues[0].start).toBeCloseTo(3.2);- expect(s().subtitles.cues[0].words![0].t0).toBeCloseTo(3.2);+ expect(clipById(b.id).start).toBeCloseTo(4);+ expect(s().overlays[0].start).toBeCloseTo(4.5);+ expect(audioById(au.id).start).toBeCloseTo(5);+ expect(s().subtitles.cues[0].start).toBeCloseTo(4.2);+ expect(s().subtitles.cues[0].words![0].t0).toBeCloseTo(4.2);});- test("growing a clip pushes them right the same way", () => {+ test("growing a clip pushes overlapping neighbors on its own row", () => {const av = asset(8);const a = vclip({ track: 0, start: 0, out: 4, assetId: av.id });const b = vclip({ track: 0, start: 5, out: 2, assetId: av.id }); // a 1s gap before bconst t = title({ start: 5, end: 6 });useEditor.setState({ assets: [av], clips: [a, b], overlays: [t] });s().setClipTrim(a.id, 0, 6);- expect(clipById(b.id).start).toBeCloseTo(7); // its gap survives- expect(s().overlays[0].start).toBeCloseTo(7);+ expect(clipById(b.id).start).toBeCloseTo(6);+ expect(s().overlays[0].start).toBeCloseTo(5);});test("growing into an abutting neighbor carries it along, never over it", () => {@@ -1075,32 +1078,19 @@ describe("resize ripple", () => {useEditor.setState({ assets: [av], clips: [a, b], overlays: [t] });s().setClipTrim(a.id, 0, 6);expect(clipById(b.id).start).toBeCloseTo(6);- expect(s().overlays[0].start).toBeCloseTo(6);+ expect(s().overlays[0].start).toBeCloseTo(4);expectLaneSound(videoLane(0));});- test("the gesture's tail growth rides the abutting run too", () => {- // The left handle's pinned reveal and the right handle's growth both feed- // the engine a positive shift; the clip butted against the tail moves.- const a = vclip({ track: 0, start: 0, out: 4 });- const b = vclip({ track: 0, start: 4, out: 2 });- useEditor.setState({ clips: [a, b] });- const engine = startTrimRipple(s(), a.id, 4)!;- engine.move(1, [{ id: a.id, patch: { out: 5 } }]);- expect(clipById(b.id).start).toBeCloseTo(5);- expectLaneSound(videoLane(0));- engine.move(0, [{ id: a.id, patch: { out: 4 } }]);- expect(clipById(b.id).start).toBeCloseTo(4);- engine.settle();- });- test("an item standing over the shortened tail falls back to the new end", () => {++ test("an item over the shortened tail keeps its time", () => {const a = vclip({ track: 0, start: 0, out: 4 });const t = title({ start: 3, end: 5 });useEditor.setState({ clips: [a], overlays: [t] });s().setClipTrim(a.id, 0, 2);- expect(s().overlays[0].start).toBeCloseTo(2);- expect(s().overlays[0].end).toBeCloseTo(4);+ expect(s().overlays[0].start).toBeCloseTo(3);+ expect(s().overlays[0].end).toBeCloseTo(5);});test("with an overlay video track a shrink keeps its own-track rules", () => {@@ -1113,43 +1103,11 @@ describe("resize ripple", () => {expect(clipById(layer.id).start).toBeCloseTo(1);});- test("the trim gesture ripples live and a retreat flows everything back", () => {- const a = vclip({ track: 0, start: 0, out: 4 });- const b = vclip({ track: 0, start: 4, out: 2 });- const t = title({ start: 4.5, end: 5 });- useEditor.setState({ clips: [a, b], overlays: [t] });- const engine = startTrimRipple(s(), a.id, 4)!;- engine.move(-1, [{ id: a.id, patch: { out: 3 } }]);- expect(clipById(a.id).out).toBeCloseTo(3);- expect(clipById(b.id).start).toBeCloseTo(3);- expect(s().overlays[0].start).toBeCloseTo(3.5);- engine.move(0, [{ id: a.id, patch: { out: 4 } }]);- expect(clipById(b.id).start).toBeCloseTo(4);- expect(s().overlays[0].start).toBeCloseTo(4.5);- engine.settle();- });- test("settling a head trim closes the footage it removed", () => {- const a = vclip({ track: 0, start: 0, out: 4 });- const b = vclip({ track: 0, start: 4, out: 2 });- const t = title({ start: 5, end: 6 });- useEditor.setState({ clips: [a, b], overlays: [t] });- const engine = startTrimRipple(s(), a.id, 4)!;- // The left handle dragged right by 1: head trimmed, gap opened before.- engine.move(0, [{ id: a.id, patch: { start: 1, in: 1 } }]);- engine.settle({ at: 0, shift: -1 });- expect(clipById(a.id).start).toBeCloseTo(0);- expect(clipById(a.id).in).toBeCloseTo(1);- expect(clipById(b.id).start).toBeCloseTo(3);- expect(s().overlays[0].start).toBeCloseTo(4);- });- test("with an overlay video track the gesture engine stays off", () => {- const a = vclip({ track: 0, start: 0, out: 4 });- const layer = vclip({ track: 1, start: 0, out: 2 });- useEditor.setState({ clips: [a, layer] });- expect(startTrimRipple(s(), a.id, 4)).toBe(null);- });+++});describe("spine grounding", () => {@@ -1489,24 +1447,17 @@ describe("bars and clip edges", () => {expect(clipById(a.id).transition).toBeUndefined();});- test("a trim carries the bar and the clip fields the export renders from", () => {- // The preview and the export read clip.transition, not the bar. A resize- // moves the clip's edge and the run behind it, so both have to land- // together: a bar left behind, or a field left stale, is a blend playing- // at a joint that no longer has one.+ test("a trim leaves independent bars in place and refreshes render fields", () => {const av = asset(8);const a = vclip({ track: 0, start: 0, out: 4, assetId: av.id });const b = vclip({ track: 0, start: 4, out: 4, assetId: av.id });useEditor.setState({ assets: [av], clips: [a, b] });s().setClipTransition(a.id, 0.8, "wipeleft");s().setClipTrim(a.id, 0, 3);- // The pair stays in contact, so the cut moved to 3s with the bar on it.- expect(clipById(b.id).start).toBeCloseTo(3);- expect(s().transitions[0].start).toBeCloseTo(2.2);- expect(parkedTransitions(s().clips, s().transitions)).toEqual([]);- // And the render fields still describe that blend.- expect(clipById(a.id).transition).toBeCloseTo(0.8);- expect(clipById(a.id).transitionStyle).toBe("wipeleft");+ expect(clipById(b.id).start).toBeCloseTo(4);+ expect(s().transitions[0].start).toBeCloseTo(3.2);+ expect(parkedTransitions(s().clips, s().transitions)).toEqual([s().transitions[0]]);+ expect(clipById(a.id).transition ?? 0).toBe(0);});test("a bar lining up with nothing reads as parked, and reanchorBars brings it back", () => {@@ -1594,7 +1545,7 @@ describe("bars and clip edges", () => {expect(s().selection).toEqual({ kind: "cue", id: pasted.id });});- test("a copied bar pastes onto the cut nearest the playhead", () => {+ test("a copied bar pastes at the requested time with its properties", () => {const av = asset(12);const a = vclip({ track: 0, start: 0, out: 4, assetId: av.id });const b = vclip({ track: 0, start: 4, out: 4, assetId: av.id });@@ -1610,14 +1561,12 @@ describe("bars and clip edges", () => {setPlayhead(7.7); // in reach of the b|c cut at 8expect(s().paste()).toBe(true);expect(s().transitions.length).toBe(2);- // The paste plays that cut with the copied blend, fields and all.- expect(clipById(b.id).transition).toBeCloseTo(0.8);- expect(clipById(b.id).transitionStyle).toBe("wipeleft");- expect(parkedTransitions(s().clips, s().transitions)).toEqual([]);+ expect(s().transitions[1]).toMatchObject({ start: 7.7, seconds: 0.8, style: "wipeleft" });+ expect(parkedTransitions(s().clips, s().transitions)).toEqual([s().transitions[1]]);expect(s().selection).toEqual({ kind: "transition", id: s().transitions[1].id });});- test("a bar pasted onto an occupied cut replaces the incumbent", () => {+ test("a pasted bar preserves existing bars", () => {const av = asset(12);const a = vclip({ track: 0, start: 0, out: 4, assetId: av.id });const b = vclip({ track: 0, start: 4, out: 4, assetId: av.id });@@ -1633,11 +1582,11 @@ describe("bars and clip edges", () => {s().copySelection();setPlayhead(8.2);s().paste();- // One bar on the b|c cut: the pasted wipe; the crossfade left with it.- expect(s().transitions.length).toBe(2);- expect(clipById(b.id).transition).toBeCloseTo(0.8);- expect(clipById(b.id).transitionStyle).toBe("wipeleft");- expect(parkedTransitions(s().clips, s().transitions)).toEqual([]);+ expect(s().transitions.length).toBe(3);+ expect(clipById(b.id).transition).toBeCloseTo(0.4);+ expect(clipById(b.id).transitionStyle ?? "crossfade").toBe("crossfade");+ expect(s().transitions[2]).toMatchObject({ start: 8.2, seconds: 0.8, style: "wipeleft" });+});test("a bar pasted far from any boundary parks at the playhead", () => {@@ -1757,7 +1706,7 @@ describe("bars and clip edges", () => {expect(clipById(b.id).animIn).toEqual({ style: "zoom", seconds: 0.3 });});- test("deleting a clip takes its parked leftover bars along", () => {+ test("deleting a clip leaves ungrouped parked bars", () => {const av = asset(8);const a = vclip({ track: 0, start: 0, out: 2, assetId: av.id });useEditor.setState({ assets: [av], clips: [a] });@@ -1768,7 +1717,7 @@ describe("bars and clip edges", () => {s().select({ kind: "clip", id: a.id });s().deleteSelection();expect(s().clips.length).toBe(0);- expect(s().transitions.length).toBe(0);+ expect(s().transitions.length).toBe(2);});test("a shared bar survives while one of its clips does", () => {@@ -2193,3 +2142,248 @@ test("closing a project invalidates its pending load and releases its editor ideexpect(s().loaded).toBe(false);} finally { globalThis.fetch = originalFetch; }});+++describe("explicit timeline groups", () => {+ function setup(): NonNullable<Selection>[] {+ const av = asset(30);+ useEditor.setState({+ assets: [av],+ clips: [vclip({ id: "v", assetId: av.id, start: 2, out: 8 })],+ audioClips: [aclip({ id: "a", assetId: av.id, start: 3 })],+ overlays: [title({ id: "text", start: 4, end: 6 }), { id: "rect", kind: "shape", shape: "rect", x: 0.5, y: 0.5, w: 0.5, h: 0.5, fill: "#fff", start: 4, end: 6, lane: 1 }],+ transitions: [{ id: "bar", start: 9, seconds: 1, style: "crossfade" }],+ subtitles: { ...emptySubtitles(), cues: [cue({ id: "cue", start: 5, end: 6, words: [{ w: "hi", t0: 5, t1: 6 }] })] },+ });+ return [{ kind: "clip", id: "v" }, { kind: "audio", id: "a" }, { kind: "overlay", id: "text" }, { kind: "overlay", id: "rect" }, { kind: "cue", id: "cue" }, { kind: "transition", id: "bar" }];+ }+ function grouped() {+ const members = setup();+ s().setMultiSelection(members);+ expect(s().groupSelection()).toBeTruthy();+ return members;+ }++ test("new and loaded overlays stay independent when the underlying video moves", () => {+ const members = setup();+ for (const member of members) expect(timelineItem(s(), member)?.groupId).toBeUndefined();+ // A saved attachment from an earlier build has no behavioral effect.+ const doc = JSON.parse(JSON.stringify(serializeDoc(s())));+ doc.overlays[0].hostClipId = "v";+ const loaded = normalizeDocState(doc, s().assets);+ useEditor.setState({ ...loaded, aspect: loaded.aspect ?? s().aspect });+ s().updateClip("v", { start: 15 });+ expect(s().overlays.map((o) => [o.start, o.end])).toEqual([[4, 6], [4, 6]]);+ });++ test("every member selects and starts a drag with the complete mixed group", () => {+ const members = grouped();+ for (const member of members) {+ s().select(null);+ const drag = selectedMembers(s(), member.kind === "clip" ? "video" : member.kind, member.id);+ expect(drag?.members).toHaveLength(members.length);+ s().select(member);+ expect(s().multiSelection).toHaveLength(members.length);+ expect(s().selection).toEqual(member);+ }+ s().toggleSelect(members[0]);+ expect(s().multiSelection).toHaveLength(0);+ s().toggleSelect(members[5]);+ expect(s().multiSelection).toHaveLength(members.length);+ });++ test("moving a mixed group preserves offsets and caption words, with one undo", () => {+ const members = grouped();+ s().select(members[5]);+ expect(s().moveTimelineSelection(10)).toBe(10);+ expect(members.map((sel) => timelineItem(s(), sel)?.start)).toEqual([12, 13, 14, 14, 15, 19]);+ expect(s().subtitles.cues[0].words).toEqual([{ w: "hi", t0: 15, t1: 16 }]);+ s().undo();+ expect(members.map((sel) => timelineItem(s(), sel)?.start)).toEqual([2, 3, 4, 4, 5, 9]);+ s().redo();+ expect(s().transitions[0].start).toBe(19);+ });++ test("a real pointer gesture from a video or transition moves every member once", () => {+ const saved = new Map(["window", "document", "requestAnimationFrame", "cancelAnimationFrame"].map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]));+ try {+ const events = new EventTarget();+ Object.defineProperty(globalThis, "window", { configurable: true, value: events });+ Object.defineProperty(globalThis, "document", { configurable: true, value: { activeElement: null } });+ Object.defineProperty(globalThis, "requestAnimationFrame", { configurable: true, value: () => 1 });+ Object.defineProperty(globalThis, "cancelAnimationFrame", { configurable: true, value: () => {} });+ for (const kind of ["video", "transition"] as const) {+ const members = grouped();+ s().select(null);+ const target = { getBoundingClientRect: () => ({ left: 0 }), closest: () => null };+ const down = { button: 0, clientX: 0, clientY: 0, metaKey: false, ctrlKey: false, shiftKey: false, currentTarget: target, preventDefault() {}, stopPropagation() {} } as unknown as React.PointerEvent;+ const ui = { pps: 10, homeRow: 0, rows: () => [], onSnap: () => {}, onDrag: () => {} };+ startLaneMove(down, kind, kind === "transition" ? "bar" : "v", ui);+ for (const type of ["pointermove", "pointerup"]) {+ events.dispatchEvent(Object.assign(new Event(type), { clientX: 100, clientY: 0, metaKey: true, ctrlKey: true }));+ }+ expect(members.map((sel) => timelineItem(s(), sel)?.start)).toEqual([12, 13, 14, 14, 15, 19]);+ expect(s().subtitles.cues[0].words).toEqual([{ w: "hi", t0: 15, t1: 16 }]);+ s().undo();+ expect(s().transitions[0].start).toBe(9);+ }+ } finally {+ for (const [key, descriptor] of saved) {+ if (descriptor) Object.defineProperty(globalThis, key, descriptor);+ else Reflect.deleteProperty(globalThis, key);+ }+ }+ });++ test("chat selects, groups, moves and ungroups every timeline kind", async () => {+ const members = setup();+ for (const [i, member] of members.entries()) await runAiTool("select", { ...member, additive: i > 0 });+ await runAiTool("group_items", {});+ await runAiTool("select", members[5]);+ expect(s().multiSelection).toHaveLength(6);+ await runAiTool("move_timeline_selection", { delta: 10 });+ expect(s().transitions[0].start).toBe(19);+ await runAiTool("ungroup_items", {});+ await runAiTool("select", members[5]);+ expect(s().multiSelection).toHaveLength(1);+ });++ test("chat reorder carries every group member and caption words in one undo step", async () => {+ const members = grouped();+ useEditor.setState({ clips: [...s().clips, vclip({ id: "tail", start: 12, out: 2 })] });+ const before = members.map((member) => timelineItem(s(), member)?.start);+ const groupId = s().clips[0].groupId;+ await runAiTool("move_clip", { clipId: "v", toIndex: 1 });+ expect(members.map((member) => timelineItem(s(), member)?.start)).toEqual([14, 15, 16, 16, 17, 21]);+ expect(s().subtitles.cues[0].words).toEqual([{ w: "hi", t0: 17, t1: 18 }]);+ expect(clipById("tail").start).toBe(12);+ for (const member of members) expect(timelineItem(s(), member)?.groupId).toBe(groupId);+ s().undo();+ expect(members.map((member) => timelineItem(s(), member)?.start)).toEqual(before);+ expect(clipById("tail").start).toBe(12);+ s().redo();+ expect(s().subtitles.cues[0].start).toBe(17);+ });++ test("reordering earlier carries displaced groups and leaves independent items in place", async () => {+ const members = grouped();+ useEditor.setState({+ clips: [...s().clips, vclip({ id: "tail", start: 12, out: 2 })],+ overlays: [...s().overlays, title({ id: "independent", start: 20, end: 22 })],+ });+ await runAiTool("move_clip", { clipId: "tail", toIndex: 0 });+ expect(clipById("tail").start).toBe(2);+ expect(members.map((member) => timelineItem(s(), member)?.start)).toEqual([4, 5, 6, 6, 7, 11]);+ expect(s().subtitles.cues[0].words).toEqual([{ w: "hi", t0: 7, t1: 8 }]);+ expect(s().overlays.find((o) => o.id === "independent")?.start).toBe(20);+ s().undo();+ expect(clipById("tail").start).toBe(12);+ expect(members.map((member) => timelineItem(s(), member)?.start)).toEqual([2, 3, 4, 4, 5, 9]);+ });++ test("reorder respects occupied member rows and keeps groups above zero", async () => {+ useEditor.setState({+ clips: [vclip({ id: "first", start: 0 }), vclip({ id: "moving", start: 4, groupId: "group" })],+ subtitles: { ...emptySubtitles(), cues: [+ cue({ id: "blocker", start: 0, end: 3 }),+ cue({ id: "caption", start: 3, end: 5, groupId: "group", words: [{ w: "hi", t0: 3, t1: 4 }] }),+ ] },+ });+ await runAiTool("move_clip", { clipId: "moving", toIndex: 0 });+ expect(clipById("moving").start).toBe(4);+ expect(clipById("first").start).toBe(6);+ expect(s().subtitles.cues.map((c) => [c.start, c.end])).toEqual([[0, 3], [3, 5]]);+ expectLaneSound(videoLane(0));+ });++ test("reorder reserves room for multiple video members in one group", async () => {+ useEditor.setState({ clips: [+ vclip({ id: "first", start: 0 }),+ vclip({ id: "moving", start: 4, groupId: "group" }),+ vclip({ id: "second-member", start: 8, groupId: "group" }),+ ] });+ await runAiTool("move_clip", { clipId: "moving", toIndex: 0 });+ expect(clipById("moving").start).toBe(0);+ expect(clipById("second-member").start).toBe(4);+ expect(clipById("first").start).toBe(6);+ expectLaneSound(videoLane(0));+ });++ test("reordering within a group leaves its timing intact", () => {+ const clips = [+ vclip({ id: "first-member", start: 4, groupId: "group" }),+ vclip({ id: "second-member", start: 8, groupId: "group" }),+ ];+ useEditor.setState({ clips });+ s().moveClip("first-member", 1);+ expect(s().clips).toBe(clips);+ });++ test("ungroup leaves every member in place and makes each independently selectable", () => {+ const members = grouped();+ s().ungroupSelection();+ for (const member of members) {+ expect(timelineItem(s(), member)?.groupId).toBeUndefined();+ s().select(member);+ expect(s().multiSelection).toEqual([member]);+ }+ expect(members.map((sel) => timelineItem(s(), sel)?.start)).toEqual([2, 3, 4, 4, 5, 9]);+ s().undo();+ expect(expandTimelineGroups(s(), [members[0]])).toHaveLength(6);+ });++ test("a grouped move stops the entire set at zero and avoids occupied rows", () => {+ const members = grouped();+ expect(s().moveTimelineSelection(-10)).toBe(-2);+ expect(members.map((sel) => timelineItem(s(), sel)?.start)).toEqual([0, 1, 2, 2, 3, 7]);+ useEditor.setState({ overlays: [...s().overlays, title({ id: "other", start: 12, end: 14 })] });+ expect(s().moveTimelineSelection(10)).toBe(12);+ expect(s().overlays.find((o) => o.id === "other")?.start).toBe(12);+ expect(s().overlays.find((o) => o.id === "text")?.start).toBe(14);+ });++ test("copy and paste preserve all offsets with a fresh shared group id", () => {+ const members = grouped();+ const originalGroup = timelineItem(s(), members[0])?.groupId;+ expect(s().copySelection()).toBe(true);+ setPlayhead(15);+ expect(s().paste()).toBe(true);+ const copies = s().multiSelection.filter((sel): sel is NonNullable<Selection> => !!sel);+ expect(copies.map((sel) => timelineItem(s(), sel)?.start)).toEqual([15, 16, 17, 17, 18, 22]);+ const ids = new Set(copies.map((sel) => timelineItem(s(), sel)?.groupId));+ expect(ids.size).toBe(1);+ expect(ids.has(originalGroup)).toBe(false);+ expect(ids.has(undefined)).toBe(false);+ s().select(copies[5]);+ expect(s().multiSelection).toHaveLength(6);+ });++ test("saving and reloading preserves every group member, including transitions", () => {+ const members = grouped();+ const loaded = normalizeDocState(serializeDoc(s()), s().assets);+ useEditor.setState({ ...loaded, aspect: loaded.aspect ?? s().aspect });+ for (const member of members) expect(expandTimelineGroups(s(), [member])).toHaveLength(6);+ });++ test("templates carry all group members and remap their group together on insertion", () => {+ grouped();+ const template = s().selectionTemplate()!;+ expect(template.transitions).toHaveLength(1);+ const original = s().clips[0].groupId;+ const assetId = s().assets[0].id;+ useEditor.setState({ clips: [], overlays: [], audioClips: [], transitions: [], subtitles: emptySubtitles() });+ s().insertTemplate({ ...template, id: "template", addedAt: 0 }, [assetId], 0);+ const group = s().clips[0].groupId;+ expect(group).toBeTruthy();+ expect(group).not.toBe(original);+ expect(expandTimelineGroups(s(), [{ kind: "transition", id: s().transitions[0].id }])).toHaveLength(6);+ });++ test("regrouping merges complete groups without leaving former members behind", () => {+ const members = grouped();+ useEditor.setState({ overlays: [...s().overlays, title({ id: "extra", start: 20, end: 22 })] });+ s().setMultiSelection([members[0], { kind: "overlay", id: "extra" }]);+ s().groupSelection();+ expect(expandTimelineGroups(s(), [members[5]])).toHaveLength(7);+ });+});diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex 238b143a..d8782baa 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -53,7 +53,8 @@ import type {VideoClip,} from "./types";import type { VideoProject } from "./genvideo/types";-import { adoptOverlayHosts, hostClipFor, settleOverlayHosts } from "./overlayHosts";+import { expandTimelineGroups, reorderTimelineClip, selectedGroupIds, setTimelineGroup } from "./timelineGroups";+import { mapTimelineItems, pasteTimelineItems, splitTimelineItems, shiftedTimelineItems, timelineCopies, timelinePlacementDelta, timelineRange } from "./timelineItems";import { fillSlot } from "./genvideo/fillSlot";import { apiFetch, apiJson, getBackend, hasLocalCompute } from "./backend";import { fetchSignedMediaUrls, pinDocBase } from "./backend/cloud";@@ -81,7 +82,7 @@ import { useGenNotify } from "./genNotify";import { clampPlayhead, playheadAt, previewAt, setPlayhead, setSkim } from "./playhead";import { engineTranscribeSamples, withEngineStt } from "./localStt";import { laneCues, subtitleLaneCount, trackLocale } from "./subtitles";-import { assertNever, clipboardItemAssetIds, clipboardItemFor, listedAssetIds, type TimelineClipboardItem } from "./itemKinds";+import { clipboardItemAssetIds, clipboardItemFor, listedAssetIds, type TimelineClipboardItem } from "./itemKinds";import { ANIM_STYLE_IDS, animStyleOfTransition, assetIsSilent, clipPoseAt, DEFAULT_BACKGROUND, emptySubtitles, frameOf, IMAGE_CLIP_SECONDS, isAudioTransition, fontAssetId, isEffectOverlay, isStickerOverlay, MAX_SUBTITLE_LANES, mediaUrl, migrateBehindSubject, migrateLegacyTransitions, normalizeAspect, overlayAnimStyle, projectBackground, SPEED_FLOOR, SPEED_MIN, stampOverlayKinds, stripDefaultOverlayKinds, TRANSITION_MAX, TRANSITION_STYLE_IDS, transitionBarAt, transitionBarStart, transitionStyleOfAnim, type TransitionBoundaryKind } from "./types";import { liftMoveTracks } from "./textMotion";import { readTextStyle } from "./textStyle";@@ -146,6 +147,7 @@ export function layerFromClip(timelineStart = c.start): TemplateLayer {return {+ ...(c.groupId ? { groupId: c.groupId } : {}),media: mediaIndex,start: timelineStart - start0,in: c.in,@@ -586,12 +588,11 @@ export interface EditorState {/** Place an effect element; a drop passes where it landed. */addEffect: (effect: EffectId, aim?: { at?: number; lane?: number }) => void;updateClip: (id: string, patch: Partial<VideoClip>) => void;- /** Set a clip's playback rate (0.25–4). The footprint change carries the- * timeline behind it the way a trim does (see resizeClipFootprint). */+ /** Set playback rate; a longer footprint pushes overlapping clips on its row. */setClipSpeed: (id: string, speed: number) => void;/** Replace a clip's speed curve (undefined clears it back to a uniform rate* at the curve's average). Nodes are [source second, rate]; the footprint- * change carries the timeline like setClipSpeed does. */+ * change follows the same row rules as setClipSpeed. */setClipSpeedCurve: (id: string, nodes: SpeedNode[] | undefined) => void;/** Lay a preset ramp over the clip's trimmed span. */setClipSpeedPreset: (id: string, preset: string) => void;@@ -601,11 +602,7 @@ export interface EditorState {/** Play the clip's footage backward (picture and sound); the footprint* stays. */setClipReverse: (id: string, reverse: boolean) => void;- /** Set a clip's source trim points. While track 0 is the only video track,- * a track-0 resize ripples: everything past the clip's tail rides the moved- * edge both directions, gaps keeping their width. Otherwise the run rules- * stay on the clip's own track: growth pushes the following clips right, a- * shrink opens a gap, and a live dissolve keeps its contact. */+ /** Set source trim points. Growth pushes overlapping neighbors on the row; shrinking leaves a gap. */setClipTrim: (id: string, nextIn: number, nextOut: number) => void;/** Set the transition into the next clip (seconds; 0 clears it), optionally* changing its style; omitting the style keeps the current one. Upserts the@@ -824,17 +821,7 @@ export interface EditorState {* from the original recording, so the word highlighter would otherwise drift). */retimeCues: (entries: { id: string; start: number; end: number }[]) => void;sortCues: () => void;- /** Delete the current selection — and only the selection: a clip delete- * never takes a title, sticker, caption or soundtrack clip with it, whatever- * footage they were laid over. While track 0 is the only video track and- * some of its footage survives, a track-0 clip delete ripples: the footprint- * it occupied closes and everything after it slides left in sync, while- * anything standing over the hole falls back to where the footage started- * (see rippleAt). With upper video layers present the slide would shear- * them against track 0, and with track 0 emptied there is nothing left for- * the close to keep contiguous, so both leave the gap and everything stands- * where it was; closing it is `removeLaneGap`. Deletes on every other track- * remove just that item. */+ /** Delete selected items and their explicit groups. Other items keep their times. */deleteSelection: () => void;/** Close the empty span on `lane` containing `at` — a video track, an audio* track, or a title track. Only that row's later items slide left; every@@ -868,14 +855,10 @@ export interface EditorState {/** Append a project asset to a template as one more part at its end. */addAssetToTemplate: (templateId: string, assetId: string) => void;select: (sel: Selection) => void;- /** Group the multi-selected overlay elements (≥2): selecting any member- * selects them all, and move/resize/rotate/timing act on the set. */- groupSelectedOverlays: () => void;- /** Dissolve a group; the members stay, ungrouped. */- ungroupOverlays: (groupId: string) => void;- /** Whether an element rides the clip under it: on homes it to that clip,- * off frees it where it is. */- setOverlayFollows: (id: string, on: boolean) => void;+ /** Group selected timeline items; selecting a member selects the whole set. */+ groupSelection: () => string | null;+ moveTimelineSelection: (delta: number) => number;+ ungroupSelection: () => void;/** ⌘/⇧-click: add the item to the selection (or remove it if already in),* making it the new primary. */toggleSelect: (sel: NonNullable<Selection>) => void;@@ -917,7 +900,7 @@ export interface EditorState {/** Paste the clipboard at the preview time (the skimmer while one is live,* the playhead otherwise) — sliding past anything already on the target* lane — and select the pasted item(s). */- paste: () => boolean;+ paste: (at?: number) => boolean;}// Per-project undo/redo stacks; both reset when a project loads. Capped so a@@ -953,11 +936,6 @@ const genAudioIds = new Set<string>();export type { TimelineClipboardItem } from "./itemKinds";let clipboard: TimelineClipboardItem[] = [];-/** How far (seconds) a pasted transition bar reaches for a cut or clip edge- * around the playhead. Within it the bar lands playing that boundary, like a- * drop from the panel; past it the bar parks exactly at the playhead. */-const BAR_PASTE_REACH = 1;-/** Bumped whenever subtitle lanes renumber (a track removal). Async work that* captured a lane index checks it before landing, so a result can't write to* what is now a different language's track. */@@ -1162,68 +1140,11 @@ function overlaysClearOf(overlays: Overlay[], ids: Set<string>): Overlay[] {});}-/** Resize a clip's footprint to `newLen` (a trim or speed change) and carry- * the timeline behind the moved tail. While track 0 is the only video track,- * a track-0 resize ripples the whole document: everything past the tail —- * clips, titles, captions, soundtrack — rides the edge in both directions and- * every pre-existing gap keeps its width, so annotations stay on the footage- * they were laid over. Layer clips, and a spine resize under overlay video- * tracks (where the slide would shear the layers), keep the run rules scoped- * to their own track: a cut the clip transitions over stays a cut, a longer- * footprint pushes the run right by the overflow, and a shorter one opens a- * gap. One undo step. */-function resizeClipFootprint(clip: VideoClip, patch: Partial<VideoClip>, newLen: number) {+/** Apply a source trim or speed change with the shared same-row collision rule. */+function resizeClipFootprint(clip: VideoClip, patch: Partial<VideoClip>) {useEditor.getState().pushHistory();- const before = useEditor.getState().clips;- const end0 = clip.start + clipLen(clip);- const newEnd = clip.start + newLen;useEditor.getState().updateClipTransient(clip.id, patch);- if (clip.track === 0 && !before.some((c) => c.track !== 0)) {- // The spine resize ripples: everything past the clip's tail — clips,- // titles, captions, soundtrack — rides the moved edge in both directions,- // every pre-existing gap keeping its width (see rippleAt).- useEditor.setState((st) => {- const out = rippleAt(- { clips: st.clips, audioClips: st.audioClips, overlays: st.overlays, cues: st.subtitles.cues },- Math.min(end0, newEnd),- newEnd - end0- );- return {- clips: out.clips === st.clips ? st.clips : [...out.clips].sort((a, b) => a.start - b.start),- audioClips: out.audioClips,- overlays: out.overlays,- ...(out.cues !== st.subtitles.cues ? { subtitles: { ...st.subtitles, cues: out.cues } } : {}),- };- });- } else {- // With upper video layers present the slide would shear them against the- // footage they were composed over, so growth pushes the same-track run- // and a shrink leaves the gap — except a live dissolve's partner, which- // follows the edge to keep the cut it blends.- const next = before- .filter((c) => c.id !== clip.id && c.track === clip.track && c.start >= clip.start)- .reduce<VideoClip | null>((m, c) => (!m || c.start < m.start ? c : m), null);- const keepContact = !!next && transitionOverlap(clip, next) > 1e-6;- const nextStart = next?.start ?? Infinity;- const delta = keepContact ? newEnd - nextStart : Math.max(0, newEnd - nextStart);- if (Math.abs(delta) > 1e-6) {- useEditor.setState((st) => ({- clips: st.clips- .map((c) =>- c.id !== clip.id && c.track === clip.track && c.start >= clip.start- ? { ...c, start: Math.max(0, c.start + delta) }- : c- )- .sort((a, b) => a.start - b.start),- }));- }- }- // The resized clip's own edge moved, and the run behind it with it, so the- // bars playing those cuts follow.- if (useEditor.getState().transitions.length)- useEditor.setState((st) => ({- transitions: reanchorTransitions(before, st.clips, st.transitions),- }));+ settleClipFootprint(clip.id, patch);}const staleLaneError = {subtitleStatus: "error" as const,@@ -1397,9 +1318,6 @@ const DOC_KEYS = ["renders",] as const;let hydrating = false;-// An undo restore writes a document that was consistent when it was taken;-// homing elements against it would only re-derive what it already says.-let restoring = false;/*** The invariants every write to the store passes through, wherever it comes@@ -1425,26 +1343,6 @@ function normalizeWrite(prev: EditorState, incoming: Partial<EditorState>): Partconst derived = deriveTransitionFields(clips, next.transitions ?? prev.transitions);if (derived !== clips) next = { ...next, clips: derived };}- // Elements ride the frames under them: a write that moved or retimed- // clips carries every hosted element over the same footage, and one that- // placed an element homes it to the clip beneath. Carried elements keep- // their exact place; the rest of their row parts clear of them.- if (!hydrating && !restoring && (next.clips || next.overlays)) {- const settled = settleOverlayHosts(- prev.clips,- next.clips ?? prev.clips,- prev.overlays,- next.overlays ?? prev.overlays- );- if (settled) {- next = {- ...next,- overlays: settled.carried.size- ? overlaysClearOf(settled.overlays, settled.carried)- : settled.overlays,- };- }- }// The playhead cannot outlive the timeline. Deleting the last of a long// row shortens the project under a playhead standing past the new end,// which leaves the readout ahead of the total and — since the playhead is@@ -1646,19 +1544,14 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {}};const nextMulti = multiSelection.filter(survives);- restoring = true;- try {- set({- ...doc,- clips: nextClips,- audioClips: nextAudio,- ...(beatsMoved ? { assets: withBeats } : {}),- selection: survives(selection) ? selection : nextMulti[nextMulti.length - 1] ?? null,- multiSelection: nextMulti,- });- } finally {- restoring = false;- }+ set({+ ...doc,+ clips: nextClips,+ audioClips: nextAudio,+ ...(beatsMoved ? { assets: withBeats } : {}),+ selection: survives(selection) ? selection : nextMulti[nextMulti.length - 1] ?? null,+ multiSelection: nextMulti,+ });};/** Seal the deferred checkpoint: commit it to history only if the doc@@ -2826,18 +2719,8 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {// The non-transient updaters are just a checkpoint plus the live update.updateClip: (id, patch) => {push();- const before = get().clips;get().updateClipTransient(id, patch);settleClipFootprint(id, patch);- // A resize moves the clip's own edge and pushes the run behind it, so- // the bars playing those cuts follow. A move is the user placing the- // clip somewhere, and a bar stays exactly where it was left.- if (- get().transitions.length &&- !touches(patch, ["start", "track"]) &&- touches(patch, ["in", "out", "speed", "speedCurve"])- )- set((s) => ({ transitions: reanchorTransitions(before, s.clips, s.transitions) }));},setClipSpeed: (id, speed) => {@@ -2848,8 +2731,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {if (!clip.speedCurve && Math.abs(clamped - clipSpeed(clip)) < 1e-4) return;resizeClipFootprint(clip,- { speed: clamped, speedCurve: undefined },- Math.max(MIN_LEN, (clip.out - clip.in) / clamped)+ { speed: clamped, speedCurve: undefined });},@@ -2862,12 +2744,12 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {// Leaving curve mode keeps the clip's length: the uniform rate is the// curve's average.const avg = Math.max(SPEED_FLOOR, retimeOf(clip).rate);- resizeClipFootprint(clip, { speed: avg, speedCurve: undefined }, Math.max(MIN_LEN, retimeOf({ ...clip, speed: avg, speedCurve: undefined }).len));+ resizeClipFootprint(clip, { speed: avg, speedCurve: undefined });return;}const next = { ...clip, speedCurve: cleaned };if (retimeOf(next).key === retimeOf(clip).key) return;- resizeClipFootprint(clip, { speedCurve: cleaned }, Math.max(MIN_LEN, retimeOf(next).len));+ resizeClipFootprint(clip, { speedCurve: cleaned });},setClipSpeedPreset: (id, preset) => {@@ -2896,8 +2778,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {if (Math.abs(nextIn - clip.in) < 1e-6 && Math.abs(nextOut - clip.out) < 1e-6) return;resizeClipFootprint(clip,- { in: nextIn, out: nextOut },- retimeOf({ ...clip, in: nextIn, out: nextOut }).len+ { in: nextIn, out: nextOut });},@@ -3482,32 +3363,12 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {})),moveClip: (id, toIndex) => {- // The AI reorder op: lift the clip out (its old spot becomes a gap) and- // open a slot at the target index — the landing clip and everything- // after it shift right by the moved footprint, everything else keeps- // its absolute time, so audio, titles, and captions stay synced to the- // clips they annotate. Pointer drags never come here — they free-place- // through the lane coordinator.- const row = track0Clips(get().clips).sort((a, b) => a.start - b.start);- const from = row.findIndex((c) => c.id === id);- if (from < 0) return;- const to = Math.max(0, Math.min(row.length - 1, toIndex));- if (to === from) return;+ const s = get();+ if (s.readOnly) return;+ const next = reorderTimelineClip(s, id, toIndex);+ if (next === s) return;push();- const moved = row[from];- const others = row.filter((c) => c.id !== id);- const len = clipLen(moved);- const anchor = to < others.length ? others[to] : null;- const newStart = anchor ? anchor.start : totalDuration(others);- set((s) => ({- clips: [- ...overlayLayers(s.clips),- ...others.map((c) =>- c.start >= newStart - 1e-6 ? { ...c, start: c.start + len } : c- ),- { ...moved, start: newStart },- ].sort((a, b) => a.start - b.start),- }));+ set({ ...next, clips: [...next.clips].sort((a, b) => a.start - b.start), subtitles: { ...s.subtitles, cues: next.subtitles.cues } });},addVideoFromAsset: (assetId, place, start) => {@@ -3568,133 +3429,22 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {},splitAtPlayhead: (at) => {- const { clips, audioClips, assets, selection } = get();+ const s = get();const t = at ?? playheadAt();-- // With a soundtrack clip selected, ⌘B slices it instead.- if (selection?.kind === "audio") {- const a = audioClips.find((c) => c.id === selection.id);- const len = a ? clipLen(a) : 0;- if (a && t > a.start + 0.05 && t < a.start + len - 0.05) {- push();- const cutIn = retimeOf(a).srcAt(t - a.start);- const [head, tail] = splitTrims(a, cutIn);- const left: AudioClip = { ...a, ...head };- const right: AudioClip = { ...a, id: uid(), start: t, ...tail };- set((s) => {- const idx = s.audioClips.findIndex((c) => c.id === a.id);- const next = [...s.audioClips];- next.splice(idx, 1, left, right);- return { audioClips: next, ...sole({ kind: "audio", id: right.id }) };- });- return;- }- }-- // A layer clip (off track 0) selected: slice it in place. A track-0- // clip falls through to the playhead-driven span split below.- if (selection?.kind === "clip") {- const c = get().clips.find((x) => x.id === selection.id);- if (c && c.track !== 0) {- const rt = retimeOf(c);- const eff = rt.len;- if (t > c.start + 0.05 && t < c.start + eff - 0.05) {- push();- const cutIn = rt.srcAt(t - c.start);- // The left half hard-cuts into the right; the right keeps the- // original dissolve into whatever came after (same as track 0).- // The cut lands mid-footage, so the edges it creates stay plain:- // the exit animation belongs to the right half's tail and the- // entrance to the left half's head.- const [head, tail] = splitTrims(c, cutIn);- const left: VideoClip = { ...c, ...head, transition: undefined, transitionStyle: undefined, animOut: undefined };- const right: VideoClip = { ...c, id: uid(), start: t, ...tail, animIn: undefined };- set((s) => {- const idx = s.clips.findIndex((x) => x.id === c.id);- const next = [...s.clips];- next.splice(idx, 1, left, right);- return { clips: next, ...sole({ kind: "clip", id: right.id }) };- });- }- return;- }- }-- // An overlay element selected: both halves keep the full content/style.- if (selection?.kind === "overlay") {- const o = get().overlays.find((x) => x.id === selection.id);- if (o && t > o.start + 0.05 && t < o.end - 0.05) {- push();- const left: Overlay = { ...o, end: t };- const right: Overlay = { ...o, id: uid(), start: t };- set((s) => ({- overlays: s.overlays.flatMap((x) => (x.id === o.id ? [left, right] : [x])),- ...sole({ kind: "overlay", id: right.id }),- }));- }- return;- }-- // A caption selected: word timings are absolute, so each half keeps the- // words it covers and its text follows them; without timings the text- // splits proportionally.- if (selection?.kind === "cue") {- const c = get().subtitles.cues.find((x) => x.id === selection.id);- if (c && t > c.start + 0.05 && t < c.end - 0.05) {- push();- const lw = c.words?.filter((w) => w.t0 < t);- const rw = c.words?.filter((w) => w.t0 >= t);- const at = Math.round(c.text.length * ((t - c.start) / (c.end - c.start)));- const left: SubtitleCue = {- ...c,- end: t,- text: lw?.length ? lw.map((w) => w.w).join(" ") : c.text.slice(0, at).trim() || c.text,- words: lw?.length ? lw : undefined,- };- const right: SubtitleCue = {- ...c,- id: uid(),- start: t,- text: rw?.length ? rw.map((w) => w.w).join(" ") : c.text.slice(at).trim() || c.text,- words: rw?.length ? rw : undefined,- };- set((s) => ({- subtitles: {- ...s.subtitles,- cues: s.subtitles.cues.flatMap((x) => (x.id === c.id ? [left, right] : [x])),- },- ...sole({ kind: "cue", id: right.id }),- }));- }- return;- }-- const spans = getClipSpans(clips, assets);- const span = spans.find(- (sp) => t > sp.start + 0.05 && t < sp.start + sp.len - 0.05- );- if (!span) return;+ if (s.readOnly || !Number.isFinite(t)) return;+ const selected = s.multiSelection.length ? s.multiSelection : s.selection ? [s.selection] : s.clips+ .filter((c) => c.track === 0 && t > c.start && t < c.start + clipLen(c))+ .map((c) => ({ kind: "clip" as const, id: c.id }));+ const result = splitTimelineItems(s, expandTimelineGroups(s, selected), t, uid);+ if (!result.selection.length) return;push();- // The clip's own map says which source second plays at `t`.- const cutAt = retimeOf(span.clip).srcAt(t - span.start);- // The left half hard-cuts into the right; the right keeps the original- // dissolve into whatever came after. Both halves stay in place. The cut- // lands mid-footage, so the edges it creates stay plain: the exit- // animation stays on the right half's tail, the entrance on the left's- // head.- const [head, tail] = splitTrims(span.clip, cutAt);- const left: VideoClip = { ...span.clip, ...head, transition: undefined, transitionStyle: undefined, animOut: undefined };- const right: VideoClip = { ...span.clip, id: uid(), ...tail, start: t, animIn: undefined };- set((s) => {- const idx = s.clips.findIndex((c) => c.id === span.clip.id);- const next = [...s.clips];- next.splice(idx, 1, left, right);- return { clips: next, ...sole({ kind: "clip", id: right.id }) };- });+ set({ ...result.next, subtitles: { ...s.subtitles, cues: result.next.subtitles.cues },+ selection: result.selection.at(-1)!, multiSelection: result.selection, selectedKey: null });},deleteSelection: () => {const st = get();+ if (st.readOnly) return;// A picked keyframe is the smaller thing under the cursor: Delete takes// the key and leaves the item it belongs to alone.const key = st.selectedKey;@@ -3726,101 +3476,11 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {return;}}- const sels = st.multiSelection.length- ? st.multiSelection- : st.selection- ? [st.selection]- : [];- if (sels.length === 0) return;+ const sels = expandTimelineGroups(st, st.multiSelection.length ? st.multiSelection : [st.selection]);+ if (!sels.length) return;push();- const idsOf = (k: NonNullable<Selection>["kind"]) =>- new Set(- sels- .filter((x): x is NonNullable<Selection> => !!x && x.kind === k)- .map((x) => x.id)- );- const clipIds = idsOf("clip");- const audioIds = idsOf("audio");- const textIds = idsOf("overlay");- const cueIds = idsOf("cue");- const barIds = idsOf("transition");- set((s) => {- let clips = s.clips.filter((c) => !clipIds.has(c.id));- let audioClips = s.audioClips.filter((c) => !audioIds.has(c.id));- let overlays = s.overlays.filter((o) => !textIds.has(o.id));- let cues = s.subtitles.cues.filter((c) => !cueIds.has(c.id));- // Deleting a track-0 clip closes the hole it leaves: everything after- // it — clips, titles, captions, soundtrack — slides left with the- // surviving footage, and anything standing over the hole falls back to- // where the footage started. Nothing but the picked clip leaves the- // project. Two conditions gate that ripple. Track 0 has to be the only- // video track, since with upper layers surviving the slide would shear- // them against the footage they were composed over. And some track-0- // footage has to survive the delete, since clearing the whole track- // leaves no hole to close. Either way the delete leaves the gap and- // closing it is an explicit act (removeGap, via right-click on the- // empty space). Deletes on every other track are plain removals- // (already applied above). Holes close right-to-left so each one's- // coordinates stay valid while the ones before it are unprocessed.- const rippling = clips.some((c) => c.track === 0) && !clips.some((c) => c.track !== 0);- const holes = rippling- ? s.clips- .filter((c) => c.track === 0 && clipIds.has(c.id))- .sort((a, b) => b.start - a.start)- : [];- for (const gone of holes) {- const next = clips.reduce(- (acc, c) => (c.track === 0 && c.start > gone.start + 0.001 ? Math.min(acc, c.start) : acc),- Infinity- );- // The clip's own footprint, capped at the next clip's start so a- // dissolve overlap (or a neighbor dragged into it) never over-closes;- // any gap that already existed after it survives.- const delta = Math.min(clipLen(gone), next - gone.start);- if (delta < 0.05) continue;- ({ clips, audioClips, overlays, cues } = rippleAt(- { clips, audioClips, overlays, cues },- gone.start,- -delta- ));- }- clips = clips.sort((a, b) => a.start - b.start);- // A blend playing a deleted clip's own edge goes with the clip; the- // rest ride the ripple to wherever their cut ended up.- const roles = s.transitions.length- ? resolveTransitions(s.clips, s.transitions)- : new Map<string, TransitionRole[]>();- // A deleted clip's edges take their parked bars along: a bar aligned- // with the clip's head or tail that lost its boundary claim (a twin)- // is this clip's leftover, and it would otherwise sit on the row as- // an orphan forever. A playing bar goes when every clip it plays is- // going; one still serving a surviving track's boundary stays.- const edges: number[] = [];- for (const c of s.clips) {- if (!clipIds.has(c.id)) continue;- edges.push(c.start, c.start + clipLen(c));- }- const onEdge = (x: number) => edges.some((e) => Math.abs(e - x) <= TOUCH_EPS);- const dropped = (t: TimelineTransition) => {- if (barIds.has(t.id)) return true;- const rs = roles.get(t.id);- if (rs && rs.length > 0) return rs.every((r) => clipIds.has(r.clipId));- return onEdge(t.start) || onEdge(transitionBarAt(t, "cut"));- };- const kept = s.transitions.some(dropped)- ? s.transitions.filter((t) => !dropped(t))- : s.transitions;- const transitions = reanchorTransitions(s.clips, clips, kept);- return {- clips,- ...(transitions !== s.transitions ? { transitions } : {}),- audioClips,- overlays,- subtitles: { ...s.subtitles, cues },- selection: null,- multiSelection: [],- };- });+ const next = mapTimelineItems(st, sels, () => null);+ set({ ...next, subtitles: { ...st.subtitles, cues: next.subtitles.cues }, selection: null, multiSelection: [], selectedKey: null });},removeLaneGap: (lane, at) => {@@ -3862,39 +3522,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {selectionRange: () => {const s = get();- const sels = (s.multiSelection.length ? s.multiSelection : s.selection ? [s.selection] : [])- .filter((x): x is NonNullable<Selection> => !!x);- if (sels.length === 0) return null;- const spans = getClipSpans(s.clips, s.assets);- let start = Infinity;- let end = -Infinity;- const add = (a: number, b: number) => {- start = Math.min(start, a);- end = Math.max(end, b);- };- for (const sel of sels) {- if (sel.kind === "clip") {- const sp = spans.find((x) => x.clip.id === sel.id);- if (sp) {- add(sp.start, sp.start + sp.len);- } else {- // A layer clip carries no span (spans are track 0); use its- // own footprint.- const c = s.clips.find((x) => x.id === sel.id);- if (c) add(c.start, c.start + clipLen(c));- }- } else if (sel.kind === "audio") {- const c = s.audioClips.find((x) => x.id === sel.id);- if (c) add(c.start, c.start + clipLen(c));- } else if (sel.kind === "overlay") {- const o = s.overlays.find((x) => x.id === sel.id);- if (o) add(o.start, o.end);- } else if (sel.kind === "cue") {- const c = s.subtitles.cues.find((x) => x.id === sel.id);- if (c) add(c.start, c.end);- }- }- return Number.isFinite(start) && end > start ? { start, end } : null;+ return timelineRange(s, expandTimelineGroups(s, s.multiSelection.length ? s.multiSelection : [s.selection]));},selectionTemplate: () => {@@ -3942,7 +3570,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {if (!c) continue;const mi = mediaFor(c.assetId);if (mi == null) continue;- audio.push({ media: mi, start: c.start - start0, in: c.in, out: c.out, volume: c.volume, fadeIn: c.fadeIn, fadeOut: c.fadeOut, speed: c.speed, speedCurve: c.speedCurve, reverse: c.reverse, sound: c.sound, duck: c.duck, lane: c.lane });+ audio.push({ groupId: c.groupId, media: mi, start: c.start - start0, in: c.in, out: c.out, volume: c.volume, fadeIn: c.fadeIn, fadeOut: c.fadeOut, speed: c.speed, speedCurve: c.speedCurve, reverse: c.reverse, sound: c.sound, duck: c.duck, lane: c.lane });} else if (sel.kind === "overlay") {const o = s.overlays.find((x) => x.id === sel.id);if (!o) continue;@@ -3959,7 +3587,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {if (c) cues.push({ ...c, start: c.start - start0, end: c.end - start0 });}}- if (media.length === 0 && texts.length === 0 && cues.length === 0) return null;+ if (media.length === 0 && texts.length === 0 && cues.length === 0 && !sels.some((x) => x.kind === "transition")) return null;const duration = range? range.end - range.start: Math.max(0.1, ...texts.map((t) => t.end), ...cues.map((c) => c.end));@@ -3967,7 +3595,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {// template's start.const roles = resolveTransitions(s.clips, s.transitions);const transitions = s.transitions- .filter((t) => (roles.get(t.id) ?? []).some((rl) => clipIds.has(rl.clipId)))+ .filter((t) => sels.some((sel) => sel.kind === "transition" && sel.id === t.id) || (roles.get(t.id) ?? []).some((rl) => clipIds.has(rl.clipId))).map((t) => ({ ...t, start: t.start - start0 }));return {name: "Template",@@ -4069,6 +3697,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {insertTemplate: (template, assetIds, offset) => {push();+ const regroup = groupRemap(uid);const usable = template.layers.filter((l) => assetIds[l.media]);// Templates saved by older builds persisted `onBase`;// read it as asClip so their footage still lands on track 0.@@ -4083,6 +3712,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {const newClips: VideoClip[] = [...clipLayers].sort((a, b) => a.start - b.start).map((l) => ({+ ...regroup(l),track: 0,start: l.start + shift,id: uid(),@@ -4104,6 +3734,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {// above too.const lowest = Math.min(...overlayLayerDefs.map((l) => Math.max(1, l.track)));const newLayers: VideoClip[] = overlayLayerDefs.map((l) => ({+ ...regroup(l),id: uid(),assetId: assetIds[l.media],track: topTrack + 1 + (Math.max(1, l.track) - lowest),@@ -4117,6 +3748,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {const newAudio: AudioClip[] = template.audio.filter((a) => assetIds[a.media]).map((a) => ({+ ...regroup(a),id: uid(),assetId: assetIds[a.media],start: a.start + shift,@@ -4137,7 +3769,6 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {// every in-memory element has its discriminant, one mask model, and its// move in the slot. Groups are remapped per application, so adding the// same template twice gives two independent groups.- const regroup = groupRemap(uid);// A sticker drawn from template media points at the copy that landed;// one whose media is unmapped stays out, like a layer would.const stickerMedia = new Map((template.stickers ?? []).map((st) => [st.text, st.media]));@@ -4157,12 +3788,11 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {start: o.start + shift,end: o.end + shift,...regroup(o),- // A template's element homes to whatever sits under where it lands.- ...(typeof o.hostClipId === "string" ? { hostClipId: undefined } : {}),...(stickerMedia.has(i) ? { assetId: assetIds[stickerMedia.get(i)!] } : {}),}));const newCues: SubtitleCue[] = template.cues.map((c) => ({...c,+ ...regroup(c),id: uid(),start: c.start + shift,end: c.end + shift,@@ -4170,6 +3800,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {...(c.words ? { words: c.words.map((w) => ({ ...w, t0: w.t0 + shift, t1: w.t1 + shift })) } : {}),}));const newBars: TimelineTransition[] = (template.transitions ?? []).map((t) => ({+ ...regroup(t),id: uid(),start: t.start + shift,seconds: clampBarSeconds(t.seconds),@@ -4189,66 +3820,60 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {},select: (sel) => {- // Selecting one member of a group selects the whole group (shallow, by- // design): bulk actions — delete, copy, drag — act on all of it, while- // the clicked member stays the primary the inspector edits.- if (sel?.kind === "overlay") {- const o = get().overlays.find((x) => x.id === sel.id);- if (o?.groupId) {- const members = get()- .overlays.filter((x) => x.groupId === o.groupId)- .map((x): NonNullable<Selection> => ({ kind: "overlay", id: x.id }));- set({ selection: sel, multiSelection: members, selectedKey: null });- return;- }- }- set({ selection: sel, multiSelection: sel ? [sel] : [], selectedKey: null });+ set({ selection: sel, multiSelection: expandTimelineGroups(get(), [sel]), selectedKey: null });},- groupSelectedOverlays: () => {+ moveTimelineSelection: (delta) => {const s = get();- const ids = s.multiSelection- .filter((x): x is NonNullable<Selection> => !!x && x.kind === "overlay")- .map((x) => x.id);- if (ids.length < 2) return;+ if (s.readOnly || !Number.isFinite(delta)) return 0;+ const members = expandTimelineGroups(s, s.multiSelection);+ const items = members.flatMap((sel) => {+ const item = clipboardItemFor(s, sel);+ return item ? [item] : [];+ });+ if (!items.length) return 0;+ const placed = timelinePlacementDelta(s, items, delta, true);+ if (Math.abs(placed) < 1e-9) return 0;push();- const groupId = uid();- get().updateOverlaysTransient(ids.map((id) => ({ id, patch: { groupId } })));+ const shifted = shiftedTimelineItems(s, items, placed);+ set({ ...shifted, subtitles: { ...s.subtitles, cues: shifted.subtitles.cues } });+ return placed;},- setOverlayFollows: (id, on) => {- if (!get().overlays.some((o) => o.id === id)) return;+ groupSelection: () => {+ const s = get();+ const members = expandTimelineGroups(s, s.multiSelection);+ if (s.readOnly || members.length < 2) return null;push();- set((s) => ({- overlays: s.overlays.map((o) =>- o.id === id ? { ...o, hostClipId: on ? hostClipFor(s.clips, o)?.id : null } : o- ),- }));+ const groupId = uid();+ const grouped = setTimelineGroup(s, members, groupId);+ set({ ...grouped, subtitles: { ...s.subtitles, cues: grouped.subtitles.cues }, multiSelection: members });+ return groupId;},- ungroupOverlays: (groupId) => {- const members = get().overlays.filter((o) => o.groupId === groupId);- if (members.length === 0) return;++ ungroupSelection: () => {+ const s = get();+ if (s.readOnly || !selectedGroupIds(s, s.multiSelection).size) return;push();- get().updateOverlaysTransient(members.map((o) => ({ id: o.id, patch: { groupId: undefined } })));+ const ungrouped = setTimelineGroup(s, s.multiSelection, undefined);+ set({ ...ungrouped, subtitles: { ...s.subtitles, cues: ungrouped.subtitles.cues } });},- toggleSelect: (sel) =>- set((s) => {- const has = s.multiSelection.some((x) => x?.kind === sel.kind && x.id === sel.id);- const next = has- ? s.multiSelection.filter((x) => !(x?.kind === sel.kind && x.id === sel.id))- : [...s.multiSelection, sel];- // Primary is the just-added item, or the last survivor when removing.- const primary = has ? next[next.length - 1] ?? null : sel;- return { multiSelection: next, selection: primary };- }),-- setMultiSelection: (sels) =>- set({- multiSelection: sels,- selection: sels[sels.length - 1] ?? null,- selectedKey: null,- }),+ toggleSelect: (sel) => set((s) => {+ const members = expandTimelineGroups(s, [sel]);+ const keys = new Set(members.map((m) => `${m.kind}:${m.id}`));+ const has = s.multiSelection.some((x) => x?.kind === sel.kind && x.id === sel.id);+ const next = has+ ? s.multiSelection.filter((x) => x && !keys.has(`${x.kind}:${x.id}`))+ : expandTimelineGroups(s, [...s.multiSelection, ...members]);+ return { multiSelection: next, selection: has ? next[next.length - 1] ?? null : sel };+ }),++ setMultiSelection: (sels) => set((s) => ({+ multiSelection: expandTimelineGroups(s, sels),+ selection: sels[sels.length - 1] ?? null,+ selectedKey: null,+ })),seek: (t) => {const total = projectDuration(get());@@ -5064,13 +4689,8 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {copySelection: () => {const s = get();- const sels = s.multiSelection.length ? s.multiSelection : s.selection ? [s.selection] : [];- const items: TimelineClipboardItem[] = [];- for (const sel of sels) {- if (!sel) continue;- const cb = clipboardItemFor(s, sel);- if (cb) items.push(cb);- }+ const sels = expandTimelineGroups(s, s.multiSelection.length ? s.multiSelection : [s.selection]);+ const items = timelineCopies(s, sels);if (items.length === 0) return false;clipboard = items;return true;@@ -5086,147 +4706,14 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {clipboard = [];},- paste: () => {- if (clipboard.length === 0) return false;+ paste: (at = previewAt()) => {const s = get();- // Every asset a copied item names must still exist in this project.- if (clipboard.some((cb) => clipboardItemAssetIds(cb).some((id) => !s.assets.some((a) => a.id === id))))- return false;+ if (s.readOnly || !clipboard.length || !Number.isFinite(at)) return false;+ if (clipboard.some((cb) => clipboardItemAssetIds(cb).some((id) => !s.assets.some((a) => a.id === id)))) return false;push();- // The paste lands under the skimmer while one is live, at the playhead- // otherwise — the same moment the preview is showing.- const t = Math.max(0, previewAt());- const newSel: Selection[] = [];- set((cur) => {- let clips = cur.clips;- let audioClips = cur.audioClips;- let overlays = cur.overlays;- let transitions = cur.transitions;- let cues = cur.subtitles.cues;- // A copy is its own thing: pasted group members stay grouped with each- // other and join nothing that was already on the timeline.- const regroup = groupRemap(uid);- // When clips ride the same paste, their transition bars follow them by- // this shift, so a copied sequence keeps its blends on its own cuts.- let clipDelta: number | null = null;- // Every item aims for the paste point but respects what already sits on- // its lane: an occupied spot slides the paste right to the next gap- // that fits. Earlier items of this same paste count too.- for (const cb of clipboard) {- if (cb.kind === "transition") continue; // placed below, once clips landed- if (cb.kind === "clip") {- // Collision is per-track: a clip lands clear of others on its own- // row only.- const taken = footprints(clips.filter((c) => c.track === cb.item.track));- const clip: VideoClip = {- ...cb.item,- id: uid(),- start: nextFreeStart(taken, t, clipLen(cb.item)),- };- clipDelta ??= clip.start - cb.item.start;- clips = [...clips, clip].sort((a, b) => a.start - b.start);- newSel.push({ kind: "clip", id: clip.id });- } else if (cb.kind === "audio") {- const taken = footprints(- audioClips.filter((a) => (a.lane ?? 0) === (cb.item.lane ?? 0)),- );- const item: AudioClip = { ...cb.item, id: uid(), start: nextFreeStart(taken, t, clipLen(cb.item)) };- audioClips = [...audioClips, item];- newSel.push({ kind: "audio", id: item.id });- } else if (cb.kind === "overlay") {- const len = Math.max(0.2, cb.item.end - cb.item.start);- const taken = overlays- .filter((o) => (o.lane ?? 0) === (cb.item.lane ?? 0))- .map((o) => ({ start: o.start, end: o.end }));- const start = nextFreeStart(taken, t, len);- const item: Overlay = {- ...cb.item,- id: uid(),- start,- end: start + len,- ...regroup(cb.item),- // A copy homes to whatever sits under where it lands.- ...(typeof cb.item.hostClipId === "string" ? { hostClipId: undefined } : {}),- };- overlays = [...overlays, item];- newSel.push({ kind: "overlay", id: item.id });- } else if (cb.kind === "cue") {- // A cue lands on its own caption track, clear of the cues there.- const lane = cb.item.lane ?? 0;- const len = Math.max(0.1, cb.item.end - cb.item.start);- const taken = cues- .filter((c) => (c.lane ?? 0) === lane)- .map((c) => ({ start: c.start, end: c.end }));- const start = nextFreeStart(taken, t, len);- const shift = start - cb.item.start;- const cue: SubtitleCue = {- ...cb.item,- id: uid(),- start,- end: start + len,- ...(cb.item.words ? { words: cb.item.words.map((w) => ({ ...w, t0: w.t0 + shift, t1: w.t1 + shift })) } : {}),- };- cues = [...cues, cue].sort((a, b) => a.start - b.start);- newSel.push({ kind: "cue", id: cue.id });- } else {- assertNever(cb);- }- }- // Transition bars land last, against the row as this paste left it.- // Bars copied together with clips keep their place in the copied- // sequence; a bar-only paste lands like a drop from the panel — onto- // the boundary nearest the paste point when one is in reach (replacing- // whatever played there), parked exactly at the paste point otherwise. A- // multi-bar paste keeps the bars' spacing, anchored by the earliest.- const barItems = clipboard- .flatMap((cb) => (cb.kind === "transition" ? [cb.item] : []))- .sort((a, b) => a.start - b.start);- if (barItems.length > 0) {- let delta = clipDelta;- if (delta === null) {- const first = barItems[0];- const bounds = transitionBoundaries(clips);- const near = bounds.reduce<TransitionBoundary | null>(- (found, b) => (!found || Math.abs(b.at - t) < Math.abs(found.at - t) ? b : found),- null- );- if (near && Math.abs(near.at - t) <= BAR_PASTE_REACH) {- delta =- transitionBarStart(first.style, near.kind, near.at, first.seconds) -- first.start;- // The landed bar takes over the boundary; the bar that played- // it leaves with it, the way a drop replaces the incumbent.- const roles = resolveTransitions(clips, transitions);- const incumbent = transitions.find((x) =>- (roles.get(x.id) ?? []).some(- (r) => r.kind === near.kind && r.clipId === near.clipId- )- );- if (incumbent) transitions = transitions.filter((x) => x.id !== incumbent.id);- } else {- delta = t - first.start;- }- }- for (const item of barItems) {- const bar: TimelineTransition = {- ...item,- id: uid(),- start: Math.max(0, item.start + delta),- };- transitions = [...transitions, bar];- newSel.push({ kind: "transition", id: bar.id });- }- }- return {- clips,- audioClips,- overlays,- transitions,- ...(cues !== cur.subtitles.cues ? { subtitles: { ...cur.subtitles, cues } } : {}),- selection: newSel[newSel.length - 1] ?? null,- multiSelection: newSel,- };- });+ const { next, selection } = pasteTimelineItems(s, clipboard, Math.max(0, at), uid);+ set({ ...next, subtitles: { ...s.subtitles, cues: next.subtitles.cues },+ selection: selection[selection.length - 1] ?? null, multiSelection: selection, selectedKey: null });return true;},@@ -5647,6 +5134,7 @@ function sanitizeTransitions(raw: TimelineTransition[] | undefined): TimelineTra.filter((t) => t && typeof t.start === "number" && typeof t.seconds === "number").map((t) => ({id: t.id || uid(),+ ...(t.groupId ? { groupId: t.groupId } : {}),start: t.start,seconds: clampBarSeconds(t.seconds),style: TRANSITION_STYLE_IDS.includes(t.style) ? t.style : "crossfade",@@ -5904,16 +5392,6 @@ function handleSeconds(return Math.max(0, Math.min(want, Math.max(0, sourceLeft) / edgeRate));}-/** The trims a split at source second `cut` leaves each half: the half- * playing first keeps the footage up to the cut on a forward item and the- * footage after it on a reversed one. */-function splitTrims(- c: { in: number; out: number; reverse?: boolean },- cut: number-): [{ in?: number; out?: number }, { in?: number; out?: number }] {- return c.reverse ? [{ in: cut }, { out: cut }] : [{ out: cut }, { in: cut }];-}-/** One row of the timeline, whichever kind of thing sits on it. Video rows are* `track`; audio and title rows are the item's `lane`. */export type LaneRef =@@ -5961,250 +5439,6 @@ export function laneGapAt(return null;}-/** Items grouped by the row they live on, each row in play order. */-function byRow<T>(items: T[], row: (x: T) => number, startOf: (x: T) => number): Map<number, T[]> {- const rows = new Map<number, T[]>();- for (const item of items) {- const list = rows.get(row(item));- if (list) list.push(item);- else rows.set(row(item), [item]);- }- for (const list of rows.values()) list.sort((a, b) => startOf(a) - startOf(b));- return rows;-}--/** Slide everything on every row past `at` by `shift` seconds — how a track-0- * delete or resize carries the rest of the document. A negative shift closes- * the span [at, at - shift): items past it slide left in sync, an item- * standing over it falls back to `at`, and each row packs so no item overruns- * the one ahead of it. A positive shift opens that span, sliding items past- * `at` right together. Either way every item survives whole — same media,- * same length — pre-existing gaps keep their width, cue words ride along, and- * a grouped set of elements moves as one. Items at or before `at` stand. */-export function rippleAt(- doc: {- clips: VideoClip[];- audioClips: AudioClip[];- overlays: Overlay[];- cues: SubtitleCue[];- },- at: number,- shift: number-): typeof doc {- if (Math.abs(shift) < 1e-9) return doc;- const EPS = 0.001;- /** The ripple's own pull on a time: past `at` it moves by the shift, and a- * close never drags anything in front of where the closed span began. An- * item sitting exactly on `at` — the clip abutting a growing tail — rides- * an open and stands through a close (a close cannot move it anyway). */- const pull =- shift > 0- ? (start: number) => (start < at - EPS ? start : start + shift)- : (start: number) => (start <= at + EPS ? start : Math.max(at, start + shift));-- /** One kind of row re-laid: each item takes the ripple's pull, then gives- * way to whatever the item ahead of it on its row needs. Returns each- * item's move. */- const slides = <T,>(- items: T[],- row: (x: T) => number,- startOf: (x: T) => number,- lenOf: (x: T) => number- ): Map<T, number> => {- const out = new Map<T, number>();- for (const list of byRow(items, row, startOf).values()) {- let cursor = -Infinity;- for (const item of list) {- const start = startOf(item);- const landed = Math.max(pull(start), cursor);- out.set(item, landed - start);- cursor = landed + lenOf(item);- }- }- return out;- };-- /** Elements re-lay the same way, with a grouped set held rigid: the group- * takes one move — the member with the least room pins the set — and a- * member crowded on its own lane raises it for all of them until every- * lane is clear. */- const elementSlides = (overlays: Overlay[]): Map<Overlay, number> => {- const unit = (o: Overlay) => o.groupId ?? o.id;- const tighter = (a: number, b: number) => (Math.abs(a) < Math.abs(b) ? a : b);- const moves = new Map<string, number>();- for (const o of overlays) {- const want = pull(o.start) - o.start;- const cur = moves.get(unit(o));- moves.set(unit(o), cur === undefined ? want : tighter(cur, want));- }- const lanes = byRow(overlays, (o) => o.lane ?? 0, (o) => o.start);- for (let pass = 0; pass <= overlays.length; pass++) {- let raised = false;- for (const list of lanes.values()) {- let cursor = -Infinity;- for (const o of list) {- if (o.start + moves.get(unit(o))! < cursor - 1e-9) {- moves.set(unit(o), cursor - o.start);- raised = true;- }- cursor = o.start + moves.get(unit(o))! + (o.end - o.start);- }- }- if (!raised) break;- }- return new Map(overlays.map((o) => [o, moves.get(unit(o))!]));- };-- const clipBy = slides(doc.clips, (c) => c.track, (c) => c.start, clipLen);- const audioBy = slides(doc.audioClips, (a) => a.lane ?? 0, (a) => a.start, clipLen);- const textBy = elementSlides(doc.overlays);- const cueBy = slides(doc.cues, (c) => c.lane ?? 0, (c) => c.start, (c) => c.end - c.start);- const moved = <T,>(item: T, moves: Map<T, number>) => {- const by = moves.get(item)!;- return Math.abs(by) < 1e-6 ? 0 : by;- };- /** Same array back when nothing on it moved, so an untouched collection- * keeps its identity (autosave tells an edit from a re-read by it). */- const keep = <T,>(orig: T[], next: T[]) => (next.some((x, i) => x !== orig[i]) ? next : orig);-- return {- clips: keep(- doc.clips,- doc.clips.map((c) => {- const by = moved(c, clipBy);- return by ? { ...c, start: c.start + by } : c;- })- ),- audioClips: keep(- doc.audioClips,- doc.audioClips.map((a) => {- const by = moved(a, audioBy);- return by ? { ...a, start: a.start + by } : a;- })- ),- overlays: keep(- doc.overlays,- doc.overlays.map((o) => {- const by = moved(o, textBy);- return by ? { ...o, start: o.start + by, end: o.end + by } : o;- })- ),- cues: keep(- doc.cues,- doc.cues.map((c) => {- const by = moved(c, cueBy);- if (!by) return c;- return {- ...c,- start: c.start + by,- end: c.end + by,- words: c.words?.map((w) => ({ ...w, t0: w.t0 + by, t1: w.t1 + by })),- };- })- ),- };-}--/** The live ripple a trim-handle drag spreads while track 0 is the only video- * track: created at grab, fed the clip's footprint-end travel each pointer- * move, settled on release. Each move re-lays every other item from the- * grab-time layout through `rippleAt` — so a retreating drag flows everything- * back to where it stood — and lands the whole document in one write. Null- * when an overlay video track is present: the slide would shear the layers- * against the footage they were composed over, so those trims keep the run- * rules on their own track (the delete gate). */-export function startTrimRipple(- s: Pick<EditorState, "clips" | "audioClips" | "overlays" | "subtitles" | "transitions">,- selfId: string,- end0: number-): {- /** Apply one pointer move: `shift` is the footprint end's travel from where- * it stood at grab (negative = shorter), `clipPatches` the gesture's own- * clip writes — the trim itself, plus any leaders it re-laid. */- move: (shift: number, clipPatches: { id: string; patch: Partial<VideoClip> }[]) => void;- /** Land the gesture. A left-handle trim that moved the head rightward- * leaves its trimmed footage as a gap; `close` names where the head stood- * and how far everything behind it slides back to swallow it. Then the- * transition bars re-seat on the cuts the gesture moved. */- settle: (close?: { at: number; shift: number }) => void;-} | null {- if (s.clips.some((c) => c.track !== 0)) return null;- const clips0 = s.clips;- const doc0 = {- clips: s.clips.filter((c) => c.id !== selfId),- audioClips: s.audioClips,- overlays: s.overlays,- cues: s.subtitles.cues,- };- // Where each rider last landed, so a move patches only what it changes and- // a retreat restores what an earlier move displaced.- const placed = new Map<string, number>();- for (const c of doc0.clips) placed.set(`c${c.id}`, c.start);- for (const a of doc0.audioClips) placed.set(`a${a.id}`, a.start);- for (const o of doc0.overlays) placed.set(`o${o.id}`, o.start);- for (const q of doc0.cues) placed.set(`q${q.id}`, q.start);- let dirty = false;- return {- move: (shift, clipPatches) => {- dirty = true;- // The trimmed clip rides along as its patch; `at` sits at the tail's- // earlier position, so a shrink pulls nothing in front of the new tail- // and growth carries everything past the old one.- const out = rippleAt(doc0, Math.min(end0, end0 + shift), shift);- const clips = [...clipPatches];- const audioClips: { id: string; patch: Partial<AudioClip> }[] = [];- const overlays: { id: string; patch: OverlayPatch }[] = [];- const cues: { id: string; patch: Partial<SubtitleCue> }[] = [];- for (const c of out.clips)- if (placed.get(`c${c.id}`) !== c.start) {- placed.set(`c${c.id}`, c.start);- clips.push({ id: c.id, patch: { start: c.start } });- }- for (const a of out.audioClips)- if (placed.get(`a${a.id}`) !== a.start) {- placed.set(`a${a.id}`, a.start);- audioClips.push({ id: a.id, patch: { start: a.start } });- }- for (const o of out.overlays)- if (placed.get(`o${o.id}`) !== o.start) {- placed.set(`o${o.id}`, o.start);- overlays.push({ id: o.id, patch: { start: o.start, end: o.end } });- }- for (const q of out.cues)- if (placed.get(`q${q.id}`) !== q.start) {- placed.set(`q${q.id}`, q.start);- cues.push({ id: q.id, patch: { start: q.start, end: q.end, words: q.words } });- }- useEditor.getState().updateDocTransient({ clips, audioClips, overlays, cues });- },- settle: (close) => {- if (close && close.shift < -1e-6) {- dirty = true;- useEditor.setState((st) => {- const out = rippleAt(- { clips: st.clips, audioClips: st.audioClips, overlays: st.overlays, cues: st.subtitles.cues },- close.at,- close.shift- );- return {- clips: out.clips,- audioClips: out.audioClips,- overlays: out.overlays,- ...(out.cues !== st.subtitles.cues ? { subtitles: { ...st.subtitles, cues: out.cues } } : {}),- };- });- }- if (!dirty) return;- const st = useEditor.getState();- // One mapping from the grab layout to where the gesture left the cuts,- // so every bar keeps playing the boundary it played.- if (st.transitions.length)- useEditor.setState({ transitions: reanchorTransitions(clips0, st.clips, st.transitions) });- st.sortClips();- },- };-}-/** One video clip's timeline window with its asset, wherever the clip lives:* track-0 clips read theirs off the span fold, layer clips straight from their* own placement. Null when the clip or its asset is gone. */@@ -6313,8 +5547,7 @@ export function normalizeDocState(// a pre-bar doc stored as a clip field.transitions: adoptTransitionFields(withLooks.clips, sanitizeTransitions(doc.transitions)),audioClips: merged.audioClips,- // Elements from before they had hosts home to the clips under them.- overlays: adoptOverlayHosts(withLooks.clips, withLooks.overlays),+ overlays: withLooks.overlays,subtitles: { ...subtitles, cues: merged.cues },aspect: normalizeAspect(doc.aspect) ?? null,fadeIn: doc.fadeIn ?? 0,diff --git a/site/src/cut/lib/timelineGroups.test.ts b/site/src/cut/lib/timelineGroups.test.tsnew file mode 100644index 00000000..48fa21e1--- /dev/null+++ b/site/src/cut/lib/timelineGroups.test.ts@@ -0,0 +1,52 @@+import { describe, expect, test } from "bun:test";+import type { ItemLists } from "./itemKinds";+import { createSelectedGroupSelector } from "./timelineGroups";+import type { Selection } from "./types";++const empty = (): ItemLists & { multiSelection: Selection[] } => ({+ clips: [], audioClips: [], overlays: [], transitions: [], subtitles: { cues: [] }, multiSelection: [],+});++describe("group toolbar selector", () => {+ test("updates after grouping, ungrouping, selection changes and document replacement", () => {+ const select = createSelectedGroupSelector();+ const s = empty();+ s.subtitles.cues = [{ id: "caption", start: 0, end: 2, text: "Hello" }];+ s.multiSelection = [{ kind: "cue", id: "caption" }];+ expect(select(s)).toBe(false);+ const grouped = { ...s, subtitles: { cues: [{ ...s.subtitles.cues[0], groupId: "g" }] } };+ expect(select(grouped)).toBe(true);+ expect(select({ ...grouped, multiSelection: [] })).toBe(false);+ expect(select(grouped)).toBe(true);+ expect(select(s)).toBe(false);+ expect(select({ ...empty(), multiSelection: s.multiSelection })).toBe(false);+ });++ test("detects selected groups across item kinds and ignores unselected groups", () => {+ const select = createSelectedGroupSelector();+ const s = empty();+ s.clips = [{ id: "same-id", assetId: "v", track: 0, start: 0, in: 0, out: 2, muted: false, groupId: "g" }];+ s.subtitles.cues = [{ id: "same-id", start: 0, end: 2, text: "Hello" }];+ s.multiSelection = [{ kind: "cue", id: "same-id" }];+ expect(select(s)).toBe(false);+ expect(select({ ...s, multiSelection: [...s.multiSelection, { kind: "clip", id: "same-id" }] })).toBe(true);+ });++ test("a large selection is scanned once and unrelated updates reuse the result", () => {+ let reads = 0;+ const s = empty();+ s.subtitles.cues = Array.from({ length: 10_000 }, (_, i) => ({+ id: String(i), start: i, end: i + 1, text: "Caption",+ get groupId() { reads++; return undefined; },+ }));+ s.multiSelection = s.subtitles.cues.map((cue) => ({ kind: "cue", id: cue.id }));+ const select = createSelectedGroupSelector();+ expect(select(s)).toBe(false);+ expect(reads).toBe(10_000);+ for (let i = 0; i < 100; i++) expect(select({ ...s })).toBe(false);+ expect(reads).toBe(10_000);+ // A change on an unselected row must not rescan the captions.+ expect(select({ ...s, clips: [{ id: "video", assetId: "v", start: 0, track: 0, in: 0, out: 2, muted: false }] })).toBe(false);+ expect(reads).toBe(10_000);+ });+});diff --git a/site/src/cut/lib/timelineGroups.ts b/site/src/cut/lib/timelineGroups.tsnew file mode 100644index 00000000..c09309de--- /dev/null+++ b/site/src/cut/lib/timelineGroups.ts@@ -0,0 +1,92 @@+import { ITEM_KIND_IDS, ITEM_KINDS, type ItemLists } from "./itemKinds";+import { mapTimelineItems, shiftedTimelineItems, timelineCopies, timelineItem, timelinePlacementDelta } from "./timelineItems";+import type { Selection } from "./types";++export const TIMELINE_ITEM_KINDS = ITEM_KIND_IDS;+type ItemSelection = NonNullable<Selection>;++/** A group is an explicit set of timeline items. Selecting any member selects the set. */+export function expandTimelineGroups(s: ItemLists, selection: readonly Selection[]): ItemSelection[] {+ const selected = new Map<string, ItemSelection>();+ const groups = new Set<string>();+ for (const sel of selection) {+ if (!sel) continue;+ const item = timelineItem(s, sel);+ if (!item) continue;+ selected.set(`${sel.kind}:${sel.id}`, sel);+ if (item.groupId) groups.add(item.groupId);+ }+ if (groups.size) for (const kind of TIMELINE_ITEM_KINDS) {+ for (const item of ITEM_KINDS[kind].list(s)) {+ if (item.groupId && groups.has(item.groupId)) selected.set(`${kind}:${item.id}`, { kind, id: item.id });+ }+ }+ return [...selected.values()];+}++export function selectedGroupIds(s: ItemLists, selection: readonly Selection[]): Set<string> {+ return new Set(selection.flatMap((sel) => {+ const id = sel && timelineItem(s, sel)?.groupId;+ return id ? [id] : [];+ }));+}++/** Keep only selection ids, list references and a boolean per kind between store updates. */+export function createSelectedGroupSelector() {+ let selection: readonly Selection[] | undefined;+ const kinds = TIMELINE_ITEM_KINDS.map((kind) => ({+ kind, ids: new Set<string>(), list: null as readonly { id: string; groupId?: string }[] | null, grouped: false,+ }));+ return (s: ItemLists & { multiSelection: readonly Selection[] }): boolean => {+ if (selection !== s.multiSelection) {+ selection = s.multiSelection;+ for (const entry of kinds) {+ entry.ids.clear();+ entry.list = null;+ for (const sel of selection) if (sel?.kind === entry.kind) entry.ids.add(sel.id);+ }+ }+ let grouped = false;+ for (const entry of kinds) {+ const list = ITEM_KINDS[entry.kind].list(s);+ if (entry.list !== list) {+ entry.list = list;+ entry.grouped = entry.ids.size > 0 && list.some((item) => !!item.groupId && entry.ids.has(item.id));+ }+ grouped ||= entry.grouped;+ }+ return grouped;+ };+}++/** Open a slot in the base sequence; every moved or displaced group keeps its offsets. */+export function reorderTimelineClip(s: ItemLists, id: string, toIndex: number): ItemLists {+ if (!Number.isFinite(toIndex)) return s;+ const row = s.clips.filter((clip) => clip.track === 0).sort((a, b) => a.start - b.start);+ const from = row.findIndex((clip) => clip.id === id);+ const to = Math.max(0, Math.min(row.length - 1, Math.round(toIndex)));+ if (from < 0 || from === to) return s;+ const moved = row[from];+ const members = expandTimelineGroups(s, [{ kind: "clip", id }]);+ const clipIds = new Set(members.filter((sel) => sel.kind === "clip").map((sel) => sel.id));+ if (clipIds.has(row[to].id)) return s;+ const others = row.filter((clip) => !clipIds.has(clip.id));+ const index = others.findIndex((clip) => clip.id === row[to].id) + (to > from ? 1 : 0);+ const start = others[index]?.start ?? others.reduce((end, clip) => Math.max(end, clip.start + ITEM_KINDS.clip.duration(clip)), 0);+ const displaced = expandTimelineGroups(s, others.filter((clip) => clip.start >= start - 1e-6).map((clip) => ({ kind: "clip", id: clip.id })));+ const movingItems = timelineCopies(s, members);+ const displacedItems = timelineCopies(s, displaced);+ const fixed = mapTimelineItems(s, displaced, () => null);+ const delta = timelinePlacementDelta(fixed, movingItems, start - moved.start, true);+ let next = shiftedTimelineItems(s, movingItems, delta);+ const slot = Math.max(...row.filter((clip) => clipIds.has(clip.id)).map((clip) => clip.start + delta + ITEM_KINDS.clip.duration(clip))) - start;+ const shift = timelinePlacementDelta(next, displacedItems, slot, true);+ next = shiftedTimelineItems(next, displacedItems, shift);+ return next;+}++export function setTimelineGroup(s: ItemLists, selection: readonly Selection[], groupId: string | undefined): ItemLists {+ return mapTimelineItems(s, expandTimelineGroups(s, selection), (cb) => ({+ ...cb, item: { ...cb.item, groupId },+ } as typeof cb));+}diff --git a/site/src/cut/lib/timelineItems.test.ts b/site/src/cut/lib/timelineItems.test.tsnew file mode 100644index 00000000..ddd3da96--- /dev/null+++ b/site/src/cut/lib/timelineItems.test.ts@@ -0,0 +1,152 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { richDoc } from "./fixtures/richDoc";+import { canSplitItem, ITEM_KINDS, ITEM_KIND_IDS, type ItemKind, type ItemKindDef, type TimelineClipboardItem } from "./itemKinds";+import { mapTimelineItems, pasteTimelineItems, timelineFootprint, timelineItem } from "./timelineItems";+import { useEditor } from "./store";+import { runAiTool } from "./aiTools";+import { setPlayhead, setSkim } from "./playhead";+import type { Selection } from "./types";++const rich = richDoc();+const samples: TimelineClipboardItem[] = [+ { kind: "clip", item: rich.clips![1] },+ { kind: "audio", item: rich.audioClips![0] },+ ...rich.overlays!.map((item) => ({ kind: "overlay" as const, item: { ...item, groupId: undefined } })),+ { kind: "overlay", item: { id: "rect", kind: "shape", shape: "rect", start: 1, end: 3, x: 0.2, y: 0.4, w: 0.2, h: 0.2, fill: "#AA22DD", lane: 3 } },+ { kind: "cue", item: { ...rich.subtitles!.cues[0], words: [{ w: "hello", t0: 0.3, t1: 0.8 }] } },+ { kind: "transition", item: { ...rich.transitions![1], hidden: true } },+];+const s = () => useEditor.getState();+const def = (kind: ItemKind) => ITEM_KINDS[kind] as ItemKindDef<ItemKind>;+const selectionOf = (cb: TimelineClipboardItem): NonNullable<Selection> => ({ kind: cb.kind, id: cb.item.id });++beforeEach(() => {+ useEditor.setState({+ projectId: "operations", readOnly: false, loaded: true, playing: false,+ assets: rich.assets.map((a) => ({ ...a, url: "" })), clips: [], audioClips: [], overlays: [], transitions: [],+ subtitles: { ...rich.subtitles!, cues: [] }, selection: null, multiSelection: [], selectedKey: null,+ });+ for (const kind of ITEM_KIND_IDS) {+ const state = s();+ const next = def(kind).withList(state, samples.filter((cb) => cb.kind === kind).map((cb) => def(kind).clone(cb.item)));+ useEditor.setState({ ...next, subtitles: { ...state.subtitles, cues: next.subtitles.cues } });+ }+ setPlayhead(30);+ setSkim(null);+});++describe("shared timeline operations", () => {+ for (const sample of samples) {+ const label = `${sample.kind}:${sample.item.id}`;+ test(`${label}: copy, paste, move, delete and undo preserve properties`, () => {+ s().select(selectionOf(sample));+ const original = timelineItem(s(), selectionOf(sample))!;+ expect(s().copySelection()).toBe(true);+ expect(s().paste(30)).toBe(true);+ const selected = s().selection!;+ const pasted = timelineItem(s(), selected)!;+ expect(pasted).toEqual({ ...def(sample.kind).at(original, 30), id: pasted.id });+ expect(pasted.id).not.toBe(original.id);+ expect(pasted).not.toBe(original);+ const delta = s().moveTimelineSelection(3);+ expect(delta).toBe(3);+ expect(timelineItem(s(), selected)).toEqual(def(sample.kind).at(pasted, 33));+ s().undo();+ expect(timelineItem(s(), selected)).toEqual(pasted);+ s().select(selected);+ s().deleteSelection();+ expect(timelineItem(s(), selected)).toBeUndefined();+ expect(timelineItem(s(), selectionOf(sample))).toEqual(original);+ s().undo();+ expect(timelineItem(s(), selected)).toEqual(pasted);+ s().undo();+ expect(timelineItem(s(), selected)).toBeUndefined();+ });++ test(`${label}: split follows its declared capability`, () => {+ s().select(selectionOf(sample));+ const before = def(sample.kind).list(s()).length;+ const range = timelineFootprint(sample);+ s().splitAtPlayhead((range.start + range.end) / 2);+ expect(def(sample.kind).list(s()).length).toBe(before + (canSplitItem(sample.kind) ? 1 : 0));+ });+ }++ test("transition Split never falls through to underlying video or creates history", () => {+ const bar = s().transitions[0];+ s().select({ kind: "transition", id: bar.id });+ const before = s();+ s().splitAtPlayhead(10);+ expect(s().clips).toBe(before.clips);+ expect(s().transitions).toBe(before.transitions);+ expect(s()).toBe(before);+ });++ test("a mixed split leaves transitions whole and splits eligible selected items", () => {+ s().setMultiSelection(samples.map(selectionOf));+ const transitions = s().transitions;+ s().splitAtPlayhead(2);+ expect(s().transitions).toBe(transitions);+ expect(s().audioClips).toHaveLength(2);+ expect(s().overlays.length).toBeGreaterThan(samples.filter((cb) => cb.kind === "overlay").length);+ });++ test("copying a mixed set keeps offsets, fresh group identities and nested data", () => {+ s().setMultiSelection(samples.map(selectionOf));+ const group = s().groupSelection();+ s().copySelection();+ const copies = s().copiedItems();+ s().paste(40);+ const pasted = s().multiSelection.map((sel) => timelineItem(s(), sel!)!);+ const origin = Math.min(...copies.map((cb) => cb.item.start));+ pasted.forEach((item, i) => {+ expect(item.start).toBeCloseTo(40 + copies[i].item.start - origin);+ expect(item.groupId).not.toBe(group);+ expect(item.groupId).toBe(pasted[0].groupId);+ });+ const source = copies.find((cb) => "kf" in cb.item && cb.item.kf?.length)!.item;+ const keyed = pasted.find((item) => "kf" in item && item.kf?.length)!;+ if ("kf" in source && source.kf && "kf" in keyed && keyed.kf) {+ const x = source.kf[0].x;+ keyed.kf[0].x = 0.123;+ expect(source.kf[0].x).toBe(x);+ }++ });++ test("pasting on an occupied transition adds a copy beyond it", () => {+ const bar = s().transitions[0];+ s().select({ kind: "transition", id: bar.id });+ s().copySelection();+ s().paste(bar.start);+ expect(s().transitions).toHaveLength(2);+ expect(s().transitions[0]).toEqual(bar);+ expect(s().transitions[1].start).toBeCloseTo(bar.start + bar.seconds);+ });++ test("edits to one kind preserve untouched collection identities", () => {+ const state = s();+ const next = mapTimelineItems(state, [{ kind: "transition", id: state.transitions[0].id }], () => null);+ expect(next.clips).toBe(state.clips);+ expect(next.overlays).toBe(state.overlays);+ expect(next.audioClips).toBe(state.audioClips);+ expect(next.subtitles).toBe(state.subtitles);+ let n = 0;+ const pasted = pasteTimelineItems(state, [{ kind: "transition", item: state.transitions[0] }], 40, () => `id-${n++}`);+ expect(pasted.next.clips).toBe(state.clips);+ });++ test("chat can copy, paste and delete transitions; split is rejected", async () => {+ const bar = s().transitions[0];+ await runAiTool("select", { kind: "transition", id: bar.id });+ expect(await runAiTool("copy_selection", {})).toMatchObject({ copied: 1 });+ const result = await runAiTool("paste_selection", { at: 40 });+ expect(result).toMatchObject({ selection: s().multiSelection });+ expect(s().transitions).toHaveLength(2);+ const fresh = s().selection!;+ await expect(runAiTool("split_at", { t: 40.2 })).rejects.toThrow("Nothing to split");+ expect(s().transitions).toHaveLength(2);+ await runAiTool("delete_item", fresh);+ expect(s().transitions).toHaveLength(1);+ });+});diff --git a/site/src/cut/lib/timelineItems.ts b/site/src/cut/lib/timelineItems.tsnew file mode 100644index 00000000..f0df19c9--- /dev/null+++ b/site/src/cut/lib/timelineItems.ts@@ -0,0 +1,140 @@+import { groupRemap } from "@donkeycut/effects-kit";+import { ITEM_KINDS, type ItemKind, type ItemKindDef, type ItemLists, type ItemOf, type TimelineClipboardItem } from "./itemKinds";+import type { Selection } from "./types";++export const itemKey = (sel: NonNullable<Selection>) => `${sel.kind}:${sel.id}`;+const definition = (kind: ItemKind) => ITEM_KINDS[kind] as ItemKindDef<ItemKind>;++/** Every basic timeline operation reads the same item registry. */+export function timelineItem(s: ItemLists, sel: NonNullable<Selection>) {+ return definition(sel.kind).list(s).find((item) => item.id === sel.id);+}++export function timelineFootprint(cb: TimelineClipboardItem) {+ const def = definition(cb.kind);+ return {+ key: `${cb.kind}:${cb.item.id}`, row: `${cb.kind}:${def.lane(cb.item)}`,+ start: cb.item.start, end: cb.item.start + def.duration(cb.item),+ };+}++export function timelineCopies(s: ItemLists, selection: readonly Selection[]): TimelineClipboardItem[] {+ return selection.flatMap((sel) => {+ const item = sel && timelineItem(s, sel);+ return sel && item ? [{ kind: sel.kind, item: definition(sel.kind).clone(item) } as TimelineClipboardItem] : [];+ });+}++export function timelineRange(s: ItemLists, selection: readonly Selection[]) {+ let start = Infinity;+ let end = -Infinity;+ for (const sel of selection) {+ const item = sel && timelineItem(s, sel);+ if (!sel || !item) continue;+ start = Math.min(start, item.start);+ end = Math.max(end, item.start + definition(sel.kind).duration(item));+ }+ return Number.isFinite(start) && end > start ? { start, end } : null;+}++export function mapTimelineItems(s: ItemLists, selection: readonly Selection[], map: (item: TimelineClipboardItem) => TimelineClipboardItem | null): ItemLists {+ const keys = new Set(selection.flatMap((sel) => sel ? [itemKey(sel)] : []));+ let next = s;+ for (const kind of new Set(selection.flatMap((sel) => sel ? [sel.kind] : []))) {+ const def = definition(kind);+ const list = def.list(next);+ let changed = false;+ const mapped = list.flatMap((item) => {+ if (!keys.has(`${kind}:${item.id}`)) return [item];+ const result = map({ kind, item } as TimelineClipboardItem);+ changed ||= result?.item !== item;+ return result ? [result.item] : [];+ });+ if (changed) next = def.withList(next, mapped);+ }+ return next;+}++/** Find one free landing for the selection, preserving all relative timing. */+export function timelinePlacementDelta(s: ItemLists, items: TimelineClipboardItem[], wanted: number, moving = false): number {+ if (!items.length) return 0;+ const blocks = items.map(timelineFootprint);+ const keys = new Set(blocks.map((b) => b.key));+ const rows = new Set(blocks.map((b) => b.row));+ const rest = new Map<string, ReturnType<typeof timelineFootprint>[]>();+ let blockers = 0;+ for (const kind of new Set(items.map((cb) => cb.kind))) {+ const def = definition(kind);+ for (const item of def.list(s)) {+ const row = `${kind}:${def.lane(item)}`;+ if (!rows.has(row) || (moving && keys.has(`${kind}:${item.id}`))) continue;+ const list = rest.get(row) ?? [];+ list.push(timelineFootprint({ kind, item } as TimelineClipboardItem));+ rest.set(row, list);+ blockers++;+ }+ }+ let delta = Math.max(wanted, -Math.min(...blocks.map((b) => b.start)));+ for (let pass = 0; pass <= blocks.length * blockers; pass++) {+ let next = delta;+ for (const b of blocks) for (const r of rest.get(b.row) ?? []) {+ if ( b.start + delta < r.end - 1e-6 && b.end + delta > r.start + 1e-6)+ next = Math.max(next, r.end - b.start);+ }+ if (next === delta) return delta;+ delta = next;+ }+ return delta;+}++export function shiftedTimelineItems(s: ItemLists, items: TimelineClipboardItem[], delta: number): ItemLists {+ return mapTimelineItems(s, items.map((cb) => ({ kind: cb.kind, id: cb.item.id })), (cb) => ({+ kind: cb.kind, item: definition(cb.kind).at(cb.item, cb.item.start + delta),+ } as TimelineClipboardItem));+}++/** Paste always adds copies. Every type keeps its properties, duration and offsets. */+export function pasteTimelineItems(s: ItemLists, items: TimelineClipboardItem[], at: number, newId: () => string) {+ if (!items.length) return { next: s, selection: [] as NonNullable<Selection>[] };+ const delta = timelinePlacementDelta(s, items, at - Math.min(...items.map((cb) => cb.item.start)));+ const regroup = groupRemap(newId);+ const selection: NonNullable<Selection>[] = [];+ const added = new Map<ItemKind, ItemOf[ItemKind][]>();+ for (const cb of items) {+ const def = definition(cb.kind);+ const copy = { ...def.at(def.clone(cb.item), cb.item.start + delta), ...regroup(cb.item), id: newId() };+ const list = added.get(cb.kind) ?? [];+ list.push(copy);+ added.set(cb.kind, list);+ selection.push({ kind: cb.kind, id: copy.id });+ }+ let next = s;+ for (const [kind, copies] of added) {+ const def = definition(kind);+ next = def.withList(next, [...def.list(next), ...copies].sort((a, b) => a.start - b.start));+ }+ return { next, selection };+}++/** Split selected items that cover the cut, in one document edit. */+export function splitTimelineItems(s: ItemLists, selection: readonly Selection[], at: number, newId: () => string) {+ const keys = new Set(selection.flatMap((sel) => sel ? [itemKey(sel)] : []));+ const selected: NonNullable<Selection>[] = [];+ let next = s;+ for (const kind of new Set(selection.flatMap((sel) => sel ? [sel.kind] : []))) {+ const def = definition(kind);+ let changed = false;+ const split = def.split;+ if (!split) continue;+ const items = def.list(next).flatMap((item) => {+ if (!keys.has(`${kind}:${item.id}`) || at <= item.start + 0.05 || at >= item.start + def.duration(item) - 0.05) return [item];+ const [left, tail] = split(def.clone(item), at);+ const right = { ...tail, id: newId() };+ selected.push({ kind, id: right.id });+ changed = true;+ return [left, right];+ });+ if (changed) next = def.withList(next, items);+ }+ return { next, selection: selected };+}diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.tsindex 82a4209c..443720f0 100644--- a/site/src/cut/lib/types.ts+++ b/site/src/cut/lib/types.ts@@ -507,6 +507,8 @@ export function regionLabel(r: FrameRect): string {* The array is kept sorted by `start` (older docs stored a packed sequence;* loading bakes their implied starts in). */export interface VideoClip {+ /** Explicit timeline group; absent means independent. */+ groupId?: string;id: string;assetId: string;/** What the person calls this clip on the timeline and in the inspector;@@ -676,6 +678,8 @@ export const transitionDefaultSeconds = (style: TransitionStyle): number =>* exactly where it is. `transitionBarStart` is where the window falls against* the boundary. */export interface TimelineTransition {+ /** Explicit timeline group; absent means independent. */+ groupId?: string;id: string;/** Bar start on the timeline, seconds; the window is [start, start+seconds]. */start: number;@@ -1007,6 +1011,8 @@ export function migrateLegacyTransitions(clips: VideoClip[]): VideoClip[] {/** A clip on the free-form soundtrack track. */export interface AudioClip {+ /** Explicit timeline group; absent means independent. */+ groupId?: string;id: string;assetId: string;/** What the person calls this clip on the timeline and in the inspector;@@ -1055,6 +1061,8 @@ export interface TemplateMedia {height?: number;}export interface TemplateLayer {+ /** Explicit timeline group; absent means independent. */+ groupId?: string;media: number; // index into `media`start: number;in: number;@@ -1092,6 +1100,8 @@ export interface TemplateLayer {asClip?: boolean;}export interface TemplateAudio {+ /** Explicit timeline group; absent means independent. */+ groupId?: string;media: number;start: number;in: number;@@ -1337,6 +1347,8 @@ export type {/** One subtitle caption, timed against the timeline (not the source files). */export interface SubtitleCue {+ /** Explicit timeline group; absent means independent. */+ groupId?: string;id: string;start: number; // timeline secondsend: number;diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.tsindex 0e56a9e0..db0a8cf0 100644--- a/site/src/cut/server/ai/catalog.ts+++ b/site/src/cut/server/ai/catalog.ts@@ -94,19 +94,19 @@ Times are in seconds on the shared timeline. The playhead is currentTime; a skim"timeline-editing": `# Timeline editing- Every track is free-positioned: items carry a start time, gaps are allowed, and a track-0 gap plays black (and silence). videoTrack entries in editor_state report gapBefore where one exists.-- A delete removes the item asked for and nothing else: a title, sticker, caption or soundtrack clip laid over a deleted clip is still there afterwards, at its full length. While track 0 is the only video track, deleting a track-0 clip ripples: the span it occupied closes, later items on every track slide left together, and anything standing over the span falls back to where the span started. A gap that already existed stays. With overlay video tracks present the delete leaves its gap (remove_gap closes it by sliding that track's later clips left). Deletes on other tracks remove just that item.-- Two ways to move a track-0 clip: place_clip sets its start (respects gaps, slides right if the spot is taken); move_clip reorders by index, opening a slot at the landing point — clips after it shift right, every other gap survives. Index-based inserts (freeze_frame, generated clips) open a slot the same way.+- A delete removes the selected item and its explicit group. Other items keep their timing. The footprint becomes a gap; remove_gap closes it on one row.+- Two ways to move a track-0 clip: place_clip sets its start (respects gaps, slides right if the spot is taken); move_clip reorders by index, opening a slot at the landing point. Moved and displaced clips carry their group members with relative timing preserved; occupied rows can move a group farther right. Index-based inserts (freeze_frame, generated clips) also open a slot.- add_clip puts a project asset on the timeline the way a drag does: video/image onto track 0 (a \`start\`, an \`index\` insert, or appended at the end), audio onto the soundtrack.- Overlay video: a video/image asset can sit on a track above track 0 (track 1, 2… — topmost wins). A full-frame overlay covers everything below it; give it a layout to share the frame — top/bottom/left/right halves for a split screen, pip for a floating corner box, or a custom region rect. add_overlay_video creates one from a media asset; update_overlay_video moves/trims/regions/mutes/hides it. The user makes them by dragging media above track 0; they drag the region in the preview.- A clip's timeline length is (out-in)/speed — or, for a clip with a speed curve, the integral of its curve over the trim (editor_state reports \`len\` either way, and lists a curved clip's \`speedCurve\` nodes as [sourceSeconds, rate]). A clip with \`reverse: true\` plays its trim backward at that same rate — same length, head showing source \`out\`, sound running backward too. Total duration runs to the last clip's end, gaps included. Transitions blend at cuts and never change layout or length.- trim_clip changes in/out inside the source media. in >= 0, out <= source duration, out-in >= 0.1.-- Resizes ripple the same way deletes do: while track 0 is the only video track, a track-0 trim or speed change slides everything past the clip's tail — clips, titles, captions, soundtrack — with the moved edge, in both directions, every existing gap keeping its width. With overlay video tracks present, growth pushes the track-0 run and a shrink leaves a gap.-- set_speed sets a clip's playback rate; it changes the clip's timeline length and carries the timeline behind it like a trim. set_speed with \`reverse: true\` plays the clip backward (a rewind, a drop that rises, a pour that un-pours); it changes nothing about length or the curve. set_speed_curve makes the rate change through the footage — nodes of {at: source seconds, speed} or a named preset ramp — for speed ramps, whips, holds, and hits on the beat: detect_beats first, then place nodes at the beats it returns (same unit, no arithmetic). set_speed on a curved clip makes it uniform again. Either tool takes \`smooth: true\` — smooth slow motion — which synthesizes the frames between source frames wherever the clip's rate is under 1× (the export estimates the motion between them) and leaves the rest of the clip alone; editor_state shows it as \`smooth\`. Ordinary footage slowed below about 0.5× wants it on.-- set_transition joins a clip into the next one (0–2s, ${TRANSITION_STYLE_IDS.length} styles); set_animation animates one clip's own entrance/exit — read the transitions-and-fades skill before styling cuts. Splitting or deleting clears the affected transition. A transition is a bar on its own row at an absolute time, so an edit can leave one lining up with nothing: every timeline mutation reports those as \`parkedTransitions\`, and clearing them (remove_transition) is part of finishing the edit. Grading a clip's picture is the color tools (set_color_preset, set_color_grade, set_color_curves, set_color_wheels, set_color_hsl — read the color-grading skill); a stylized post treatment (grain, VHS, halation…) is add_effect, dropped over the stretch of timeline it covers — as is a treatment on the sound (echo, reverb, telephone…).+- Trims and speed changes keep other rows in place. Growth pushes overlapping neighbors on the same row; shrinking leaves a gap.+- set_speed sets a clip's playback rate; it changes the clip's timeline length and pushes overlapping neighbors on its own row. set_speed with \`reverse: true\` plays the clip backward (a rewind, a drop that rises, a pour that un-pours); it changes nothing about length or the curve. set_speed_curve makes the rate change through the footage — nodes of {at: source seconds, speed} or a named preset ramp — for speed ramps, whips, holds, and hits on the beat: detect_beats first, then place nodes at the beats it returns (same unit, no arithmetic). set_speed on a curved clip makes it uniform again. Either tool takes \`smooth: true\` — smooth slow motion — which synthesizes the frames between source frames wherever the clip's rate is under 1× (the export estimates the motion between them) and leaves the rest of the clip alone; editor_state shows it as \`smooth\`. Ordinary footage slowed below about 0.5× wants it on.+- set_transition joins a clip into the next one (0–2s, ${TRANSITION_STYLE_IDS.length} styles); set_animation animates one clip's own entrance/exit — read the transitions-and-fades skill before styling cuts. A transition is a bar on its own row at an absolute time, so an edit can leave one lining up with nothing: every timeline mutation reports those as \`parkedTransitions\`, and clearing them (remove_transition) is part of finishing the edit. Grading a clip's picture is the color tools (set_color_preset, set_color_grade, set_color_curves, set_color_wheels, set_color_hsl — read the color-grading skill); a stylized post treatment (grain, VHS, halation…) is add_effect, dropped over the stretch of timeline it covers — as is a treatment on the sound (echo, reverb, telephone…).- Cutting a clip's background away — the AI person matte, ink around the silhouette, a fill behind the subject — is the cutout suite (set_removal, set_removal_stroke, set_removal_background); read the background-removal skill.-- split_at cuts the track-0 clip under that time into two clips at the exact frame. With a soundtrack or overlay clip selected it splits that instead.+- split_at cuts selected video, audio, elements and captions at the requested time. Transitions stay whole. With no selection, it cuts the track-0 clip at that time.- The Media and Library panels take the same gestures: ⌘/⇧-click a tile, or sweep a rubber-band over the grid, and dragging any picked tile onto the timeline lays the whole set down one after another from the drop point.-- The user can multi-select (⌘/⇧-click, or sweep a rubber-band over empty track space) across every kind at once — clips, audio, titles, cues. Deleting or copying acts on the whole selection, and dragging any member moves the set as one: every item shifts by the same time, relative spacing kept, while unselected items slide out of the way and flow back. A selection of one kind on the multi-row tracks (audio, titles) also retracks together on a vertical drag, one row past the end opening a new track. A hover chip on each video clip toggles its own audio.+- The user can multi-select (⌘/⇧-click, or sweep a rubber-band over empty track space) across every kind at once — clips, audio, elements, cues and transitions. Deleting or copying acts on the whole selection, and dragging any member moves the set as one: every item shifts by the same time, relative spacing kept, while unselected items slide out of the way and flow back. A selection of one kind on the multi-row tracks (audio, titles) also retracks together on a vertical drag, one row past the end opening a new track. A hover chip on each video clip toggles its own audio.- Anything can be named: rename_item puts the person's own name on a clip, a soundtrack clip, or an element, and the timeline bar, the inspector title and editor_state show it (\`name\`, beside the file's \`asset\` name). A cut full of look-alike rectangles or takes stays legible that way; an empty name clears it.- Anything can be hidden instead of deleted: it stays on the timeline (grayed) but drops out of playback and export — a hidden video clip plays black and silent. Per item: set_clip_hidden, or the \`hidden\` field on update_overlay_video/update_overlay/update_audio. A transition bar hides the same way (set_transition_hidden): it keeps the cut it sits on so nothing else claims that boundary, and the cut renders hard. Whole rows: set_track_hidden (video/soundtrack/text/subtitles/transitions) and set_track_muted (video) — the eye/speaker toggles on the track headers.- detach_audio lifts a video clip's sound to the soundtrack track (and mutes the clip) so audio can be cut independently of video.@@ -198,7 +198,8 @@ What the user sees on the transitions row: one badge per bar, and nothing else"graphics": `# Graphics: titles, shapes & stickersThe title lanes hold three overlay element kinds, all sharing a name (what the person calls it, or what it is — a title's text, "Rectangle"; \`name\` on update_overlay renames it, and the timeline chip, the inspector and @-references show that name, which is how a cut full of look-alike shapes stays legible), timing (start/end), center position (x/y fractions 0..1), rotation (degrees), opacity, and a lane; \`overlays\` in editor_state lists them with their \`kind\` and \`name\`. All burn into the export exactly as previewed.Rows are how elements stack (\`lane\`, 0 = the front row; a higher row draws further back). Two elements on ONE row never overlap in time — placing a title where a shape already sits slides it clear instead of laying it on top — so anything meant to sit over something else names a LOWER row: title on lane 0, the background shape behind it on lane 1. Every add tool and update_overlay take \`lane\`.-An element rides the frames of the video clip under it (\`host\` in editor_state, the clip on the highest track beneath its middle): moving, retiming or splitting that clip carries the element over the same footage, trimming leaves it on the frames it covered, and deleting the clip leaves the element in place, homed to whatever now sits under it. Placing an element homes it automatically; update_overlay follows_clip:false frees one (\`free\` in editor_state) and true homes it again.+Use copy_selection and paste_selection for any timeline type, including effects and transitions. Copies preserve properties and relative timing. Transitions cannot be split.+Timeline items start ungrouped. Use group_items to group any selection of clips, audio, elements, captions and transitions, and ungroup_items to separate them. Selecting a member selects the whole group; moving the group preserves its relative timing.The frame's own color is set_background, not a full-frame rectangle: it fills the frame behind every clip and element, holds for the whole cut, letterboxes fitted footage, and shows through gaps on track 0. "Make the background white", a colored card behind a title sequence, a text-and-graphics video with no footage at all — all of them start with set_background. A rectangle element is for a panel or a band inside the frame, not for the frame itself.A whole run of text — lines timed to speech, a lyric video, a kinetic-typography passage, a quote sequence — is add_text_sequence, one call. Design is opt-in: called with no look it lays a plain run — one readable face, one place in the frame, a short fade, no cards, no moves, the frame color untouched — which is what a bare "add the text" asks for. Name a look when the user asked for a designed run, and then the look is the design of the whole run, not one line: the frame color, the cards, and the ensemble the run walks across its length — faces, entrances, where successive lines land, and what each holds. Read the text-videos skill for the looks and the flow, and text-creativity for the fifty devices that keep a long run from reading flat. A designed run is composed in the call, never laid identical and then repaired element by element. A video needs no footage. Titles, shapes and stickers over a background color are a complete cut on their own: it plays, exports, and shares like any other. Build one by setting the background, then placing elements on the rows the stack needs — reach for footage only when the user asks for it.Titles (add_title / update_overlay): text, size (frame px; the design short side is 1080), font (${FONTS.map((f) => `${f.id}=${f.label}`).join(", ")}), weight (400/700), color, shadow, plate (translucent dark backdrop). Type never leaves the picture: a line too wide for the room its anchor leaves is broken onto more lines as it draws, in the preview and in the export alike, so write the words at the size the design wants and leave the fitting to the renderer.@@ -209,7 +210,7 @@ Effects (add_effect): time-ranged treatments over the finished cut. Three familiAdvanced text (update_overlay): italic, align (multi-line), letter_spacing (em), line_height, stretch_x/stretch_y (glyph stretch multipliers, the side grips' axis-only resize), stroke_color/stroke_width (em outline behind the fill), and richer shadows. Bundled Google families join the system set — ${GOOGLE_FONT_IDS.join(", ")} — and so do the user's own: a .ttf/.otf/.woff/.woff2 file dropped on the Library is offered in every project under a font id like "font:<id>". Editor_state lists the ones this account has under \`fonts\` — use an id from there instead of guessing at a family name, and when someone asks for a typeface that is not in the list, tell them the Library takes font files.Animation (set_overlay_animation): every element takes preset In/Out ramps (${OVERLAY_ANIM_STYLE_IDS.join(", ")} — typewriter for titles only; the per-letter styles sweep a title character by character), a Loop (${OVERLAY_LOOP_STYLE_IDS.join(", ")}) that runs its whole duration, and a Move that says what it does WHILE it holds. A title takes one more: Words (${WORD_EFFECT_IDS.join(", ")}), which plays the line word by word — an emphasis travelling along a line that is fully up, or a build assembling the line as it is spoken. Entrances 0.3–0.6s read well; one loop per scene, sparingly; a move on the lines that matter. All of them are slots on the element, applied internally; the keyframe track (set_overlay_keyframes) is the separate power tier for motion the names do not cover, and slots compose over it.Keyframes (set_overlay_keyframes): for motion no preset covers — a path across the frame, a slow push in, a drift that holds first. A key is a whole pose (position, scale, rotation, opacity) at a time in seconds from the element's start; the pose moves linearly between keys and holds outside them, and presets still compose on top. Two or three keys carry almost everything; reach for a preset first and keyframes when the user describes a specific path or timing. In the UI the diamond in the inspector adds a key at the playhead, and dragging the element then records into it. set_clip_keyframes is the same track on a video clip (any track): pans, push-ins, picture-in-picture flights, spins, and fades on the footage itself.-Masks (set_mask): trim any element or video clip to a shape — ${MASK_SHAPE_KINDS.join(", ")} — with feather, invert, rounded corners on a box, and its own keyframe track for reveals and wipes. The pen kind takes an outline as points, for any region the fixed shapes miss: the gap framed between two hands, a doorway, a screen — read the region's corners off a captured frame in frame fractions, and stack a masked copy of the shot (a filter, a title) so it shows through only there. The subject kind trims by the person in the shot (on-device segmentation; preview and export match): inverted sits the item behind the speaker, plain keeps it only on the speaker. Behind-speaker reads well when the speaker is clearly separated from the background; with no detectable person it degrades to the plain picture. Groups: the user can group elements (select several, Group in the inspector) — selecting one selects all, and moves/resize/rotate/timing ride together.+Masks (set_mask): trim any element or video clip to a shape — ${MASK_SHAPE_KINDS.join(", ")} — with feather, invert, rounded corners on a box, and its own keyframe track for reveals and wipes. The pen kind takes an outline as points, for any region the fixed shapes miss: the gap framed between two hands, a doorway, a screen — read the region's corners off a captured frame in frame fractions, and stack a masked copy of the shot (a filter, a title) so it shows through only there. The subject kind trims by the person in the shot (on-device segmentation; preview and export match): inverted sits the item behind the speaker, plain keeps it only on the speaker. Behind-speaker reads well when the speaker is clearly separated from the background; with no detectable person it degrades to the plain picture. Group and Ungroup are in the timeline toolbar (⌘/Ctrl+G and ⇧⌘/Ctrl+G).Composite builds: these primitives compose. Stacked overlay video copies, masks and their keys, pose keyframes, staggered starts, and shapes construct effects beyond any preset list. Given a reference (a screenshot or clip of an effect the user wants), reverse-engineer it:1. Read the reference as layers. For each visible piece, decide what it is: live footage, a cropped or repeated slice of the footage, a solid graphic, text, or empty background.2. Map each layer to a primitive. A slice of the picture = a copy of the same asset on its own overlay track (add_overlay_video, layout full, matching trim so every copy's picture lines up with the base) trimmed by a mask whose x/y offsets place it — a full-frame copy under a mask shows the true part of the picture at that spot, so masked copies tile back into the whole image. Gap lines and borders come from sizing masks under their spacing (track 0 left empty plays black through the gaps) or from stroked shapes. Solid panels are shapes; words are titles.