浏览代码

More clean up of file type handling

Steven Silvester 7 年之前
父节点
当前提交
5c50d1a06f

+ 33 - 21
packages/application/src/mimerenderers.ts

@@ -70,14 +70,13 @@ function createRendermimePlugin(item: IRenderMime.IExtension): JupyterLabPlugin<
         return;
       }
 
-      let factory = new MimeDocumentFactory({
-        registry: app.docRegistry,
-        renderTimeout: item.renderTimeout,
-        dataType: item.dataType,
-        rendermime: app.rendermime,
-        ...item.documentWidgetFactoryOptions,
-      });
-      app.docRegistry.addWidgetFactory(factory);
+      let registry = app.docRegistry;
+      let options: IRenderMime.IDocumentWidgetFactoryOptions[] = [];
+      if (Array.isArray(item.documentWidgetFactoryOptions)) {
+        options = item.documentWidgetFactoryOptions;
+      } else {
+        options = [item.documentWidgetFactoryOptions as IRenderMime.IDocumentWidgetFactoryOptions];
+      }
 
       if (item.fileTypes) {
         item.fileTypes.forEach(ft => {
@@ -85,21 +84,34 @@ function createRendermimePlugin(item: IRenderMime.IExtension): JupyterLabPlugin<
         });
       }
 
-      const factoryName = factory.name;
-      const namespace = `${factoryName}-renderer`;
-      const tracker = new InstanceTracker<MimeDocument>({ namespace });
+      options.forEach(option => {
+        let factory = new MimeDocumentFactory({
+          renderTimeout: item.renderTimeout,
+          dataType: item.dataType,
+          rendermime: app.rendermime,
+          name: option.name,
+          primaryFileType: registry.getFileType(option.primaryFileType),
+          fileTypes: option.fileTypes,
+          defaultFor: option.defaultFor
+        });
+        registry.addWidgetFactory(factory);
+
+        const factoryName = factory.name;
+        const namespace = `${factoryName}-renderer`;
+        const tracker = new InstanceTracker<MimeDocument>({ namespace });
 
-      // Handle state restoration.
-      restorer.restore(tracker, {
-        command: 'docmanager:open',
-        args: widget => ({ path: widget.context.path, factory: factoryName }),
-        name: widget => widget.context.path
-      });
+        // Handle state restoration.
+        restorer.restore(tracker, {
+          command: 'docmanager:open',
+          args: widget => ({ path: widget.context.path, factory: factoryName }),
+          name: widget => widget.context.path
+        });
 
-      factory.widgetCreated.connect((sender, widget) => {
-        // Notify the instance tracker if restore data needs to update.
-        widget.context.pathChanged.connect(() => { tracker.save(widget); });
-        tracker.add(widget);
+        factory.widgetCreated.connect((sender, widget) => {
+          // Notify the instance tracker if restore data needs to update.
+          widget.context.pathChanged.connect(() => { tracker.save(widget); });
+          tracker.add(widget);
+        });
       });
     }
   };

+ 6 - 0
packages/csvviewer-extension/src/index.ts

@@ -57,10 +57,16 @@ function activate(app: JupyterLab, restorer: ILayoutRestorer): void {
   });
 
   app.docRegistry.addWidgetFactory(factory);
+  let ft = app.docRegistry.getFileType('csv');
   factory.widgetCreated.connect((sender, widget) => {
     // Track the widget.
     tracker.add(widget);
     // Notify the instance tracker if restore data needs to update.
     widget.context.pathChanged.connect(() => { tracker.save(widget); });
+
+    if (ft) {
+      widget.title.iconClass = ft.iconClass;
+      widget.title.iconLabel = ft.iconLabel;
+    }
   });
 }

+ 6 - 6
packages/docregistry/src/default.ts

