Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio): an escape that cancels a timeline band stops there (#4700)
Miguel Ángelf7207247de18
diff --git a/packages/studio/src/player/components/Timeline.bandEscape.test.tsx b/packages/studio/src/player/components/Timeline.bandEscape.test.tsxnew file mode 100644index 0000000000..812e658f8d--- /dev/null+++ b/packages/studio/src/player/components/Timeline.bandEscape.test.tsx@@ -0,0 +1,75 @@+// @vitest-environment happy-dom+import { act } from "react";+import type { Root } from "react-dom/client";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { Timeline } from "./Timeline";+import { usePlayerStore } from "../store/playerStore";+import { installReactActEnvironment, mountReactHarness } from "../../hooks/domSelectionTestHarness";++installReactActEnvironment();+for (const [prop, px] of [+ ["clientWidth", 1000],+ ["clientHeight", 400],+ ["offsetWidth", 1000],+ ["offsetHeight", 400],+] as const) {+ vi.spyOn(HTMLElement.prototype, prop, "get").mockReturnValue(px);+}++const pointer = (type: string, clientX: number, clientY: number) =>+ new PointerEvent(type, { bubbles: true, button: 0, pointerId: 1, clientX, clientY });+const band = () => document.querySelector('[style*="dashed"]');++let root: Root;+let viewport: HTMLElement;+let hostEscapes: KeyboardEvent[];+const hostListener = (e: KeyboardEvent) => {+ if (e.key === "Escape") hostEscapes.push(e);+};++beforeEach(async () => {+ usePlayerStore.setState({+ duration: 20,+ timelineReady: true,+ elements: [{ id: "intro", key: "intro", tag: "div", start: 0, duration: 1, track: 0 }],+ });+ usePlayerStore.getState().setSelectedElementId("intro");+ root = mountReactHarness(<Timeline />);+ await act(async () => new Promise((r) => setTimeout(r, 50)));+ viewport = document.querySelector<HTMLElement>("[data-timeline-scroll-viewport]")!;+ hostEscapes = [];+ document.addEventListener("keydown", hostListener);+});++afterEach(() => {+ document.removeEventListener("keydown", hostListener);+ act(() => root.unmount());+ usePlayerStore.setState({ elements: [], timelineReady: false, selectedElementId: null });+ document.body.innerHTML = "";+});++const escape = () => {+ const event = new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true });+ act(() => void document.body.dispatchEvent(event));+ return event;+};++it("an escape that cancels a marquee band stops there and never reaches the host", () => {+ act(() => void viewport.dispatchEvent(pointer("pointerdown", 800, 200)));+ act(() => void viewport.dispatchEvent(pointer("pointermove", 860, 260)));+ expect(band()).not.toBeNull();++ const event = escape();++ expect(hostEscapes).toHaveLength(0);+ expect(event.defaultPrevented).toBe(true);+ expect(band()).toBeNull();+ expect(usePlayerStore.getState().selectedElementId).toBe("intro");+});++it("an escape with no band in flight still reaches the host", () => {+ const event = escape();++ expect(event.defaultPrevented).toBe(false);+ expect(hostEscapes).toHaveLength(1);+});diff --git a/packages/studio/src/player/components/useTimelineRangeSelection.ts b/packages/studio/src/player/components/useTimelineRangeSelection.tsindex 66bc59fb66..3213548a99 100644--- a/packages/studio/src/player/components/useTimelineRangeSelection.ts+++ b/packages/studio/src/player/components/useTimelineRangeSelection.ts@@ -546,29 +546,30 @@ export function useTimelineRangeSelection({[cancelActiveGesture, isGestureSessionCurrent],);- // Escape: cancel an in-flight marquee (restores the pre-drag selection);- // otherwise clear any lingering multi-selection.+ // Escape cancels an in-flight band, captured so a host's own Escape never also fires;+ // otherwise it clears any lingering multi-selection.useEffect(() => {+ const onBandEscape = (e: KeyboardEvent) => {+ if (e.key !== "Escape" || !(marqueeRef.current || isRangeSelecting.current)) return;+ e.preventDefault();+ e.stopPropagation();+ cancelActiveGesture(true, true);+ };const onKeyDown = (e: KeyboardEvent) => {if (e.key !== "Escape") return;const store = usePlayerStore.getState();- const marquee = marqueeRef.current;- if (marquee) {- cancelActiveGesture(true, true);- return;- }- if (isRangeSelecting.current || isDragging.current) {+ if (isDragging.current) {cancelActiveGesture(true, true);return;}- // Escape with no marquee clears the whole selection — primary AND set.- // setSelectedElementId(null) also collapses the multi-select set.if (store.selectedElementId || store.selectedElementIds.size > 0) {store.setSelectedElementId(null);}};- window.addEventListener("keydown", onKeyDown);- return () => window.removeEventListener("keydown", onKeyDown);+ const listeners = new AbortController();+ window.addEventListener("keydown", onBandEscape, { capture: true, signal: listeners.signal });+ window.addEventListener("keydown", onKeyDown, { signal: listeners.signal });+ return () => listeners.abort();}, [cancelActiveGesture, isDragging]);const previousSessionEpochRef = useRef(sessionEpoch);