Welcome to mirror list, hosted at ThFree Co, Russian Federation.

keybindings.js « shortcuts « behaviors « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8fe00d26e68d9df1a3aa033da33e385fd711298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { memoize } from 'lodash';
import AccessorUtilities from '~/lib/utils/accessor';
import { s__ } from '~/locale';

const isCustomizable = (command) =>
  'customizable' in command ? Boolean(command.customizable) : true;

export const LOCAL_STORAGE_KEY = 'gl-keyboard-shortcuts-customizations';

/**
 * @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.
 *
 * @example
 * { "globalShortcuts.togglePerformanceBar": ["p e r f"] }
 */
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;
});

// All available commands
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'],
};

export const TOGGLE_CANARY = {
  id: 'globalShortcuts.toggleCanary',
  description: s__('KeyboardShortcuts|Toggle GitLab Next'),
  // eslint-disable-next-line @gitlab/require-i18n-strings
  defaultKeys: ['g x'],
};

export const WEB_IDE_COMMIT = {
  id: 'webIDE.commit',
  description: s__('KeyboardShortcuts|Commit (when editing commit message)'),
  defaultKeys: ['mod+enter'],
  customizable: false,
};

// 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];

/**
 * Gets keyboard shortcuts associated with a command
 *
 * @param {string} command The command object. All command
 * objects 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);
 */
export const keysFor = (command) => {
  if (!isCustomizable(command)) {
    // if the command is defined with `customizable: false`,
    // don't allow this command to be customized.
    return command.defaultKeys;
  }

  return getCustomizations()[command.id] || command.defaultKeys;
};