Skip to content

Commit 240c62e

Browse files
committed
fix(editor): turn a clip before its trim joins it, freehand outline included
Two gaps in the Edit-clip Apply, both from review: - A freehand privacy blur mirrored its box but not its outline points, so after a turn the mosaic covered the wrong spot and the content it was drawn over showed in the clear. The points now mirror in the same save. - The rotation was written after setClipSourceRange, whose join ran while the clip was still upright: a trim that made its media meet an upright neighbour let the neighbour swallow it and the turn was lost. The turn and its mirrored zooms and blurs now go in first.
1 parent 889ea16 commit 240c62e

2 files changed

Lines changed: 125 additions & 13 deletions

File tree

‎src/lib/ai-edition/store/useTimeline.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,104 @@ describe("useTimeline.applyClipEdit (Edit-clip modal)", () => {
805805
const saved = useProjectStore.getState().document?.annotations[0];
806806
expect(saved?.position).toEqual({ x: 0, y: 0 });
807807
});
808+
809+
// The outline of a freehand blur turns with its box, or the mosaic lands next to what it
810+
// was drawn over and that content shows in the clear.
811+
it("mirrors a turned freehand blur's outline with its box", async () => {
812+
const doc = useProjectStore.getState().document as AxcutDocument;
813+
const blur: AxcutDocument["annotations"][number] = {
814+
id: "blur_freehand",
815+
startMs: 2000,
816+
endMs: 3000,
817+
clipId: "clip_a",
818+
sourceStartSec: 2,
819+
sourceEndSec: 3,
820+
type: "blur",
821+
content: "",
822+
position: { x: 10, y: 20 },
823+
size: { width: 30, height: 15 },
824+
style: {
825+
color: "#ffffff",
826+
backgroundColor: "transparent",
827+
fontSize: 32,
828+
fontFamily: "Inter",
829+
fontWeight: "bold",
830+
fontStyle: "normal",
831+
textDecoration: "none",
832+
textAlign: "center",
833+
textAnimation: "none",
834+
},
835+
zIndex: 1,
836+
blurData: {
837+
type: "mosaic",
838+
shape: "freehand",
839+
color: "black",
840+
intensity: 12,
841+
blockSize: 20,
842+
freehandPoints: [
843+
{ x: 0, y: 10 },
844+
{ x: 75, y: 0 },
845+
{ x: 100, y: 90 },
846+
],
847+
},
848+
};
849+
useProjectStore.setState({ document: { ...doc, annotations: [blur] } });
850+
const { result } = renderTimeline();
851+
const saved = () => useProjectStore.getState().document?.annotations[0];
852+
853+
await act(async () => {
854+
await result.current.applyClipEdit("clip_a", 0, 10, undefined, 180);
855+
});
856+
expect(saved()?.blurData?.freehandPoints).toEqual([
857+
{ x: 100, y: 90 },
858+
{ x: 25, y: 100 },
859+
{ x: 0, y: 10 },
860+
]);
861+
expect(saved()?.blurData).toMatchObject({ shape: "freehand", color: "black", blockSize: 20 });
862+
863+
await act(async () => {
864+
await result.current.applyClipEdit("clip_a", 0, 10, undefined, 0);
865+
});
866+
expect(saved()?.blurData?.freehandPoints).toEqual(blur.blurData?.freehandPoints);
867+
});
868+
869+
// Trimming a clip so its media meets an upright neighbour and turning it in the same Apply
870+
// must leave two clips: the join may only see the clip as it is after the edit.
871+
it("keeps the rotation when the same Apply makes the clip's media meet a neighbour", async () => {
872+
const clipA = {
873+
...sampleDoc.timeline.clips[0],
874+
sourceStartSec: 0,
875+
sourceEndSec: 10,
876+
timelineStartSec: 0,
877+
timelineEndSec: 10,
878+
};
879+
const clipB = {
880+
...clipA,
881+
id: "clip_b",
882+
sourceStartSec: 12,
883+
sourceEndSec: 20,
884+
timelineStartSec: 10,
885+
timelineEndSec: 18,
886+
};
887+
const doc = useProjectStore.getState().document as AxcutDocument;
888+
useProjectStore.setState({
889+
document: {
890+
...doc,
891+
timeline: { ...doc.timeline, clips: [clipA, clipB] },
892+
zoomRanges: [],
893+
},
894+
});
895+
const { result } = renderTimeline();
896+
897+
await act(async () => {
898+
await result.current.applyClipEdit("clip_b", 10, 20, undefined, 180);
899+
});
900+
const clips = useProjectStore.getState().document?.timeline.clips;
901+
expect(clips?.map((c) => [c.id, c.sourceStartSec, c.sourceEndSec, c.rotation])).toEqual([
902+
["clip_a", 0, 10, undefined],
903+
["clip_b", 10, 20, 180],
904+
]);
905+
});
808906
});
809907

