Skip to content

Commit 59d7c37

Browse files
committed
fix(studio): extract hotkey dispatch and close the stripHfIds hoist gap
useAppHotkeys.ts crossed the studio 600-line cap once merged against main's own independent growth (CI's file-size check runs on the merge ref, not the branch alone). Extracts dispatchModifierKey, dispatchPlainKey and their shared HotkeyCallbacks type into appHotkeysDispatch.ts, the same split useTimelineDeleteOps.ts already used for this file's sibling. Also ports main's independent "A returns to select while razor is armed" hotkey into the new location so it isn't lost at the next rebase. stripHfIds silently returned html unstripped when DOMParser found no body-level element (a tag the parser hoists into <head>, e.g. title or meta). Closes it with a regex fallback and a test reproducing the exact hoisted-tag case, not just the no-tag-at-all case from the prior commit.
1 parent 26b1b18 commit 59d7c37

6 files changed

Lines changed: 322 additions & 256 deletions

File tree

‎.fallowrc.jsonc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@
899899
"packages/studio/src/components/ui/Tooltip.tsx",
900900
"packages/studio/src/components/ui/useDialogBehavior.ts",
901901
"packages/studio/src/hooks/useAppHotkeys.ts",
902+
// dispatchModifierKey/dispatchPlainKey moved verbatim out of the
903+
// already-suppressed useAppHotkeys.ts above, to keep that file under
904+
// the 600-line cap. Same pre-existing complexity, new file path.
905+
"packages/studio/src/hooks/appHotkeysDispatch.ts",
902906
"packages/studio/src/hooks/useCaptionDetection.ts",
903907
"packages/studio/src/hooks/useFileManager.ts",
904908
"packages/studio/src/hooks/useFrameCapture.ts",
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
import { automationOwnsKey } from "./useAutomationSelectionKeyboard";
2+
import { usePlayerStore } from "../player";
3+
import type { TimelineElement } from "../player";
4+
import type { DomEditSelection } from "../components/editor/domEditing";
5+
import type { LeftSidebarHandle } from "../components/sidebar/LeftSidebar";
6+
import { isTypingTarget } from "../utils/typingTarget";
7+
import { isEditableTarget } from "../utils/timelineDiscovery";
8+
import { shouldIgnoreHistoryShortcut } from "../utils/studioHelpers";
9+
import { canSplitElement } from "../utils/timelineElementSplit";
10+
import { trackStudioEvent } from "../utils/studioTelemetry";
11+
12+
// Extracted from useAppHotkeys.ts to keep it under the studio 600-line cap,
13+
// following useTimelineDeleteOps's precedent. Pure functions, no hooks — the
14+
// hook still owns the actual keydown listeners and calls into these.
15+
16+
/** Exported so useAppHotkeys's own history-only preview listener can reuse
17+
* the same undo/redo key arbitration without duplicating it. */
18+
export function handleUndoRedoKey(
19+
event: KeyboardEvent,
20+
onUndo: () => void,
21+
onRedo: () => void,
22+
): boolean {
23+
const key = event.key.toLowerCase();
24+
if (key === "z" && !event.shiftKey) {
25+
event.preventDefault();
26+
onUndo();
27+
return true;
28+
}
29+
if ((key === "z" && event.shiftKey) || (event.ctrlKey && !event.metaKey && key === "y")) {
30+
event.preventDefault();
31+
onRedo();
32+
return true;
33+
}
34+
return false;
35+
}
36+
37+
export interface HotkeyCallbacks {
38+
handleTimelineElementsDelete: (elements: TimelineElement[]) => Promise<void>;
39+
handleTimelineElementSplit: (element: TimelineElement, splitTime: number) => Promise<void>;
40+
handleDomEditElementDelete: (
41+
selection: DomEditSelection,
42+
options?: { expandGroup?: boolean },
43+
) => Promise<void>;
44+
handleUndo: () => Promise<void>;
45+
handleRedo: () => Promise<void>;
46+
handleCopy: () => boolean;
47+
handlePaste: () => Promise<void>;
48+
handleCut: () => Promise<boolean>;
49+
handleDuplicate: () => Promise<boolean>;
50+
onResetKeyframes: () => boolean;
51+
onDeleteSelectedKeyframes: () => void;
52+
onToggleRecording?: () => void;
53+
onGroupSelection?: () => void;
54+
onUngroupSelection?: () => void;
55+
leftSidebarRef: React.RefObject<LeftSidebarHandle | null>;
56+
domEditSelectionRef: React.MutableRefObject<DomEditSelection | null>;
57+
showToast: (message: string, tone?: "error" | "info") => void;
58+
}
59+
60+
/** Exported for tests, like dispatchPlainKey below: lets the Cmd+C/Cmd+V
61+
* arbitration between an automation range and the clip clipboard be asserted
62+
* without standing up the whole hook. */
63+
export function dispatchModifierKey(
64+
event: KeyboardEvent,
65+
key: string,
66+
cb: HotkeyCallbacks,
67+
): boolean {
68+
if (
69+
!shouldIgnoreHistoryShortcut(event.target) &&
70+
handleUndoRedoKey(
71+
event,
72+
() => {
73+
trackStudioEvent("keyboard_shortcut", { action: "undo" });
74+
void cb.handleUndo();
75+
},
76+
() => {
77+
trackStudioEvent("keyboard_shortcut", { action: "redo" });
78+
void cb.handleRedo();
79+
},
80+
)
81+
)
82+
return true;
83+
84+
if (event.key === "1") {
85+
event.preventDefault();
86+
trackStudioEvent("keyboard_shortcut", { action: "tab_compositions" });
87+
cb.leftSidebarRef.current?.selectTab("compositions");
88+
return true;
89+
}
90+
if (event.key === "2") {
91+
event.preventDefault();
92+
trackStudioEvent("keyboard_shortcut", { action: "tab_assets" });
93+
cb.leftSidebarRef.current?.selectTab("assets");
94+
return true;
95+
}
96+
97+
if (key === "g" && !event.altKey && !isTypingTarget(event.target)) {
98+
event.preventDefault();
99+
if (event.shiftKey) cb.onUngroupSelection?.();
100+
else cb.onGroupSelection?.();
101+
return true;
102+
}
103+
104+
if (!event.shiftKey && !event.altKey && !isEditableTarget(event.target)) {
105+
// An active automation range owns Cmd+C/Cmd+V, same as Delete below: this
106+
// capture listener runs before useAutomationSelectionKeyboard's, so without
107+
// this both clipboards raced the same key. No preventDefault, so the
108+
// downstream handler still sees it.
109+
if (automationOwnsKey(event)) return true;
110+
if (key === "c") {
111+
if (cb.handleCopy()) {
112+
event.preventDefault();
113+
trackStudioEvent("keyboard_shortcut", { action: "copy" });
114+
}
115+
return true;
116+
}
117+
if (key === "v") {
118+
event.preventDefault();
119+
trackStudioEvent("keyboard_shortcut", { action: "paste" });
120+
void cb.handlePaste();
121+
return true;
122+
}
123+
if (key === "x") {
124+
if (usePlayerStore.getState().selectedElementId || cb.domEditSelectionRef.current) {
125+
event.preventDefault();
126+
trackStudioEvent("keyboard_shortcut", { action: "cut" });
127+
void cb.handleCut();
128+
}
129+
return true;
130+
}
131+
if (key === "d") {
132+
// Always own this key here, even with nothing selected — otherwise the
133+
// browser's own Cmd+D (bookmark this page) fires over the editor.
134+
event.preventDefault();
135+
if (usePlayerStore.getState().selectedElementId) {
136+
trackStudioEvent("keyboard_shortcut", { action: "duplicate" });
137+
void cb.handleDuplicate();
138+
}
139+
return true;
140+
}
141+
}
142+
return false;
143+
}
144+
145+
// fallow-ignore-next-line complexity
146+
/** Exported for tests: the unmodified-key half of the dispatcher, so the
147+
* Delete arbitration between keyframes, an automation range and the clip can
148+
* be asserted without standing up the whole hook. */
149+
export function dispatchPlainKey(event: KeyboardEvent, key: string, cb: HotkeyCallbacks): void {
150+
if (key === "f" && !event.shiftKey && !event.altKey) {
151+
event.preventDefault();
152+
if (document.fullscreenElement) void document.exitFullscreen();
153+
else
154+
document.querySelector<HTMLElement>("[data-studio-fullscreen-target]")?.requestFullscreen();
155+
return;
156+
}
157+
158+
if (event.key === "s" && !event.altKey) {
159+
// Reserve bare `s` for Split even when the current selection cannot split,
160+
// so secondary listeners do not reinterpret the same key as Snap toggle.
161+
event.preventDefault();
162+
const { selectedElementId, elements, currentTime } = usePlayerStore.getState();
163+
if (selectedElementId) {
164+
const el = elements.find((e) => (e.key ?? e.id) === selectedElementId);
165+
if (
166+
el &&
167+
canSplitElement(el) &&
168+
currentTime > el.start &&
169+
currentTime < el.start + el.duration
170+
) {
171+
void cb.handleTimelineElementSplit(el, currentTime);
172+
return;
173+
}
174+
// Expanded sub-comp children carry a qualified `sourceFile#id` selection
175+
// that isn't in the raw `elements` list, so the s-key can't resolve them.
176+
// Nudge toward the razor tool instead of failing silently.
177+
if (!el && selectedElementId.includes("#")) {
178+
cb.showToast("Use the razor tool (B) to split clips inside a sub-composition", "info");
179+
return;
180+
}
181+
}
182+
}
183+
184+
if (key === "b" && !event.shiftKey && !event.altKey) {
185+
event.preventDefault();
186+
const { activeTool, setActiveTool } = usePlayerStore.getState();
187+
setActiveTool(activeTool === "razor" ? "select" : "razor");
188+
return;
189+
}
190+
191+
if (key === "v" && !event.shiftKey && !event.altKey) {
192+
event.preventDefault();
193+
usePlayerStore.getState().setActiveTool("select");
194+
return;
195+
}
196+
197+
// CapCut returns to the select tool with "A". Scoped to the razor being
198+
// armed; usePlaybackKeyboard.ts's own "A" (seek to in-point) checks the
199+
// same activeTool to stay out of the way.
200+
if (key === "a" && !event.shiftKey && !event.altKey) {
201+
const { activeTool, setActiveTool } = usePlayerStore.getState();
202+
if (activeTool === "razor") {
203+
event.preventDefault();
204+
setActiveTool("select");
205+
return;
206+
}
207+
}
208+
209+
if (event.key === "Escape") {
210+
const { activeTool, selectedElementId, setActiveTool, setSelectedElementId } =
211+
usePlayerStore.getState();
212+
if (activeTool === "razor") {
213+
if (selectedElementId) setSelectedElementId(null);
214+
else setActiveTool("select");
215+
event.preventDefault();
216+
return;
217+
}
218+
}
219+
220+
if ((event.key === "Delete" || event.key === "Backspace") && !event.altKey) {
221+
if (usePlayerStore.getState().selectedKeyframes.size > 0) {
222+
cb.onDeleteSelectedKeyframes();
223+
usePlayerStore.getState().clearSelectedKeyframes();
224+
event.preventDefault();
225+
return;
226+
}
227+
// An active automation range owns Delete (empties the range in place).
228+
// No preventDefault: this capture listener runs first, so falling through
229+
// is what lets the document-level handler still see the key instead of
230+
// the press reaching the clip delete below.
231+
if (usePlayerStore.getState().automationSelection) return;
232+
if (event.key === "Backspace") {
233+
const { selectedElementId, keyframeCache } = usePlayerStore.getState();
234+
if (selectedElementId && keyframeCache.has(selectedElementId) && cb.onResetKeyframes()) {
235+
event.preventDefault();
236+
return;
237+
}
238+
}
239+
// The canvas selection owns Delete first: the timeline mirror of it is
240+
// lossy (drops a member with no timeline row of its own), so deleting via
241+
// the timeline left other selected elements behind. Timeline stays as the
242+
// fallback for rows with no canvas node (audio, an inactive comp).
243+
const domSel = cb.domEditSelectionRef.current;
244+
if (domSel) {
245+
event.preventDefault();
246+
// The whole marquee group, not just the primary the ref holds.
247+
void cb.handleDomEditElementDelete(domSel, { expandGroup: true });
248+
return;
249+
}
250+
// Takes the WHOLE selection: `find` returned the first match, so selecting
251+
// every clip and pressing Delete removed exactly one of them.
252+
const { selectedElementId, selectedElementIds, elements } = usePlayerStore.getState();
253+
const selectionKeys = new Set(selectedElementIds);
254+
if (selectedElementId) selectionKeys.add(selectedElementId);
255+
const selected = elements.filter((e) => selectionKeys.has(e.key ?? e.id));
256+
if (selected.length > 0) {
257+
event.preventDefault();
258+
void cb.handleTimelineElementsDelete(selected);
259+
}
260+
return;
261+
}
262+
263+
if (event.key === "r" && !event.shiftKey && !event.altKey && cb.onToggleRecording) {
264+
event.preventDefault();
265+
cb.onToggleRecording();
266+
}
267+
}

