Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
refactor(timeline): share the editor's insert-clip and crop writes as pure mutators
EtienneLescot039f38a9cedf
diff --git a/src/lib/ai-edition/document/timeline.test.ts b/src/lib/ai-edition/document/timeline.test.tsindex a33f1c6e7..7f203f32a 100644--- a/src/lib/ai-edition/document/timeline.test.ts+++ b/src/lib/ai-edition/document/timeline.test.ts@@ -9,6 +9,7 @@ import { audioLanePills } from "./audioTracks";import {buildTimelineFromIntervals,duplicateClip,+ insertAssetClip,invertIntervals,moveClip,normalizeIntervals,@@ -23,6 +24,7 @@ import {resequenceClips,resolvePlaybackSegments,restoreFullTimeline,+ setClipCrop,setClipSourceRange,subtractInterval,timelineIntervals,@@ -2260,3 +2262,70 @@ describe("readSpeedRegions", () => {expect(readSpeedRegions({ legacyEditor: null })).toEqual([]);});});++describe("insertAssetClip", () => {+ const doc = () =>+ makeDoc({+ timeline: {+ ...makeDoc().timeline,+ clips: [makeClip({ id: "clip_a", sourceStartSec: 0, sourceEndSec: 5 })],+ },+ });+ const clip = (sourceStartSec: number, sourceEndSec: number) => ({+ id: "clip_new",+ assetId: "asset_1",+ sourceStartSec,+ sourceEndSec,+ origin: "user" as const,+ reason: "",+ });++ it("places the clip at the index, lays the timeline back to back", () => {+ const next = insertAssetClip(doc(), 0, clip(20, 23));+ expect(next.timeline.clips.map((c) => [c.id, c.timelineStartSec, c.timelineEndSec])).toEqual([+ ["clip_new", 0, 3],+ ["clip_a", 3, 8],+ ]);+ });++ it("clamps the index and folds media that continues its neighbour into it", () => {+ const next = insertAssetClip(doc(), 99, clip(5, 9));+ expect(next.timeline.clips).toMatchObject([+ { id: "clip_a", sourceStartSec: 0, sourceEndSec: 9 },+ ]);+ });+});++describe("setClipCrop", () => {+ const crop = { x: 0, y: 0, width: 0.5, height: 0.5 };++ it("sets and clears a clip's crop", () => {+ const doc = makeDoc({+ timeline: { ...makeDoc().timeline, clips: [makeClip()] },+ });+ const cropped = setClipCrop(doc, "clip_a", crop);+ expect(cropped.timeline.clips[0].cropRegion).toEqual(crop);+ expect(setClipCrop(cropped, "clip_a", null).timeline.clips[0].cropRegion).toBeUndefined();+ });++ it("makes one clip of two meeting neighbours once their crops match", () => {+ const doc = makeDoc({+ timeline: {+ ...makeDoc().timeline,+ clips: [+ makeClip({ id: "clip_a", cropRegion: crop }),+ makeClip({+ id: "clip_b",+ sourceStartSec: 5,+ sourceEndSec: 9,+ timelineStartSec: 5,+ timelineEndSec: 9,+ }),+ ],+ },+ });+ expect(setClipCrop(doc, "clip_b", crop).timeline.clips).toMatchObject([+ { id: "clip_a", sourceStartSec: 0, sourceEndSec: 9, cropRegion: crop },+ ]);+ });+});diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.tsindex ba256fd3b..4cbe47869 100644--- a/src/lib/ai-edition/document/timeline.ts+++ b/src/lib/ai-edition/document/timeline.ts@@ -4,7 +4,13 @@// or a new document with updated clips.import { clampToBound } from "../../projectDefaults";-import type { AxcutClip, AxcutDocument, AxcutTranscript, AxcutTrimRange } from "../schema";+import type {+ AxcutClip,+ AxcutClipCropRegion,+ AxcutDocument,+ AxcutTranscript,+ AxcutTrimRange,+} from "../schema";/*** What `resolvePlaybackSegments` returns: a clip-shaped slice of playable film.@@ -992,8 +998,7 @@ export function moveClip(return withClipsChanged(document, reordered);}-// ponytail: duplicate a clip (preserves the original). Used for "split this-// clip into two" or "make a copy". Mirrors axcut's+// ponytail: duplicate a clip (preserves the original): "make a copy". Mirrors axcut's// apps/server/src/lib/timeline.ts#duplicateClip.//// The copy takes its own COPY of the original's trims, re-anchored to the new clip id.@@ -1053,6 +1058,49 @@ export function duplicateClip();}+/**+ * The single mutator for "place a clip of this asset at this position": the media panel's drop+ * onto the timeline and the agent's `insertClip`. `index` counts the clips before it (0 first,+ * `clips.length` last) and is clamped. The new clip goes through `withClipsChanged` like every+ * other clip edit, so one whose media meets a neighbour's is folded into it. Pure.+ */+export function insertAssetClip(+ document: AxcutDocument,+ index: number,+ clip: Pick<AxcutClip, "id" | "assetId" | "sourceStartSec" | "origin" | "reason"> & {+ sourceEndSec: number;+ },+): AxcutDocument {+ const length = clip.sourceEndSec - clip.sourceStartSec;+ const clips = [...document.timeline.clips];+ clips.splice(Math.max(0, Math.min(clips.length, index)), 0, {+ ...clip,+ timelineStartSec: 0,+ timelineEndSec: length,+ wordRefs: [],+ });+ return withClipsChanged(document, clips);+}++/**+ * The single mutator for a clip's crop: the clip's Edit modal and the agent's `setClipRange`.+ * `null` clears it back to the full frame rather than storing the identity region. Through+ * `withClipsChanged`, since crop is the one property that keeps two meeting clips apart. An+ * unknown `clipId` is a no-op. Pure.+ */+export function setClipCrop(+ document: AxcutDocument,+ clipId: string,+ cropRegion: AxcutClipCropRegion | null,+): AxcutDocument {+ return withClipsChanged(+ document,+ document.timeline.clips.map((c) =>+ c.id === clipId ? { ...c, cropRegion: cropRegion ?? undefined } : c,+ ),+ );+}+/*** The single mutator for "narrow/extend a clip's own source in/out" — the edit the* clip's Edit modal, the renderer op dispatcher, and the LLM's `setClipRange` tool alldiff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.tsindex b8a867f77..8312e600c 100644--- a/src/lib/ai-edition/store/useTimeline.ts+++ b/src/lib/ai-edition/store/useTimeline.ts@@ -27,6 +27,7 @@ import {clearEditRegions,countEditRegions,duplicateClip as duplicateClipInDocument,+ insertAssetClip,moveClip as moveClipInDocument,PLACEHOLDER_DURATION_SEC,type RegionKind,@@ -34,8 +35,8 @@ import {removeClip as removeClipInDocument,removeRegion as removeRegionInDocument,resequenceClips,+ setClipCrop,setClipSourceRange,- withClipsChanged,} from "../document/timeline";import type { AxcutAudioTrack, AxcutClipCropRegion, AxcutDocument } from "../schema";import { appendAutoZoomSuggestions } from "../timeline/apply-auto-zooms";@@ -81,8 +82,6 @@ interface RegionHandle {id: string;}-type Clip = AxcutDocument["timeline"]["clips"][number];-/*** Patch the region `id` belongs to: every fragment of it across clip junctions, or the* pieces of one region would disagree and, by the merge rule, visibly split. Not the whole@@ -1223,18 +1222,7 @@ export function useTimeline() {const doc = useProjectStore.getState().document;if (!doc) return;const ranged = setClipSourceRange(doc, clipId, sourceStartSec, sourceEndSec);- const next: AxcutDocument =- cropRegion === undefined- ? ranged- : {- ...ranged,- timeline: {- ...ranged.timeline,- clips: ranged.timeline.clips.map((c) =>- c.id === clipId ? { ...c, cropRegion: cropRegion ?? undefined } : c,- ),- },- };+ const next = cropRegion === undefined ? ranged : setClipCrop(ranged, clipId, cropRegion);await saveDocument(next, { history: true });},[saveDocument],@@ -1339,22 +1327,15 @@ export function useTimeline() {// durationSec we use it; otherwise we fall back to the placeholder// and let the background probe correct it.const knownDuration = asset.durationSec ?? PLACEHOLDER_DURATION_SEC;- const newClip: Clip = {+ const newClip = {id: createId("clip"),assetId,sourceStartSec: 0,sourceEndSec: knownDuration,- timelineStartSec: 0,- timelineEndSec: knownDuration,- wordRefs: [],- origin: "user",+ origin: "user" as const,reason: "Inserted from media panel",};- const oldClips = currentDoc.timeline.clips;- const arr = [...oldClips];- const at = Math.max(0, Math.min(arr.length, index));- arr.splice(at, 0, newClip);- const finalDoc = withClipsChanged(currentDoc, arr);+ const finalDoc = insertAssetClip(currentDoc, index, newClip);if (!(await saveDocument(finalDoc, { history: true }))) return;setClipSelection(newClip.id);