Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): apply a clip's range and crop in one pass

EtienneLescotc3d167f2569a
diff --git a/electron/ai-edition/agent-tools.test.ts b/electron/ai-edition/agent-tools.test.ts
index 98487a3d5..26fd98cb5 100644
--- a/electron/ai-edition/agent-tools.test.ts
+++ b/electron/ai-edition/agent-tools.test.ts
@@ -3656,6 +3656,28 @@ describe("clip structure (#1180)", () => {
expect(run(twoTakes(), "setClipRange", { clipId: "clip_1" }).ok).toBe(false);
});
+ it("setClipRange weighs a new range and a new crop together", () => {
+ // clip_2 is moved to meet clip_1 in the media AND given its own crop in one call: the
+ // crop keeps them apart, so neither edit is lost to a fold.
+ const placed = run(twoTakes(), "insertClip", {
+ assetId: "asset_1",
+ sourceStartSec: 12,
+ sourceEndSec: 16,
+ });
+ const { clipId } = JSON.parse(placed.resultJson);
+ const crop = { x: 0, y: 0, width: 0.5, height: 0.5 };
+ const result = run(placed.document as AxcutDocument, "setClipRange", {
+ clipId,
+ sourceStartSec: 10,
+ sourceEndSec: 16,
+ cropRegion: crop,
+ });
+ expect((result.document as AxcutDocument).timeline.clips).toMatchObject([
+ { id: "clip_1", sourceEndSec: 10 },
+ { id: clipId, sourceStartSec: 10, sourceEndSec: 16, cropRegion: crop },
+ ]);
+ });
+
it("setClipRange moves one edge and keeps the other", () => {
const result = run(twoTakes(), "setClipRange", { clipId: "clip_1", sourceEndSec: 6 });
expect((result.document as AxcutDocument).timeline.clips[0]).toMatchObject({
diff --git a/electron/ai-edition/agent-tools.ts b/electron/ai-edition/agent-tools.ts
index 83ea23f9b..2df9ab491 100644
--- a/electron/ai-edition/agent-tools.ts
+++ b/electron/ai-edition/agent-tools.ts
@@ -2363,13 +2363,11 @@ export function executeAgentTool(
cropRegion.y === 0 &&
cropRegion.width === 1 &&
cropRegion.height === 1;
- const afterRange = ranged
- ? setClipSourceRange(document, clipId, sourceStartSec, sourceEndSec)
- : document;
- const next =
- cropRegion === undefined
- ? afterRange
- : setClipCrop(afterRange, clipId, wholeFrame ? null : cropRegion);
+ const crop = cropRegion === undefined ? undefined : wholeFrame ? null : cropRegion;
+ // One pass for both, as the dialog's Apply: the fold weighs range and crop together.
+ const next = ranged
+ ? setClipSourceRange(document, clipId, sourceStartSec, sourceEndSec, crop)
+ : setClipCrop(document, clipId, crop ?? null);
const dropped = droppedByEdit(document, next);
const casualties = dropped.droppedModifierIds.length + dropped.droppedTrimIds.length;
const after = next.timeline.clips.find((c) => c.id === clipId);
diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.ts
index 4cbe47869..4e1a75141 100644
--- a/src/lib/ai-edition/document/timeline.ts
+++ b/src/lib/ai-edition/document/timeline.ts
@@ -1083,7 +1083,8 @@ export function insertAssetClip(
}
/**
- * The single mutator for a clip's crop: the clip's Edit modal and the agent's `setClipRange`.
+ * The single mutator for a clip's crop alone (the agent's crop-only `setClipRange`); with a range,
+ * `setClipSourceRange` takes the crop in the same pass.
* `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.
@@ -1113,19 +1114,31 @@ export function setClipCrop(
* its source length), lay everything back-to-back, then `rederiveRegionMs` clamps every
* anchored pill to the clip's kept window (dropping what the trim removed) and refreshes the
* derived ms of the clips that reflowed. An unknown `clipId` is a no-op.
+ *
+ * `cropRegion` is the Edit modal's crop, set in the same pass: `undefined` leaves it, `null`
+ * clears it. One pass, because the fold decides on range AND crop together: a range applied
+ * first can fold the clip into a neighbour whose crop the new one would have told apart.
*/
export function setClipSourceRange(
document: AxcutDocument,
clipId: string,
sourceStartSec: number,
sourceEndSec: number,
+ cropRegion?: AxcutClipCropRegion | null,
): AxcutDocument {
const clamp = (n: number) => (Number.isFinite(n) ? Math.max(0, n) : 0);
const lo = Math.min(clamp(sourceStartSec), clamp(sourceEndSec));
const hi = Math.max(clamp(sourceStartSec), clamp(sourceEndSec));
const arr = document.timeline.clips.map((c) =>
c.id === clipId
- ? { ...c, sourceStartSec: lo, sourceEndSec: hi, timelineStartSec: 0, timelineEndSec: 0 }
+ ? {
+ ...c,
+ sourceStartSec: lo,
+ sourceEndSec: hi,
+ timelineStartSec: 0,
+ timelineEndSec: 0,
+ ...(cropRegion === undefined ? {} : { cropRegion: cropRegion ?? undefined }),
+ }
: c,
);
return withClipsChanged(document, arr);
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 8312e600c..7c173f870 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -35,7 +35,6 @@ import {
removeClip as removeClipInDocument,
removeRegion as removeRegionInDocument,
resequenceClips,
- setClipCrop,
setClipSourceRange,
} from "../document/timeline";
import type { AxcutAudioTrack, AxcutClipCropRegion, AxcutDocument } from "../schema";
@@ -1221,8 +1220,7 @@ export function useTimeline() {
) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- const ranged = setClipSourceRange(doc, clipId, sourceStartSec, sourceEndSec);
- const next = cropRegion === undefined ? ranged : setClipCrop(ranged, clipId, cropRegion);
+ const next = setClipSourceRange(doc, clipId, sourceStartSec, sourceEndSec, cropRegion);
await saveDocument(next, { history: true });
},
[saveDocument],