Browse Source

Update shortcut extension to have an old plugin and a new one.

Afshin Darian 6 years ago
parent
commit
68c0367018

+ 19 - 2
packages/shortcuts-extension/schema/plugin.json

@@ -1,9 +1,26 @@
 {
+  "jupyter.lab.setting-deprecated": true,
   "jupyter.lab.setting-icon-class": "jp-LauncherIcon",
   "jupyter.lab.setting-icon-label": "Keyboard Shortcuts",
-  "jupyter.lab.setting-transform": true,
   "title": "Keyboard Shortcuts",
   "description": "Keyboard shortcut settings for JupyterLab.",
   "properties": {},
-  "type": "object"
+  "oneOf": [{ "$ref": "#/definitions/shortcut" }],
+  "type": "object",
+  "definitions": {
+    "shortcut": {
+      "properties": {
+        "command": { "type": "string" },
+        "keys": {
+          "items": { "type": "string" },
+          "minItems": 1,
+          "type": "array"
+        },
+        "selector": { "type": "string" },
+        "title": { "type": "string" },
+        "category": { "type": "string" }
+      },
+      "type": "object"
+    }
+  }
 }

+ 34 - 0
packages/shortcuts-extension/schema/shortcuts.json

@@ -0,0 +1,34 @@
+{
+  "jupyter.lab.setting-icon-class": "jp-LauncherIcon",
+  "jupyter.lab.setting-icon-label": "Keyboard Shortcuts",
+  "jupyter.lab.setting-transform": true,
+  "title": "Keyboard Shortcuts",
+  "description": "Keyboard shortcut settings for JupyterLab.",
+  "type": "object",
+  "additionalProperties": false,
+  "properties": {
+    "shortcuts": {
+      "items": { "$ref": "#/definitions/shortcut" },
+      "type": "array",
+      "default": []
+    }
+  },
+  "definitions": {
+    "shortcut": {
+      "properties": {
+        "args": { "type": "object" },
+        "command": { "type": "string" },
+        "keys": {
+          "items": { "type": "string" },
+          "minItems": 1,
+          "type": "array"
+        },
+        "selector": { "type": "string" },
+        "title": { "type": "string" },
+        "category": { "type": "string" }
+      },
+      "required": ["command", "keys", "selector", "title", "category"],
+      "type": "object"
+    }
+  }
+}

+ 41 - 8
packages/shortcuts-extension/src/index.ts

@@ -11,6 +11,29 @@ import { ReadonlyJSONObject, ReadonlyJSONValue } from '@phosphor/coreutils';
 
 import { DisposableSet, IDisposable } from '@phosphor/disposable';
 
+/**
+ * This plugin and its schema are deprecated and will be removed in a future
+ * version of JupyterLab. This plugin will load old keyboard shortcuts and add
+ * them to the new keyboard shortcuts plugin below before removing the old
+ * shortcuts.
+ */
+const plugin: JupyterLabPlugin<void> = {
+  id: '@jupyterlab/shortcuts-extension:plugin',
+  requires: [ISettingRegistry],
+  activate: async (app: JupyterLab, registry: ISettingRegistry) => {
+    try {
+      const settings = await registry.load(plugin.id);
+
+      // TODO
+      // Handle old-style shortcuts by loading them into the new plugin.
+      console.log(`${plugin.id}`, settings.user);
+    } catch (error) {
+      console.error(`Loading ${plugin.id} failed.`, error);
+    }
+  },
+  autoStart: true
+};
+
 /**
  * The default shortcuts extension.
  *
@@ -40,8 +63,8 @@ import { DisposableSet, IDisposable } from '@phosphor/disposable';
  * (`'*'`) selector. For almost any use case where a global keyboard shortcut is
  * required, using the `'body'` selector is more appropriate.
  */
-const plugin: JupyterLabPlugin<void> = {
-  id: '@jupyterlab/shortcuts-extension:plugin',
+const shortcuts: JupyterLabPlugin<void> = {
+  id: '@jupyterlab/shortcuts-extension:shortcuts',
   requires: [ISettingRegistry],
   activate: async (app: JupyterLab, registry: ISettingRegistry) => {
     const { commands } = app;
@@ -49,25 +72,27 @@ const plugin: JupyterLabPlugin<void> = {
     // Transform the settings object to return different annotated defaults
     // calculated from all the keyboard shortcuts in the registry instead of
     // using the default values from this plugin's schema.
-    registry.transform(plugin.id, Private.transform(commands, registry));
+    registry.transform(shortcuts.id, Private.transform(commands, registry));
 
     try {
-      const settings = await registry.load(plugin.id);
+      const settings = await registry.load(shortcuts.id);
       Private.loadShortcuts(commands, settings.composite);
       settings.changed.connect(() => {
         Private.loadShortcuts(commands, settings.composite);
       });
     } catch (error) {
-      console.error('Loading shortcut settings failed.', error.message);
+      console.error(`Loading ${shortcuts.id} failed.`, error);
     }
   },
   autoStart: true
 };
 
 /**
- * Export the plugin as default.
+ * Export the plugins as default.
  */
-export default plugin;
+const plugins: JupyterLabPlugin<any>[] = [plugin, shortcuts];
+
+export default plugins;
 
 /**
  * A namespace for private module data.
@@ -131,7 +156,15 @@ namespace Private {
     // using the default values from this plugin's schema.
     class ShortcutSettings extends Settings {
       annotatedDefaults(): string {
-        return 'These are the annotated defaults.';
+        const plugins = registry.plugins.slice().sort((a, b) => {
+          return (a.schema.title || a.id).localeCompare(b.schema.title || b.id);
+        });
+        const shortcuts = plugins.reduce(
+          (acc, val) => acc.concat(val.schema['jupyter.lab.shortcuts'] || []),
+          []
+        );
+
+        return JSON.stringify({ shortcuts }, null, 2);
       }
     }