Skip to content

Commit f6f2ee1

Browse files
committed
fix(studio): modifier combos fire for playback keys, caption guard and held stop key follow the map
1 parent bdcd4c7 commit f6f2ee1

5 files changed

Lines changed: 70 additions & 28 deletions

File tree

‎packages/studio/src/player/components/studioShortcuts.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ export const keyHint = (action: StudioKeyAction, keys: StudioKeys = current) =>
165165
export const matchesKey = (e: ComboEvent, action: StudioKeyAction) =>
166166
matchCombo(e, parseCombo(current[action])!);
167167

168-
export const keyOf = (action: StudioKeyAction) => parseCombo(current[action])!.key;
169-
170168
export function buildShortcutSections(keys: StudioKeys): readonly ShortcutSection[] {
171169
const k = (action: StudioKeyAction) => keyHint(action, keys);
172170
return [

‎packages/studio/src/player/hooks/usePlaybackKeyboard.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,19 @@ function setupHook(): HookHandle {
7676
};
7777
}
7878

79-
function keydown(init: { code: string; key: string; shiftKey?: boolean }): KeyboardEvent {
79+
function keydown(init: {
80+
code: string;
81+
key: string;
82+
shiftKey?: boolean;
83+
metaKey?: boolean;
84+
altKey?: boolean;
85+
}): KeyboardEvent {
8086
return new KeyboardEvent("keydown", {
8187
code: init.code,
8288
key: init.key,
8389
shiftKey: init.shiftKey ?? false,
90+
metaKey: init.metaKey ?? false,
91+
altKey: init.altKey ?? false,
8492
cancelable: true,
8593
});
8694
}
@@ -447,3 +455,31 @@ describe("usePlaybackKeyboard: space, arrows, loop and clear keys are combos", (
447455
expect(usePlayerStore.getState().inPoint).toBeNull();
448456
});
449457
});
458+
459+
describe("usePlaybackKeyboard: modifier combos and named keys", () => {
460+
const send = (dispatch: HookHandle["dispatch"], init: Parameters<typeof keydown>[0]) =>
461+
act(() => {
462+
dispatch(keydown(init));
463+
});
464+
465+
it("a playback action remapped to a Cmd or Alt combo answers, and the plain key does not", () => {
466+
setStudioKeys({ playPause: "mod+p", mute: "alt+m" });
467+
const { dispatch, spies } = setupHook();
468+
usePlayerStore.setState({ isPlaying: false, audioMuted: false });
469+
send(dispatch, { code: "KeyP", key: "p", metaKey: true });
470+
expect(spies.play).toHaveBeenCalledTimes(1);
471+
send(dispatch, { code: "KeyM", key: "µ", altKey: true });
472+
expect(usePlayerStore.getState().audioMuted).toBe(true);
473+
send(dispatch, { code: "KeyP", key: "p" });
474+
expect(spies.play).toHaveBeenCalledTimes(1);
475+
});
476+
477+
it("holding a stop key that is a named key still frame-steps instead of shuttling", () => {
478+
setStudioKeys({ pause: "Home" });
479+
const { dispatch, spies } = setupHook();
480+
send(dispatch, { code: "Home", key: "Home" });
481+
send(dispatch, { code: "KeyJ", key: "j" });
482+
expect(spies.seek).toHaveBeenCalledTimes(1);
483+
expect(spies.playBackward).not.toHaveBeenCalled();
484+
});
485+
});

