Don't change KEYBOARD_SHORTCUTS and do some refactoring (#7818)

This commit is contained in:
Šimon Brandner 2022-02-16 16:24:00 +01:00 committed by GitHub
parent 0dc1355441
commit 81f52283cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View file

@ -250,7 +250,8 @@ export const CATEGORIES: Record<CategoryName, ICategory> = {
// This is very intentionally modelled after SETTINGS as it will make it easier // This is very intentionally modelled after SETTINGS as it will make it easier
// to implement customizable keyboard shortcuts // to implement customizable keyboard shortcuts
// TODO: TravisR will fix this nightmare when the new version of the SettingsStore becomes a thing // TODO: TravisR will fix this nightmare when the new version of the SettingsStore becomes a thing
const KEYBOARD_SHORTCUTS: IKeyboardShortcuts = { // XXX: Exported for tests
export const KEYBOARD_SHORTCUTS: IKeyboardShortcuts = {
[KeyBindingAction.FormatBold]: { [KeyBindingAction.FormatBold]: {
default: { default: {
ctrlOrCmdKey: true, ctrlOrCmdKey: true,
@ -582,7 +583,7 @@ const getNonCustomizableShortcuts = (): IKeyboardShortcuts => {
}; };
export const getCustomizableShortcuts = (): IKeyboardShortcuts => { export const getCustomizableShortcuts = (): IKeyboardShortcuts => {
const keyboardShortcuts = KEYBOARD_SHORTCUTS; const keyboardShortcuts = Object.assign({}, KEYBOARD_SHORTCUTS);
keyboardShortcuts[KeyBindingAction.EditRedo] = { keyboardShortcuts[KeyBindingAction.EditRedo] = {
default: { default: {
@ -607,11 +608,10 @@ export const getKeyboardShortcuts = (): IKeyboardShortcuts => {
...Object.entries(getCustomizableShortcuts()), ...Object.entries(getCustomizableShortcuts()),
]; ];
const keyboardShortcuts: IKeyboardShortcuts = {}; return entries.reduce((acc, [key, value]) => {
for (const [key, value] of entries) { acc[key] = value;
keyboardShortcuts[key] = value; return acc;
} }, {});
return keyboardShortcuts;
}; };
export const registerShortcut = (shortcutName: string, categoryName: CategoryName, shortcut: ISetting): void => { export const registerShortcut = (shortcutName: string, categoryName: CategoryName, shortcut: ISetting): void => {

View file

@ -17,13 +17,24 @@ limitations under the License.
import { import {
CATEGORIES, CATEGORIES,
CategoryName, CategoryName,
getCustomizableShortcuts,
getKeyboardShortcuts, getKeyboardShortcuts,
KEYBOARD_SHORTCUTS,
registerShortcut, registerShortcut,
} from "../../src/accessibility/KeyboardShortcuts"; } from "../../src/accessibility/KeyboardShortcuts";
import { Key } from "../../src/Keyboard"; import { Key } from "../../src/Keyboard";
import { ISetting } from "../../src/settings/Settings"; import { ISetting } from "../../src/settings/Settings";
describe("KeyboardShortcuts", () => { describe("KeyboardShortcuts", () => {
it("doesn't change KEYBOARD_SHORTCUTS when getting shortcuts", () => {
const copyKeyboardShortcuts = Object.assign({}, KEYBOARD_SHORTCUTS);
getCustomizableShortcuts();
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
getKeyboardShortcuts();
expect(KEYBOARD_SHORTCUTS).toEqual(copyKeyboardShortcuts);
});
describe("registerShortcut()", () => { describe("registerShortcut()", () => {
it("correctly registers shortcut", () => { it("correctly registers shortcut", () => {
const shortcutName = "Keybinding.definitelyARealShortcut"; const shortcutName = "Keybinding.definitelyARealShortcut";