Skip to content

Commit 9fa02ba

Browse files
feat(studio): every editor and player shortcut is remappable (#6966)
* feat(studio): shortcut combos in the key resolver * feat(studio): every editor shortcut is a remappable combo * refactor(studio): playback keys dispatch through one table * fix(studio): combo parsing without backtracking, symbol keys match, fewer comments * style(studio): format the shortcut files * chore(studio): drop a doc comment the ratchet counts * chore(studio): drop a doc comment the ratchet counts * fix(studio): modifier combos fire for playback keys, caption guard and held stop key follow the map
1 parent c06427b commit 9fa02ba

37 files changed

Lines changed: 890 additions & 370 deletions

‎packages/studio/src/captions/components/CaptionOverlay.tsx‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { memo, useEffect, useState, useCallback, useRef } from "react";
22
import { useLivePreviewIframe } from "../../player/store/previewIframeStore";
33
import { useCaptionStore } from "../store";
44
import { usePlayerStore } from "../../player";
5-
import { shouldHandleCaptionNudgeKey, isEditableEventTarget } from "../keyboard";
5+
import { resolveCaptionNudge, isEditableEventTarget } from "../keyboard";
6+
import { matchesKey } from "../../player/components/studioShortcuts";
67
import {
78
readWordBoxes,
89
getWordEl,
@@ -171,7 +172,7 @@ export const CaptionOverlay = memo(function CaptionOverlay({ iframeRef }: Captio
171172
const { selectedSegmentIds: sel, model: m } = store;
172173

173174
// Escape: cancel an in-flight drag first, else clear the selection.
174-
if (e.key === "Escape") {
175+
if (matchesKey(e, "cancel")) {
175176
if (cancelActiveDrag()) {
176177
e.preventDefault();
177178
scheduleTick();
@@ -185,28 +186,20 @@ export const CaptionOverlay = memo(function CaptionOverlay({ iframeRef }: Captio
185186
}
186187

187188
// ⌘A / Ctrl+A selects every caption word (unless typing in a field).
188-
if (
189-
(e.metaKey || e.ctrlKey) &&
190-
e.key.toLowerCase() === "a" &&
191-
!e.shiftKey &&
192-
!e.altKey &&
193-
!isEditableEventTarget(e.target)
194-
) {
189+
if (matchesKey(e, "captionSelectAll") && !isEditableEventTarget(e.target)) {
195190
e.preventDefault();
196191
store.selectAll();
197192
return;
198193
}
199194

200195
if (sel.size === 0 || !m) return;
201-
const arrow = e.key;
202196
// Pass the event target so arrows inside inputs keep their native
203197
// behavior instead of nudging the canvas word.
204-
if (!shouldHandleCaptionNudgeKey(e, e.target)) return;
198+
const nudge = resolveCaptionNudge(e, e.target);
199+
if (!nudge) return;
205200

206201
e.preventDefault();
207-
const step = e.shiftKey ? 10 : 1;
208-
const dx = arrow === "ArrowLeft" ? -step : arrow === "ArrowRight" ? step : 0;
209-
const dy = arrow === "ArrowUp" ? -step : arrow === "ArrowDown" ? step : 0;
202+
const { dx, dy } = nudge;
210203

211204
const iframe = iframeRef.current;
212205
const win = iframe?.contentWindow;

‎packages/studio/src/captions/keyboard.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @vitest-environment happy-dom
22

3-
import { describe, expect, it } from "vitest";
3+
import { afterEach, describe, expect, it } from "vitest";
4+
import { setStudioKeys } from "../player/components/studioShortcuts";
45
import { shouldHandleCaptionNudgeKey } from "./keyboard";
56

67
function mockKeyboardEvent(
@@ -60,3 +61,13 @@ describe("shouldHandleCaptionNudgeKey", () => {
6061
expect(shouldHandleCaptionNudgeKey(mockKeyboardEvent("ArrowRight"), player)).toBe(false);
6162
});
6263
});
64+
65+
describe("shouldHandleCaptionNudgeKey: remapped", () => {
66+
afterEach(() => setStudioKeys(null));
67+
68+
it("follows the canvas nudge keys", () => {
69+
setStudioKeys({ nudgeUp: "u" });
70+
expect(shouldHandleCaptionNudgeKey(mockKeyboardEvent("ArrowUp"))).toBe(false);
71+
expect(shouldHandleCaptionNudgeKey(mockKeyboardEvent("u"))).toBe(true);
72+
});
73+
});

‎packages/studio/src/captions/keyboard.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { isEditableTarget } from "../utils/timelineDiscovery";
22
import { ownsPlainKeys } from "../utils/typingTarget";
3+
import { resolveCanvasNudgeDelta } from "../components/editor/domEditNudge";
34

4-
const CAPTION_NUDGE_KEYS = new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
5+
type CaptionNudgeKeyEvent = Parameters<typeof resolveCanvasNudgeDelta>[0];
56

6-
type CaptionNudgeKeyEvent = Pick<KeyboardEvent, "altKey" | "ctrlKey" | "metaKey" | "key">;
7+
export function resolveCaptionNudge(event: CaptionNudgeKeyEvent, target?: EventTarget | null) {
8+
if (target != null && ownsPlainKeys(target)) return null;
9+
return resolveCanvasNudgeDelta(event);
10+
}
711

8-
export function shouldHandleCaptionNudgeKey(
12+
export const shouldHandleCaptionNudgeKey = (
913
event: CaptionNudgeKeyEvent,
1014
target?: EventTarget | null,
11-
): boolean {
12-
if (event.metaKey || event.ctrlKey || event.altKey) return false;
13-
if (target != null && ownsPlainKeys(target)) return false;
14-
return CAPTION_NUDGE_KEYS.has(event.key);
15-
}
15+
): boolean => resolveCaptionNudge(event, target) !== null;
1616

1717
// Single editable-target check for the caption surface — re-exported from the
1818
// shared timeline helper so the two never diverge (it also covers ARIA

‎packages/studio/src/components/EditorShell.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { memo, useCallback, type ReactNode } from "react";
2+
import { matchesKey } from "../player/components/studioShortcuts";
23
import { PreviewPane } from "./nle/PreviewPane";
34
import { TimelinePane } from "./nle/TimelinePane";
45
import { PreviewOverlays } from "./nle/PreviewOverlays";
@@ -293,7 +294,7 @@ function EditorShellBody({
293294
// Keyboard: Escape to pop composition level
294295
const handleKeyDown = useCallback(
295296
(e: React.KeyboardEvent) => {
296-
if (e.key === "Escape" && compositionStack.length > 1) {
297+
if (matchesKey(e, "cancel") && compositionStack.length > 1) {
297298
updateCompositionStack((prev) => prev.slice(0, -1));
298299
}
299300
},

‎packages/studio/src/components/TimelineToolbar.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { LinkedSelectionToggle } from "./LinkedSelectionToggle";
4040
import { useProjectHasAudio } from "../utils/audioMeterMath";
4141
import { canAddBeatAt, addBeatAtCompositionTime } from "../utils/beatEditActions";
4242
import { isTypingTarget } from "../utils/typingTarget";
43-
import { getStudioKey, keyHint, useStudioKeys } from "../player/components/studioShortcuts";
43+
import { keyHint, matchesKey, useStudioKeys } from "../player/components/studioShortcuts";
4444
import type { TimelineProps } from "../player/components/TimelineTypes";
4545

4646
interface DomEditSessionSlice extends EnableKeyframesSession {
@@ -213,8 +213,7 @@ export const TimelineToolbar = memo(function TimelineToolbar({
213213
// Skip when typing in an input/contenteditable.
214214
useEffect(() => {
215215
const onKeyDown = (e: KeyboardEvent) => {
216-
if (e.key.toLowerCase() !== getStudioKey("timelineSnap")) return;
217-
if (e.metaKey || e.ctrlKey || e.altKey) return;
216+
if (!matchesKey(e, "timelineSnap")) return;
218217
if (isTypingTarget(e.target)) return;
219218
const store = usePlayerStore.getState();
220219
store.setTimelineSnapEnabled(!store.timelineSnapEnabled);

‎packages/studio/src/components/editor/InspectorHeaderActions.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { useTrackDesignInput } from "../../contexts/DesignPanelInputContext";
44
import type { DomEditSelection } from "./domEditingTypes";
55

66
/** The action buttons in the inspector header: visibility, Ungroup (groups only), copy, clear. */
7+
import { keyHint, useStudioKeys } from "../../player/components/studioShortcuts";
8+
79
export function InspectorHeaderActions({
810
element,
911
copied,
@@ -53,7 +55,7 @@ export function InspectorHeaderActions({
5355
track("button", "Ungroup");
5456
onUngroup();
5557
}}
56-
title="Ungroup (⌘⇧G)"
58+
title={`Ungroup (${keyHint("ungroup", useStudioKeys())})`}
5759
className="flex h-6 items-center rounded-sm px-2 text-[11px] font-medium text-neutral-400 transition-colors hover:bg-neutral-800 hover:text-neutral-200"
5860
>
5961
Ungroup

‎packages/studio/src/components/editor/PropertyPanelFlatHeader.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const ICON_COLOR_BY_KIND = {
99
other: "text-panel-container",
1010
} as const;
1111

12+
import { keyHint, useStudioKeys } from "../../player/components/studioShortcuts";
13+
1214
export function PropertyPanelFlatHeader({
1315
name,
1416
meta,
@@ -54,7 +56,7 @@ export function PropertyPanelFlatHeader({
5456
<button
5557
type="button"
5658
aria-label="Ungroup"
57-
title="Ungroup (⌘⇧G)"
59+
title={`Ungroup (${keyHint("ungroup", useStudioKeys())})`}
5860
onClick={() => {
5961
track("button", "Ungroup");
6062
onUngroup?.();

‎packages/studio/src/components/editor/SnapToolbar.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MagnetStraight, GridFour, Path, Ruler, FrameCorners } from "@phosphor-i
44
import { usePlayerStore } from "../../player/store/playerStore";
55
import { usePreviewOverlayContext } from "./PreviewOverlayProvider";
66
import { ownsPlainKeys } from "../../utils/typingTarget";
7-
import { getStudioKey, keyHint, useStudioKeys } from "../../player/components/studioShortcuts";
7+
import { keyHint, matchesKey, useStudioKeys } from "../../player/components/studioShortcuts";
88

99
// fallow-ignore-next-line complexity
1010
export const SnapToolbar = memo(function SnapToolbar() {
@@ -53,12 +53,12 @@ export const SnapToolbar = memo(function SnapToolbar() {
5353
if (e.defaultPrevented) return;
5454
if (ownsPlainKeys(e.target)) return;
5555
if (e.target instanceof HTMLIFrameElement) return;
56-
if (e.key === getStudioKey("snap") && !e.metaKey && !e.ctrlKey && !e.altKey) {
56+
if (matchesKey(e, "snap")) {
5757
e.preventDefault();
5858
updatePrefs({ snapEnabled: !prefs.snapEnabled });
5959
if (!e.repeat) trackPreviewFeatureUsed("snapping", "keyboard");
6060
}
61-
if (e.key === getStudioKey("grid") && !e.metaKey && !e.ctrlKey && !e.altKey) {
61+
if (matchesKey(e, "grid")) {
6262
e.preventDefault();
6363
updatePrefs({ gridVisible: !prefs.gridVisible });
6464
if (!e.repeat) trackPreviewFeatureUsed("grid", "keyboard");

‎packages/studio/src/components/editor/domEditNudge.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { describe, expect, it } from "vitest";
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { setStudioKeys } from "../../player/components/studioShortcuts";
23
import {
34
CANVAS_NUDGE_SHIFT_STEP_PX,
45
CANVAS_NUDGE_STEP_PX,
@@ -78,3 +79,18 @@ describe("canCanvasNudgeTargets", () => {
7879
expect(canCanvasNudgeTargets([movable, locked])).toBe(false);
7980
});
8081
});
82+
83+
describe("resolveCanvasNudgeDelta: remapped keys", () => {
84+
afterEach(() => setStudioKeys(null));
85+
86+
it("a remapped direction and step key answer and the arrow goes dead", () => {
87+
setStudioKeys({ nudgeLeft: "h", nudgeRightTen: "alt+l" });
88+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowLeft"))).toBeNull();
89+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("h"))).toEqual({ dx: -1, dy: 0 });
90+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("l", { altKey: true }))).toEqual({
91+
dx: 10,
92+
dy: 0,
93+
});
94+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowRight", { shiftKey: true }))).toBeNull();
95+
});
96+
});

‎packages/studio/src/components/editor/domEditNudge.ts‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* mutually exclusively (caption edit mode returns before the DOM overlay).
77
*/
88

9-
const CANVAS_NUDGE_KEYS = new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
9+
import { matchesKey, type StudioKeyAction } from "../../player/components/studioShortcuts";
1010

1111
export const CANVAS_NUDGE_STEP_PX = 1;
1212
export const CANVAS_NUDGE_SHIFT_STEP_PX = 10;
@@ -16,22 +16,25 @@ export const CANVAS_NUDGE_COMMIT_DEBOUNCE_MS = 400;
1616
type CanvasNudgeKeyEvent = Pick<
1717
KeyboardEvent,
1818
"altKey" | "ctrlKey" | "metaKey" | "shiftKey" | "key"
19-
>;
19+
> & { code?: string };
2020

21-
/**
22-
* Arrow key → composition-px delta (Shift = 10). Null when the key is not a
23-
* plain/Shift arrow, so browser and app shortcut chords pass through.
24-
*/
21+
const NUDGES: ReadonlyArray<[StudioKeyAction, number, number, number]> = [
22+
["nudgeLeft", -1, 0, CANVAS_NUDGE_STEP_PX],
23+
["nudgeRight", 1, 0, CANVAS_NUDGE_STEP_PX],
24+
["nudgeUp", 0, -1, CANVAS_NUDGE_STEP_PX],
25+
["nudgeDown", 0, 1, CANVAS_NUDGE_STEP_PX],
26+
["nudgeLeftTen", -1, 0, CANVAS_NUDGE_SHIFT_STEP_PX],
27+
["nudgeRightTen", 1, 0, CANVAS_NUDGE_SHIFT_STEP_PX],
28+
["nudgeUpTen", 0, -1, CANVAS_NUDGE_SHIFT_STEP_PX],
29+
["nudgeDownTen", 0, 1, CANVAS_NUDGE_SHIFT_STEP_PX],
30+
];
31+
32+
/** Nudge key (default: arrows, Shift = 10) to a composition-px delta; null for any other key. */
2533
export function resolveCanvasNudgeDelta(
2634
event: CanvasNudgeKeyEvent,
2735
): { dx: number; dy: number } | null {
28-
if (event.metaKey || event.ctrlKey || event.altKey) return null;
29-
if (!CANVAS_NUDGE_KEYS.has(event.key)) return null;
30-
const step = event.shiftKey ? CANVAS_NUDGE_SHIFT_STEP_PX : CANVAS_NUDGE_STEP_PX;
31-
return {
32-
dx: event.key === "ArrowLeft" ? -step : event.key === "ArrowRight" ? step : 0,
33-
dy: event.key === "ArrowUp" ? -step : event.key === "ArrowDown" ? step : 0,
34-
};
36+
const hit = NUDGES.find(([action]) => matchesKey(event, action));
37+
return hit ? { dx: hit[1] * hit[3], dy: hit[2] * hit[3] } : null;
3538
}
3639

3740
export interface CanvasNudgeTarget {

0 commit comments

Comments
 (0)