Open source CapCut alternative.
TypeScript
feat(site): copy items onto a chosen lane [rebuild]
David Le391c7d95540f
diff --git a/site/src/cut/components/Timeline.tools.ts b/site/src/cut/components/Timeline.tools.tsindex 16e750d0..2793de72 100644--- a/site/src/cut/components/Timeline.tools.ts+++ b/site/src/cut/components/Timeline.tools.ts@@ -415,11 +415,12 @@ export const TIMELINE_TOOLS = [{name: "copy_from_timeline",description:- "Copy items another timeline holds onto the open one, exactly as that timeline has them: a soundtrack clip keeps its in/out, volume, fades, speed and sound treatment; clips, elements, captions and transitions keep every property, and several items keep their spacing. This is how \"Main's music\" or \"the same title as on Main\" lands on this timeline. A parked timeline's soundtrack is in editor_state project.timelines; get_state with `timeline` lists all of its items. The source timeline is left as it was.",+ "Copy items another timeline holds onto the open one, exactly as that timeline has them: a soundtrack clip keeps its in/out, volume, fades, speed and sound treatment; clips, elements, captions and transitions keep every property, and several items keep their spacing. This is how \"Main's music\" or \"the same title as on Main\" lands on this timeline. A parked timeline's soundtrack is in editor_state project.timelines; get_state with `timeline` lists all of its items. The source timeline is left as it was. A copy that would overlap an item on its row slides right past it, and the result gives that copy's askedStart and why.",inputSchema: obj({timeline: { type: "string", enum: [...TIMELINE_IDS], description: "The timeline to copy from" },ids: { type: "array", items: { type: "string" }, description: "Ids of the items on that timeline" },at: num("Where the earliest copy starts on the open timeline, seconds; omit to keep the source's times"),+ lane: { type: "integer", minimum: 0, description: "Soundtrack lane or element row for the copies (the lowest one when they span several); omit to keep the source's. Video clips keep their tracks." },}, ["timeline", "ids"]),},{diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex 05caabf6..5fa23445 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -14,9 +14,9 @@ import { projectOperation, type ProjectOperation } from "./projectOperation";import { captureRenderSnapshot, renderDoc } from "./renderSnapshot";import { submitPreviewSnapshot } from "./exportClient";-import { ITEM_KIND_IDS, ITEM_KINDS, ROW_ITEM_KINDS, type ItemKind } from "./itemKinds";+import { ITEM_KIND_IDS, ITEM_KINDS, ROW_ITEM_KINDS, type ItemKind, type TimelineClipboardItem } from "./itemKinds";import { insertItemRow, packRows } from "./laneTracks";-import { timelineCopies, timelineItem } from "./timelineItems";+import { timelineBlocker, timelineCopies, timelineItem } from "./timelineItems";import { movePreviewSelection, previewSelectionSnapshot } from "@/cut/lib/previewSelection";import { libraryShareTargetSchema, shareSettingsSchema, librarySharePath } from "@/cut/lib/librarySharing";@@ -321,6 +321,7 @@ import {IMAGE_CLIP_SECONDS,isEffectOverlay,isTextOverlay,+ laneOf,LAYOUTS,MAX_SUBTITLE_LANES,nearestAspect,@@ -5841,16 +5842,37 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const missing = ids.filter((_, i) => !found[i]);if (missing.length > 0)throw new ToolError(`${TIMELINE_LABELS[input.timeline]} has no item ${missing.join(", ")}; get_state with timeline "${input.timeline}" lists its items.`);- const items = timelineCopies(source, found);+ if (input.lane !== undefined && (!isNum(input.lane) || !Number.isInteger(input.lane) || input.lane < 0)) {+ throw new ToolError("lane must be a non-negative integer.");+ }+ const copies = timelineCopies(source, found);+ const items = isNum(input.lane) ? copiesOnLane(copies, input.lane) : copies;const at = isNum(input.at) ? Math.max(0, input.at) : Math.min(...items.map((cb) => cb.item.start));++ // What holds each copy's row at the asked time, read before the paste+ // slides the copies past it.+ const asked = at - Math.min(...items.map((cb) => cb.item.start));+ const blockers = items.map((cb) => timelineBlocker(s, cb, asked));+const landed = s.pasteCopies(items, at);if (!landed) throw new ToolError("Nothing could be copied onto this timeline.");const now = useEditor.getState();return {timeline: now.timeline,- copied: landed.map((sel) => {+ copied: landed.map((sel, i) => {const item = timelineItem(now, sel)!;- return { kind: sel.kind, id: sel.id, start: roundT(item.start) };+ const start = roundT(item.start);+ const askedStart = roundT(items[i].item.start + asked);+ if (start === askedStart) {+ return { kind: sel.kind, id: sel.id, start };+ }++ // A slid copy names what took its spot, or the copy it kept its spacing with.+ const blocker = blockers[i];+ const why = blocker+ ? `${blocker.kind} ${blocker.id} holds lane ${blocker.lane} from ${roundT(blocker.start)} to ${roundT(blocker.end)} s`+ : "kept its spacing with a copy that slid past a taken spot";+ return { kind: sel.kind, id: sel.id, start, askedStart, why };}),};},@@ -7623,6 +7645,26 @@ function audioPackHint(id: string): { freeLane?: number; note?: string } {};}+/** Soundtrack and element copies moved onto `lane`, each kind keeping its rows'+ * order: copies from lanes 2 and 3 asked onto lane 0 land on 0 and 1. Video+ * clips keep their tracks. */+function copiesOnLane(items: TimelineClipboardItem[], lane: number): TimelineClipboardItem[] {+ const lowest = { audio: Infinity, overlay: Infinity };+ for (const cb of items) {+ if (cb.kind === "audio" || cb.kind === "overlay") {+ lowest[cb.kind] = Math.min(lowest[cb.kind], laneOf(cb.item));+ }+ }++ // Each copy moves by the distance from its kind's lowest row to `lane`.+ return items.map((cb) => {+ if (cb.kind !== "audio" && cb.kind !== "overlay") {+ return cb;+ }+ return { ...cb, item: { ...cb.item, lane: lane + laneOf(cb.item) - lowest[cb.kind] } } as TimelineClipboardItem;+ });+}+function audioLaneInput(value: unknown): number {if (!isNum(value) || !Number.isInteger(value) || value < 0)throw new ToolError("Audio lane must be a non-negative integer.");diff --git a/site/src/cut/lib/timelineCopyTool.test.ts b/site/src/cut/lib/timelineCopyTool.test.tsindex 7a2a6f0c..8184fa5c 100644--- a/site/src/cut/lib/timelineCopyTool.test.ts+++ b/site/src/cut/lib/timelineCopyTool.test.ts@@ -56,3 +56,37 @@ test("copy_from_timeline names the ids it could not find", async () => {const error = await runAiTool("copy_from_timeline", { timeline: "main", ids: ["nope"] }).catch((e: unknown) => e);expect(error instanceof Error && error.message.includes("nope")).toBe(true);});++// Asked for Main's music at 5.5 s on lane 1 beside a click on lane 0, the copy+// took the source's lane 0, slid past the click to 6.117 s, and the result+// read as if it landed where asked.++const clickAsset: MediaAsset = { id: "click-file", fileName: "click.wav", name: "Click", type: "audio", duration: 0.9, url: "" };+const click: AudioClip = { id: "click", assetId: "click-file", start: 5.217, in: 0, out: 0.9, volume: 1, fadeIn: 0, fadeOut: 0 };++// Second holds the click on lane 0 before each copy.+function seedClick() {+ useEditor.setState((s) => ({ assets: [...s.assets, clickAsset], audioClips: [click] }));+}++test("copy_from_timeline lands on the asked lane at the asked time", async () => {+ seedClick();+ const result = (await runAiTool("copy_from_timeline", { timeline: "main", ids: ["bed"], at: 5.5, lane: 1 })) as {+ copied: { start: number; askedStart?: number }[];+ };+ const copy = useEditor.getState().audioClips.find((a) => a.assetId === "music")!;+ expect(copy).toMatchObject({ start: 5.5, lane: 1 });+ expect(result.copied[0].start).toBe(5.5);+ expect(result.copied[0].askedStart).toBeUndefined();+});++test("copy_from_timeline says when a copy slid past a taken spot", async () => {+ seedClick();+ const result = (await runAiTool("copy_from_timeline", { timeline: "main", ids: ["bed"], at: 5.5 })) as {+ copied: { start: number; askedStart?: number; why?: string }[];+ };+ const [item] = result.copied;+ expect(item.start).toBe(6.117);+ expect(item.askedStart).toBe(5.5);+ expect(item.why).toContain("click");+});diff --git a/site/src/cut/lib/timelineItems.ts b/site/src/cut/lib/timelineItems.tsindex f0df19c9..d9fc7fbb 100644--- a/site/src/cut/lib/timelineItems.ts+++ b/site/src/cut/lib/timelineItems.ts@@ -87,6 +87,24 @@ export function timelinePlacementDelta(s: ItemLists, items: TimelineClipboardItereturn delta;}+/** The item already on a copy's row where the copy, shifted by `delta`, would land. */+export function timelineBlocker(s: ItemLists, cb: TimelineClipboardItem, delta: number) {+ const def = definition(cb.kind);+ const copy = timelineFootprint(cb);++ // Only the copy's own row can block it, and only where the spans overlap.+ for (const item of def.list(s)) {+ const held = timelineFootprint({ kind: cb.kind, item } as TimelineClipboardItem);+ if (held.row !== copy.row) {+ continue;+ }+ if (copy.start + delta < held.end - 1e-6 && copy.end + delta > held.start + 1e-6) {+ return { kind: cb.kind, id: item.id, lane: def.lane(item), start: held.start, end: held.end };+ }+ }+ return null;+}+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),