Prechádzať zdrojové kódy

Context should have a resolver instead of being one

Rename to urlResolver

Update tests

Context should have a resolver instead of being one

Rename to urlResolver

Update tests
Steven Silvester 7 rokov pred
rodič
commit
970b579087

+ 19 - 23
packages/docregistry/src/context.ts

@@ -26,13 +26,21 @@ import {
 } from '@jupyterlab/apputils';
 
 import {
-  PathExt, URLExt
+  PathExt
 } from '@jupyterlab/coreutils';
 
 import {
   IModelDB, ModelDB
 } from '@jupyterlab/observables';
 
+import {
+  RenderMime
+} from '@jupyterlab/rendermime';
+
+import {
+  IRenderMime
+} from '@jupyterlab/rendermime-interfaces';
+
 import {
   DocumentRegistry
 } from './registry';
@@ -79,6 +87,11 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
     });
     this.session.propertyChanged.connect(this._onSessionChanged, this);
     manager.contents.fileChanged.connect(this._onFileChanged, this);
+
+    this.urlResolver = new RenderMime.UrlResolver({
+      session: this.session,
+      contents: manager.contents
+    });
   }
 
   /**
@@ -189,6 +202,11 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
     return this._readyPromise;
   }
 
+  /**
+   * The url resolver for the context.
+   */
+  readonly urlResolver: IRenderMime.IResolver;
+
   /**
    * Populate the contents of the model, either from
    * disk or from the modelDB backend.
@@ -363,28 +381,6 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
     });
   }
 
-  /**
-   * Resolve a relative url to a correct server path.
-   */
-  resolveUrl(url: string): Promise<string> {
-    if (URLExt.isLocal(url)) {
-      let cwd = PathExt.dirname(this._path);
-      url = PathExt.resolve(cwd, url);
-    }
-    return Promise.resolve(url);
-  }
-
-  /**
-   * Get the download url of a given absolute server path.
-   */
-  getDownloadUrl(path: string): Promise<string> {
-    let contents = this._manager.contents;
-    if (URLExt.isLocal(path)) {
-      return this._manager.ready.then(() => contents.getDownloadUrl(path));
-    }
-    return Promise.resolve(path);
-  }
-
   /**
    * Add a sibling widget to the document manager.
    */

+ 3 - 1
packages/docregistry/src/mimedocument.ts

@@ -48,7 +48,9 @@ class MimeDocument extends Widget implements DocumentRegistry.IReadyWidget {
     layout.addWidget(toolbar);
     BoxLayout.setStretch(toolbar, 0);
     let context = options.context;
-    this.rendermime = options.rendermime.clone({ resolver: context });
+    this.rendermime = options.rendermime.clone({
+      resolver: context.urlResolver
+    });
 
     this._context = context;
     this._mimeType = options.mimeType;

+ 9 - 10
packages/docregistry/src/registry.ts

@@ -41,6 +41,10 @@ import {
   IModelDB
 } from '@jupyterlab/observables';
 
+import {
+  IRenderMime
+} from '@jupyterlab/rendermime-interfaces';
+
 import {
   TextModelFactory
 } from './default';
@@ -701,6 +705,11 @@ namespace DocumentRegistry {
      */
     readonly contentsModel: Contents.IModel | null;
 
+    /**
+     * The url resolver for the context.
+     */
+    readonly urlResolver: IRenderMime.IResolver;
+
     /**
      * Whether the context is ready.
      */
@@ -761,16 +770,6 @@ namespace DocumentRegistry {
      */
     listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
 
-    /**
-     * Resolve a relative url to a correct server path.
-     */
-    resolveUrl(url: string): Promise<string>;
-
-    /**
-     * Get the download url of a given absolute server path.
-     */
-    getDownloadUrl(path: string): Promise<string>;
-
     /**
      * Add a sibling widget to the document manager.
      *

+ 1 - 1
packages/notebook/src/widgetfactory.ts

@@ -66,7 +66,7 @@ class NotebookWidgetFactory extends ABCWidgetFactory<NotebookPanel, INotebookMod
    * the default toolbar items using `ToolbarItems.populateDefaults`.
    */
   protected createNewWidget(context: DocumentRegistry.IContext<INotebookModel>): NotebookPanel {
-    let rendermime = this.rendermime.clone({ resolver: context });
+    let rendermime = this.rendermime.clone({ resolver: context.urlResolver });
     let panel = new NotebookPanel({
       rendermime,
       contentFactory: this.contentFactory,

+ 7 - 30
test/src/docregistry/context.spec.ts

@@ -19,6 +19,10 @@ import {
   Context, DocumentRegistry, TextModelFactory
 } from '@jupyterlab/docregistry';
 
+import {
+  RenderMime
+} from '@jupyterlab/rendermime';
+
 import {
   waitForDialog, acceptDialog, dismissDialog
 } from '../utils';
@@ -386,37 +390,10 @@ describe('docregistry/context', () => {
 
     });
 
-    describe('#resolveUrl()', () => {
-
-      it('should resolve a relative url to a correct server path', () => {
-        return context.resolveUrl('./foo').then(path => {
-          expect(path).to.be('foo');
-        });
-      });
-
-      it('should ignore urls that have a protocol', () => {
-        return context.resolveUrl('http://foo').then(path => {
-          expect(path).to.be('http://foo');
-        });
-      });
+    describe('#urlResolver', () => {
 
-    });
-
-    describe('#getDownloadUrl()', () => {
-
-      it('should resolve an absolute server url to a download url', () => {
-        let contextPromise = context.getDownloadUrl('foo');
-        let contentsPromise = manager.contents.getDownloadUrl('foo');
-        return Promise.all([contextPromise, contentsPromise])
-        .then(values => {
-          expect(values[0]).to.be(values[1]);
-        });
-      });
-
-      it('should ignore urls that have a protocol', () => {
-        return context.getDownloadUrl('http://foo').then(path => {
-          expect(path).to.be('http://foo');
-        });
+      it('should be a url resolver', () => {
+        expect(context.urlResolver).to.be.a(RenderMime.UrlResolver);
       });
 
     });

+ 1 - 1
test/src/rendermime/registry.spec.ts

@@ -36,7 +36,7 @@ import {
 } from '../utils';
 
 
-const RESOLVER: IRenderMime.IResolver = createFileContext();
+const RESOLVER: IRenderMime.IResolver = createFileContext().urlResolver;
 
 
 function createModel(data: JSONObject): IRenderMime.IMimeModel {