Only support a single key in the KeyCombo

Keep it simple...
pull/21833/head
Clemens Zeidler 2021-02-15 19:21:08 +13:00
parent b4c5dec4e5
commit 4a138f3b84
2 changed files with 17 additions and 18 deletions

View File

@ -20,8 +20,7 @@ export enum KeyAction {
* The combo is evaluated strictly, i.e. the KeyboardEvent must match exactly what is specified in the KeyCombo. * The combo is evaluated strictly, i.e. the KeyboardEvent must match exactly what is specified in the KeyCombo.
*/ */
export type KeyCombo = { export type KeyCombo = {
/** Currently only one `normal` key is supported */ key?: string;
keys: string[];
/** On PC: ctrl is pressed; on Mac: meta is pressed */ /** On PC: ctrl is pressed; on Mac: meta is pressed */
ctrlOrCmd?: boolean; ctrlOrCmd?: boolean;
@ -42,7 +41,7 @@ const messageComposerBindings = (): KeyBinding[] => {
{ {
action: KeyAction.SelectPrevSendHistory, action: KeyAction.SelectPrevSendHistory,
keyCombo: { keyCombo: {
keys: [Key.ARROW_UP], key: Key.ARROW_UP,
altKey: true, altKey: true,
ctrlKey: true, ctrlKey: true,
}, },
@ -50,7 +49,7 @@ const messageComposerBindings = (): KeyBinding[] => {
{ {
action: KeyAction.SelectNextSendHistory, action: KeyAction.SelectNextSendHistory,
keyCombo: { keyCombo: {
keys: [Key.ARROW_DOWN], key: Key.ARROW_DOWN,
altKey: true, altKey: true,
ctrlKey: true, ctrlKey: true,
}, },
@ -58,7 +57,7 @@ const messageComposerBindings = (): KeyBinding[] => {
{ {
action: KeyAction.EditLastMessage, action: KeyAction.EditLastMessage,
keyCombo: { keyCombo: {
keys: [Key.ARROW_UP], key: Key.ARROW_UP,
} }
}, },
]; ];
@ -66,7 +65,7 @@ const messageComposerBindings = (): KeyBinding[] => {
bindings.push({ bindings.push({
action: KeyAction.Send, action: KeyAction.Send,
keyCombo: { keyCombo: {
keys: [Key.ENTER], key: Key.ENTER,
ctrlOrCmd: true, ctrlOrCmd: true,
}, },
}); });
@ -74,7 +73,7 @@ const messageComposerBindings = (): KeyBinding[] => {
bindings.push({ bindings.push({
action: KeyAction.Send, action: KeyAction.Send,
keyCombo: { keyCombo: {
keys: [Key.ENTER], key: Key.ENTER,
}, },
}); });
} }
@ -88,7 +87,7 @@ const messageComposerBindings = (): KeyBinding[] => {
* Note, this method is only exported for testing. * Note, this method is only exported for testing.
*/ */
export function isKeyComboMatch(ev: KeyboardEvent, combo: KeyCombo, onMac: boolean): boolean { export function isKeyComboMatch(ev: KeyboardEvent, combo: KeyCombo, onMac: boolean): boolean {
if (combo.keys.length > 0 && ev.key !== combo.keys[0]) { if (combo.key !== undefined && ev.key !== combo.key) {
return false; return false;
} }

View File

@ -19,7 +19,7 @@ function mockKeyEvent(key: string, modifiers?: {
describe('KeyBindingsManager', () => { describe('KeyBindingsManager', () => {
it('should match basic key combo', () => { it('should match basic key combo', () => {
const combo1: KeyCombo = { const combo1: KeyCombo = {
keys: ['k'], key: 'k',
}; };
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k'), combo1, false), true); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k'), combo1, false), true);
assert.strictEqual(isKeyComboMatch(mockKeyEvent('n'), combo1, false), false); assert.strictEqual(isKeyComboMatch(mockKeyEvent('n'), combo1, false), false);
@ -28,7 +28,7 @@ describe('KeyBindingsManager', () => {
it('should match key + modifier key combo', () => { it('should match key + modifier key combo', () => {
const combo: KeyCombo = { const combo: KeyCombo = {
keys: ['k'], key: 'k',
ctrlKey: true, ctrlKey: true,
}; };
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { ctrlKey: true }), combo, false), true); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { ctrlKey: true }), combo, false), true);
@ -38,7 +38,7 @@ describe('KeyBindingsManager', () => {
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { shiftKey: true, metaKey: true }), combo, false), false); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { shiftKey: true, metaKey: true }), combo, false), false);
const combo2: KeyCombo = { const combo2: KeyCombo = {
keys: ['k'], key: 'k',
metaKey: true, metaKey: true,
}; };
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { metaKey: true }), combo2, false), true); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { metaKey: true }), combo2, false), true);
@ -47,7 +47,7 @@ describe('KeyBindingsManager', () => {
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { altKey: true, metaKey: true }), combo2, false), false); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { altKey: true, metaKey: true }), combo2, false), false);
const combo3: KeyCombo = { const combo3: KeyCombo = {
keys: ['k'], key: 'k',
altKey: true, altKey: true,
}; };
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { altKey: true }), combo3, false), true); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { altKey: true }), combo3, false), true);
@ -56,7 +56,7 @@ describe('KeyBindingsManager', () => {
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { ctrlKey: true, metaKey: true }), combo3, false), false); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { ctrlKey: true, metaKey: true }), combo3, false), false);
const combo4: KeyCombo = { const combo4: KeyCombo = {
keys: ['k'], key: 'k',
shiftKey: true, shiftKey: true,
}; };
assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { shiftKey: true }), combo4, false), true); assert.strictEqual(isKeyComboMatch(mockKeyEvent('k', { shiftKey: true }), combo4, false), true);
@ -67,7 +67,7 @@ describe('KeyBindingsManager', () => {
it('should match key + multiple modifiers key combo', () => { it('should match key + multiple modifiers key combo', () => {
const combo: KeyCombo = { const combo: KeyCombo = {
keys: ['k'], key: 'k',
ctrlKey: true, ctrlKey: true,
altKey: true, altKey: true,
}; };
@ -78,7 +78,7 @@ describe('KeyBindingsManager', () => {
false), false); false), false);
const combo2: KeyCombo = { const combo2: KeyCombo = {
keys: ['k'], key: 'k',
ctrlKey: true, ctrlKey: true,
shiftKey: true, shiftKey: true,
altKey: true, altKey: true,
@ -92,7 +92,7 @@ describe('KeyBindingsManager', () => {
{ ctrlKey: true, shiftKey: true, altKey: true, metaKey: true }), combo2, false), false); { ctrlKey: true, shiftKey: true, altKey: true, metaKey: true }), combo2, false), false);
const combo3: KeyCombo = { const combo3: KeyCombo = {
keys: ['k'], key: 'k',
ctrlKey: true, ctrlKey: true,
shiftKey: true, shiftKey: true,
altKey: true, altKey: true,
@ -108,7 +108,7 @@ describe('KeyBindingsManager', () => {
it('should match ctrlOrMeta key combo', () => { it('should match ctrlOrMeta key combo', () => {
const combo: KeyCombo = { const combo: KeyCombo = {
keys: ['k'], key: 'k',
ctrlOrCmd: true, ctrlOrCmd: true,
}; };
// PC: // PC:
@ -123,7 +123,7 @@ describe('KeyBindingsManager', () => {
it('should match advanced ctrlOrMeta key combo', () => { it('should match advanced ctrlOrMeta key combo', () => {
const combo: KeyCombo = { const combo: KeyCombo = {
keys: ['k'], key: 'k',
ctrlOrCmd: true, ctrlOrCmd: true,
altKey: true, altKey: true,
}; };