Explorar o código

Add the ability to create new file

Steven Silvester %!s(int64=9) %!d(string=hai) anos
pai
achega
fb0cb6bf23
Modificáronse 3 ficheiros con 29 adicións e 17 borrados
  1. 6 8
      examples/lab/src/plugin.ts
  2. 5 0
      src/fileopener/index.ts
  3. 18 9
      src/fileopener/plugin.ts

+ 6 - 8
examples/lab/src/plugin.ts

@@ -94,19 +94,17 @@ class DefaultHandler {
     let newFileCommandItem = {
       id: 'jupyter-plugins:new-text-file',
       command: new DelegateCommand(() => {
-        // TODO
-        let editor = new CodeMirrorWidget();
-        editor.title.text = 'untitled.txt'
-        this._shell.addToMainArea(editor);
+        this._browser.newUntitled('file', '.txt').then(
+          contents => this._opener.open(contents.path)
+        );
       })
     }
     let newNotebookCommandItem = {
       id: 'jupyter-plugins:new-notebook',
       command: new DelegateCommand(() => {
-        let widget = new Widget();
-        widget.title.text = 'Untitled'
-        widget.node.innerHTML = '<h1>New Notebook</h1>';
-        this._shell.addToMainArea(widget);
+        this._browser.newUntitled('notebook').then(
+          contents => this._opener.open(contents.path)
+        );
       })
     }
     this._registry.add([termCommandItem, newFileCommandItem,

+ 5 - 0
src/fileopener/index.ts

@@ -38,6 +38,11 @@ interface IFileHandler {
  */
 export
 interface IFileOpener {
+  /**
+   * Open the file and add the widget to the application shell.
+   */
+  open(path: string): Promise<void>;
+
   /**
    * Register a file opener.
    */

+ 18 - 9
src/fileopener/plugin.ts

@@ -75,9 +75,9 @@ class FileOpener implements IFileOpener {
   }
 
   /**
-   * Handle an `openRequested` signal by invoking the appropriate handler.
+   * Open a file and add it to the application shell.
    */
-  private _openRequested(browser: FileBrowserWidget, path: string): void {
+  open(path: string): Promise<void> {
     if (this._handlers.length === 0) {
       return;
     }
@@ -89,8 +89,7 @@ class FileOpener implements IFileOpener {
     }
     // If there was only one match, use it.
     if (handlers.length === 1) {
-      this._open(handlers[0], path);
-      return;
+      return this._open(handlers[0], path);
 
     // If there were no matches, look for default handler(s).
     } else if (handlers.length === 0) {
@@ -101,20 +100,30 @@ class FileOpener implements IFileOpener {
 
     // If there we no matches, do nothing.
     if (handlers.length == 0) {
-      console.warn('Could not open file ')
+      return Promise.reject(new Error(`Could not open file '${path}'`));
 
     // If there was one handler, use it.
     } else if (handlers.length === 1) {
-      this._open(handlers[0], path);
+      return this._open(handlers[0], path);
     } else {
       // There are more than one possible handlers.
       // TODO: Ask the user to choose one.
-      this._open(handlers[0], path);
+      return this._open(handlers[0], path);
     }
   }
 
-  private _open(handler: IFileHandler, path: string): void {
-    handler.open(path).then(widget => {
+  /**
+   * Handle an `openRequested` signal by invoking the appropriate handler.
+   */
+  private _openRequested(browser: FileBrowserWidget, path: string): void {
+    this.open(path);
+  }
+
+  /**
+   * Open a file and add it to the application shell.
+   */
+  private _open(handler: IFileHandler, path: string): Promise<void> {
+    return handler.open(path).then(widget => {
       this._appShell.addToMainArea(widget);
     });
   }