‎packages/studio/src/hooks/useAppHotkeys.test.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @vitest-environment happy-dom
22
import { afterEach, describe, expect, it, vi } from "vitest";
3-
import { dispatchModifierKey, dispatchPlainKey } from "./useAppHotkeys";
3+
import { dispatchModifierKey, dispatchPlainKey } from "./appHotkeysDispatch";
44
import { usePlayerStore } from "../player/store/playerStore";
55
import { clearAutomationClipboard, copyRange } from "../player/components/automationClipboard";
66
import { VOLUME_RANGE } from "@hyperframes/core/audio-automation";
@@ -277,3 +277,25 @@ describe("dispatchModifierKey — Cmd+C/Cmd+V arbitration", () => {
277277
expect(e.defaultPrevented).toBe(false);
278278
});
279279
});
280+
281+
describe('dispatchPlainKey — "A" returns to select while the razor is armed', () => {
282+
afterEach(() => {
283+
usePlayerStore.setState({ activeTool: "select" });
284+
});
285+
286+
it("returns to the select tool, matching CapCut's keybinding", () => {
287+
usePlayerStore.setState({ activeTool: "razor" });
288+
const e = press("a");
289+
dispatchPlainKey(e, "a", callbacks());
290+
expect(usePlayerStore.getState().activeTool).toBe("select");
291+
expect(e.defaultPrevented).toBe(true);
292+
});
293+
294+
it("does not intercept plain \"a\" when the razor isn't armed, leaving playback's seek-to-in-point live", () => {
295+
usePlayerStore.setState({ activeTool: "select" });
296+
const e = press("a");
297+
dispatchPlainKey(e, "a", callbacks());
298+
expect(usePlayerStore.getState().activeTool).toBe("select");
299+
expect(e.defaultPrevented).toBe(false);
300+
});
301+
});

0 commit comments

Comments
 (0)