Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
refactor(studio): extract hotkey dispatch out of useAppHotkeys (#4079)
Miguel Ángel700b94834de9
diff --git a/.fallowrc.jsonc b/.fallowrc.jsoncindex bb5010e282..5f97cf6393 100644--- a/.fallowrc.jsonc+++ b/.fallowrc.jsonc@@ -899,6 +899,10 @@"packages/studio/src/components/ui/Tooltip.tsx","packages/studio/src/components/ui/useDialogBehavior.ts","packages/studio/src/hooks/useAppHotkeys.ts",+ // dispatchModifierKey/dispatchPlainKey moved verbatim out of the+ // already-suppressed useAppHotkeys.ts above, to keep that file under+ // the 600-line cap. Same pre-existing complexity, new file path.+ "packages/studio/src/hooks/appHotkeysDispatch.ts","packages/studio/src/hooks/useCaptionDetection.ts","packages/studio/src/hooks/useFileManager.ts","packages/studio/src/hooks/useFrameCapture.ts",diff --git a/packages/studio/src/hooks/appHotkeysDispatch.ts b/packages/studio/src/hooks/appHotkeysDispatch.tsnew file mode 100644index 0000000000..d0acbd67a7--- /dev/null+++ b/packages/studio/src/hooks/appHotkeysDispatch.ts@@ -0,0 +1,256 @@+import { automationOwnsKey } from "./useAutomationSelectionKeyboard";+import { usePlayerStore } from "../player";+import type { TimelineElement } from "../player";+import type { DomEditSelection } from "../components/editor/domEditing";+import type { LeftSidebarHandle } from "../components/sidebar/LeftSidebar";+import { isTypingTarget } from "../utils/typingTarget";+import { isEditableTarget } from "../utils/timelineDiscovery";+import { shouldIgnoreHistoryShortcut } from "../utils/studioHelpers";+import { canSplitElement } from "../utils/timelineElementSplit";+import { trackStudioEvent } from "../utils/studioTelemetry";++// Extracted from useAppHotkeys.ts to keep it under the studio 600-line cap,+// following useTimelineDeleteOps's precedent. Pure functions, no hooks — the+// hook still owns the actual keydown listeners and calls into these.++/** Exported so useAppHotkeys's own history-only preview listener can reuse+ * the same undo/redo key arbitration without duplicating it. */+export function handleUndoRedoKey(+ event: KeyboardEvent,+ onUndo: () => void,+ onRedo: () => void,+): boolean {+ const key = event.key.toLowerCase();+ if (key === "z" && !event.shiftKey) {+ event.preventDefault();+ onUndo();+ return true;+ }+ if ((key === "z" && event.shiftKey) || (event.ctrlKey && !event.metaKey && key === "y")) {+ event.preventDefault();+ onRedo();+ return true;+ }+ return false;+}++export interface HotkeyCallbacks {+ handleTimelineElementsDelete: (elements: TimelineElement[]) => Promise<void>;+ handleTimelineElementSplit: (element: TimelineElement, splitTime: number) => Promise<void>;+ handleDomEditElementDelete: (+ selection: DomEditSelection,+ options?: { expandGroup?: boolean },+ ) => Promise<void>;+ handleUndo: () => Promise<void>;+ handleRedo: () => Promise<void>;+ handleCopy: () => boolean;+ handlePaste: () => Promise<void>;+ handleCut: () => Promise<boolean>;+ onResetKeyframes: () => boolean;+ onDeleteSelectedKeyframes: () => void;+ onToggleRecording?: () => void;+ onGroupSelection?: () => void;+ onUngroupSelection?: () => void;+ leftSidebarRef: React.RefObject<LeftSidebarHandle | null>;+ domEditSelectionRef: React.MutableRefObject<DomEditSelection | null>;+ showToast: (message: string, tone?: "error" | "info") => void;+}++/** Exported for tests, like dispatchPlainKey below: lets the Cmd+C/Cmd+V+ * arbitration between an automation range and the clip clipboard be asserted+ * without standing up the whole hook. */+export function dispatchModifierKey(+ event: KeyboardEvent,+ key: string,+ cb: HotkeyCallbacks,+): boolean {+ if (+ !shouldIgnoreHistoryShortcut(event.target) &&+ handleUndoRedoKey(+ event,+ () => {+ trackStudioEvent("keyboard_shortcut", { action: "undo" });+ void cb.handleUndo();+ },+ () => {+ trackStudioEvent("keyboard_shortcut", { action: "redo" });+ void cb.handleRedo();+ },+ )+ )+ return true;++ if (event.key === "1") {+ event.preventDefault();+ trackStudioEvent("keyboard_shortcut", { action: "tab_compositions" });+ cb.leftSidebarRef.current?.selectTab("compositions");+ return true;+ }+ if (event.key === "2") {+ event.preventDefault();+ trackStudioEvent("keyboard_shortcut", { action: "tab_assets" });+ cb.leftSidebarRef.current?.selectTab("assets");+ return true;+ }++ if (key === "g" && !event.altKey && !isTypingTarget(event.target)) {+ event.preventDefault();+ if (event.shiftKey) cb.onUngroupSelection?.();+ else cb.onGroupSelection?.();+ return true;+ }++ if (!event.shiftKey && !event.altKey && !isEditableTarget(event.target)) {+ // An active automation range owns Cmd+C/Cmd+V, same as Delete below: this+ // capture listener runs before useAutomationSelectionKeyboard's, so without+ // this both clipboards raced the same key. No preventDefault, so the+ // downstream handler still sees it.+ if (automationOwnsKey(event)) return true;+ if (key === "c") {+ if (cb.handleCopy()) {+ event.preventDefault();+ trackStudioEvent("keyboard_shortcut", { action: "copy" });+ }+ return true;+ }+ if (key === "v") {+ event.preventDefault();+ trackStudioEvent("keyboard_shortcut", { action: "paste" });+ void cb.handlePaste();+ return true;+ }+ if (key === "x") {+ if (usePlayerStore.getState().selectedElementId || cb.domEditSelectionRef.current) {+ event.preventDefault();+ trackStudioEvent("keyboard_shortcut", { action: "cut" });+ void cb.handleCut();+ }+ return true;+ }+ }+ return false;+}++// fallow-ignore-next-line complexity+/** Exported for tests: the unmodified-key half of the dispatcher, so the+ * Delete arbitration between keyframes, an automation range and the clip can+ * be asserted without standing up the whole hook. */+export function dispatchPlainKey(event: KeyboardEvent, key: string, cb: HotkeyCallbacks): void {+ if (key === "f" && !event.shiftKey && !event.altKey) {+ event.preventDefault();+ if (document.fullscreenElement) void document.exitFullscreen();+ else+ document.querySelector<HTMLElement>("[data-studio-fullscreen-target]")?.requestFullscreen();+ return;+ }++ if (event.key === "s" && !event.altKey) {+ // Reserve bare `s` for Split even when the current selection cannot split,+ // so secondary listeners do not reinterpret the same key as Snap toggle.+ event.preventDefault();+ const { selectedElementId, elements, currentTime } = usePlayerStore.getState();+ if (selectedElementId) {+ const el = elements.find((e) => (e.key ?? e.id) === selectedElementId);+ if (+ el &&+ canSplitElement(el) &&+ currentTime > el.start &&+ currentTime < el.start + el.duration+ ) {+ void cb.handleTimelineElementSplit(el, currentTime);+ return;+ }+ // Expanded sub-comp children carry a qualified `sourceFile#id` selection+ // that isn't in the raw `elements` list, so the s-key can't resolve them.+ // Nudge toward the razor tool instead of failing silently.+ if (!el && selectedElementId.includes("#")) {+ cb.showToast("Use the razor tool (B) to split clips inside a sub-composition", "info");+ return;+ }+ }+ }++ if (key === "b" && !event.shiftKey && !event.altKey) {+ event.preventDefault();+ const { activeTool, setActiveTool } = usePlayerStore.getState();+ setActiveTool(activeTool === "razor" ? "select" : "razor");+ return;+ }++ if (key === "v" && !event.shiftKey && !event.altKey) {+ event.preventDefault();+ usePlayerStore.getState().setActiveTool("select");+ return;+ }++ // CapCut returns to the select tool with "A". Scoped to the razor being+ // armed; usePlaybackKeyboard.ts's own "A" (seek to in-point) checks the+ // same activeTool to stay out of the way.+ if (key === "a" && !event.shiftKey && !event.altKey) {+ const { activeTool, setActiveTool } = usePlayerStore.getState();+ if (activeTool === "razor") {+ event.preventDefault();+ setActiveTool("select");+ return;+ }+ }++ if (event.key === "Escape") {+ const { activeTool, selectedElementId, setActiveTool, setSelectedElementId } =+ usePlayerStore.getState();+ if (activeTool === "razor") {+ if (selectedElementId) setSelectedElementId(null);+ else setActiveTool("select");+ event.preventDefault();+ return;+ }+ }++ if ((event.key === "Delete" || event.key === "Backspace") && !event.altKey) {+ if (usePlayerStore.getState().selectedKeyframes.size > 0) {+ cb.onDeleteSelectedKeyframes();+ usePlayerStore.getState().clearSelectedKeyframes();+ event.preventDefault();+ return;+ }+ // An active automation range owns Delete (empties the range in place).+ // No preventDefault: this capture listener runs first, so falling through+ // is what lets the document-level handler still see the key instead of+ // the press reaching the clip delete below.+ if (usePlayerStore.getState().automationSelection) return;+ if (event.key === "Backspace") {+ const { selectedElementId, keyframeCache } = usePlayerStore.getState();+ if (selectedElementId && keyframeCache.has(selectedElementId) && cb.onResetKeyframes()) {+ event.preventDefault();+ return;+ }+ }+ // The canvas selection owns Delete first: the timeline mirror of it is+ // lossy (drops a member with no timeline row of its own), so deleting via+ // the timeline left other selected elements behind. Timeline stays as the+ // fallback for rows with no canvas node (audio, an inactive comp).+ const domSel = cb.domEditSelectionRef.current;+ if (domSel) {+ event.preventDefault();+ // The whole marquee group, not just the primary the ref holds.+ void cb.handleDomEditElementDelete(domSel, { expandGroup: true });+ return;+ }+ // Takes the WHOLE selection: `find` returned the first match, so selecting+ // every clip and pressing Delete removed exactly one of them.+ const { selectedElementId, selectedElementIds, elements } = usePlayerStore.getState();+ const selectionKeys = new Set(selectedElementIds);+ if (selectedElementId) selectionKeys.add(selectedElementId);+ const selected = elements.filter((e) => selectionKeys.has(e.key ?? e.id));+ if (selected.length > 0) {+ event.preventDefault();+ void cb.handleTimelineElementsDelete(selected);+ }+ return;+ }++ if (event.key === "r" && !event.shiftKey && !event.altKey && cb.onToggleRecording) {+ event.preventDefault();+ cb.onToggleRecording();+ }+}diff --git a/packages/studio/src/hooks/useAppHotkeys.test.ts b/packages/studio/src/hooks/useAppHotkeys.test.tsindex d0d0d5e403..dfbf8fc09b 100644--- a/packages/studio/src/hooks/useAppHotkeys.test.ts+++ b/packages/studio/src/hooks/useAppHotkeys.test.ts@@ -1,6 +1,6 @@// @vitest-environment happy-domimport { afterEach, describe, expect, it, vi } from "vitest";-import { dispatchModifierKey, dispatchPlainKey } from "./useAppHotkeys";+import { dispatchModifierKey, dispatchPlainKey } from "./appHotkeysDispatch";import { usePlayerStore } from "../player/store/playerStore";import { clearAutomationClipboard, copyRange } from "../player/components/automationClipboard";import { VOLUME_RANGE } from "@hyperframes/core/audio-automation";diff --git a/packages/studio/src/hooks/useAppHotkeys.ts b/packages/studio/src/hooks/useAppHotkeys.tsindex 9241c10734..a7d138dcbd 100644--- a/packages/studio/src/hooks/useAppHotkeys.ts+++ b/packages/studio/src/hooks/useAppHotkeys.ts@@ -1,21 +1,23 @@import { useCallback, useEffect, useRef } from "react";-import { automationOwnsKey } from "./useAutomationSelectionKeyboard";import { usePlayerStore } from "../player";import type { TimelineElement } from "../player";import type { DomEditSelection } from "../components/editor/domEditing";import type { LeftSidebarHandle } from "../components/sidebar/LeftSidebar";import { STUDIO_MOTION_PATH } from "../components/editor/studioMotion";import { isTypingTarget } from "../utils/typingTarget";-import { isEditableTarget } from "../utils/timelineDiscovery";import { useCaptionStore } from "../captions/store";import {applyCaptionModelToIframe,isCaptionPreviewVisible,} from "../captions/components/CaptionOverlayUtils";import { shouldIgnoreHistoryShortcut } from "../utils/studioHelpers";-import { canSplitElement } from "../utils/timelineElementSplit";-import { trackStudioEvent } from "../utils/studioTelemetry";import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator";+import {+ type HotkeyCallbacks,+ dispatchModifierKey,+ dispatchPlainKey,+ handleUndoRedoKey,+} from "./appHotkeysDispatch";function iframeContentWindow(iframe: HTMLIFrameElement | null): Window | null {try {@@ -40,22 +42,6 @@ function safeRemoveListener(t: EventTarget | null, type: string, h: EventListene}}-// fallow-ignore-next-line complexity-function handleUndoRedoKey(event: KeyboardEvent, onUndo: () => void, onRedo: () => void): boolean {- const key = event.key.toLowerCase();- if (key === "z" && !event.shiftKey) {- event.preventDefault();- onUndo();- return true;- }- if ((key === "z" && event.shiftKey) || (event.ctrlKey && !event.metaKey && key === "y")) {- event.preventDefault();- onRedo();- return true;- }- return false;-}-// Beat edits live in an in-memory stack interleaved with file history by// timestamp. Undo steps to the NEWER op (beatAt >= fileAt); redo replays the// inverse, stepping to the OLDER op (beatAt <= fileAt). Returns true when it@@ -146,237 +132,6 @@ interface UseAppHotkeysParams {forceReloadSdkSession?: () => void;}-// ── Extracted keydown dispatch (pure function, no hooks) ──--interface HotkeyCallbacks {- handleTimelineElementsDelete: (elements: TimelineElement[]) => Promise<void>;- handleTimelineElementSplit: (element: TimelineElement, splitTime: number) => Promise<void>;- handleDomEditElementDelete: (- selection: DomEditSelection,- options?: { expandGroup?: boolean },- ) => Promise<void>;- handleUndo: () => Promise<void>;- handleRedo: () => Promise<void>;- handleCopy: () => boolean;- handlePaste: () => Promise<void>;- handleCut: () => Promise<boolean>;- onResetKeyframes: () => boolean;- onDeleteSelectedKeyframes: () => void;- onToggleRecording?: () => void;- onGroupSelection?: () => void;- onUngroupSelection?: () => void;- leftSidebarRef: React.RefObject<LeftSidebarHandle | null>;- domEditSelectionRef: React.MutableRefObject<DomEditSelection | null>;- showToast: (message: string, tone?: "error" | "info") => void;-}--/** Exported for tests, like dispatchPlainKey below: lets the Cmd+C/Cmd+V- * arbitration between an automation range and the clip clipboard be asserted- * without standing up the whole hook. */-export function dispatchModifierKey(- event: KeyboardEvent,- key: string,- cb: HotkeyCallbacks,-): boolean {- if (- !shouldIgnoreHistoryShortcut(event.target) &&- handleUndoRedoKey(- event,- () => {- trackStudioEvent("keyboard_shortcut", { action: "undo" });- void cb.handleUndo();- },- () => {- trackStudioEvent("keyboard_shortcut", { action: "redo" });- void cb.handleRedo();- },- )- )- return true;-- if (event.key === "1") {- event.preventDefault();- trackStudioEvent("keyboard_shortcut", { action: "tab_compositions" });- cb.leftSidebarRef.current?.selectTab("compositions");- return true;- }- if (event.key === "2") {- event.preventDefault();- trackStudioEvent("keyboard_shortcut", { action: "tab_assets" });- cb.leftSidebarRef.current?.selectTab("assets");- return true;- }-- if (key === "g" && !event.altKey && !isTypingTarget(event.target)) {- event.preventDefault();- if (event.shiftKey) cb.onUngroupSelection?.();- else cb.onGroupSelection?.();- return true;- }-- if (!event.shiftKey && !event.altKey && !isEditableTarget(event.target)) {- // An active automation range owns Cmd+C/Cmd+V, the same way it owns Delete- // below. This listener is on window/capture and runs before- // useAutomationSelectionKeyboard's document/capture handler, so without- // this the clip clipboard also claimed the key: Cmd+V duplicated the clip- // while the automation paste wrote the same file, and Cmd+C armed both- // clipboards and toasted "Copied clip". Return without preventDefault so- // the downstream handler still sees the key.- if (automationOwnsKey(event)) return true;- if (key === "c") {- if (cb.handleCopy()) {- event.preventDefault();- trackStudioEvent("keyboard_shortcut", { action: "copy" });- }- return true;- }- if (key === "v") {- event.preventDefault();- trackStudioEvent("keyboard_shortcut", { action: "paste" });- void cb.handlePaste();- return true;- }- if (key === "x") {- if (usePlayerStore.getState().selectedElementId || cb.domEditSelectionRef.current) {- event.preventDefault();- trackStudioEvent("keyboard_shortcut", { action: "cut" });- void cb.handleCut();- }- return true;- }- }- return false;-}--// fallow-ignore-next-line complexity-/** Exported for tests: the unmodified-key half of the dispatcher, so the- * Delete arbitration between keyframes, an automation range and the clip can- * be asserted without standing up the whole hook. */-export function dispatchPlainKey(event: KeyboardEvent, key: string, cb: HotkeyCallbacks): void {- if (key === "f" && !event.shiftKey && !event.altKey) {- event.preventDefault();- if (document.fullscreenElement) void document.exitFullscreen();- else- document.querySelector<HTMLElement>("[data-studio-fullscreen-target]")?.requestFullscreen();- return;- }-- if (event.key === "s" && !event.altKey) {- // Reserve bare `s` for Split even when the current selection cannot split,- // so secondary listeners do not reinterpret the same key as Snap toggle.- event.preventDefault();- const { selectedElementId, elements, currentTime } = usePlayerStore.getState();- if (selectedElementId) {- const el = elements.find((e) => (e.key ?? e.id) === selectedElementId);- if (- el &&- canSplitElement(el) &&- currentTime > el.start &&- currentTime < el.start + el.duration- ) {- void cb.handleTimelineElementSplit(el, currentTime);- return;- }- // Expanded sub-comp children carry a qualified `sourceFile#id` selection- // that isn't in the raw `elements` list, so the s-key can't resolve them.- // Nudge toward the razor tool instead of failing silently.- if (!el && selectedElementId.includes("#")) {- cb.showToast("Use the razor tool (B) to split clips inside a sub-composition", "info");- return;- }- }- }-- if (key === "b" && !event.shiftKey && !event.altKey) {- event.preventDefault();- const { activeTool, setActiveTool } = usePlayerStore.getState();- setActiveTool(activeTool === "razor" ? "select" : "razor");- return;- }-- if (key === "v" && !event.shiftKey && !event.altKey) {- event.preventDefault();- usePlayerStore.getState().setActiveTool("select");- return;- }-- // CapCut returns to the select tool with "A". Scoped to the razor being- // armed; usePlaybackKeyboard.ts's own "A" (seek to in-point) checks the- // same activeTool to stay out of the way.- if (key === "a" && !event.shiftKey && !event.altKey) {- const { activeTool, setActiveTool } = usePlayerStore.getState();- if (activeTool === "razor") {- event.preventDefault();- setActiveTool("select");- return;- }- }-- if (event.key === "Escape") {- const { activeTool, selectedElementId, setActiveTool, setSelectedElementId } =- usePlayerStore.getState();- if (activeTool === "razor") {- if (selectedElementId) setSelectedElementId(null);- else setActiveTool("select");- event.preventDefault();- return;- }- }-- if ((event.key === "Delete" || event.key === "Backspace") && !event.altKey) {- if (usePlayerStore.getState().selectedKeyframes.size > 0) {- cb.onDeleteSelectedKeyframes();- usePlayerStore.getState().clearSelectedKeyframes();- event.preventDefault();- return;- }- // An active automation range owns Delete: useAutomationSelectionKeyboard- // empties the range in place, pinning the anchors. Fall through WITHOUT- // preventDefault so that document-level handler still sees the key — this- // listener is on window/capture, so it runs first and everything below- // would otherwise win. Without this the press reaches the clip delete- // below and destroys the whole clip the lane belongs to.- if (usePlayerStore.getState().automationSelection) return;- if (event.key === "Backspace") {- const { selectedElementId, keyframeCache } = usePlayerStore.getState();- if (selectedElementId && keyframeCache.has(selectedElementId) && cb.onResetKeyframes()) {- event.preventDefault();- return;- }- }- // The canvas selection is what the user actually drew a marquee around, so- // it owns Delete whenever it holds something. The timeline mirror of that- // selection is derived and lossy — a member with no timeline row of its own- // is dropped from it — so deleting through the timeline removed the handful- // of clips it knew about and left every other selected element behind,- // still drawn as selected. The timeline path stays as the fallback for rows- // with no canvas node to select (audio, a comp that is not the active one).- const domSel = cb.domEditSelectionRef.current;- if (domSel) {- event.preventDefault();- // The whole marquee group, not just the primary the ref holds.- void cb.handleDomEditElementDelete(domSel, { expandGroup: true });- return;- }- // Takes the WHOLE selection: `find` returned the first match, so selecting- // every clip and pressing Delete removed exactly one of them.- const { selectedElementId, selectedElementIds, elements } = usePlayerStore.getState();- const selectionKeys = new Set(selectedElementIds);- if (selectedElementId) selectionKeys.add(selectedElementId);- const selected = elements.filter((e) => selectionKeys.has(e.key ?? e.id));- if (selected.length > 0) {- event.preventDefault();- void cb.handleTimelineElementsDelete(selected);- }- return;- }-- if (event.key === "r" && !event.shiftKey && !event.altKey && cb.onToggleRecording) {- event.preventDefault();- cb.onToggleRecording();- }-}-// ── Hook ──export function useAppHotkeys({