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(editor): apply the Edit Clip source range and crop as one document, one save

EtienneLescotc2ffd7c6977c
diff --git a/src/components/ai-edition/Modals.tsx b/src/components/ai-edition/Modals.tsx
index cc077efd1..9ac9d1079 100644
--- a/src/components/ai-edition/Modals.tsx
+++ b/src/components/ai-edition/Modals.tsx
@@ -704,7 +704,7 @@ interface EditClipModalProps extends BaseModalProps {
// a draggable dual-handle range over the asset's full source duration —
// replaces the old numeric-input-only form. Trim range AND crop are both
// per-clip and both edited here (see clipSchema.cropRegion / useTimeline's
-// updateClipSourceRange + updateClipCrop) — crop used to be a document-wide
+// applyClipEdit, which composes the two into one save) — crop used to be a document-wide
// setting behind its own facet-rail button; it's a framing choice for one
// piece of footage, so it belongs with the rest of this clip's edits.
export function EditClipModal({
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index 8c9ad63b5..4026266cb 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -1331,8 +1331,14 @@ export function NewEditorShell() {
videoSources={videoSources}
onApply={(sStart, sEnd, cropRegion) => {
if (!editClipTarget) return;
- void tl.updateClipSourceRange(editClipTarget.id, sStart, sEnd);
- if (cropRegion !== undefined) void tl.updateClipCrop(editClipTarget.id, cropRegion);
+ const clipId = editClipTarget.id;
+ // One user action, one document, one save. This used to be two calls —
+ // `updateClipSourceRange` then `updateClipCrop` — each building its next
+ // document from the same pre-Apply one, so the second write clobbered the
+ // first and one of the two edits vanished silently (#355). It goes on the
+ // shared write queue for the same reason every other timeline edit does:
+ // so it can't clobber, or be clobbered by, a save already in flight.
+ void enqueueTimelineWrite(() => tl.applyClipEdit(clipId, sStart, sEnd, cropRegion));
setEditClipTarget(null);
}}
/>
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index cc51b3a01..5eeef2e32 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -341,7 +341,7 @@ describe("useTimeline backfills missing source dimensions on load", () => {
});
});
-describe("useTimeline.updateClipSourceRange (Edit-clip modal)", () => {
+describe("useTimeline.applyClipEdit (Edit-clip modal)", () => {
const anchoredZoom = (id: string, s: number, e: number) => ({
id,
startMs: s * 1000,
@@ -380,7 +380,7 @@ describe("useTimeline.updateClipSourceRange (Edit-clip modal)", () => {
const { result } = renderTimeline();
// Trim the 10s clip down to its first 4s of source.
await act(async () => {
- await result.current.updateClipSourceRange("clip_a", 0, 4);
+ await result.current.applyClipEdit("clip_a", 0, 4);
});
const clip = useProjectStore.getState().document?.timeline.clips[0];
expect(clip).toMatchObject({ sourceStartSec: 0, sourceEndSec: 4 });
@@ -392,7 +392,7 @@ describe("useTimeline.updateClipSourceRange (Edit-clip modal)", () => {
it("drops a pill sitting over the truncated tail and keeps the one that survives", async () => {
const { result } = renderTimeline();
await act(async () => {
- await result.current.updateClipSourceRange("clip_a", 0, 4);
+ await result.current.applyClipEdit("clip_a", 0, 4);
});
const zooms = useProjectStore.getState().document?.zoomRanges ?? [];
// z_keep (source 2-3) stays; z_drop (source 6-8) is entirely past the new 4s end.
@@ -414,7 +414,7 @@ describe("useTimeline.updateClipSourceRange (Edit-clip modal)", () => {
});
const { result } = renderTimeline();
await act(async () => {
- await result.current.updateClipSourceRange("clip_a", 0, 5);
+ await result.current.applyClipEdit("clip_a", 0, 5);
});
const zooms = useProjectStore.getState().document?.zoomRanges ?? [];
expect(zooms).toHaveLength(1);
@@ -426,6 +426,57 @@ describe("useTimeline.updateClipSourceRange (Edit-clip modal)", () => {
endMs: 5000,
});
});
+
+ // #355. Apply used to fire `updateClipSourceRange` and `updateClipCrop` as two
+ // concurrent saves, each built from the same pre-Apply document — so the second
+ // write clobbered the first and one of the two edits vanished with no error and no
+ // toast. Which one survived depended on IPC timing, which is why it read as "the app
+ // randomly forgets my crop".
+ it("keeps BOTH the source range and the crop when Apply changes them together", async () => {
+ const { result } = renderTimeline();
+ const crop = { x: 0.1, y: 0.2, width: 0.5, height: 0.5 };
+ await act(async () => {
+ await result.current.applyClipEdit("clip_a", 0, 4, crop);
+ });
+ const clip = useProjectStore.getState().document?.timeline.clips[0];
+ expect(clip).toMatchObject({ sourceStartSec: 0, sourceEndSec: 4, cropRegion: crop });
+ // The width still followed the range edit — the crop is applied to the
+ // RESEQUENCED clips, not to a stale copy of them.
+ expect(clip?.timelineEndSec).toBe(4);
+ // One user action, one document, one write: two saves is the race itself.
+ expect(bridgeMocks.save).toHaveBeenCalledTimes(1);
+ });
+
+ it("clears the crop on an explicit null and leaves it alone on undefined", async () => {
+ useProjectStore.setState({
+ document: {
+ ...sampleDoc,
+ timeline: {
+ ...sampleDoc.timeline,
+ clips: [
+ { ...sampleDoc.timeline.clips[0], cropRegion: { x: 0, y: 0, width: 0.5, height: 1 } },
+ ],
+ },
+ },
+ });
+ const { result } = renderTimeline();
+ // `undefined` is the modal's "crop section untouched" — the stored region stays.
+ await act(async () => {
+ await result.current.applyClipEdit("clip_a", 0, 6);
+ });
+ expect(useProjectStore.getState().document?.timeline.clips[0].cropRegion).toEqual({
+ x: 0,
+ y: 0,
+ width: 0.5,
+ height: 1,
+ });
+ // `null` is "reset to no crop", stored as an absent field rather than the
+ // identity region.
+ await act(async () => {
+ await result.current.applyClipEdit("clip_a", 0, 6, null);
+ });
+ expect(useProjectStore.getState().document?.timeline.clips[0].cropRegion).toBeUndefined();
+ });
});
describe("useTimeline.addAnnotation", () => {
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 716b4003a..11f3773b0 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -858,38 +858,56 @@ export function useTimeline() {
setClipSelection(null);
}, []);
- // Axcut-consistent clip trim: only the source range is user-editable (the
- // Edit Clip dialog's draggable track). Changing it changes the clip's
- // effective duration, so every clip is resequenced back-to-back afterward —
- // same invariant as insertClipAt/moveClip/removeClip — instead of leaving
- // downstream clips at their old timeline positions (which would overlap).
- // The whole recipe (resequence width + clamp/rederive pills) lives in the one
- // pure `setClipSourceRange`, shared with the op dispatcher and the LLM tool.
- const updateClipSourceRange = useCallback(
- async (clipId: string, sourceStartSec: number, sourceEndSec: number) => {
- if (!document) return;
- await saveDocument(setClipSourceRange(document, clipId, sourceStartSec, sourceEndSec));
- },
- [document, saveDocument],
- );
-
- // Crop is a per-clip framing, not a document-wide setting — two clips
- // (even from the same asset) can reasonably want different crops. Passing
- // `null` clears it back to "no crop" (full frame) instead of storing the
- // identity region explicitly.
- const updateClipCrop = useCallback(
- async (clipId: string, region: AxcutClipCropRegion | null) => {
- if (!document) return;
- const arr = document.timeline.clips.map((c) =>
- c.id === clipId ? { ...c, cropRegion: region ?? undefined } : c,
- );
- const next: AxcutDocument = {
- ...document,
- timeline: { ...document.timeline, clips: arr },
- };
+ // The Edit Clip dialog's Apply, as ONE document and ONE save.
+ //
+ // Source range and crop are two edits made in a single user action, and they used to
+ // be two independent saves fired back to back. Both built their next document from
+ // the SAME pre-Apply one — the crop write never saw the source-range change — so
+ // whichever IPC write landed last silently dropped the other edit, with no error and
+ // no toast (#355). Composing them means the crop is applied to the *resequenced*
+ // clips, which is also the only order that can be right.
+ //
+ // Axcut-consistent clip trim: only the source range is user-editable (the dialog's
+ // draggable track). Changing it changes the clip's effective duration, so every clip
+ // is resequenced back-to-back afterward — same invariant as
+ // insertClipAt/moveClip/removeClip — instead of leaving downstream clips at their old
+ // timeline positions (which would overlap). That whole recipe (resequence width +
+ // clamp/rederive pills) lives in the one pure `setClipSourceRange`, shared with the op
+ // dispatcher and the LLM tool.
+ //
+ // Crop is a per-clip framing, not a document-wide setting — two clips (even from the
+ // same asset) can reasonably want different crops. `undefined` means the dialog's crop
+ // section was never touched (leave the stored value alone); `null` clears it back to
+ // "no crop" (full frame) rather than storing the identity region explicitly.
+ //
+ // The document is read from the store, not off the render closure, so this composes
+ // with `useSequentialTimelineOps`: queued behind another timeline write, it still sees
+ // what that write committed. Same reason as `setTrimEntries` / `insertClipAt`.
+ const applyClipEdit = useCallback(
+ async (
+ clipId: string,
+ sourceStartSec: number,
+ sourceEndSec: number,
+ cropRegion?: AxcutClipCropRegion | null,
+ ) => {
+ 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,
+ ),
+ },
+ };
await saveDocument(next);
},
- [document, saveDocument],
+ [saveDocument],
);
// Background probe: read the asset's actual duration and patch the
@@ -1111,8 +1129,7 @@ export function useTimeline() {
removeRegions,
selectRegion,
clearSelection,
- updateClipSourceRange,
- updateClipCrop,
+ applyClipEdit,
insertClipAt,
moveClip,
duplicateClip,