mimerenderers.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. InstanceTracker
  5. } from '@jupyterlab/apputils';
  6. import {
  7. MimeDocument, MimeDocumentFactory
  8. } from '@jupyterlab/docregistry';
  9. import {
  10. IRenderMime
  11. } from '@jupyterlab/rendermime-interfaces';
  12. import {
  13. JupyterLab, JupyterLabPlugin
  14. } from '.';
  15. import {
  16. ILayoutRestorer
  17. } from './layoutrestorer';
  18. /**
  19. * Create rendermime plugins for rendermime extension modules.
  20. */
  21. export
  22. function createRendermimePlugins(extensions: IRenderMime.IExtensionModule[]): JupyterLabPlugin<void>[] {
  23. let plugins: JupyterLabPlugin<void>[] = [];
  24. extensions.forEach(mod => {
  25. let data = mod.default;
  26. // Handle commonjs exports.
  27. if (!mod.hasOwnProperty('__esModule')) {
  28. data = mod as any;
  29. }
  30. if (!Array.isArray(data)) {
  31. data = [data] as ReadonlyArray<IRenderMime.IExtension>;
  32. }
  33. (data as ReadonlyArray<IRenderMime.IExtension>).forEach(item => {
  34. let plugin = createRendermimePlugin(item);
  35. plugins.push(plugin);
  36. });
  37. });
  38. return plugins;
  39. }
  40. /**
  41. * Create rendermime plugins for rendermime extension modules.
  42. */
  43. export
  44. function createRendermimePlugin(item: IRenderMime.IExtension): JupyterLabPlugin<void> {
  45. return {
  46. id: `jupyter.services.mimerenderer-${item.mimeType}`,
  47. requires: [ILayoutRestorer],
  48. autoStart: true,
  49. activate: (app: JupyterLab, restorer: ILayoutRestorer) => {
  50. // Add the mime renderer.
  51. if (item.rank !== undefined) {
  52. app.rendermime.addFactory(
  53. item.rendererFactory, item.rank
  54. );
  55. } else {
  56. app.rendermime.addFactory(item.rendererFactory);
  57. }
  58. // Handle the widget factory.
  59. if (!item.documentWidgetFactoryOptions) {
  60. return;
  61. }
  62. let factory = new MimeDocumentFactory({
  63. mimeType: item.mimeType,
  64. renderTimeout: item.renderTimeout,
  65. dataType: item.dataType,
  66. rendermime: app.rendermime,
  67. iconClass: item.iconClass,
  68. iconLabel: item.iconLabel,
  69. ...item.documentWidgetFactoryOptions,
  70. });
  71. app.docRegistry.addWidgetFactory(factory);
  72. if (item.fileType) {
  73. app.docRegistry.addFileType({
  74. ...item.fileType,
  75. mimeTypes: [item.mimeType],
  76. iconClass: item.iconClass,
  77. iconLabel: item.iconLabel
  78. });
  79. }
  80. const factoryName = factory.name;
  81. const namespace = `${factoryName}-renderer`;
  82. const tracker = new InstanceTracker<MimeDocument>({ namespace });
  83. // Handle state restoration.
  84. restorer.restore(tracker, {
  85. command: 'docmanager:open',
  86. args: widget => ({ path: widget.context.path, factory: factoryName }),
  87. name: widget => widget.context.path
  88. });
  89. factory.widgetCreated.connect((sender, widget) => {
  90. // Notify the instance tracker if restore data needs to update.
  91. widget.context.pathChanged.connect(() => { tracker.save(widget); });
  92. tracker.add(widget);
  93. });
  94. }
  95. };
  96. }