810908
// #353. The toolbar button and the `C` shortcut both used to write a region on a

‎src/lib/ai-edition/store/useTimeline.ts‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,9 @@ export function useTimeline() {
13331333
// "no crop" (full frame) rather than storing the identity region explicitly.
13341334
//
13351335
// Rotation follows the same rule: `undefined` leaves the stored value, `0` removes the
1336-
// field, `180` sets it.
1336+
// field, `180` sets it. It goes in before the source range, because `setClipSourceRange`
1337+
// joins contiguous clips: a trim that makes the media meet an upright neighbour must not
1338+
// let that neighbour swallow a clip turned in the same Apply.
13371339
//
13381340
// The document is read from the store, not off the render closure, so this composes
13391341
// with `useSequentialTimelineOps`: queued behind another timeline write, it still sees
@@ -1348,17 +1350,16 @@ export function useTimeline() {
13481350
) => {
13491351
const doc = useProjectStore.getState().document;
13501352
if (!doc) return;
1351-
const ranged = setClipSourceRange(doc, clipId, sourceStartSec, sourceEndSec, cropRegion);
13521353
const current = doc.timeline.clips.find((c) => c.id === clipId);
13531354
const turning = rotation !== undefined && !!current && clipRotation(current) !== rotation;
1354-
const next: AxcutDocument =
1355+
const turned: AxcutDocument =
13551356
rotation === undefined
1356-
? ranged
1357+
? doc
13571358
: {
1358-
...ranged,
1359+
...doc,
13591360
timeline: {
1360-
...ranged.timeline,
1361-
clips: ranged.timeline.clips.map((c) => {
1361+
...doc.timeline,
1362+
clips: doc.timeline.clips.map((c) => {
13621363
if (c.id !== clipId) return c;
13631364
const { rotation: _old, ...rest } = c;
13641365
return rotation === 180 ? { ...rest, rotation: 180 as const } : rest;
@@ -1367,29 +1368,42 @@ export function useTimeline() {
13671368
// A stored zoom focus is where the viewer sees it; turning the clip moves that
13681369
// content to the mirrored point, so the clip's own zooms follow it.
13691370
zoomRanges: turning
1370-
? ranged.zoomRanges.map((z) =>
1371+
? doc.zoomRanges.map((z) =>
13711372
z.clipId === clipId
13721373
? { ...z, focus: { ...z.focus, cx: 1 - z.focus.cx, cy: 1 - z.focus.cy } }
13731374
: z,
13741375
)
1375-
: ranged.zoomRanges,
1376+
: doc.zoomRanges,
13761377
// A footage-space privacy blur must stay on what it hides, so it moves with
1377-
// that content. Frame-space annotations, text and figures do not turn. Clamped
1378-
// at 0 like `turnCropRegion`: a box flush against the far edge would turn to -5e-15.
1378+
// that content, freehand outline included. Frame-space annotations, text and
1379+
// figures do not turn. Clamped at 0 like `turnCropRegion`: a box flush against
1380+
// the far edge would turn to -5e-15.
13791381
annotations: turning
1380-
? ranged.annotations.map((a) =>
1382+
? doc.annotations.map((a) =>
13811383
a.clipId === clipId && a.type === "blur" && a.space !== "frame"
13821384
? {
13831385
...a,
13841386
position: {
13851387
x: Math.max(0, 100 - a.position.x - a.size.width),
13861388
y: Math.max(0, 100 - a.position.y - a.size.height),
13871389
},
1390+
...(a.blurData?.freehandPoints
1391+
? {
1392+
blurData: {
1393+
...a.blurData,
1394+
freehandPoints: a.blurData.freehandPoints.map((p) => ({
1395+
x: Math.max(0, 100 - p.x),
1396+
y: Math.max(0, 100 - p.y),
1397+
})),
1398+
},
1399+
}
1400+
: {}),
13881401
}
13891402
: a,
13901403
)
1391-
: ranged.annotations,
1404+
: doc.annotations,
13921405
};
1406+
const next = setClipSourceRange(turned, clipId, sourceStartSec, sourceEndSec, cropRegion);
13931407
await saveDocument(next, { history: true });
13941408
},
13951409
[saveDocument],

0 commit comments

Comments
 (0)