Prechádzať zdrojové kódy

Connect fieldset property add signal to setting saves.

Afshin Darian 7 rokov pred
rodič
commit
987b41a0b5

+ 37 - 9
packages/settingeditor-extension/src/settingeditor.ts

@@ -646,6 +646,7 @@ class PluginEditor extends Widget {
     this.handleMoved = panel.handleMoved;
     this._editor = new JSONEditor({ collapsible, editorFactory });
     this._fieldset = new PluginFieldset();
+    this._fieldset.propertyAdded.connect(this._onPropertyAdded, this);
 
     layout.addWidget(panel);
     panel.addWidget(this._editor);
@@ -769,6 +770,29 @@ class PluginEditor extends Widget {
     fieldset.hide();
   }
 
+  /**
+   * Handle a property add signal.
+   */
+  private _onPropertyAdded(sender: any, property: string): void {
+    const settings = this._settings;
+
+    settings.save({ ...settings.user, [property]: settings.default(property) })
+      .catch(this._onSaveError);
+  }
+
+  /**
+   * Handle save errors.
+   */
+  private _onSaveError(reason: any): void {
+    console.error(`Saving setting editor value failed: ${reason.message}`);
+
+    showDialog({
+      title: 'Your changes were not saved.',
+      body: reason.message,
+      buttons: [Dialog.okButton()]
+    });
+  }
+
   /**
    * Handle updates to the settings.
    */
@@ -792,15 +816,7 @@ class PluginEditor extends Widget {
       return;
     }
 
-    settings.save(source.toJSON()).catch(reason => {
-      console.error(`Saving setting editor value failed: ${reason.message}`);
-
-      return showDialog({
-        title: 'Your changes were not saved.',
-        body: reason.message,
-        buttons: [Dialog.okButton()]
-      }).then(() => void 0);
-    });
+    settings.save(source.toJSON()).catch(this._onSaveError);
   }
 
   private _editor: JSONEditor;
@@ -853,7 +869,12 @@ class PluginFieldset extends Widget {
     return this._settings;
   }
   set settings(settings: ISettingRegistry.ISettings | null) {
+    if (this._settings) {
+      this._settings.changed.disconnect(this._onSettingsChanged, this);
+    }
+
     this._settings = settings;
+    this._settings.changed.connect(this._onSettingsChanged, this);
     this.update();
   }
 
@@ -926,6 +947,13 @@ class PluginFieldset extends Widget {
     }
   }
 
+  /**
+   * Handle setting changes.
+   */
+  private _onSettingsChanged(): void {
+    this.update();
+  }
+
   private _propertyAdded = new Signal<any, string>(this);
   private _settings: ISettingRegistry.ISettings | null = null;
 }