Browse Source

Backport PR #12466: Setting to use the advanced setting editor for the settings

Eric Charles 3 years ago
parent
commit
6d6864079c

+ 14 - 0
packages/settingeditor-extension/schema/form-ui.json

@@ -0,0 +1,14 @@
+{
+  "title": "Settings Editor Form UI",
+  "description": "Settings editor form ui settings.",
+  "properties": {
+    "settingEditorType": {
+      "title": "Type of editor for the setting.",
+      "description": "Set the type of editor to use while editing your settings.",
+      "enum": ["json", "ui"],
+      "default": "ui"
+    }
+  },
+  "additionalProperties": false,
+  "type": "object"
+}

+ 65 - 49
packages/settingeditor-extension/src/index.ts

@@ -50,6 +50,8 @@ namespace CommandIDs {
   export const save = 'settingeditor:save';
 }
 
+type SettingEditorType = 'ui' | 'json';
+
 /**
  * The default setting editor extension.
  */
@@ -98,64 +100,70 @@ function activate(
     });
   }
 
-  if (palette) {
-    palette.addItem({
-      category: trans.__('Settings'),
-      command: CommandIDs.open
-    });
-  }
-
-  commands.addCommand(CommandIDs.open, {
-    execute: async args => {
-      if (tracker.currentWidget && !tracker.currentWidget.isDisposed) {
-        if (!tracker.currentWidget.isAttached) {
-          shell.add(tracker.currentWidget);
-        }
-        shell.activateById(tracker.currentWidget.id);
-        return;
+  const openUi = async (args: { query: string }) => {
+    if (tracker.currentWidget && !tracker.currentWidget.isDisposed) {
+      if (!tracker.currentWidget.isAttached) {
+        shell.add(tracker.currentWidget);
       }
+      shell.activateById(tracker.currentWidget.id);
+      return;
+    }
 
-      const key = plugin.id;
+    const key = plugin.id;
+
+    const { SettingsEditor } = await import('@jupyterlab/settingeditor');
 
-      const { SettingsEditor } = await import('@jupyterlab/settingeditor');
+    const editor = new MainAreaWidget<SettingsEditor>({
+      content: new SettingsEditor({
+        editorRegistry,
+        key,
+        registry,
+        state,
+        commands,
+        toSkip: [
+          '@jupyterlab/application-extension:context-menu',
+          '@jupyterlab/mainmenu-extension:plugin'
+        ],
+        translator,
+        status,
+        query: args.query as string
+      })
+    });
 
-      const editor = new MainAreaWidget<SettingsEditor>({
-        content: new SettingsEditor({
-          editorRegistry,
-          key,
-          registry,
-          state,
+    if (jsonEditor) {
+      editor.toolbar.addItem('spacer', Toolbar.createSpacerItem());
+      editor.toolbar.addItem(
+        'open-json-editor',
+        new CommandToolbarButton({
           commands,
-          toSkip: [
-            '@jupyterlab/application-extension:context-menu',
-            '@jupyterlab/mainmenu-extension:plugin'
-          ],
-          translator,
-          status,
-          query: args.query as string
+          id: CommandIDs.openJSON,
+          icon: launchIcon,
+          label: trans.__('JSON Settings Editor')
         })
-      });
+      );
+    }
 
-      if (jsonEditor) {
-        editor.toolbar.addItem('spacer', Toolbar.createSpacerItem());
-        editor.toolbar.addItem(
-          'open-json-editor',
-          new CommandToolbarButton({
-            commands,
-            id: CommandIDs.openJSON,
-            icon: launchIcon,
-            label: trans.__('JSON Settings Editor')
-          })
-        );
-      }
+    editor.id = namespace;
+    editor.title.icon = settingsIcon;
+    editor.title.label = trans.__('Settings');
+    editor.title.closable = true;
 
-      editor.id = namespace;
-      editor.title.icon = settingsIcon;
-      editor.title.label = trans.__('Settings');
-      editor.title.closable = true;
+    void tracker.add(editor);
+    shell.add(editor);
+  };
 
-      void tracker.add(editor);
-      shell.add(editor);
+  commands.addCommand(CommandIDs.open, {
+    execute: async (args: {
+      query?: string;
+      settingEditorType?: SettingEditorType;
+    }) => {
+      registry.load(plugin.id).then(settings => {
+        args.settingEditorType ??
+        (settings.get('settingEditorType').composite as SettingEditorType) ===
+          'json'
+          ? commands.execute(CommandIDs.openJSON)
+          : openUi({ query: args.query ?? '' });
+      });
     },
     label: args => {
       if (args.label) {
@@ -165,6 +173,14 @@ function activate(
     }
   });
 
+  if (palette) {
+    palette.addItem({
+      category: trans.__('Settings'),
+      command: CommandIDs.open,
+      args: { settingEditorType: 'ui' }
+    });
+  }
+
   return tracker;
 }