소스 검색

Convert markdown viewer to a pure rendermime extension.

Ian Rose 6 년 전
부모
커밋
2edd1ec3cc

+ 1 - 1
dev_mode/package.json

@@ -137,7 +137,6 @@
       "@jupyterlab/inspector-extension": "",
       "@jupyterlab/launcher-extension": "",
       "@jupyterlab/mainmenu-extension": "",
-      "@jupyterlab/markdownviewer-extension": "",
       "@jupyterlab/mathjax2-extension": "",
       "@jupyterlab/notebook-extension": "",
       "@jupyterlab/rendermime-extension": "",
@@ -153,6 +152,7 @@
     "mimeExtensions": {
       "@jupyterlab/javascript-extension": "",
       "@jupyterlab/json-extension": "",
+      "@jupyterlab/markdownviewer-extension": "",
       "@jupyterlab/pdf-extension": "",
       "@jupyterlab/vdom-extension": "",
       "@jupyterlab/vega3-extension": ""

+ 22 - 0
packages/docmanager-extension/src/index.ts

@@ -34,6 +34,11 @@ import {
 } from '@phosphor/disposable';
 
 
+/**
+ * The name of the factory that creates markdown widgets.
+ */
+const MARKDOWN_FACTORY = 'Markdown Preview';
+
 /**
  * The command IDs used by the document manager plugin.
  */
@@ -85,6 +90,9 @@ namespace CommandIDs {
 
   export
   const showInFileBrowser = 'docmanager:show-in-file-browser';
+
+  export
+  const markdownPreview = 'markdownviewer:open';
 }
 
 const pluginId = '@jupyterlab/docmanager-extension:plugin';
@@ -450,6 +458,20 @@ function addCommands(app: JupyterLab, docManager: IDocumentManager, palette: ICo
     }
   });
 
+  commands.addCommand(CommandIDs.markdownPreview, {
+    label: 'Markdown Preview',
+    execute: (args) => {
+      let path = args['path'];
+      if (typeof path !== 'string') {
+        return;
+      }
+      return commands.execute('docmanager:open', {
+        path, factory: MARKDOWN_FACTORY
+      });
+    }
+  });
+
+
   app.contextMenu.addItem({
     command: CommandIDs.rename,
     selector: '[data-type="document-title"]',

+ 1 - 4
packages/markdownviewer-extension/package.json

@@ -30,9 +30,6 @@
     "watch": "tsc -w --listEmittedFiles"
   },
   "dependencies": {
-    "@jupyterlab/application": "^0.16.2",
-    "@jupyterlab/apputils": "^0.16.3",
-    "@jupyterlab/docregistry": "^0.16.2",
     "@jupyterlab/rendermime": "^0.16.2"
   },
   "devDependencies": {
@@ -40,6 +37,6 @@
     "typescript": "~2.9.1"
   },
   "jupyterlab": {
-    "extension": true
+    "mimeExtension": true
   }
 }

+ 13 - 76
packages/markdownviewer-extension/src/index.ts

@@ -2,19 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  ILayoutRestorer, JupyterLab, JupyterLabPlugin
-} from '@jupyterlab/application';
-
-import {
-  InstanceTracker
-} from '@jupyterlab/apputils';
-
-import {
-  MimeDocumentFactory, MimeDocument
-} from '@jupyterlab/docregistry';
-
-import {
-  IRenderMimeRegistry
+  IRenderMime, markdownRendererFactory
 } from '@jupyterlab/rendermime';
 
 import '../style/index.css';
@@ -26,71 +14,20 @@ const FACTORY = 'Markdown Preview';
 
 
 /**
- * The command IDs used by the document manager plugin.
- */
-namespace CommandIDs {
-  export
-  const preview = 'markdownviewer:open';
-}
-
-
-/**
- * The markdown handler extension.
+ * The markdown mime renderer extension.
  */
-const plugin: JupyterLabPlugin<void> = {
-  activate,
-  id: '@jupyterlab/markdownviewer-extension:plugin',
-  requires: [ILayoutRestorer, IRenderMimeRegistry],
-  autoStart: true
+const extension: IRenderMime.IExtension = {
+  id: '@jupyterlab/markdownviewer-extension:factory',
+  rendererFactory: markdownRendererFactory,
+  dataType: 'string',
+  documentWidgetFactoryOptions: {
+    name: FACTORY,
+    primaryFileType: 'markdown',
+    fileTypes: ['markdown'],
+  },
 };
 
-
-/**
- * Activate the markdown plugin.
- */
-function activate(app: JupyterLab, restorer: ILayoutRestorer, rendermime: IRenderMimeRegistry) {
-    const primaryFileType = app.docRegistry.getFileType('markdown');
-    const factory = new MimeDocumentFactory({
-      name: FACTORY,
-      primaryFileType,
-      fileTypes: ['markdown'],
-      rendermime
-    });
-    const { commands } = app;
-    const namespace = 'rendered-markdown';
-    const tracker = new InstanceTracker<MimeDocument>({ namespace });
-
-    app.docRegistry.addWidgetFactory(factory);
-
-    // Handle state restoration.
-    restorer.restore(tracker, {
-      command: 'docmanager:open',
-      args: widget => ({ path: widget.context.path, factory: FACTORY }),
-      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);
-    });
-
-    commands.addCommand(CommandIDs.preview, {
-      label: 'Markdown Preview',
-      execute: (args) => {
-        let path = args['path'];
-        if (typeof path !== 'string') {
-          return;
-        }
-        return commands.execute('docmanager:open', {
-          path, factory: FACTORY
-        });
-      }
-    });
-  }
-
-
 /**
- * Export the plugin as default.
+ * Export the extension as default.
  */
-export default plugin;
+export default extension;