2020-10-12 11:08:32 -04:00
|
|
|
import { flatten } from 'lodash';
|
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { s__ } from '~/locale';
|
2020-10-12 11:08:32 -04:00
|
|
|
import { shouldDisableShortcuts } from './shortcuts_toggle';
|
|
|
|
|
|
|
|
export const LOCAL_STORAGE_KEY = 'gl-keyboard-shortcuts-customizations';
|
|
|
|
|
|
|
|
let parsedCustomizations = {};
|
|
|
|
const localStorageIsSafe = AccessorUtilities.isLocalStorageAccessSafe();
|
|
|
|
|
|
|
|
if (localStorageIsSafe) {
|
|
|
|
try {
|
|
|
|
parsedCustomizations = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) || '{}');
|
|
|
|
} catch (e) {
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A map of command => keys of all keyboard shortcuts
|
|
|
|
* that have been customized by the user.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* { "globalShortcuts.togglePerformanceBar": ["p e r f"] }
|
|
|
|
*
|
|
|
|
* @type { Object.<string, string[]> }
|
|
|
|
*/
|
|
|
|
export const customizations = parsedCustomizations;
|
|
|
|
|
|
|
|
// All available commands
|
|
|
|
export const TOGGLE_PERFORMANCE_BAR = 'globalShortcuts.togglePerformanceBar';
|
2021-01-27 19:09:33 -05:00
|
|
|
export const TOGGLE_CANARY = 'globalShortcuts.toggleCanary';
|
2020-10-12 11:08:32 -04:00
|
|
|
|
|
|
|
/** All keybindings, grouped and ordered with descriptions */
|
|
|
|
export const keybindingGroups = [
|
|
|
|
{
|
|
|
|
groupId: 'globalShortcuts',
|
|
|
|
name: s__('KeyboardShortcuts|Global Shortcuts'),
|
|
|
|
keybindings: [
|
|
|
|
{
|
|
|
|
description: s__('KeyboardShortcuts|Toggle the Performance Bar'),
|
|
|
|
command: TOGGLE_PERFORMANCE_BAR,
|
|
|
|
// eslint-disable-next-line @gitlab/require-i18n-strings
|
|
|
|
defaultKeys: ['p b'],
|
|
|
|
},
|
2021-01-27 19:09:33 -05:00
|
|
|
{
|
|
|
|
description: s__('KeyboardShortcuts|Toggle GitLab Next'),
|
|
|
|
command: TOGGLE_CANARY,
|
|
|
|
// eslint-disable-next-line @gitlab/require-i18n-strings
|
|
|
|
defaultKeys: ['g x'],
|
|
|
|
},
|
2020-10-12 11:08:32 -04:00
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
// For each keybinding object, add a `customKeys` property populated with the
|
|
|
|
// user's custom keybindings (if the command has been customized).
|
|
|
|
// `customKeys` will be `undefined` if the command hasn't been customized.
|
2020-12-23 16:10:24 -05:00
|
|
|
.map((group) => {
|
2020-10-12 11:08:32 -04:00
|
|
|
return {
|
|
|
|
...group,
|
2020-12-23 16:10:24 -05:00
|
|
|
keybindings: group.keybindings.map((binding) => ({
|
2020-10-12 11:08:32 -04:00
|
|
|
...binding,
|
|
|
|
customKeys: customizations[binding.command],
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple map of command => keys. All user customizations are included in this map.
|
|
|
|
* This mapping is used to simplify `keysFor` below.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* { "globalShortcuts.togglePerformanceBar": ["p e r f"] }
|
|
|
|
*/
|
2020-12-23 16:10:24 -05:00
|
|
|
const commandToKeys = flatten(keybindingGroups.map((group) => group.keybindings)).reduce(
|
2020-10-12 11:08:32 -04:00
|
|
|
(acc, binding) => {
|
|
|
|
acc[binding.command] = binding.customKeys || binding.defaultKeys;
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets keyboard shortcuts associated with a command
|
|
|
|
*
|
|
|
|
* @param {string} command The command string. All command
|
|
|
|
* strings are available as imports from this file.
|
|
|
|
*
|
|
|
|
* @returns {string[]} An array of keyboard shortcut strings bound to the command
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* import { keysFor, TOGGLE_PERFORMANCE_BAR } from '~/behaviors/shortcuts/keybindings'
|
|
|
|
*
|
|
|
|
* Mousetrap.bind(keysFor(TOGGLE_PERFORMANCE_BAR), handler);
|
|
|
|
*/
|
2020-12-23 16:10:24 -05:00
|
|
|
export const keysFor = (command) => {
|
2020-10-12 11:08:32 -04:00
|
|
|
if (shouldDisableShortcuts()) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return commandToKeys[command];
|
|
|
|
};
|