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(edit-clip): move and resize the crop from the keyboard

EtienneLescotb7924a2b6b57
diff --git a/src/components/ai-edition/EditClipModal.test.tsx b/src/components/ai-edition/EditClipModal.test.tsx
index eb12ad059..ff907b97a 100644
--- a/src/components/ai-edition/EditClipModal.test.tsx
+++ b/src/components/ai-edition/EditClipModal.test.tsx
@@ -143,3 +143,36 @@ describe("EditClipModal trim duration readout (#558)", () => {
expect(screen.getByTestId("edit-clip-final-duration")).toHaveTextContent("1:20.0");
});
});
+
+describe("EditClipModal crop from the keyboard", () => {
+ it("moves the crop with the arrows and resizes it with Shift + the arrows", () => {
+ const onApply = vi.fn();
+ renderWithI18n(
+ <EditClipModal
+ open
+ onClose={vi.fn()}
+ clip={CLIP}
+ assetMeta={ASSET}
+ videoSources={[]}
+ onApply={onApply}
+ />,
+ );
+ // The editor shell seeks on the arrows from window: the crop must keep them.
+ const seek = vi.fn();
+ window.addEventListener("keydown", seek);
+ try {
+ const crop = screen.getByRole("slider", { name: "Crop" });
+ fireEvent.keyDown(crop, { key: "ArrowLeft", shiftKey: true });
+ fireEvent.keyDown(crop, { key: "ArrowRight" });
+ expect(seek).not.toHaveBeenCalled();
+ } finally {
+ window.removeEventListener("keydown", seek);
+ }
+ fireEvent.click(screen.getByRole("button", { name: "Apply" }));
+ expect(onApply).toHaveBeenCalledWith(
+ 20,
+ 105,
+ expect.objectContaining({ x: 0.01, y: 0, width: 0.99 }),
+ );
+ });
+});
diff --git a/src/components/ai-edition/Modals.tsx b/src/components/ai-edition/Modals.tsx
index 32de7312b..b8a99fe8a 100644
--- a/src/components/ai-edition/Modals.tsx
+++ b/src/components/ai-edition/Modals.tsx
@@ -1,5 +1,6 @@
import { AlertTriangle, Crop, FolderOpen, FolderPlus, Pencil, Plus, Trash2, X } from "lucide-react";
import {
+ type KeyboardEvent as ReactKeyboardEvent,
type ReactNode,
type PointerEvent as ReactPointerEvent,
useEffect,
@@ -563,6 +564,63 @@ const MIN_PCT = 4;
const clampPct = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
type ResizeEdges = { left?: boolean; right?: boolean; top?: boolean; bottom?: boolean };
+type CropPct = { x: number; y: number; w: number; h: number };
+
+/** `start` with its `edges` moved by (dxPct, dyPct), kept inside the frame. With a locked
+ * fraction-space ratio `fr`, the opposite dimension follows to keep width/height locked. */
+function resizeCropPct(
+ start: CropPct,
+ edges: ResizeEdges,
+ dxPct: number,
+ dyPct: number,
+ fr: number | null,
+): CropPct {
+ let { x, y, w, h } = start;
+ if (edges.left) {
+ const nx = clampPct(start.x + dxPct, 0, start.x + start.w - MIN_PCT);
+ w = start.w - (nx - start.x);
+ x = nx;
+ }
+ if (edges.right) {
+ w = clampPct(start.w + dxPct, MIN_PCT, 100 - start.x);
+ }
+ if (edges.top) {
+ const ny = clampPct(start.y + dyPct, 0, start.y + start.h - MIN_PCT);
+ h = start.h - (ny - start.y);
+ y = ny;
+ }
+ if (edges.bottom) {
+ h = clampPct(start.h + dyPct, MIN_PCT, 100 - start.y);
+ }
+ if (fr) {
+ // Locked ratio: the crop stays a fixed shape anchored at the corner the
+ // user isn't dragging. The dragged size is capped at the largest rect of
+ // this ratio that fits from that anchor — so it's simply sized to fit,
+ // never placed out of frame. (A crop can't leave the frame, so there is
+ // no out-of-bounds state to correct after the fact.)
+ const fixedLeft = !edges.left; // the x-edge that stays put
+ const fixedTop = !edges.top; // the y-edge that stays put
+ const anchorX = fixedLeft ? start.x : start.x + start.w;
+ const anchorY = fixedTop ? start.y : start.y + start.h;
+ const roomW = fixedLeft ? 100 - anchorX : anchorX;
+ const roomH = fixedTop ? 100 - anchorY : anchorY;
+ // Which axis the pointer drives; the other is derived from the ratio.
+ const drivenByHeight = (edges.top || edges.bottom) && !(edges.left || edges.right);
+ let nextW = Math.min(drivenByHeight ? h * fr : w, roomW, roomH * fr);
+ nextW = Math.max(MIN_PCT, nextW);
+ let nextH = nextW / fr;
+ if (nextH < MIN_PCT) {
+ nextH = MIN_PCT;
+ nextW = nextH * fr;
+ }
+ w = nextW;
+ h = nextH;
+ x = fixedLeft ? anchorX : anchorX - w;
+ y = fixedTop ? anchorY : anchorY - h;
+ }
+ return { x, y, w, h };
+}
+
const CROP_CORNERS = ["nw", "ne", "sw", "se"] as const;
const CROP_EDGES = ["n", "s", "w", "e"] as const;
@@ -777,6 +835,33 @@ export function EditClipModal({
window.addEventListener("pointerup", up);
};
+ const setCrop = (next: CropPct) => {
+ setCropXPct(next.x);
+ setCropYPct(next.y);
+ setCropWPct(next.w);
+ setCropHPct(next.h);
+ };
+
+ // The keyboard's way to the same two gestures: the arrows move the crop, Shift + the
+ // arrows resize it from its bottom-right corner, a locked ratio held as the handles hold it.
+ const onCropKeyDown = (e: ReactKeyboardEvent) => {
+ const dx = e.key === "ArrowLeft" ? -1 : e.key === "ArrowRight" ? 1 : 0;
+ const dy = e.key === "ArrowUp" ? -1 : e.key === "ArrowDown" ? 1 : 0;
+ if (dx === 0 && dy === 0) return;
+ // The editor shell seeks on the arrows, from WINDOW: keep them here.
+ e.preventDefault();
+ e.nativeEvent.stopPropagation();
+ setCropTouched(true);
+ const start = { x: cropXPct, y: cropYPct, w: cropWPct, h: cropHPct };
+ if (e.shiftKey) {
+ const edges = { right: dx !== 0, bottom: dy !== 0 };
+ setCrop(resizeCropPct(start, edges, dx, dy, lockedFractionRatio));
+ return;
+ }
+ setCropXPct(clampPct(start.x + dx, 0, 100 - start.w));
+ setCropYPct(clampPct(start.y + dy, 0, 100 - start.h));
+ };
+
// Drag one of the 8 edge/corner handles to resize. When a fixed ratio is
// active, the opposite dimension follows to keep width/height locked.
const startCropResize = (edges: ResizeEdges) => (e: ReactPointerEvent) => {
@@ -794,53 +879,7 @@ export function EditClipModal({
const move = (ev: PointerEvent) => {
const dxPct = ((ev.clientX - startX) / r.width) * 100;
const dyPct = ((ev.clientY - startY) / r.height) * 100;
- let { x, y, w, h } = start;
- if (edges.left) {
- const nx = clampPct(start.x + dxPct, 0, start.x + start.w - MIN_PCT);
- w = start.w - (nx - start.x);
- x = nx;
- }
- if (edges.right) {
- w = clampPct(start.w + dxPct, MIN_PCT, 100 - start.x);
- }
- if (edges.top) {
- const ny = clampPct(start.y + dyPct, 0, start.y + start.h - MIN_PCT);
- h = start.h - (ny - start.y);
- y = ny;
- }
- if (edges.bottom) {
- h = clampPct(start.h + dyPct, MIN_PCT, 100 - start.y);
- }
- if (fr) {
- // Locked ratio: the crop stays a fixed shape anchored at the corner the
- // user isn't dragging. The dragged size is capped at the largest rect of
- // this ratio that fits from that anchor — so it's simply sized to fit,
- // never placed out of frame. (A crop can't leave the frame, so there is
- // no out-of-bounds state to correct after the fact.)
- const fixedLeft = !edges.left; // the x-edge that stays put
- const fixedTop = !edges.top; // the y-edge that stays put
- const anchorX = fixedLeft ? start.x : start.x + start.w;
- const anchorY = fixedTop ? start.y : start.y + start.h;
- const roomW = fixedLeft ? 100 - anchorX : anchorX;
- const roomH = fixedTop ? 100 - anchorY : anchorY;
- // Which axis the pointer drives; the other is derived from the ratio.
- const drivenByHeight = (edges.top || edges.bottom) && !(edges.left || edges.right);
- let nextW = Math.min(drivenByHeight ? h * fr : w, roomW, roomH * fr);
- nextW = Math.max(MIN_PCT, nextW);
- let nextH = nextW / fr;
- if (nextH < MIN_PCT) {
- nextH = MIN_PCT;
- nextW = nextH * fr;
- }
- w = nextW;
- h = nextH;
- x = fixedLeft ? anchorX : anchorX - w;
- y = fixedTop ? anchorY : anchorY - h;
- }
- setCropXPct(x);
- setCropYPct(y);
- setCropWPct(w);
- setCropHPct(h);
+ setCrop(resizeCropPct(start, edges, dxPct, dyPct, fr));
};
const up = () => {
window.removeEventListener("pointermove", move);
@@ -901,6 +940,16 @@ export function EditClipModal({
/>
) : null}
<div
+ className={styles.cropRegion}
+ role="slider"
+ aria-label={ts("crop.title")}
+ aria-valuemin={0}
+ aria-valuemax={100}
+ aria-valuenow={Math.round(cropWPct)}
+ aria-valuetext={`${Math.round(cropXPct)}%, ${Math.round(cropYPct)}%, ${Math.round(cropWPct)}% × ${Math.round(cropHPct)}%`}
+ aria-keyshortcuts="Shift+ArrowLeft Shift+ArrowRight Shift+ArrowUp Shift+ArrowDown"
+ tabIndex={0}
+ onKeyDown={onCropKeyDown}
style={{
position: "absolute",
left: `${cropXPct}%`,
diff --git a/src/components/ai-edition/NewEditorShell.module.css b/src/components/ai-edition/NewEditorShell.module.css
index 80f0d43de..f61bf76fe 100644
--- a/src/components/ai-edition/NewEditorShell.module.css
+++ b/src/components/ai-edition/NewEditorShell.module.css
@@ -1228,6 +1228,8 @@ textarea.control {
.framingHandle[data-corner="ne"]::before { top: 8px; right: 8px; border-width: var(--bracket-w, 3.5px) var(--bracket-w, 3.5px) 0 0; border-top-right-radius: 5px; }
.framingHandle[data-corner="sw"]::before { bottom: 8px; left: 8px; border-width: 0 0 var(--bracket-w, 3.5px) var(--bracket-w, 3.5px); border-bottom-left-radius: 5px; }
.framingHandle[data-corner="se"]::before { bottom: 8px; right: 8px; border-width: 0 var(--bracket-w, 3.5px) var(--bracket-w, 3.5px) 0; border-bottom-right-radius: 5px; }
+/* Edit clip's crop region: focusable, since the arrows move it and Shift + the arrows resize it. */
+.cropRegion:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
/* A crop's sides: a short thick bar on the edge, in the brackets' white, and a grab area around it. */
.cropEdge {
position: absolute;