Skip to content

Write HTML. Render video. Built for agents.

fix(studio): a crop drag that ends where it started saves nothing (#4745)

Miguel Ángel67327a8b25a4
diff --git a/packages/studio/src/components/editor/DomEditCropHandles.test.tsx b/packages/studio/src/components/editor/DomEditCropHandles.test.tsx
index 8ea374f9e3..33ec296a83 100644
--- a/packages/studio/src/components/editor/DomEditCropHandles.test.tsx
+++ b/packages/studio/src/components/editor/DomEditCropHandles.test.tsx
@@ -126,6 +126,69 @@ describe("DomEditCropHandles clip lift/restore", () => {
expect(a.style.getPropertyValue("clip-path")).toBe("inset(10px 30px 10px 10px)");
});
+ it.each([
+ { name: "a crop edge", clip: "inset(10px)", handle: "Crop right", dx: -20, dy: 0 },
+ {
+ name: "the reposition handle on fractional insets",
+ clip: "inset(20px 0.42px 40px 0.03px)",
+ handle: "Reposition crop",
+ dx: 0,
+ dy: 10,
+ },
+ ])("commits nothing when $name is dragged back to where it started", (c) => {
+ const onStyleCommit = vi.fn();
+ render(makeEl("a", c.clip), onStyleCommit);
+ const handle = document.querySelector<HTMLButtonElement>(`[aria-label="${c.handle}"]`)!;
+ for (const [type, d] of [
+ ["pointerdown", 0],
+ ["pointermove", 1],
+ ["pointermove", 0],
+ ["pointerup", 0],
+ ] as const) {
+ const at = { clientX: 100 + d * c.dx, clientY: 50 + d * c.dy };
+ act(() =>
+ handle.dispatchEvent(new PointerEvent(type, { bubbles: true, pointerId: 3, ...at })),
+ );
+ }
+ expect(onStyleCommit).not.toHaveBeenCalled();
+ });
+
+ it("commits where the pointer was released, even before its last move renders", () => {
+ const onStyleCommit = vi.fn();
+ render(makeEl("a", "inset(10px)"), onStyleCommit);
+ const handle = document.querySelector<HTMLButtonElement>('[aria-label="Crop right"]')!;
+ act(() => {
+ for (const [type, clientX] of [
+ ["pointerdown", 100],
+ ["pointermove", 90],
+ ["pointermove", 80],
+ ["pointerup", 80],
+ ] as const) {
+ handle.dispatchEvent(new PointerEvent(type, { bubbles: true, pointerId: 4, clientX }));
+ }
+ });
+ expect(onStyleCommit).toHaveBeenCalledWith("clip-path", "inset(10px 30px 10px 10px)");
+ });
+
+ it("keeps a first crop on deselect when the release beats the last render", async () => {
+ const a = makeEl("a", "");
+ const { root } = render(a, (property, value) => void a.style.setProperty(property, value));
+ const handle = document.querySelector<HTMLButtonElement>('[aria-label="Crop right"]')!;
+ act(() => {
+ for (const [type, clientX] of [
+ ["pointerdown", 100],
+ ["pointermove", 90],
+ ["pointermove", 80],
+ ["pointerup", 80],
+ ] as const) {
+ handle.dispatchEvent(new PointerEvent(type, { bubbles: true, pointerId: 5, clientX }));
+ }
+ });
+ await act(async () => undefined);
+ act(() => root.unmount());
+ expect(a.style.getPropertyValue("clip-path")).toBe("inset(0px 20px 0px 0px)");
+ });
+
it("re-lifts when the crop commit rejects", async () => {
const a = makeEl("a", "inset(10px)");
const onStyleCommit = vi.fn((property: string, value: string) => {
diff --git a/packages/studio/src/components/editor/DomEditCropHandles.tsx b/packages/studio/src/components/editor/DomEditCropHandles.tsx
index 9dae0f82b9..b223d7a900 100644
--- a/packages/studio/src/components/editor/DomEditCropHandles.tsx
+++ b/packages/studio/src/components/editor/DomEditCropHandles.tsx
@@ -18,7 +18,7 @@ interface CropGestureState {
startX: number;
startY: number;
startInsets: ClipPathInsetSides;
- didMove: boolean;
+ insets: ClipPathInsetSides;
/** Element frame captured at gesture start: pointer deltas rotate into it. */
angleDeg: number;
scaleX: number;
@@ -186,7 +186,7 @@ export function DomEditCropHandles({
startX: event.clientX,
startY: event.clientY,
startInsets: state.insets,
- didMove: false,
+ insets: state.insets,
angleDeg: frame.angleDeg,
scaleX: frame.scaleX,
scaleY: frame.scaleY,
@@ -217,7 +217,7 @@ export function DomEditCropHandles({
gesture.edge === "move"
? resolveCropInsetFromMoveDrag(drag)
: resolveCropInsetFromEdgeDrag({ ...drag, edge: gesture.edge, width, height });
- gesture.didMove = true;
+ gesture.insets = nextInsets;
setState((prev) => ({ ...prev, insets: nextInsets }));
};
@@ -234,7 +234,6 @@ export function DomEditCropHandles({
const finishCropGesture = (event: ReactPointerEvent<HTMLElement>) => {
const gesture = endCropGesture(event);
if (!gesture) return;
- if (!gesture.didMove) return;
// Commit to the file. The commit path re-applies the value to the live
// element synchronously, so re-lift in the same turn to keep showing the full
// content + dim while selected. Re-lift again on rejection so a failed commit
@@ -243,12 +242,10 @@ export function DomEditCropHandles({
const reLift = () => {
if (liftedRef.current) el.style.setProperty("clip-path", "none");
};
- const committedValue = buildInsetClipPathSides(state.insets, state.radius);
- const cropped =
- state.insets.top > 0 ||
- state.insets.right > 0 ||
- state.insets.bottom > 0 ||
- state.insets.left > 0;
+ const { insets } = gesture;
+ const committedValue = buildInsetClipPathSides(insets, state.radius);
+ if (committedValue === buildInsetClipPathSides(gesture.startInsets, state.radius)) return;
+ const cropped = insets.top > 0 || insets.right > 0 || insets.bottom > 0 || insets.left > 0;
const commit = onStyleCommit?.("clip-path", committedValue);
// handleDomStyleCommit applies the persisted value to the live element
// synchronously before its first await. Restore the crop-mode lift in this