Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio): a corner drag on a small element resizes it instead of starting a crop (#4730)
Miguel Ángel3e4ccd956700
diff --git a/packages/studio/src/components/editor/DomEditCropHandles.test.tsx b/packages/studio/src/components/editor/DomEditCropHandles.test.tsxindex a75fc39493..8ea374f9e3 100644--- a/packages/studio/src/components/editor/DomEditCropHandles.test.tsx+++ b/packages/studio/src/components/editor/DomEditCropHandles.test.tsx@@ -36,6 +36,7 @@ function makeEl(id: string, clip: string): HTMLElement {function render(el: HTMLElement,onStyleCommit: (property: string, value: string) => Promise<unknown> | void = () => undefined,+ rect: OverlayRect = overlayRect,): { root: Root; rerender: (next: HTMLElement) => void } {const host = document.createElement("div");document.body.append(host);@@ -45,7 +46,7 @@ function render(root.render(<DomEditCropHandlesselection={selectionFor(target)}- overlayRect={overlayRect}+ overlayRect={rect}onStyleCommit={onStyleCommit}/>,);@@ -155,3 +156,88 @@ describe("DomEditCropHandles clip lift/restore", () => {act(() => root.unmount());});});++describe("DomEditCropHandles leaves the corner resize dots free", () => {+ type Box = { left: number; top: number; width: number; height: number };+ const HIT = 16;+ const cornerSquares = (r: Box): Box[] =>+ [+ [r.left, r.top],+ [r.left + r.width, r.top],+ [r.left, r.top + r.height],+ [r.left + r.width, r.top + r.height],+ ].map(([x, y]) => ({ left: x - HIT / 2, top: y - HIT / 2, width: HIT, height: HIT }));+ const overlaps = (a: Box, b: Box) =>+ a.left < b.left + b.width &&+ b.left < a.left + a.width &&+ a.top < b.top + b.height &&+ b.top < a.top + a.height;+ const handles = () =>+ [...document.querySelectorAll<HTMLElement>("[data-dom-edit-crop-handle]")].map((el) => ({+ label: el.getAttribute("aria-label"),+ box: {+ left: parseFloat(el.style.left),+ top: parseFloat(el.style.top),+ width: parseFloat(el.style.width),+ height: parseFloat(el.style.height),+ },+ }));+ const rectOf = (width: number, height: number): OverlayRect => ({+ left: 0,+ top: 0,+ width,+ height,+ editScaleX: 1,+ editScaleY: 1,+ });++ it.each([+ {+ name: "29x14",+ size: [29, 14],+ clip: "",+ cropped: { left: 0, top: 0, width: 29, height: 14 },+ },+ {+ name: "40x40",+ size: [40, 40],+ clip: "",+ cropped: { left: 0, top: 0, width: 40, height: 40 },+ },+ {+ name: "29x14",+ size: [29, 14],+ clip: "inset(2px)",+ cropped: { left: 2, top: 2, width: 25, height: 10 },+ },+ {+ name: "200x100",+ size: [200, 100],+ clip: "inset(10px)",+ cropped: { left: 10, top: 10, width: 180, height: 80 },+ },+ ])("no crop handle covers a corner of a $name box clipped '$clip'", (c) => {+ render(makeEl("a", c.clip), undefined, rectOf(c.size[0], c.size[1]));+ const found = handles();+ expect(found.length).toBeGreaterThan(0);+ for (const handle of found) {+ for (const corner of cornerSquares(c.cropped)) {+ expect(overlaps(handle.box, corner), `${handle.label} covers a corner`).toBe(false);+ }+ }+ });++ it("keeps every handle where the edges have room, and drops only those with none", () => {+ render(makeEl("a", "inset(10px)"), undefined, rectOf(200, 100));+ expect(handles().map((h) => h.label)).toEqual([+ "Reposition crop",+ "Crop top",+ "Crop right",+ "Crop bottom",+ "Crop left",+ ]);+ document.body.innerHTML = "";+ render(makeEl("b", ""), undefined, rectOf(29, 14));+ expect(handles().map((h) => h.label)).toEqual(["Crop top", "Crop bottom"]);+ });+});diff --git a/packages/studio/src/components/editor/DomEditCropHandles.tsx b/packages/studio/src/components/editor/DomEditCropHandles.tsxindex 62ab287a70..9dae0f82b9 100644--- a/packages/studio/src/components/editor/DomEditCropHandles.tsx+++ b/packages/studio/src/components/editor/DomEditCropHandles.tsx@@ -1,6 +1,6 @@import { useEffect, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";import type { DomEditSelection } from "./domEditing";-import type { OverlayRect } from "./domEditOverlayGeometry";+import { type OverlayRect, RESIZE_HANDLE_HIT_PX } from "./domEditOverlayGeometry";import {type CropEdge,cropRectFromInsets,@@ -37,47 +37,53 @@ interface DomEditCropHandlesProps {// and centered inside the strip.const EDGE_HIT_THICKNESS = 12;const EDGE_HIT_LENGTH = 32;+const EDGE_PILL_LENGTH = 24;+const REPOSITION_HANDLE_PX = 22;-/** Place an edge handle's hit strip just OUTSIDE the given crop edge- * (translate pushes it fully past the boundary). Keeps the element body free- * for moving. Corners stay free for the selection's own resize handles. */-function edgeHandlePlacement(- edge: CropEdge,- rect: { left: number; top: number; width: number; height: number },-) {- const cx = rect.left + rect.width / 2;- const cy = rect.top + rect.height / 2;- if (edge === "top") {- return { left: cx, top: rect.top, transform: "translate(-50%, -100%)" };- }- if (edge === "bottom") {- return { left: cx, top: rect.top + rect.height, transform: "translate(-50%, 0)" };- }- if (edge === "left") {- return { left: rect.left, top: cy, transform: "translate(-100%, -50%)" };- }- return { left: rect.left + rect.width, top: cy, transform: "translate(0, -50%)" };-}+type Rect = { left: number; top: number; width: number; height: number };-const EDGES: CropEdge[] = ["top", "right", "bottom", "left"];--/** Hit-strip + pill dimensions for an edge handle, keyed on its orientation. */-function edgeHandleMetrics(vertical: boolean): {- hitWidth: number;- hitHeight: number;- cursor: string;- pillWidth: number;- pillHeight: number;-} {+/** An edge handle's hit strip, just OUTSIDE the crop edge so the body stays free+ * for moving, and short of the corner squares the resize dots own, with its+ * cursor and pill. Null when the edge is too short to leave any room. */+function edgeHandleLayout(edge: CropEdge, rect: Rect) {+ const vertical = edge === "left" || edge === "right";+ const length = Math.min(+ EDGE_HIT_LENGTH,+ (vertical ? rect.height : rect.width) - RESIZE_HANDLE_HIT_PX,+ );+ if (length <= 0) return null;+ const pill = Math.min(EDGE_PILL_LENGTH, length);+ if (vertical) {+ return {+ hit: {+ left: edge === "left" ? rect.left - EDGE_HIT_THICKNESS : rect.left + rect.width,+ top: rect.top + (rect.height - length) / 2,+ width: EDGE_HIT_THICKNESS,+ height: length,+ },+ cursor: "ew-resize",+ pill: { width: 4, height: pill },+ };+ }return {- hitWidth: vertical ? EDGE_HIT_THICKNESS : EDGE_HIT_LENGTH,- hitHeight: vertical ? EDGE_HIT_LENGTH : EDGE_HIT_THICKNESS,- cursor: vertical ? "ew-resize" : "ns-resize",- pillWidth: vertical ? 4 : 24,- pillHeight: vertical ? 24 : 4,+ hit: {+ left: rect.left + (rect.width - length) / 2,+ top: edge === "top" ? rect.top - EDGE_HIT_THICKNESS : rect.top + rect.height,+ width: length,+ height: EDGE_HIT_THICKNESS,+ },+ cursor: "ns-resize",+ pill: { width: pill, height: 4 },};}+/** The reposition handle's size: shrunk until its box clears the corner squares. */+function repositionHandleSize(rect: Rect): number {+ return Math.min(REPOSITION_HANDLE_PX, Math.max(rect.width, rect.height) - RESIZE_HANDLE_HIT_PX);+}++const EDGES: CropEdge[] = ["top", "right", "bottom", "left"];+/*** Always-on crop, integrated with the selection (no crop "mode"): while a* croppable element is selected its clip is lifted so the FULL content shows and@@ -167,6 +173,7 @@ export function DomEditCropHandles({frame.scaleX,frame.scaleY,);+ const repositionSize = repositionHandleSize(cropRect);const startCropGesture = (edge: CropEdge | "move", event: ReactPointerEvent<HTMLElement>) => {if (!onStyleCommit) return;@@ -214,13 +221,19 @@ export function DomEditCropHandles({setState((prev) => ({ ...prev, insets: nextInsets }));};- const finishCropGesture = (event: ReactPointerEvent<HTMLElement>) => {+ const endCropGesture = (event: ReactPointerEvent<HTMLElement>) => {const gesture = gestureRef.current;- if (!gesture || gesture.pointerId !== event.pointerId) return;+ if (!gesture || gesture.pointerId !== event.pointerId) return null;event.preventDefault();event.stopPropagation();gestureRef.current = null;setDragging(false);+ return gesture;+ };++ 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@@ -250,12 +263,8 @@ export function DomEditCropHandles({};const cancelCropGesture = (event: ReactPointerEvent<HTMLElement>) => {- const gesture = gestureRef.current;- if (!gesture || gesture.pointerId !== event.pointerId) return;- event.preventDefault();- event.stopPropagation();- gestureRef.current = null;- setDragging(false);+ const gesture = endCropGesture(event);+ if (!gesture) return;// Clip stays lifted; the dim follows the reset insets.setState((prev) => ({ ...prev, insets: gesture.startInsets }));};@@ -317,7 +326,7 @@ export function DomEditCropHandles({{/* Reposition handle — a center circle shown only once cropped. Drag it topan the crop window (which part of the element shows) without resizingthe crop. It's a small, discrete target, so a body drag still MOVES. */}- {hasCrop && (+ {hasCrop && repositionSize > 0 && (<buttontype="button"aria-label="Reposition crop"@@ -325,11 +334,10 @@ export function DomEditCropHandles({data-dom-edit-crop-handle="true"className="pointer-events-auto absolute rounded-full border-2 border-studio-accent bg-studio-accent/30 shadow-[0_0_0_1px_rgba(0,0,0,0.4)]"style={{- left: cropRect.left + cropRect.width / 2,- top: cropRect.top + cropRect.height / 2,- width: 22,- height: 22,- transform: "translate(-50%, -50%)",+ left: cropRect.left + (cropRect.width - repositionSize) / 2,+ top: cropRect.top + (cropRect.height - repositionSize) / 2,+ width: repositionSize,+ height: repositionSize,cursor: "move",touchAction: "none",}}@@ -340,16 +348,15 @@ export function DomEditCropHandles({/>)}{/* Edge handles — drag a side to crop it. Positioned just OUTSIDE the crop- edge (via edgeHandlePlacement) so they never overlap the element body:+ edge (via edgeHandleLayout) so they never overlap the element body:dragging the body always MOVES, only a handle crops. The pill ishover-revealed (or shown while dragging / once a crop exists) so theresting selection chrome stays uncluttered; the hit strip is alwayslive, and the title names the affordance. */}{EDGES.map((edge) => {- const vertical = edge === "left" || edge === "right";- const place = edgeHandlePlacement(edge, cropRect);+ const layout = edgeHandleLayout(edge, cropRect);+ if (!layout) return null;const revealed = dragging || hasCrop || hotEdge === edge;- const m = edgeHandleMetrics(vertical);return (<buttonkey={edge}@@ -359,12 +366,8 @@ export function DomEditCropHandles({data-dom-edit-crop-handle="true"className="pointer-events-auto absolute flex items-center justify-center border-0 bg-transparent p-0"style={{- left: place.left,- top: place.top,- width: m.hitWidth,- height: m.hitHeight,- transform: place.transform,- cursor: m.cursor,+ ...layout.hit,+ cursor: layout.cursor,touchAction: "none",}}onPointerEnter={() => setHotEdge(edge)}@@ -377,8 +380,7 @@ export function DomEditCropHandles({<spanclassName="pointer-events-none rounded-full bg-studio-accent/90 shadow-[0_0_0_1px_rgba(0,0,0,0.4)] transition-opacity duration-100"style={{- width: m.pillWidth,- height: m.pillHeight,+ ...layout.pill,opacity: revealed ? 1 : 0,}}/>diff --git a/packages/studio/src/components/editor/DomEditSelectionChrome.tsx b/packages/studio/src/components/editor/DomEditSelectionChrome.tsxindex 0527975716..83481e7583 100644--- a/packages/studio/src/components/editor/DomEditSelectionChrome.tsx+++ b/packages/studio/src/components/editor/DomEditSelectionChrome.tsx@@ -1,6 +1,10 @@import type { RefObject } from "react";import { type DomEditSelection } from "./domEditing";-import type { GroupOverlayItem, OverlayRect } from "./domEditOverlayGeometry";+import {+ type GroupOverlayItem,+ type OverlayRect,+ RESIZE_HANDLE_HIT_PX,+} from "./domEditOverlayGeometry";import type { ResizeHandle } from "./domEditOverlayGestures";import type { createDomEditOverlayGestureHandlers } from "./useDomEditOverlayGestures";import { DomEditCropHandles } from "./DomEditCropHandles";@@ -23,10 +27,6 @@ const RESIZE_HANDLE_DEFS: Array<{{ handle: "se", cursor: "nwse-resize", x: "right", y: "bottom" },];-// Visible dot is 9px; the pointer target is a 16px invisible square centered-// on the corner so click targets don't shrink with the smaller dot.-const RESIZE_HANDLE_HIT_PX = 16;-type CropInset = { top: number; right: number; bottom: number; left: number };const NO_CROP_INSET: CropInset = { top: 0, right: 0, bottom: 0, left: 0 };diff --git a/packages/studio/src/components/editor/domEditOverlayGeometry.ts b/packages/studio/src/components/editor/domEditOverlayGeometry.tsindex 2504d04187..5055dd4cfe 100644--- a/packages/studio/src/components/editor/domEditOverlayGeometry.ts+++ b/packages/studio/src/components/editor/domEditOverlayGeometry.ts@@ -9,6 +9,8 @@ import { hugRectForElement } from "./domEditOverlayCrop";import { composeElementTransform, type PlanarTransformOps } from "./domEditOverlayTransform";import { type OverlayMeasurePass, readThroughPass } from "./domEditOverlayMeasurePass";+export const RESIZE_HANDLE_HIT_PX = 16;+export interface OverlayRect {left: number;top: number;