浏览代码

Add a saveas dialog for contexts

Steven Silvester 8 年之前
父节点
当前提交
08606ac877
共有 3 个文件被更改,包括 124 次插入75 次删除
  1. 41 17
      src/docmanager/context.ts
  2. 2 2
      src/docregistry/interfaces.ts
  3. 81 56
      src/filebrowser/plugin.ts

+ 41 - 17
src/docmanager/context.ts

@@ -170,10 +170,10 @@ class Context implements IDocumentContext<IDocumentModel> {
   }
   }
 
 
   /**
   /**
-   * Save the document to a different path.
+   * Save the document to a different path chosen by the user.
    */
    */
-  saveAs(path: string): Promise<void> {
-    return this._manager.saveAs(this._id, path);
+  saveAs(): Promise<void> {
+    return this._manager.saveAs(this._id);
   }
   }
 
 
   /**
   /**
@@ -442,24 +442,30 @@ class ContextManager implements IDisposable {
   }
   }
 
 
   /**
   /**
-   * Save a document to a new file name.
+   * Save a document to a new file path chosen by the user.
    *
    *
    * This results in a new session.
    * This results in a new session.
    */
    */
-  saveAs(id: string, newPath: string): Promise<void> {
+  saveAs(id: string): Promise<void> {
     let contextEx = this._contexts[id];
     let contextEx = this._contexts[id];
-    contextEx.path = newPath;
-    contextEx.context.pathChanged.emit(newPath);
-    if (contextEx.session) {
-      let options = {
-        notebook: { path: newPath },
-        kernel: { id: contextEx.session.id }
-      };
-      return this._startSession(id, options).then(() => {
-        return this.save(id);
-      });
-    }
-    return this.save(id);
+    return Private.saveAs(contextEx.path).then(newPath => {
+      if (!newPath) {
+        return;
+      }
+      contextEx.path = newPath;
+      contextEx.context.pathChanged.emit(newPath);
+      if (contextEx.session) {
+        let options = {
+          path: newPath,
+          kernelId: contextEx.session.kernel.id,
+          kernelName: contextEx.session.kernel.name
+        };
+        return this._startSession(id, options).then(() => {
+          return this.save(id);
+        });
+      }
+      return this.save(id);
+    });
   }
   }
 
 
   /**
   /**
@@ -641,4 +647,22 @@ namespace Private {
    */
    */
   export
   export
   const populatedSignal = new Signal<Context, void>();
   const populatedSignal = new Signal<Context, void>();
+
+  /**
+   * Get a new file path from the user.
+   */
+  export
+  function saveAs(path: string): Promise<string> {
+    let input = document.createElement('input');
+    input.value = path;
+    return showDialog({
+      title: 'Save File As..',
+      body: input,
+      okText: 'SAVE'
+    }).then(result => {
+      if (result.text === 'SAVE') {
+        return input.value;
+      }
+    });
+  }
 }
 }

+ 2 - 2
src/docregistry/interfaces.ts

@@ -191,9 +191,9 @@ export interface IDocumentContext<T extends IDocumentModel> extends IDisposable
   save(): Promise<void>;
   save(): Promise<void>;
 
 
   /**
   /**
-   * Save the document to a different path.
+   * Save the document to a different path chosen by the user.
    */
    */
-  saveAs(path: string): Promise<void>;
+  saveAs(): Promise<void>;
 
 
   /**
   /**
    * Revert the document contents to disk contents.
    * Revert the document contents to disk contents.

+ 81 - 56
src/filebrowser/plugin.ts

@@ -117,7 +117,7 @@ const TEXTEDITOR_ICON_CLASS = 'jp-ImageTextEditor';
 /**
 /**
  * Activate the file browser.
  * Activate the file browser.
  */
  */
-function activateFileBrowser(app: Application, manager: ServiceManager, registry: DocumentRegistry, mainMenu: MainMenu): Promise<void> {
+function activateFileBrowser(app: Application, manager: ServiceManager, registry: DocumentRegistry, mainMenu: MainMenu): void {
   let id = 0;
   let id = 0;
 
 
   let tracker = new WidgetTracker<Widget>();
   let tracker = new WidgetTracker<Widget>();
@@ -252,6 +252,30 @@ function activateFileBrowser(app: Application, manager: ServiceManager, registry
     }
     }
   ]);
   ]);
 
 
