Explorar el Código

Clean up handling of saveAs and writable file logic

Steven Silvester hace 7 años
padre
commit
28869a3a5e
Se han modificado 2 ficheros con 16 adiciones y 15 borrados
  1. 3 2
      packages/docmanager/src/savehandler.ts
  2. 13 13
      packages/docregistry/src/context.ts

+ 3 - 2
packages/docmanager/src/savehandler.ts

@@ -121,9 +121,10 @@ class SaveHandler implements IDisposable {
       return;
     }
 
-    // Bail if the model is not dirty or it is read only, or the dialog
+    // Bail if the model is not dirty or the file is not writable, or the dialog
     // is already showing.
-    if (!context.model.dirty || context.model.readOnly || this._inDialog) {
+    let writable = context.contentsModel && context.contentsModel.writable;
+    if (!writable || !context.model.dirty || this._inDialog) {
       return;
     }
 

+ 13 - 13
packages/docregistry/src/context.ts

@@ -200,9 +200,6 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
    */
   save(): Promise<void> {
     let model = this._model;
-    if (model.readOnly) {
-      return Promise.reject(new Error('Read only'));
-    }
     let content: JSONValue;
     if (this._factory.fileFormat === 'json') {
       content = model.toJSON();
@@ -258,10 +255,11 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
         return;
       }
       this._path = newPath;
-      this.session.setName(newPath.split('/').pop()!);
       return this.session.setPath(newPath).then(() => {
-        this.model.readOnly = false;
-        this.save()
+        this.session.setName(newPath.split('/').pop()!);
+        return this.save();
+      }).then(() => {
+        this._pathChanged.emit(this._path);
       });
     });
   }
@@ -416,7 +414,10 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
   /**
    * Handle a change to a session property.
    */
-  private _onSessionChanged() {
+  private _onSessionChanged(sender: IClientSession, type: string): void {
+    if (type !== 'path') {
+      return;
+    }
     let path = this.session.path;
     if (path !== this._path) {
       this._path = path;
@@ -441,9 +442,6 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
     };
     let mod = this._contentsModel ? this._contentsModel.last_modified : null;
     this._contentsModel = newModel;
-    if (!newModel.writable) {
-      this._model.readOnly = true;
-    }
     if (!mod || newModel.last_modified !== mod) {
       this._fileChanged.emit(newModel);
     }
@@ -455,11 +453,13 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
   private _populate(): Promise<void> {
     this._isPopulated = true;
 
-    // Add a checkpoint if none exists and the file is not read only.
+    // Add a checkpoint if none exists and the file is writable.
     let promise = Promise.resolve(void 0);
-    if (!this._model.readOnly) {
+    let writable = this._contentsModel && this._contentsModel.writable;
+    if (writable) {
       promise = this.listCheckpoints().then(checkpoints => {
-        if (!this.isDisposed && !checkpoints && !this._model.readOnly) {
+        writable = this._contentsModel && this._contentsModel.writable;
+        if (!this.isDisposed && !checkpoints && writable) {
           return this.createCheckpoint().then(() => { /* no-op */ });
         }
       }).catch(err => {