hyper/app/utils/map-keys.ts

44 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-12-25 00:52:32 -09:00
const generatePrefixedCommand = (command: string, shortcuts: string[]) => {
const result: Record<string, string[]> = {};
const baseCmd = command.replace(/:prefix$/, '');
for (let i = 1; i <= 9; i++) {
// 9 is a special number because it means 'last'
const index = i === 9 ? 'last' : i;
2020-03-25 02:15:08 -08:00
const prefixedShortcuts = shortcuts.map((shortcut) => `${shortcut}+${i}`);
result[`${baseCmd}:${index}`] = prefixedShortcuts;
}
return result;
};
2023-07-25 08:11:02 -08:00
const mapKeys = (config: Record<string, string[] | string>) => {
2019-12-25 00:52:32 -09:00
return Object.keys(config).reduce((keymap: Record<string, string[]>, command: string) => {
if (!command) {
2019-12-25 00:52:32 -09:00
return keymap;
}
// We can have different keys for a same command.
2019-12-25 00:52:32 -09:00
const _shortcuts = config[command];
const shortcuts = Array.isArray(_shortcuts) ? _shortcuts : [_shortcuts];
const fixedShortcuts: string[] = [];
2020-03-25 02:15:08 -08:00
shortcuts.forEach((shortcut) => {
let newShortcut = shortcut;
if (newShortcut.indexOf('cmd') !== -1) {
// Mousetrap use `command` and not `cmd`
console.warn('Your config use deprecated `cmd` in key combination. Please use `command` instead.');
newShortcut = newShortcut.replace('cmd', 'command');
}
fixedShortcuts.push(newShortcut);
});
if (command.endsWith(':prefix')) {
return Object.assign(keymap, generatePrefixedCommand(command, fixedShortcuts));
}
keymap[command] = fixedShortcuts;
return keymap;
}, {});
};
2023-07-25 08:11:02 -08:00
export default mapKeys;