@@ -573,18 +573,18 @@ class MimeDocumentFactory extends ABCWidgetFactory<MimeDocument, DocumentRegistr
    */
   constructor(options: MimeDocumentFactory.IOptions) {
     super(Private.createRegistryOptions(options));
-    this._registry = options.registry;
     this._rendermime = options.rendermime;
     this._renderTimeout = options.renderTimeout || 1000;
     this._dataType = options.dataType || 'string';
+    this._fileType = options.primaryFileType;
   }
 
   /**
    * Create a new widget given a context.
    */
   protected createNewWidget(context: DocumentRegistry.Context): MimeDocument {
-    let ft = this._registry.getFileTypeForModel({ name: context.path });
-    let mimeType = ft.mimeTypes.length > 0 ? ft.mimeTypes[0] : 'text/plain';
+    let ft = this._fileType;
+    let mimeType = ft.mimeTypes.length ? ft.mimeTypes[0] : 'text/plain';
     let widget = new MimeDocument({
       context,
       mimeType,
@@ -601,7 +601,7 @@ class MimeDocumentFactory extends ABCWidgetFactory<MimeDocument, DocumentRegistr
   private _rendermime: RenderMime;
   private _renderTimeout: number;
   private _dataType: 'string' | 'json';
-  private _registry: DocumentRegistry;
+  private _fileType: DocumentRegistry.IFileType;
 }
 
 
@@ -616,9 +616,9 @@ namespace MimeDocumentFactory {
   export
   interface IOptions extends DocumentRegistry.IWidgetFactoryOptions {
     /**
-     * The parent document registry.
+     * The primary file type associated with the document.
      */
-    registry: DocumentRegistry;
+    primaryFileType: DocumentRegistry.IFileType;
 
     /**
      * The rendermime instance.

+ 48 - 6
packages/docregistry/src/registry.ts

@@ -37,10 +37,6 @@ import {
   IChangedArgs as IChangedArgsGeneric, PathExt, IModelDB
 } from '@jupyterlab/coreutils';
 
-import {
-  IRenderMime
-} from '@jupyterlab/rendermime';
-
 
 /**
  * The document registry.
@@ -826,7 +822,22 @@ namespace DocumentRegistry {
    * The options used to initialize a widget factory.
    */
   export
-  interface IWidgetFactoryOptions extends IRenderMime.IDocumentWidgetFactoryOptions {
+  interface IWidgetFactoryOptions {
+    /**
+     * The name of the widget to display in dialogs.
+     */
+    readonly name: string;
+
+    /**
+     * The file types the widget can view.
+     */
+    readonly fileTypes: ReadonlyArray<string>;
+
+    /**
+     * The file types for which the factory should be the default.
+     */
+    readonly defaultFor?: ReadonlyArray<string>;
+
     /**
      * Whether the widget factory is read only.
      */
@@ -952,7 +963,38 @@ namespace DocumentRegistry {
    * An interface for a file type.
    */
   export
-  interface IFileType extends IRenderMime.IFileType {
+  interface IFileType {
+    /**
+     * The name of the file type.
+     */
+    readonly name: string;
+
+    /**
+     * The mime types associated the file type.
+     */
+    readonly mimeTypes: ReadonlyArray<string>;
+
+    /**
+     * The extensions of the file type (e.g. `".txt"`).  Can be a compound
+     * extension (e.g. `".table.json`).
+     */
+    readonly extensions: ReadonlyArray<string>;
+
+    /**
+     * An optional pattern for a file name (e.g. `^Dockerfile$`).
+     */
+    readonly pattern?: string;
+
+    /**
+     * The icon class name for the file type.
+     */
+    readonly iconClass?: string;
+
+    /**
+     * The icon label for the file type.
+     */
+    readonly iconLabel?: string;
+
     /**
      * The content type of the new file.
      */

+ 7 - 0
packages/imageviewer-extension/src/index.ts

@@ -90,6 +90,13 @@ function activate(app: JupyterLab, palette: ICommandPalette, restorer: ILayoutRe
     // Notify the instance tracker if restore data needs to update.
     widget.context.pathChanged.connect(() => { tracker.save(widget); });
     tracker.add(widget);
+
+    let fts = app.docRegistry.getFileTypesForPath(widget.context.path);
+    if (fts.length > 0) {
+      widget.title.iconClass = fts[0].iconClass;
+      widget.title.iconLabel = fts[0].iconLabel;
+    }
+
   });
 
   addCommands(tracker, app.commands);

+ 2 - 7
packages/markdownviewer-extension/src/index.ts

@@ -16,11 +16,6 @@ import {
 
 import '../style/index.css';
 
-/**
- * The class name for the text editor icon from the default theme.
- */
-const TEXTEDITOR_ICON_CLASS = 'jp-TextEditorIcon';
-
 /**
  * The name of the factory that creates markdown widgets.
  */
@@ -51,10 +46,11 @@ const plugin: JupyterLabPlugin<void> = {
  * Activate the markdown plugin.
  */
 function activate(app: JupyterLab, restorer: ILayoutRestorer) {
+    const primaryFileType = app.docRegistry.getFileType('markdown');
     const factory = new MimeDocumentFactory({
       name: FACTORY,
+      primaryFileType,
       fileTypes: ['markdown'],
-      registry: app.docRegistry,
       rendermime: app.rendermime
     });
     app.docRegistry.addWidgetFactory(factory);
@@ -71,7 +67,6 @@ function activate(app: JupyterLab, restorer: ILayoutRestorer) {
     });
 
     factory.widgetCreated.connect((sender, widget) => {
-      widget.title.icon = TEXTEDITOR_ICON_CLASS;
       // Notify the instance tracker if restore data needs to update.
       widget.context.pathChanged.connect(() => { tracker.save(widget); });
       tracker.add(widget);

+ 1 - 1
packages/notebook-extension/src/index.ts

@@ -367,7 +367,7 @@ function activateNotebookHandler(app: JupyterLab, services: IServiceManager, mai
     name: FACTORY,
     fileTypes: ['notebook'],
     modelName: 'notebook',
-    defaultFor: ['.ipynb'],
+    defaultFor: ['notebook'],
     preferKernel: true,
     canStartKernel: true,
     rendermime: app.rendermime,

+ 6 - 1
packages/rendermime-interfaces/src/index.ts

@@ -82,6 +82,11 @@ namespace IRenderMime {
      */
     readonly name: string;
 
+    /**
+     * The primary file type of the widget.
+     */
+    readonly primaryFileType: string;
+
     /**
      * The file types the widget can view.
      */
@@ -163,7 +168,7 @@ namespace IRenderMime {
     /**
      * The options used to open a document with the renderer factory.
      */
-    readonly documentWidgetFactoryOptions?: IDocumentWidgetFactoryOptions;
+    readonly documentWidgetFactoryOptions?: IDocumentWidgetFactoryOptions |ReadonlyArray<IDocumentWidgetFactoryOptions>;
 
     /**
      * The optional file type associated with the extension.

+ 10 - 3
packages/vega2-extension/src/index.ts

@@ -131,11 +131,18 @@ const extension: IRenderMime.IExtension = {
   rendererFactory,
   rank: 0,
   dataType: 'json',
-  documentWidgetFactoryOptions: {
+  documentWidgetFactoryOptions: [{
     name: 'Vega',
-    fileTypes: ['vega', 'vega-lite', 'json'],
-    defaultFor: ['vega', 'vega-lite']
+    primaryFileType: 'vega',
+    fileTypes: ['vega', 'json'],
+    defaultFor: ['vega']
   },
+  {
+    name: 'Vega Lite',
+    primaryFileType: 'vega-lite',
+    fileTypes: ['vega-lite', 'json'],
+    defaultFor: ['vega-lite']
+  }],
   fileTypes: [{
     mimeTypes: [VEGA_MIME_TYPE],
     name: 'vega',