2021-02-14 10:56:55 +08:00
|
|
|
import { isMac, Key } from './Keyboard';
|
|
|
|
import SettingsStore from './settings/SettingsStore';
|
2021-02-11 17:18:10 +08:00
|
|
|
|
|
|
|
export enum KeyBindingContext {
|
2021-02-16 14:05:39 +08:00
|
|
|
/** Key bindings for the chat message composer component */
|
|
|
|
MessageComposer = 'MessageComposer',
|
2021-02-11 17:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export enum KeyAction {
|
|
|
|
None = 'None',
|
2021-02-16 14:05:39 +08:00
|
|
|
|
2021-02-14 10:56:55 +08:00
|
|
|
// SendMessageComposer actions:
|
2021-02-16 14:05:39 +08:00
|
|
|
|
|
|
|
/** Send a message */
|
2021-02-14 10:56:55 +08:00
|
|
|
Send = 'Send',
|
2021-02-16 14:05:39 +08:00
|
|
|
/** Go backwards through the send history and use the message in composer view */
|
2021-02-14 10:56:55 +08:00
|
|
|
SelectPrevSendHistory = 'SelectPrevSendHistory',
|
2021-02-16 14:05:39 +08:00
|
|
|
/** Go forwards through the send history */
|
2021-02-14 10:56:55 +08:00
|
|
|
SelectNextSendHistory = 'SelectNextSendHistory',
|
2021-02-16 14:05:39 +08:00
|
|
|
/** Start editing the user's last sent message */
|
|
|
|
EditPrevMessage = 'EditPrevMessage',
|
|
|
|
/** Start editing the user's next sent message */
|
|
|
|
EditNextMessage = 'EditNextMessage',
|
|
|
|
|
|
|
|
/** Cancel editing a message */
|
|
|
|
CancelEditing = 'CancelEditing',
|
2021-02-11 17:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represent a key combination.
|
2021-02-16 14:05:39 +08:00
|
|
|
*
|
2021-02-14 10:56:55 +08:00
|
|
|
* The combo is evaluated strictly, i.e. the KeyboardEvent must match exactly what is specified in the KeyCombo.
|
2021-02-11 17:18:10 +08:00
|
|
|
*/
|
|
|
|
export type KeyCombo = {
|
2021-02-15 14:21:08 +08:00
|
|
|
key?: string;
|
2021-02-11 17:18:10 +08:00
|
|
|
|
|
|
|
/** On PC: ctrl is pressed; on Mac: meta is pressed */
|
|
|
|
ctrlOrCmd?: boolean;
|
|
|
|
|
|
|
|
altKey?: boolean;
|
|
|
|
ctrlKey?: boolean;
|
|
|
|
metaKey?: boolean;
|
|
|
|
shiftKey?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type KeyBinding = {
|
|
|
|
action: KeyAction;
|
2021-02-14 10:56:55 +08:00
|
|
|
keyCombo: KeyCombo;
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageComposerBindings = (): KeyBinding[] => {
|
|
|
|
const bindings: KeyBinding[] = [
|
|
|
|
{
|
|
|
|
action: KeyAction.SelectPrevSendHistory,
|
|
|
|
keyCombo: {
|
2021-02-15 14:21:08 +08:00
|
|
|
key: Key.ARROW_UP,
|
2021-02-14 10:56:55 +08:00
|
|
|
altKey: true,
|
|
|
|
ctrlKey: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: KeyAction.SelectNextSendHistory,
|
|
|
|
keyCombo: {
|
2021-02-15 14:21:08 +08:00
|
|
|
key: Key.ARROW_DOWN,
|
2021-02-14 10:56:55 +08:00
|
|
|
altKey: true,
|
|
|
|
ctrlKey: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-02-16 14:05:39 +08:00
|
|
|
action: KeyAction.EditPrevMessage,
|
2021-02-14 10:56:55 +08:00
|
|
|
keyCombo: {
|
2021-02-15 14:21:08 +08:00
|
|
|
key: Key.ARROW_UP,
|
2021-02-16 14:05:39 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: KeyAction.EditNextMessage,
|
|
|
|
keyCombo: {
|
|
|
|
key: Key.ARROW_DOWN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: KeyAction.CancelEditing,
|
|
|
|
keyCombo: {
|
|
|
|
key: Key.ESCAPE,
|
|
|
|
},
|
2021-02-14 10:56:55 +08:00
|
|
|
},
|
|
|
|
];
|
|
|
|
if (SettingsStore.getValue('MessageComposerInput.ctrlEnterToSend')) {
|
|
|
|
bindings.push({
|
|
|
|
action: KeyAction.Send,
|
|
|
|
keyCombo: {
|
2021-02-15 14:21:08 +08:00
|
|
|
key: Key.ENTER,
|
2021-02-14 10:56:55 +08:00
|
|
|
ctrlOrCmd: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
bindings.push({
|
|
|
|
action: KeyAction.Send,
|
|
|
|
keyCombo: {
|
2021-02-15 14:21:08 +08:00
|
|
|
key: Key.ENTER,
|
2021-02-14 10:56:55 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings;
|
2021-02-11 17:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to check if a KeyboardEvent matches a KeyCombo
|
2021-02-16 14:05:39 +08:00
|
|
|
*
|
2021-02-11 17:18:10 +08:00
|
|
|
* Note, this method is only exported for testing.
|
|
|
|
*/
|
|
|
|
export function isKeyComboMatch(ev: KeyboardEvent, combo: KeyCombo, onMac: boolean): boolean {
|
2021-02-15 14:21:08 +08:00
|
|
|
if (combo.key !== undefined && ev.key !== combo.key) {
|
2021-02-11 17:18:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const comboCtrl = combo.ctrlKey ?? false;
|
|
|
|
const comboAlt = combo.altKey ?? false;
|
|
|
|
const comboShift = combo.shiftKey ?? false;
|
|
|
|
const comboMeta = combo.metaKey ?? false;
|
|
|
|
// When ctrlOrCmd is set, the keys need do evaluated differently on PC and Mac
|
|
|
|
if (combo.ctrlOrCmd) {
|
|
|
|
if (onMac) {
|
|
|
|
if (!ev.metaKey
|
|
|
|
|| ev.ctrlKey !== comboCtrl
|
|
|
|
|| ev.altKey !== comboAlt
|
|
|
|
|| ev.shiftKey !== comboShift) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!ev.ctrlKey
|
|
|
|
|| ev.metaKey !== comboMeta
|
|
|
|
|| ev.altKey !== comboAlt
|
|
|
|
|| ev.shiftKey !== comboShift) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.metaKey !== comboMeta
|
|
|
|
|| ev.ctrlKey !== comboCtrl
|
|
|
|
|| ev.altKey !== comboAlt
|
|
|
|
|| ev.shiftKey !== comboShift) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-14 10:56:55 +08:00
|
|
|
export type KeyBindingsGetter = () => KeyBinding[];
|
|
|
|
|
2021-02-11 17:18:10 +08:00
|
|
|
export class KeyBindingsManager {
|
2021-02-14 10:56:55 +08:00
|
|
|
/**
|
|
|
|
* Map of KeyBindingContext to a KeyBinding getter arrow function.
|
2021-02-16 14:05:39 +08:00
|
|
|
*
|
2021-02-14 10:56:55 +08:00
|
|
|
* Returning a getter function allowed to have dynamic bindings, e.g. when settings change the bindings can be
|
|
|
|
* recalculated.
|
|
|
|
*/
|
|
|
|
contextBindings: Record<KeyBindingContext, KeyBindingsGetter> = {
|
2021-02-16 14:05:39 +08:00
|
|
|
[KeyBindingContext.MessageComposer]: messageComposerBindings,
|
2021-02-14 10:56:55 +08:00
|
|
|
};
|
2021-02-11 17:18:10 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a matching KeyAction for a given KeyboardEvent
|
|
|
|
*/
|
|
|
|
getAction(context: KeyBindingContext, ev: KeyboardEvent): KeyAction {
|
2021-02-14 10:56:55 +08:00
|
|
|
const bindings = this.contextBindings[context]?.();
|
2021-02-11 17:18:10 +08:00
|
|
|
if (!bindings) {
|
|
|
|
return KeyAction.None;
|
|
|
|
}
|
|
|
|
const binding = bindings.find(it => isKeyComboMatch(ev, it.keyCombo, isMac));
|
|
|
|
if (binding) {
|
|
|
|
return binding.action;
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeyAction.None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const manager = new KeyBindingsManager();
|
|
|
|
|
|
|
|
export function getKeyBindingsManager(): KeyBindingsManager {
|
|
|
|
return manager;
|
|
|
|
}
|