Skip to content

Commit 52d7822

Browse files
authored
[v5] [core] fix(HotkeyParser): restore support for 'space' in combos (#6139)
1 parent cfdcbd3 commit 52d7822

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

packages/core/src/components/hotkeys/hotkeyParser.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ export const CONFIG_ALIASES: KeyMap = {
5353
plus: "+",
5454
return: "enter",
5555
win: "meta",
56-
// need these direction aliases for backwards-compatibility (but they're also convenient)
56+
// need these aliases for backwards-compatibility (but they're also convenient)
5757
up: "ArrowUp",
5858
left: "ArrowLeft",
5959
down: "ArrowDown",
6060
right: "ArrowRight",
61+
space: " ",
6162
};
6263

6364
export const SHIFT_KEYS: KeyMap = {
@@ -148,8 +149,15 @@ export const getKeyComboString = (e: KeyboardEvent): string => {
148149
comboParts.push("meta");
149150
}
150151

151-
if (e.key !== undefined && !MODIFIER_KEYS.has(e.key)) {
152-
comboParts.push(e.key.toLowerCase());
152+
if (e.key !== undefined) {
153+
if (e.key === " ") {
154+
// special case for "space" key, which would otherwise be printed as illegible whitespace
155+
comboParts.push("space");
156+
} else if (MODIFIER_KEYS.has(e.key)) {
157+
// do nothing
158+
} else {
159+
comboParts.push(e.key.toLowerCase());
160+
}
153161
}
154162

155163
return comboParts.join(" + ");

packages/core/test/hotkeys/hotkeysParserTests.ts

+18-12
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ describe("HotkeysParser", () => {
3434
parsedKeyCombo: KeyCombo;
3535
}
3636

37-
const makeComboTest = (combo: string, event: KeyboardEvent) => {
37+
const makeComboTest = (combo: string, event: Partial<KeyboardEvent>) => {
3838
return {
3939
combo,
40-
eventKeyCombo: getKeyCombo(event),
40+
eventKeyCombo: getKeyCombo(event as KeyboardEvent),
4141
parsedKeyCombo: parseKeyCombo(combo),
42-
stringKeyCombo: getKeyComboString(event),
42+
stringKeyCombo: getKeyComboString(event as KeyboardEvent),
4343
};
4444
};
4545

@@ -58,8 +58,7 @@ describe("HotkeysParser", () => {
5858
Array.apply(null, Array(26)).map((_: any, i: number) => {
5959
const charString = String.fromCharCode(alpha + i).toLowerCase();
6060
const combo = charString;
61-
const event: KeyboardEvent = { key: charString } as any;
62-
return makeComboTest(combo, event);
61+
return makeComboTest(combo, { key: charString });
6362
}),
6463
);
6564
});
@@ -70,8 +69,7 @@ describe("HotkeysParser", () => {
7069
Array.apply(null, Array(26)).map((_: any, i: number) => {
7170
const charString = String.fromCharCode(alpha + i).toLowerCase();
7271
const combo = charString.toUpperCase();
73-
const event: KeyboardEvent = { key: charString } as any;
74-
return makeComboTest(combo, event);
72+
return makeComboTest(combo, { key: charString });
7573
}),
7674
false,
7775
); // don't compare string combos
@@ -83,8 +81,7 @@ describe("HotkeysParser", () => {
8381
Array.apply(null, Array(26)).map((_: any, i: number) => {
8482
const charString = String.fromCharCode(alpha + i).toLowerCase();
8583
const combo = "shift + " + charString;
86-
const event: KeyboardEvent = { shiftKey: true, key: charString } as any;
87-
return makeComboTest(combo, event);
84+
return makeComboTest(combo, { shiftKey: true, key: charString });
8885
}),
8986
);
9087
});
@@ -111,9 +108,9 @@ describe("HotkeysParser", () => {
111108
// these tests no longer make sense with the migration from key codes to named keys, they can likely be deleted
112109
it.skip("adds Shift to keys that imply it", () => {
113110
const tests = [] as ComboTest[];
114-
tests.push(makeComboTest("!", { shiftKey: true, key: "!" } as any as KeyboardEvent));
115-
tests.push(makeComboTest("@", { shiftKey: true, key: "@" } as any as KeyboardEvent));
116-
tests.push(makeComboTest("{", { shiftKey: true, key: "{" } as any as KeyboardEvent));
111+
tests.push(makeComboTest("!", { shiftKey: true, key: "!" }));
112+
tests.push(makeComboTest("@", { shiftKey: true, key: "@" }));
113+
tests.push(makeComboTest("{", { shiftKey: true, key: "{" }));
117114
// don't verify the strings because these will be converted to
118115
// `Shift + 1`, etc.
119116
verifyCombos(tests, false);
@@ -125,6 +122,15 @@ describe("HotkeysParser", () => {
125122
expect(comboMatches(parseKeyCombo("cmd + plus"), parseKeyCombo("meta + plus"))).to.be.true;
126123
});
127124

125+
it("handles space key", () => {
126+
const tests = [] as ComboTest[];
127+
tests.push(
128+
makeComboTest("space", { key: " " }),
129+
makeComboTest("ctrl + space", { ctrlKey: true, key: " " }),
130+
);
131+
verifyCombos(tests);
132+
});
133+
128134
it("applies aliases", () => {
129135
expect(comboMatches(parseKeyCombo("return"), parseKeyCombo("enter"))).to.be.true;
130136

0 commit comments

Comments
 (0)