Quellcode durchsuchen

Add reify default value function.

Afshin Darian vor 7 Jahren
Ursprung
Commit
73faa657dd

+ 1 - 0
packages/settingeditor-extension/package.json

@@ -17,6 +17,7 @@
     "@jupyterlab/apputils": "^0.9.0",
     "@jupyterlab/codeeditor": "^0.9.0",
     "@jupyterlab/coreutils": "^0.9.0",
+    "@phosphor/coreutils": "^1.2.0",
     "@phosphor/messaging": "^1.2.1",
     "@phosphor/signaling": "^1.2.1",
     "@phosphor/virtualdom": "^1.1.1",

+ 43 - 0
packages/settingeditor-extension/src/settingeditor.ts

@@ -15,6 +15,10 @@ import {
   ICON_CLASS_KEY, ICON_LABEL_KEY, ISettingRegistry, IStateDB, ObservableJSON
 } from '@jupyterlab/coreutils';
 
+import {
+  JSONObject, JSONValue
+} from '@phosphor/coreutils';
+
 import {
   Message
 } from '@phosphor/messaging';
@@ -968,6 +972,45 @@ namespace Private {
     }
   }
 
+  /**
+   * Create a fully extrapolated default value for a key in a plugin schema.
+   */
+  function reifyDefault(schema: ISettingRegistry.ISchema, key?: string): JSONValue {
+    const { properties } = schema;
+
+    if (schema.type !== 'object') {
+      return 'default' in schema ? schema.default : '';
+    }
+
+    if (!properties) {
+      return 'default' in schema ? schema.default : undefined;
+    }
+
+    const property = key ? properties[key] : schema;
+
+    if (property.type !== 'object') {
+      return 'default' in property ? property.default : '';
+    }
+
+    const result: JSONObject = property.default || { };
+
+    if (!property.properties) {
+      return result;
+    }
+
+    Object.keys(property.properties).forEach(prop => {
+      if ('default' in property.properties[prop]) {
+        const value = reifyDefault(property.properties[prop]);
+
+        if (value !== undefined) {
+          result[prop] = value;
+        }
+      }
+    });
+
+    return result;
+  }
+
   /**
    * Sort a list of plugins by ID.
    */