Skip to content

Commit 881481f

Browse files
feat(studio): an embedder can change what the shortcuts panel lists (#4416)
* feat(studio): an embedder can change what the shortcuts panel lists The panel read a private list, so an app that embeds PlayerControls could not drop a key Studio binds but it does not (bare S split). The list is now exported as DEFAULT_SHORTCUT_SECTIONS, PlayerControls takes shortcutSections, and the dispatcher names its bare F, S and R keys from the same module. Defaults unchanged. * fix(studio): an embedder's shortcuts list may repeat a key or a section title Rows were keyed by their key text and sections by title, so a relabelled list with a repeat hit React duplicate keys. The list is static, so rows and sections are keyed by position.
1 parent 6c9216e commit 881481f

6 files changed

Lines changed: 175 additions & 93 deletions

File tree

‎packages/studio/src/hooks/appHotkeysDispatch.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isEditableTarget } from "../utils/timelineDiscovery";
88
import { shouldIgnoreHistoryShortcut } from "../utils/studioHelpers";
99
import { canSplitElement } from "../utils/timelineElementSplit";
1010
import { trackStudioEvent } from "../utils/studioTelemetry";
11+
import { STUDIO_PLAIN_KEYS } from "../player/components/studioShortcuts";
1112

1213
// Extracted from useAppHotkeys.ts to keep it under the studio 600-line cap,
1314
// following useTimelineDeleteOps's precedent. Pure functions, no hooks — the
@@ -158,15 +159,15 @@ export function dispatchModifierKey(
158159
* Delete arbitration between keyframes, an automation range and the clip can
159160
* be asserted without standing up the whole hook. */
160161
export function dispatchPlainKey(event: KeyboardEvent, key: string, cb: HotkeyCallbacks): void {
161-
if (key === "f" && !event.shiftKey && !event.altKey) {
162+
if (key === STUDIO_PLAIN_KEYS.fullscreen && !event.shiftKey && !event.altKey) {
162163
event.preventDefault();
163164
if (document.fullscreenElement) void document.exitFullscreen();
164165
else
165166
document.querySelector<HTMLElement>("[data-studio-fullscreen-target]")?.requestFullscreen();
166167
return;
167168
}
168169

169-
if (event.key === "s" && !event.altKey) {
170+
if (event.key === STUDIO_PLAIN_KEYS.split && !event.altKey) {
170171
// Reserve bare `s` for Split even when the current selection cannot split,
171172
// so secondary listeners do not reinterpret the same key as Snap toggle.
172173
event.preventDefault();
@@ -274,7 +275,12 @@ export function dispatchPlainKey(event: KeyboardEvent, key: string, cb: HotkeyCa
274275
return;
275276
}
276277

277-
if (event.key === "r" && !event.shiftKey && !event.altKey && cb.onToggleRecording) {
278+
if (
279+
event.key === STUDIO_PLAIN_KEYS.record &&
280+
!event.shiftKey &&
281+
!event.altKey &&
282+
cb.onToggleRecording
283+
) {
278284
event.preventDefault();
279285
cb.onToggleRecording();
280286
}

‎packages/studio/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export type { ToggleProps } from "./components/ui/Toggle";
3535
export { EditorShell } from "./components/EditorShell";
3636
export type { EditorShellProps } from "./components/EditorShell";
3737
export { NLEPreview } from "./components/nle/NLEPreview";
38+
export { DEFAULT_SHORTCUT_SECTIONS } from "./player/components/studioShortcuts";
39+
export type { ShortcutHint, ShortcutSection } from "./player/components/studioShortcuts";
3840
export { CompositionBreadcrumb } from "./components/nle/CompositionBreadcrumb";
3941
export type { CompositionLevel } from "./components/nle/CompositionBreadcrumb";
4042

‎packages/studio/src/player/components/PlayerControls.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { trackStudioEvent } from "../../utils/studioTelemetry";
77
import { Tooltip } from "../../components/ui";
88
import { useMountEffect } from "../../hooks/useMountEffect";
99
import { ShortcutsPanel } from "./ShortcutsPanel";
10+
import type { ShortcutSection } from "./studioShortcuts";
1011
import { SpeedMenu } from "./SpeedMenu";
1112
import { VolumeControl } from "./VolumeControl";
1213

@@ -161,6 +162,8 @@ interface PlayerControlsProps {
161162
disabled?: boolean;
162163
isFullscreen?: boolean;
163164
onToggleFullscreen?: () => void;
165+
/** Replaces the shortcuts panel's list, e.g. DEFAULT_SHORTCUT_SECTIONS with entries dropped or relabelled. */
166+
shortcutSections?: readonly ShortcutSection[];
164167
}
165168

166169
export const PlayerControls = memo(function PlayerControls({
@@ -169,6 +172,7 @@ export const PlayerControls = memo(function PlayerControls({
169172
disabled = false,
170173
isFullscreen = false,
171174
onToggleFullscreen,
175+
shortcutSections,
172176
}: PlayerControlsProps) {
173177
const isPlaying = usePlayerStore((s) => s.isPlaying);
174178
const duration = usePlayerStore((s) => s.duration);
@@ -291,6 +295,7 @@ export const PlayerControls = memo(function PlayerControls({
291295
setInPoint={setInPoint}
292296
setOutPoint={setOutPoint}
293297
onSeek={onSeek}
298+
sections={shortcutSections}
294299
/>
295300
</div>
296301
</div>

‎packages/studio/src/player/components/ShortcutsPanel.test.tsx‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import React, { act, Profiler } from "react";
44
import { describe, expect, it, vi } from "vitest";
55
import { ShortcutsPanel } from "./ShortcutsPanel";
6+
import { PlayerControls } from "./PlayerControls";
7+
import { DEFAULT_SHORTCUT_SECTIONS, type ShortcutSection } from "./studioShortcuts";
68
import { createHappyDomRootHarness } from "./testRootHarness";
79

810
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -149,4 +151,53 @@ describe("ShortcutsPanel", () => {
149151
expect(panel?.getAttribute("aria-modal")).toBeNull();
150152
expect(panel?.id).toBe(panelId);
151153
});
154+
155+
it("lists what an embedder passes to PlayerControls instead of Studio's defaults", () => {
156+
const listed = (sections?: readonly ShortcutSection[]) => {
157+
const host = document.createElement("div");
158+
document.body.append(host);
159+
act(() => {
160+
mount(host).render(
161+
<PlayerControls onTogglePlay={vi.fn()} onSeek={vi.fn()} shortcutSections={sections} />,
162+
);
163+
});
164+
openPanel(host.querySelector<HTMLButtonElement>('button[aria-label="Shortcuts and tools"]')!);
165+
return host.textContent ?? "";
166+
};
167+
const withoutSplit = DEFAULT_SHORTCUT_SECTIONS.map((section) => ({
168+
...section,
169+
hints: section.hints.filter((hint) => hint.label !== "Split clip at playhead"),
170+
}));
171+
172+
expect(listed()).toContain("Split clip at playhead");
173+
expect(listed(withoutSplit)).not.toContain("Split clip at playhead");
174+
expect(listed(withoutSplit)).toContain("Toggle fullscreen");
175+
});
176+
177+
it("renders an embedder list that repeats a key or a section title without key clashes", () => {
178+
const errors = vi.spyOn(console, "error").mockImplementation(() => {});
179+
const host = document.createElement("div");
180+
document.body.append(host);
181+
const sections = [
182+
{
183+
title: "Keys",
184+
hints: [
185+
{ key: "S", label: "Select" },
186+
{ key: "S", label: "Snap" },
187+
],
188+
},
189+
{ title: "Keys", hints: [{ key: "V", label: "Move" }] },
190+
];
191+
act(() => {
192+
mount(host).render(
193+
<PlayerControls onTogglePlay={vi.fn()} onSeek={vi.fn()} shortcutSections={sections} />,
194+
);
195+
});
196+
openPanel(host.querySelector<HTMLButtonElement>('button[aria-label="Shortcuts and tools"]')!);
197+
198+
expect(host.textContent).toContain("Select");
199+
expect(host.textContent).toContain("Snap");
200+
expect(errors.mock.calls.flat().join(" ")).not.toMatch(/same key/);
201+
errors.mockRestore();
202+
});
152203
});

‎packages/studio/src/player/components/ShortcutsPanel.tsx‎

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,7 @@ import { useState, useCallback, useEffect, useId, useRef, memo } from "react";
22
import { formatTime, frameToSeconds } from "../lib/time";
33
import { Tooltip } from "../../components/ui";
44
import { useContextMenuDismiss } from "../../hooks/useContextMenuDismiss";
5-
6-
const SHORTCUT_SECTIONS = [
7-
{
8-
title: "Playback",
9-
hints: [
10-
{ key: "Space", label: "Play / Pause" },
11-
{ key: "J", label: "Play backward" },
12-
{ key: "K", label: "Stop" },
13-
{ key: "L", label: "Play forward" },
14-
{ key: "M", label: "Toggle mute" },
15-
{ key: "⇧L", label: "Toggle loop" },
16-
{ key: "←/→", label: "Step 1 frame" },
17-
{ key: "⇧←/⇧→", label: "Step 10 frames" },
18-
{ key: "F", label: "Toggle fullscreen" },
19-
],
20-
},
21-
{
22-
title: "Keyframes (when an element is selected)",
23-
hints: [
24-
{ key: "K", label: "Add keyframe at playhead" },
25-
{ key: "Del", label: "Delete selected keyframe" },
26-
{ key: "H", label: "Toggle hold / bezier" },
27-
{ key: "U", label: "Expand / collapse properties" },
28-
{ key: "R", label: "Record gesture" },
29-
],
30-
},
31-
{
32-
title: "Editing",
33-
hints: [
34-
{ key: "⌘Z", label: "Undo" },
35-
{ key: "⌘⇧Z", label: "Redo" },
36-
{ key: "⌘C", label: "Copy element" },
37-
{ key: "⌘V", label: "Paste element" },
38-
{ key: "⌘X", label: "Cut element" },
39-
{ key: "S", label: "Split clip at playhead" },
40-
{ key: "⇧Click", label: "Razor tool: split all tracks" },
41-
{ key: "⌘G", label: "Group elements" },
42-
{ key: "⌘⇧G", label: "Ungroup" },
43-
{ key: "Del", label: "Delete selected element (no keyframe selected)" },
44-
],
45-
},
46-
{
47-
title: "Gesture recording modifiers",
48-
hints: [
49-
{ key: "Drag", label: "Record x / y position" },
50-
{ key: "Scroll", label: "Record z depth" },
51-
{ key: "⇧ Drag", label: "Record rotationX / rotationY" },
52-
{ key: "⌥ Drag", label: "Record rotation" },
53-
{ key: "⌘ Drag↕", label: "Record opacity" },
54-
{ key: "⌘ Scroll", label: "Record scale" },
55-
],
56-
},
57-
{
58-
title: "Canvas",
59-
hints: [
60-
{ key: "Drag", label: "Move element / add keyframe" },
61-
{ key: "⌥ Drag", label: "Move entire animation path" },
62-
{ key: "⇧ Drag", label: "Uniform resize" },
63-
],
64-
},
65-
{
66-
title: "Crop",
67-
hints: [
68-
{ key: "Drag edge", label: "Crop a side" },
69-
{ key: "Drag center", label: "Reposition the crop" },
70-
],
71-
},
72-
{
73-
title: "Panels",
74-
hints: [
75-
{ key: "⌘1", label: "Compositions tab" },
76-
{ key: "⌘2", label: "Assets tab" },
77-
],
78-
},
79-
{
80-
title: "Work area",
81-
hints: [
82-
{ key: "I", label: "Set in-point" },
83-
{ key: "⇧I", label: "Clear in-point" },
84-
{ key: "O", label: "Set out-point" },
85-
{ key: "⇧O", label: "Clear out-point" },
86-
{ key: "A", label: "Jump to in-point" },
87-
{ key: "E", label: "Jump to out-point" },
88-
],
89-
},
90-
] as const;
5+
import { DEFAULT_SHORTCUT_SECTIONS, type ShortcutSection } from "./studioShortcuts";
916

927
interface ShortcutsPanelProps {
938
disabled: boolean;
@@ -97,6 +12,7 @@ interface ShortcutsPanelProps {
9712
setInPoint: (v: number | null) => void;
9813
setOutPoint: (v: number | null) => void;
9914
onSeek: (time: number) => void;
15+
sections?: readonly ShortcutSection[];
10016
}
10117

10218
export const ShortcutsPanel = memo(function ShortcutsPanel({
@@ -107,6 +23,7 @@ export const ShortcutsPanel = memo(function ShortcutsPanel({
10723
setInPoint,
10824
setOutPoint,
10925
onSeek,
26+
sections = DEFAULT_SHORTCUT_SECTIONS,
11027
}: ShortcutsPanelProps) {
11128
const [showShortcuts, setShowShortcuts] = useState(false);
11229
const [jumpFrame, setJumpFrame] = useState("");
@@ -319,14 +236,14 @@ export const ShortcutsPanel = memo(function ShortcutsPanel({
319236
</div>
320237
<div style={{ borderTop: "1px solid rgba(255,255,255,0.06)" }} />
321238
<div className="px-3 pt-2.5 pb-3 flex flex-col gap-3">
322-
{SHORTCUT_SECTIONS.map((section) => (
323-
<div key={section.title}>
239+
{sections.map((section, sectionIndex) => (
240+
<div key={sectionIndex}>
324241
<p className="text-[9px] font-medium text-neutral-500 uppercase tracking-wider mb-1.5">
325242
{section.title}
326243
</p>
327244
<div className="flex flex-col gap-1">
328-
{section.hints.map((hint) => (
329-
<div key={hint.key} className="flex items-center gap-3">
245+
{section.hints.map((hint, hintIndex) => (
246+
<div key={hintIndex} className="flex items-center gap-3">
330247
<span
331248
className="font-mono text-[10px] rounded-sm border border-neutral-700 px-1.5 py-0.5 text-neutral-300 min-w-[36px] text-center"
332249
style={{ background: "rgba(255,255,255,0.05)" }}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/** Bare keys Studio's app hotkeys bind; the shortcuts list names them from here. */
2+
export const STUDIO_PLAIN_KEYS = { fullscreen: "f", split: "s", record: "r" } as const;
3+
4+
export interface ShortcutHint {
5+
key: string;
6+
label: string;
7+
}
8+
9+
export interface ShortcutSection {
10+
title: string;
11+
hints: readonly ShortcutHint[];
12+
}
13+
14+
const hintKey = (key: string) => key.toUpperCase();
15+
16+
/** What PlayerControls' shortcuts panel lists unless an embedder passes its own sections. */
17+
export const DEFAULT_SHORTCUT_SECTIONS: readonly ShortcutSection[] = [
18+
{
19+
title: "Playback",
20+
hints: [
21+
{ key: "Space", label: "Play / Pause" },
22+
{ key: "J", label: "Play backward" },
23+
{ key: "K", label: "Stop" },
24+
{ key: "L", label: "Play forward" },
25+
{ key: "M", label: "Toggle mute" },
26+
{ key: "⇧L", label: "Toggle loop" },
27+
{ key: "←/→", label: "Step 1 frame" },
28+
{ key: "⇧←/⇧→", label: "Step 10 frames" },
29+
{ key: hintKey(STUDIO_PLAIN_KEYS.fullscreen), label: "Toggle fullscreen" },
30+
],
31+
},
32+
{
33+
title: "Keyframes (when an element is selected)",
34+
hints: [
35+
{ key: "K", label: "Add keyframe at playhead" },
36+
{ key: "Del", label: "Delete selected keyframe" },
37+
{ key: "H", label: "Toggle hold / bezier" },
38+
{ key: "U", label: "Expand / collapse properties" },
39+
{ key: hintKey(STUDIO_PLAIN_KEYS.record), label: "Record gesture" },
40+
],
41+
},
42+
{
43+
title: "Editing",
44+
hints: [
45+
{ key: "⌘Z", label: "Undo" },
46+
{ key: "⌘⇧Z", label: "Redo" },
47+
{ key: "⌘C", label: "Copy element" },
48+
{ key: "⌘V", label: "Paste element" },
49+
{ key: "⌘X", label: "Cut element" },
50+
{ key: hintKey(STUDIO_PLAIN_KEYS.split), label: "Split clip at playhead" },
51+
{ key: "⇧Click", label: "Razor tool: split all tracks" },
52+
{ key: "⌘G", label: "Group elements" },
53+
{ key: "⌘⇧G", label: "Ungroup" },
54+
{ key: "Del", label: "Delete selected element (no keyframe selected)" },
55+
],
56+
},
57+
{
58+
title: "Gesture recording modifiers",
59+
hints: [
60+
{ key: "Drag", label: "Record x / y position" },
61+
{ key: "Scroll", label: "Record z depth" },
62+
{ key: "⇧ Drag", label: "Record rotationX / rotationY" },
63+
{ key: "⌥ Drag", label: "Record rotation" },
64+
{ key: "⌘ Drag↕", label: "Record opacity" },
65+
{ key: "⌘ Scroll", label: "Record scale" },
66+
],
67+
},
68+
{
69+
title: "Canvas",
70+
hints: [
71+
{ key: "Drag", label: "Move element / add keyframe" },
72+
{ key: "⌥ Drag", label: "Move entire animation path" },
73+
{ key: "⇧ Drag", label: "Uniform resize" },
74+
],
75+
},
76+
{
77+
title: "Crop",
78+
hints: [
79+
{ key: "Drag edge", label: "Crop a side" },
80+
{ key: "Drag center", label: "Reposition the crop" },
81+
],
82+
},
83+
{
84+
title: "Panels",
85+
hints: [
86+
{ key: "⌘1", label: "Compositions tab" },
87+
{ key: "⌘2", label: "Assets tab" },
88+
],
89+
},
90+
{
91+
title: "Work area",
92+
hints: [
93+
{ key: "I", label: "Set in-point" },
94+
{ key: "⇧I", label: "Clear in-point" },
95+
{ key: "O", label: "Set out-point" },
96+
{ key: "⇧O", label: "Clear out-point" },
97+
{ key: "A", label: "Jump to in-point" },
98+
{ key: "E", label: "Jump to out-point" },
99+
],
100+
},
101+
];

0 commit comments

Comments
 (0)