Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
feat(studio): a host can turn off body drags and keep the resize, rotate and crop handles (#4732)
Miguel Ángelac754b25d0c0
diff --git a/packages/studio/src/components/editor/ConnectedDomEditOverlay.test.tsx b/packages/studio/src/components/editor/ConnectedDomEditOverlay.test.tsxindex cc0888d3cb..66cd3c272f 100644--- a/packages/studio/src/components/editor/ConnectedDomEditOverlay.test.tsx+++ b/packages/studio/src/components/editor/ConnectedDomEditOverlay.test.tsx@@ -149,11 +149,13 @@ describe("ConnectedDomEditOverlay", () => {showHoverSelection={false}shouldShowSelectedDomBounds={true}canvasInput="host"+ allowBodyDrag={false}onTextEditingChange={onTextEditingChange}/>,);expect(props).toMatchObject({canvasInput: "host",+ allowBodyDrag: false,onTextEditingChange,hoverSelection: null,selection: ctx.selection.domEditSelection,diff --git a/packages/studio/src/components/editor/ConnectedDomEditOverlay.tsx b/packages/studio/src/components/editor/ConnectedDomEditOverlay.tsxindex f677c46c2c..6f2e2cad3b 100644--- a/packages/studio/src/components/editor/ConnectedDomEditOverlay.tsx+++ b/packages/studio/src/components/editor/ConnectedDomEditOverlay.tsx@@ -13,7 +13,7 @@ import { runZLaneGesture } from "../nle/zLaneGesture";type HostInputProps = Pick<ComponentProps<typeof DomEditOverlay>,- "canvasInput" | "onSelectionBoxClick"+ "canvasInput" | "onSelectionBoxClick" | "allowBodyDrag">;export interface ConnectedDomEditOverlayProps extends HostInputProps {@@ -121,6 +121,7 @@ export function ConnectedDomEditOverlay({shouldShowSelectedDomBounds,isGestureRecording,canvasInput,+ allowBodyDrag,onTextEditingChange,onSelectionBoxClick,}: ConnectedDomEditOverlayProps) {@@ -154,6 +155,7 @@ export function ConnectedDomEditOverlay({groupSelections={shouldShowSelectedDomBounds ? domEditGroupSelections : []}allowCanvasMovement={!isGestureRecording}canvasInput={canvasInput}+ allowBodyDrag={allowBodyDrag}onTextEditingChange={onTextEditingChange}onSelectionBoxClick={onSelectionBoxClick}onCanvasMouseDown={handlePreviewCanvasMouseDown}diff --git a/packages/studio/src/components/editor/DomEditOverlay.test.ts b/packages/studio/src/components/editor/DomEditOverlay.test.tsindex 2aef34ef98..8a79e143b2 100644--- a/packages/studio/src/components/editor/DomEditOverlay.test.ts+++ b/packages/studio/src/components/editor/DomEditOverlay.test.ts@@ -485,6 +485,45 @@ describe("DomEditOverlay", () => {host.remove();});+ it.each([+ { canvasInput: "overlay" as const, drags: true },+ { canvasInput: "host" as const, drags: false },+ ])("with body drag off, a $canvasInput canvas drags the body: $drags", async (c) => {+ const restoreRect = stubViewportRect();+ const host = document.createElement("div");+ document.body.append(host);+ const root = createRoot(host);+ const selection = makeDomEditSelection();+ const iframeRef = { current: document.createElement("iframe") as HTMLIFrameElement | null };+ const originalPointerCapture = HTMLDivElement.prototype.setPointerCapture;+ HTMLDivElement.prototype.setPointerCapture = () => {};+ gestureSpies.startGesture.mockClear();++ act(() => {+ root.render(+ React.createElement(DomEditOverlay, {+ ...createOverlayProps({ iframeRef, selection, hoverSelection: null }),+ allowBodyDrag: false,+ canvasInput: c.canvasInput,+ }),+ );+ });+ await flushOverlayRaf();+ const selectionBox = host.querySelector(+ '[data-dom-edit-selection-box="true"]',+ ) as HTMLDivElement;+ dispatchOverlayPointerDown(selectionBox);++ expect(gestureSpies.startGesture.mock.calls.some((call) => call[0] === "drag")).toBe(c.drags);++ act(() => {+ root.unmount();+ });+ HTMLDivElement.prototype.setPointerCapture = originalPointerCapture;+ restoreRect();+ host.remove();+ });+it("passes the tracked hover selection when clicking the existing selection box", async () => {const restoreRect = stubViewportRect();diff --git a/packages/studio/src/components/editor/DomEditOverlay.tsx b/packages/studio/src/components/editor/DomEditOverlay.tsxindex f3691152b1..5940364d81 100644--- a/packages/studio/src/components/editor/DomEditOverlay.tsx+++ b/packages/studio/src/components/editor/DomEditOverlay.tsx@@ -54,7 +54,8 @@ export interface DomEditOverlayProps {groupSelections?: DomEditSelection[];hoverSelection: DomEditSelection | null;allowCanvasMovement?: boolean;- /** "host": no hover, marquee or box re-select; Enter with nothing focused still opens text. */+ allowBodyDrag?: boolean;+ /** "host": no hover, marquee, box re-select, or body drag if allowBodyDrag is false; Enter still opens text. */canvasInput?: "overlay" | "host";onTextEditingChange?: (editing: boolean) => void;/** A click on a single selection's box, in either mode; the event may be the pointerup. */@@ -128,6 +129,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({groupSelections = [],hoverSelection,allowCanvasMovement = true,+ allowBodyDrag = true,canvasInput = "overlay",onTextEditingChange,onSelectionBoxClick,@@ -148,6 +150,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({}: DomEditOverlayProps) {const readOnly = usePreviewReadOnly();const hostInput = canvasInput === "host";+ const bodyDrag = allowBodyDrag || !hostInput;const overlayRef = useRef<HTMLDivElement | null>(null);const boxRef = useRef<HTMLDivElement | null>(null);const onMarqueeSelectRef = useRef(onMarqueeSelect);@@ -503,6 +506,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({groupOverlayItems={groupOverlayItems}groupBounds={groupBounds}allowCanvasMovement={allowCanvasMovement}+ allowBodyDrag={bodyDrag}groupCanMove={groupCanMove}gestures={gestures}onBoxMouseDown={suppressBoxMouseDown}@@ -515,6 +519,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({selection={selection}overlayRect={overlayRect}allowCanvasMovement={allowCanvasMovement}+ allowBodyDrag={bodyDrag}cropOutlineInsetPx={cropOutlineInsetPx ?? undefined}boxRef={boxRef}boxChromeClass={boxChromeClass}diff --git a/packages/studio/src/components/editor/DomEditSelectionChrome.test.tsx b/packages/studio/src/components/editor/DomEditSelectionChrome.test.tsxindex 801fb585ca..5047b3508f 100644--- a/packages/studio/src/components/editor/DomEditSelectionChrome.test.tsx+++ b/packages/studio/src/components/editor/DomEditSelectionChrome.test.tsx@@ -4,7 +4,7 @@ import React, { act, createRef } from "react";import { createRoot } from "react-dom/client";import { describe, expect, it, vi } from "vitest";import type { DomEditSelection } from "./domEditing";-import { DomEditSelectionChrome } from "./DomEditSelectionChrome";+import { DomEditGroupChrome, DomEditSelectionChrome } from "./DomEditSelectionChrome";(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;@@ -48,6 +48,7 @@ describe("DomEditSelectionChrome crop composition", () => {selection={selection}overlayRect={{ left: 44, top: 52, width: 220, height: 48, editScaleX: 1, editScaleY: 1 }}allowCanvasMovement={false}+ allowBodyDragboxRef={createRef()}boxChromeClass="border border-studio-accent/80"boxClipPath={undefined}@@ -103,6 +104,7 @@ describe("DomEditSelectionChrome crop composition", () => {angle: 30,}}allowCanvasMovement={true}+ allowBodyDragboxRef={createRef()}boxChromeClass=""boxClipPath={undefined}@@ -163,6 +165,7 @@ describe("DomEditSelectionChrome while editing text", () => {selection={selection}overlayRect={{ left: 10, top: 20, width: 200, height: 60, editScaleX: 1, editScaleY: 1 }}allowCanvasMovement={true}+ allowBodyDragboxRef={createRef()}boxChromeClass="border border-studio-accent/80"boxClipPath={undefined}@@ -214,3 +217,113 @@ describe("DomEditSelectionChrome while editing text", () => {unmount();});});++describe("DomEditSelectionChrome with body drag off", () => {+ const rect = { left: 10, top: 10, width: 200, height: 100, editScaleX: 1, editScaleY: 1 };+ const gestureSpies = () => ({+ startGesture: vi.fn(),+ startGroupDrag: vi.fn(),+ startBlockedMove: vi.fn(),+ });+ const press = (el: Element) => {+ const event = new PointerEvent("pointerdown", {+ bubbles: true,+ cancelable: true,+ button: 0,+ pointerId: 1,+ });+ act(() => {+ el.dispatchEvent(event);+ });+ return event;+ };++ function renderChrome(allowBodyDrag: boolean) {+ const element = document.createElement("div");+ document.body.append(element);+ const { selection, host, root } = selectionFixture(element, "#box", true);+ const hostPress = vi.fn();+ host.addEventListener("pointerdown", hostPress);+ const gestures = gestureSpies();+ act(() => {+ root.render(+ <DomEditSelectionChrome+ selection={selection}+ overlayRect={rect}+ allowCanvasMovement+ allowBodyDrag={allowBodyDrag}+ boxRef={createRef()}+ boxChromeClass=""+ boxClipPath={undefined}+ selectionKey="box"+ groupSelectionCount={0}+ gestures={gestures as never}+ onBoxMouseDown={vi.fn()}+ onBoxClick={vi.fn()}+ />,+ );+ });+ const box = host.querySelector<HTMLElement>('[data-dom-edit-selection-box="true"]')!;+ return { host, box, gestures, hostPress, cleanup: () => act(() => root.unmount()) };+ }++ it("leaves a body press untouched for the host: no drag, no capture, no cursor", () => {+ const { box, gestures, hostPress, cleanup } = renderChrome(false);+ const event = press(box);+ expect(hostPress).toHaveBeenCalledTimes(1);+ expect(event.defaultPrevented).toBe(false);+ expect(box.hasPointerCapture(1)).toBe(false);+ expect(gestures.startGesture).not.toHaveBeenCalled();+ expect(gestures.startBlockedMove).not.toHaveBeenCalled();+ expect(box.style.cursor).toBe("");+ cleanup();+ });++ it("keeps the resize and rotate handles working", () => {+ const { host, gestures, cleanup } = renderChrome(false);+ const corner = host.querySelector<HTMLElement>('[style*="nwse-resize"]')!;+ press(corner);+ expect(gestures.startGesture).toHaveBeenCalledWith("resize", expect.anything(), {+ resizeHandle: "nw",+ });+ press(host.querySelector('[aria-label="Rotate selection"]')!);+ expect(gestures.startGesture).toHaveBeenCalledWith("rotate", expect.anything());+ cleanup();+ });++ it("still drags the body by default", () => {+ const { box, gestures, cleanup } = renderChrome(true);+ press(box);+ expect(gestures.startGesture).toHaveBeenCalledWith("drag", expect.anything());+ expect(box.style.cursor).toBe("move");+ cleanup();+ });++ it("leaves a group body press untouched too", () => {+ const { host, root } = selectionFixture(document.createElement("div"), "#g", true);+ const hostPress = vi.fn();+ host.addEventListener("pointerdown", hostPress);+ const gestures = gestureSpies();+ act(() => {+ root.render(+ <DomEditGroupChrome+ groupOverlayItems={[]}+ groupBounds={rect}+ allowCanvasMovement+ allowBodyDrag={false}+ groupCanMove+ gestures={gestures as never}+ onBoxMouseDown={vi.fn()}+ onBoxClick={vi.fn()}+ />,+ );+ });+ const groupBox = host.querySelector<HTMLElement>('[data-dom-edit-selection-box="true"]')!;+ const event = press(groupBox);+ expect(groupBox.style.cursor).toBe("");+ expect(hostPress).toHaveBeenCalledTimes(1);+ expect(event.defaultPrevented).toBe(false);+ expect(gestures.startGroupDrag).not.toHaveBeenCalled();+ act(() => root.unmount());+ });+});diff --git a/packages/studio/src/components/editor/DomEditSelectionChrome.tsx b/packages/studio/src/components/editor/DomEditSelectionChrome.tsxindex ef900cbe2f..0527975716 100644--- a/packages/studio/src/components/editor/DomEditSelectionChrome.tsx+++ b/packages/studio/src/components/editor/DomEditSelectionChrome.tsx@@ -59,6 +59,7 @@ interface DomEditGroupChromeProps {groupOverlayItems: GroupOverlayItem[];groupBounds: OverlayRect;allowCanvasMovement: boolean;+ allowBodyDrag: boolean;groupCanMove: boolean;gestures: GestureHandlers;onBoxMouseDown: (e: React.MouseEvent) => void;@@ -71,6 +72,7 @@ export function DomEditGroupChrome({groupOverlayItems,groupBounds,allowCanvasMovement,+ allowBodyDrag,groupCanMove,gestures,onBoxMouseDown,@@ -100,10 +102,10 @@ export function DomEditGroupChrome({top: groupBounds.top,width: groupBounds.width,height: groupBounds.height,- cursor: canManipulate && groupCanMove ? "move" : "default",+ cursor: !allowBodyDrag ? undefined : canManipulate && groupCanMove ? "move" : "default",}}onPointerDown={(e) => {- if (!canManipulate || e.shiftKey) return;+ if (!canManipulate || e.shiftKey || !allowBodyDrag) return;gestures.startGroupDrag(e);}}onMouseDown={onBoxMouseDown}@@ -117,6 +119,7 @@ interface DomEditSelectionChromeProps {selection: DomEditSelection;overlayRect: OverlayRect;allowCanvasMovement: boolean;+ allowBodyDrag: boolean;cropOutlineInsetPx?: { top: number; right: number; bottom: number; left: number };boxRef: RefObject<HTMLDivElement | null>;boxChromeClass: string;@@ -145,6 +148,7 @@ export function DomEditSelectionChrome({selection,overlayRect,allowCanvasMovement,+ allowBodyDrag,cropOutlineInsetPx,boxRef,boxChromeClass,@@ -197,8 +201,11 @@ export function DomEditSelectionChrome({width: overlayRect.width,height: overlayRect.height,clipPath: boxClipPath,- cursor:- canManipulate && selection.capabilities.canApplyManualOffset ? "move" : "default",+ cursor: !allowBodyDrag+ ? undefined+ : canManipulate && selection.capabilities.canApplyManualOffset+ ? "move"+ : "default",}}onPointerDown={(e) => {// A second press opens the element's text for editing, and must be@@ -211,7 +218,7 @@ export function DomEditSelectionChrome({e.stopPropagation();return;}- if (!canManipulate || e.shiftKey) return;+ if (!allowBodyDrag || !canManipulate || e.shiftKey) return;if (selection.capabilities.canApplyManualOffset) {gestures.startGesture("drag", e);return;