Skip to content

Commit 5fca435

Browse files
feat(studio): hosts can ask which action holds a key combination (#6979)
* feat(studio): hosts can ask which Studio action holds a key combination * fix(studio): the clash check skips a key map entry that is not a combo * refactor(studio): studioKeyClash in fewer lines
1 parent e5b10f8 commit 5fca435

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎packages/studio/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ export {
1111
buildShortcutSections,
1212
getStudioKeys,
1313
setStudioKeys,
14+
studioKeyClash,
1415
useStudioKeys,
1516
} from "./player/components/studioShortcuts";
17+
export { formatCombo, matchCombo, parseCombo } from "./player/components/studioCombo";
18+
export type { Combo } from "./player/components/studioCombo";
1619
export type { StudioKeyAction, StudioKeys } from "./player/components/studioShortcuts";
1720
export { ShortcutsButton } from "./player/components/ShortcutsPanel";
1821
export { dispatchLinkShortcut } from "./hooks/linkShortcuts";

‎packages/studio/src/player/components/studioCombo.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// @vitest-environment happy-dom
22
import { afterEach, describe, expect, it, vi } from "vitest";
33
import { formatCombo, matchCombo, parseCombo } from "./studioCombo";
4-
import { getStudioKey, keyHint, matchesKey, setStudioKeys } from "./studioShortcuts";
4+
import {
5+
getStudioKey,
6+
getStudioKeys,
7+
keyHint,
8+
matchesKey,
9+
setStudioKeys,
10+
studioKeyClash,
11+
} from "./studioShortcuts";
512

613
afterEach(() => {
714
setStudioKeys(null);
815
vi.restoreAllMocks();
916
});
1017

18+
const getAll = () => ({ ...getStudioKeys() });
1119
const press = (init: KeyboardEventInit) => new KeyboardEvent("keydown", init);
1220
const matches = (text: string, init: KeyboardEventInit) =>
1321
matchCombo(press(init), parseCombo(text)!);
@@ -97,3 +105,15 @@ describe("combos with symbol keys", () => {
97105
expect(parseCombo("shift+1")).toBeNull();
98106
});
99107
});
108+
109+
describe("studioKeyClash", () => {
110+
it("names the action that holds a combo, by modifiers and key, and lets a design-shared pair through", () => {
111+
expect(studioKeyClash("mute", "mod+z")).toBe("undo");
112+
expect(studioKeyClash("mute", "mod+shift+z")).toBe("redo");
113+
expect(studioKeyClash("mute", "q")).toBeNull();
114+
expect(studioKeyClash("pause", "k")).toBeNull();
115+
expect(studioKeyClash("mute", "b")).toBe("razorTool");
116+
expect(studioKeyClash("mute", "mod+z", { ...getAll(), undo: "q" })).toBeNull();
117+
expect(studioKeyClash("mute", "q", { ...getAll(), undo: "" })).toBeNull();
118+
});
119+
});

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ function findClash(keys: StudioKeys): string | null {
110110
return null;
111111
}
112112

113+
export function studioKeyClash(
114+
action: StudioKeyAction,
115+
text: string,
116+
keys: StudioKeys = current,
117+
): StudioKeyAction | null {
118+
const wanted = parseCombo(text);
119+
const holds = (other: StudioKeyAction) => {
120+
const held = parseCombo(keys[other]);
121+
return !!held && !!wanted && comboClashKey(held) === comboClashKey(wanted);
122+
};
123+
const keyed = Object.keys(keys) as StudioKeyAction[];
124+
return keyed.find((o) => o !== action && !sharedByDesign(action, o) && holds(o)) ?? null;
125+
}
126+
113127
function resolve(overrides: Partial<Record<StudioKeyAction, string>>): StudioKeys {
114128
const next: Record<string, string> = { ...DEFAULT_STUDIO_KEYS };
115129
for (const [action, text] of Object.entries(overrides)) {

0 commit comments

Comments
 (0)