+
+// Add the command for saving a document with a new name.
+  let saveDocumentAsId = 'file-operations:saveas';
+
+  app.commands.add([
+    {
+      id: saveDocumentAsId,
+      handler: () => {
+        if (activeWidget) {
+          let context = docManager.contextForWidget(activeWidget);
+          context.saveAs().then(() => { fbModel.refresh(); });
+        }
+      }
+    }
+  ]);
+  app.palette.add([
+    {
+      command: saveDocumentAsId,
+      category: 'File Operations',
+      text: 'Save As...',
+      caption: 'Save the current document as...'
+    }
+  ]);
+
   // Add the command for closing a document.
   // Add the command for closing a document.
   let closeDocumentId = 'file-operations:close';
   let closeDocumentId = 'file-operations:close';
 
 
@@ -331,66 +355,67 @@ function activateFileBrowser(app: Application, manager: ServiceManager, registry
 
 
 
 
 
 
-    // Adding Top Menu
-      let newSubMenu = new Menu ([
-        new MenuItem({
-          text: 'Notebook',
-          handler: () => {
-            app.commands.execute(newNotebookId);
-          }
-        }),
-        new MenuItem({
-          text: 'Text File',
-          handler: () => {
-            app.commands.execute(newTextFileId);
-          }
-        })
-
-      ]);
-
-
-
-      let menu = new Menu ([
-        new MenuItem({
-          text: 'New',
-          submenu: newSubMenu
+  // Adding Top Menu
+  let newSubMenu = new Menu ([
+    new MenuItem({
+      text: 'Notebook',
+      handler: () => {
+        app.commands.execute(newNotebookId);
+      }
+    }),
+    new MenuItem({
+      text: 'Text File',
+      handler: () => {
+        app.commands.execute(newTextFileId);
+      }
+    })
 
 
-        }),
-        new MenuItem({
-          text: 'Save Document',
-          handler: () => {
-            app.commands.execute(saveDocumentId);
-          }
-        }),
-        new MenuItem({
-          text: 'Revert Document',
-          handler: () => {
-            app.commands.execute(revertDocumentId);
-          }
-        }),
-        new MenuItem({
-          text: 'Close Current',
-          handler: () => {
-            app.commands.execute(closeDocumentId);
-          }
-        }),
-        new MenuItem({
-          text: 'Close All',
-          handler: () => {
-            app.commands.execute(closeAllId);
-          }
-        }),
+  ]);
 
 
-      ]);
+  let menu = new Menu ([
+    new MenuItem({
+      text: 'New',
+      submenu: newSubMenu
 
 
-      let fileMenu = new MenuItem({
-        text: 'File',
-        submenu: menu
-      });
-      mainMenu.addItem(fileMenu, {rank: 1});
+    }),
+    new MenuItem({
+      text: 'Save Document',
+      handler: () => {
+        app.commands.execute(saveDocumentId);
+      }
+    }),
+    new MenuItem({
+      text: 'Save Document As...',
+      handler: () => {
+        app.commands.execute(saveDocumentAsId);
+      }
+    }),
+    new MenuItem({
+      text: 'Revert Document',
+      handler: () => {
+        app.commands.execute(revertDocumentId);
+      }
+    }),
+    new MenuItem({
+      text: 'Close Current',
+      handler: () => {
+        app.commands.execute(closeDocumentId);
+      }
+    }),
+    new MenuItem({
+      text: 'Close All',
+      handler: () => {
+        app.commands.execute(closeAllId);
+      }
+    }),
 
 
+  ]);
 
 
-  return Promise.resolve(void 0);
+  let fileMenu = new MenuItem({
+    text: 'File',
+    submenu: menu
+  });
+  mainMenu.addItem(fileMenu, {rank: 1});
 
 
   function showBrowser(): void {
   function showBrowser(): void {
     app.shell.activateLeft(fbWidget.id);
     app.shell.activateLeft(fbWidget.id);