‎packages/studio/src/player/hooks/usePlaybackKeyboard.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "../lib/playbackShortcuts";
1616
import { canvasNudgeKeysClaimed } from "../../utils/canvasNudgeGate";
1717
import { usePlayerStore } from "../store/playerStore";
18-
import { keyOf, matchesKey, type StudioKeyAction } from "../components/studioShortcuts";
18+
import { matchesKey, type StudioKeyAction } from "../components/studioShortcuts";
1919
import { resolveCanvasNudgeDelta } from "../../components/editor/domEditNudge";
2020
import { stepFrameTime } from "../lib/time";
2121
import { useLinkedClipPreferences } from "../../utils/linkedClipPreferences";
@@ -102,7 +102,7 @@ export function usePlaybackKeyboard({
102102
pause,
103103
seek,
104104
}: UsePlaybackKeyboardParams) {
105-
const pressedKeysRef = useRef(new Set<string>());
105+
const heldPauseRef = useRef(new Set<string>());
106106
const playbackKeyDownRef = useRef<(e: KeyboardEvent) => void>(() => {});
107107
const playbackKeyUpRef = useRef<(e: KeyboardEvent) => void>(() => {});
108108

@@ -150,16 +150,16 @@ export function usePlaybackKeyboard({
150150
const handlePlaybackKeyDown = useCallback(
151151
(e: KeyboardEvent) => {
152152
if (e.defaultPrevented || ignoresPlaybackKey(e)) return;
153-
pressedKeysRef.current.add(e.key.toLowerCase());
154153
const action = playbackActionOf(e);
154+
if (action === "pause") heldPauseRef.current.add(e.code || e.key);
155155
if (!action) return;
156156
const isFrameStep = action in FRAME_STEPS;
157157
// A nudgeable canvas selection owns the arrow keys (DomEditOverlay moves
158158
// the element); frame-stepping would double-handle the same keystroke.
159159
if (isFrameStep && canvasNudgeKeysClaimed() && resolveCanvasNudgeDelta(e)) return;
160160
if (e.repeat && action !== "playPause" && !isFrameStep) return;
161161
const shuttleOrStep = (direction: -1 | 1) =>
162-
pressedKeysRef.current.has(keyOf("pause"))
162+
heldPauseRef.current.size > 0
163163
? stepFrames(direction)
164164
: shuttle(direction < 0 ? "backward" : "forward");
165165
const player = usePlayerStore.getState();
@@ -187,7 +187,7 @@ export function usePlaybackKeyboard({
187187
);
188188

189189
const handlePlaybackKeyUp = useCallback((e: KeyboardEvent) => {
190-
pressedKeysRef.current.delete(e.key.toLowerCase());
190+
heldPauseRef.current.delete(e.code || e.key);
191191
}, []);
192192

193193
playbackKeyDownRef.current = handlePlaybackKeyDown;

‎packages/studio/src/player/hooks/useTimelinePlayer.test.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setStudioKeys } from "../components/studioShortcuts";
12
import { afterEach, describe, expect, it, vi } from "vitest";
23
import { Window } from "happy-dom";
34
import {
@@ -48,8 +49,10 @@ function mockTargetMatching(selectorNeedle: string): EventTarget {
4849
function mockKeyboardEvent(
4950
code: string,
5051
overrides: Partial<Pick<KeyboardEvent, "altKey" | "ctrlKey" | "metaKey" | "target">> = {},
51-
): Pick<KeyboardEvent, "altKey" | "ctrlKey" | "metaKey" | "code" | "target"> {
52+
): Pick<KeyboardEvent, "key" | "altKey" | "ctrlKey" | "metaKey" | "shiftKey" | "code" | "target"> {
5253
return {
54+
key: code,
55+
shiftKey: false,
5356
altKey: false,
5457
ctrlKey: false,
5558
metaKey: false,
@@ -556,18 +559,6 @@ describe("shouldIgnorePlaybackShortcutTarget", () => {
556559
});
557560

558561
describe("shouldIgnorePlaybackShortcutEvent", () => {
559-
it("ignores modified playback shortcuts so browser and app chords can handle them", () => {
560-
expect(
561-
shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("ArrowLeft", { altKey: true })),
562-
).toBe(true);
563-
expect(shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("KeyK", { ctrlKey: true }))).toBe(
564-
true,
565-
);
566-
expect(shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("KeyL", { metaKey: true }))).toBe(
567-
true,
568-
);
569-
});
570-
571562
it("defers Arrow frame shortcuts while caption edit mode has selected words", () => {
572563
const captionSelection = { isCaptionEditMode: true, selectedCaptionSegmentCount: 1 };
573564

@@ -582,6 +573,21 @@ describe("shouldIgnorePlaybackShortcutEvent", () => {
582573
);
583574
});
584575

576+
it("defers a remapped frame key, not the arrows, while caption words are selected", () => {
577+
setStudioKeys({ frameBack: "," });
578+
const captionSelection = { isCaptionEditMode: true, selectedCaptionSegmentCount: 1 };
579+
expect(
580+
shouldIgnorePlaybackShortcutEvent(
581+
{ ...mockKeyboardEvent("Comma"), key: "," },
582+
captionSelection,
583+
),
584+
).toBe(true);
585+
expect(
586+
shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("ArrowLeft"), captionSelection),
587+
).toBe(false);
588+
setStudioKeys(null);
589+
});
590+
585591
it("allows Arrow frame shortcuts when captions are not selected", () => {
586592
expect(
587593
shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("ArrowRight"), {

‎packages/studio/src/player/lib/playbackShortcuts.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
*/
88

99
import { ownsPlainKeys } from "../../utils/typingTarget";
10+
import type { ComboEvent } from "../components/studioCombo";
1011
import { matchesKey } from "../components/studioShortcuts";
1112
import { usePlayerStore } from "../store/playerStore";
1213

13-
const PLAYBACK_FRAME_STEP_CODES = new Set(["ArrowLeft", "ArrowRight"]);
14+
const FRAME_STEP_ACTIONS = [
15+
"frameBack",
16+
"frameForward",
17+
"frameBackTen",
18+
"frameForwardTen",
19+
] as const;
1420

1521
const PLAYBACK_SHORTCUT_IGNORED_SELECTOR = [
1622
"button",
@@ -61,10 +67,7 @@ interface PlaybackShortcutCaptionState {
6167
selectedCaptionSegmentCount: number;
6268
}
6369

64-
type PlaybackShortcutEvent = Pick<
65-
KeyboardEvent,
66-
"altKey" | "ctrlKey" | "metaKey" | "code" | "target"
67-
>;
70+
type PlaybackShortcutEvent = ComboEvent & Pick<KeyboardEvent, "target">;
6871

6972
export function shouldIgnorePlaybackShortcutEvent(
7073
event: PlaybackShortcutEvent,
@@ -73,11 +76,10 @@ export function shouldIgnorePlaybackShortcutEvent(
7376
selectedCaptionSegmentCount: 0,
7477
},
7578
): boolean {
76-
if (event.metaKey || event.ctrlKey || event.altKey) return true;
7779
if (shouldIgnorePlaybackShortcutTarget(event.target)) return true;
7880
if (isModalDialogOpen()) return true;
7981
return (
80-
PLAYBACK_FRAME_STEP_CODES.has(event.code) &&
82+
FRAME_STEP_ACTIONS.some((action) => matchesKey(event, action)) &&
8183
captionState.isCaptionEditMode &&
8284
captionState.selectedCaptionSegmentCount > 0
8385
);

0 commit comments

Comments
 (0)