debian-mirror-gitlab/app/assets/javascripts/behaviors/shortcuts/keybindings.js

95 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-04-17 20:07:23 +05:30
import { memoize } from 'lodash';
2021-01-03 14:25:43 +05:30
import AccessorUtilities from '~/lib/utils/accessor';
2021-03-11 19:13:27 +05:30
import { s__ } from '~/locale';
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
const isCustomizable = (command) =>
'customizable' in command ? Boolean(command.customizable) : true;
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
export const LOCAL_STORAGE_KEY = 'gl-keyboard-shortcuts-customizations';
2021-01-03 14:25:43 +05:30
/**
2021-04-17 20:07:23 +05:30
* @returns { Object.<string, string[]> } A map of command ID => keys of all
* keyboard shortcuts that have been customized by the user. These
* customizations are fetched from `localStorage`. This function is memoized,
* so its return value will not reflect changes made to the `localStorage` data
* after it has been called once.
2021-01-03 14:25:43 +05:30
*
* @example
* { "globalShortcuts.togglePerformanceBar": ["p e r f"] }
*/
2021-04-17 20:07:23 +05:30
export const getCustomizations = memoize(() => {
let parsedCustomizations = {};
const localStorageIsSafe = AccessorUtilities.isLocalStorageAccessSafe();
if (localStorageIsSafe) {
try {
parsedCustomizations = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) || '{}');
} catch (e) {
/* do nothing */
}
}
return parsedCustomizations;
});
2021-01-03 14:25:43 +05:30
// All available commands
2021-04-17 20:07:23 +05:30
export const TOGGLE_PERFORMANCE_BAR = {
id: 'globalShortcuts.togglePerformanceBar',
description: s__('KeyboardShortcuts|Toggle the Performance Bar'),
// eslint-disable-next-line @gitlab/require-i18n-strings
defaultKeys: ['p b'],
};
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
export const TOGGLE_CANARY = {
id: 'globalShortcuts.toggleCanary',
description: s__('KeyboardShortcuts|Toggle GitLab Next'),
// eslint-disable-next-line @gitlab/require-i18n-strings
defaultKeys: ['g x'],
};
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
export const WEB_IDE_COMMIT = {
id: 'webIDE.commit',
description: s__('KeyboardShortcuts|Commit (when editing commit message)'),
defaultKeys: ['mod+enter'],
customizable: false,
};
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
// All keybinding groups
export const GLOBAL_SHORTCUTS_GROUP = {
id: 'globalShortcuts',
name: s__('KeyboardShortcuts|Global Shortcuts'),
keybindings: [TOGGLE_PERFORMANCE_BAR, TOGGLE_CANARY],
};
export const WEB_IDE_GROUP = {
id: 'webIDE',
name: s__('KeyboardShortcuts|Web IDE'),
keybindings: [WEB_IDE_COMMIT],
};
/** All keybindings, grouped and ordered with descriptions */
export const keybindingGroups = [GLOBAL_SHORTCUTS_GROUP, WEB_IDE_GROUP];
2021-01-03 14:25:43 +05:30
/**
* Gets keyboard shortcuts associated with a command
*
2021-04-17 20:07:23 +05:30
* @param {string} command The command object. All command
* objects are available as imports from this file.
2021-01-03 14:25:43 +05:30
*
* @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);
*/
2021-03-08 18:12:59 +05:30
export const keysFor = (command) => {
2021-04-17 20:07:23 +05:30
if (!isCustomizable(command)) {
// if the command is defined with `customizable: false`,
// don't allow this command to be customized.
return command.defaultKeys;
2021-01-03 14:25:43 +05:30
}
2021-04-17 20:07:23 +05:30
return getCustomizations()[command.id] || command.defaultKeys;
2021-01-03 14:25:43 +05:30
};