浏览代码

Merge pull request #9858 from jasongrout/overrideshortcuts

Allow overrides.json to override default shortcuts.
Steven Silvester 4 年之前
父节点
当前提交
272b7a4091
共有 2 个文件被更改,包括 23 次插入18 次删除
  1. 21 17
      packages/settingregistry/src/settingregistry.ts
  2. 2 1
      packages/shortcuts-extension/src/index.ts

+ 21 - 17
packages/settingregistry/src/settingregistry.ts

@@ -911,7 +911,7 @@ export namespace SettingRegistry {
   ): ISettingRegistry.IShortcut[] {
     const memo: {
       [keys: string]: {
-        [selector: string]: boolean; // If `true`, this is a default shortcut.
+        [selector: string]: boolean; // If `true`, should warn if a default shortcut conflicts.
       };
     } = {};
 
@@ -920,8 +920,6 @@ export namespace SettingRegistry {
       const keys = CommandRegistry.normalizeKeys(shortcut).join(
         RECORD_SEPARATOR
       );
-      const { selector } = shortcut;
-
       if (!keys) {
         console.warn(
           'Skipping this shortcut because there are no actionable keys on this platform',
@@ -932,8 +930,10 @@ export namespace SettingRegistry {
       if (!(keys in memo)) {
         memo[keys] = {};
       }
+
+      const { selector } = shortcut;
       if (!(selector in memo[keys])) {
-        memo[keys][selector] = false; // User shortcuts are `false`.
+        memo[keys][selector] = false; // Do not warn if a default shortcut conflicts.
         return true;
       }
 
@@ -944,33 +944,37 @@ export namespace SettingRegistry {
       return false;
     });
 
-    // If a default shortcut collides with another default, warn and filter.
-    // If a shortcut has already been added by the user preferences, filter it
-    // out too (this includes shortcuts that are disabled by user preferences).
-    defaults = defaults.filter(shortcut => {
-      const { disabled } = shortcut;
+    // If a default shortcut collides with another default, warn and filter,
+    // unless one of the shortcuts is a disabling shortcut (so look through
+    // disabled shortcuts first). If a shortcut has already been added by the
+    // user preferences, filter it out too (this includes shortcuts that are
+    // disabled by user preferences).
+    defaults = [
+      ...defaults.filter(s => !!s.disabled),
+      ...defaults.filter(s => !s.disabled)
+    ].filter(shortcut => {
       const keys = CommandRegistry.normalizeKeys(shortcut).join(
         RECORD_SEPARATOR
       );
 
-      if (disabled || !keys) {
+      if (!keys) {
         return false;
       }
       if (!(keys in memo)) {
         memo[keys] = {};
       }
 
-      const { selector } = shortcut;
-
+      const { disabled, selector } = shortcut;
       if (!(selector in memo[keys])) {
-        memo[keys][selector] = true; // Default shortcuts are `true`.
+        // Warn of future conflicts if the default shortcut is not disabled.
+        memo[keys][selector] = !disabled;
         return true;
       }
 
-      // Only warn if a default shortcut collides with another default shortcut.
+      // We have a conflict now. Warn the user if we need to do so.
       if (memo[keys][selector]) {
         console.warn(
-          'Skipping this shortcut because it collides with another shortcut.',
+          'Skipping this default shortcut because it collides with another default shortcut.',
           shortcut
         );
       }
@@ -978,8 +982,8 @@ export namespace SettingRegistry {
       return false;
     });
 
-    // Filter out disabled user shortcuts and concat defaults before returning.
-    return user.filter(shortcut => !shortcut.disabled).concat(defaults);
+    // Return all the shortcuts that should be registered
+    return user.concat(defaults).filter(shortcut => !shortcut.disabled);
   }
 }
 

+ 2 - 1
packages/shortcuts-extension/src/index.ts

@@ -80,7 +80,8 @@ const shortcuts: JupyterFrontEndPlugin<void> = {
           loaded[plugin] = shortcuts;
           return shortcuts;
         })
-        .reduce((acc, val) => acc.concat(val), [])
+        .concat([schema.properties!.shortcuts.default as any[]])
+        .reduce((acc, val) => acc.concat(val), []) // flatten one level
         .sort((a, b) => a.command.localeCompare(b.command));
 
       schema.properties!.shortcuts.description = trans.__(