index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ILayoutRestorer } from '@jupyterlab/application';
  4. import { WidgetTracker } from '@jupyterlab/apputils';
  5. import {
  6. MarkdownViewer,
  7. MarkdownViewerFactory,
  8. IMarkdownViewerTracker
  9. } from '@jupyterlab/markdownviewer';
  10. import {
  11. IRenderMimeRegistry,
  12. markdownRendererFactory
  13. } from '@jupyterlab/rendermime';
  14. import { ISettingRegistry } from '@jupyterlab/settingregistry';
  15. import { PathExt } from '@jupyterlab/coreutils';
  16. import { IMiddleToken } from '@jupyterlab/example-federated-middle';
  17. /**
  18. * The command IDs used by the markdownviewer plugin.
  19. */
  20. let CommandIDs;
  21. (function (CommandIDs) {
  22. CommandIDs.markdownPreview = 'markdownviewer:open';
  23. CommandIDs.markdownEditor = 'markdownviewer:edit';
  24. })(CommandIDs || (CommandIDs = {}));
  25. /**
  26. * The name of the factory that creates markdown viewer widgets.
  27. */
  28. const FACTORY = 'Markdown Preview (Federated)';
  29. /**
  30. * The markdown viewer plugin.
  31. */
  32. const plugin = {
  33. activate,
  34. id: '@jupyterlab/example-federated-md:plugin',
  35. provides: IMarkdownViewerTracker,
  36. requires: [
  37. ILayoutRestorer,
  38. IRenderMimeRegistry,
  39. ISettingRegistry,
  40. IMiddleToken
  41. ],
  42. autoStart: true
  43. };
  44. /**
  45. * Activate the markdown viewer plugin.
  46. */
  47. function activate(app, restorer, rendermime, settingRegistry, middleToken) {
  48. console.log(middleToken);
  49. const { commands, docRegistry } = app;
  50. // Add the markdown renderer factory.
  51. rendermime.addFactory(markdownRendererFactory);
  52. const namespace = 'markdownviewer-widget';
  53. const tracker = new WidgetTracker({
  54. namespace
  55. });
  56. let config = Object.assign({}, MarkdownViewer.defaultConfig);
  57. /**
  58. * Update the settings of a widget.
  59. */
  60. function updateWidget(widget) {
  61. Object.keys(config).forEach(k => {
  62. let _a;
  63. widget.setOption(
  64. k,
  65. (_a = config[k]) !== null && _a !== void 0 ? _a : null
  66. );
  67. });
  68. }
  69. /**
  70. * Update the setting values.
  71. */
  72. function updateSettings(settings) {
  73. config = settings.composite;
  74. tracker.forEach(widget => {
  75. updateWidget(widget.content);
  76. });
  77. }
  78. // Fetch the initial state of the settings.
  79. settingRegistry
  80. .load(plugin.id)
  81. .then(settings => {
  82. settings.changed.connect(() => {
  83. updateSettings(settings);
  84. });
  85. updateSettings(settings);
  86. })
  87. .catch(reason => {
  88. console.error(reason.message);
  89. });
  90. // Register the MarkdownViewer factory.
  91. const factory = new MarkdownViewerFactory({
  92. rendermime,
  93. name: FACTORY,
  94. primaryFileType: docRegistry.getFileType('markdown'),
  95. fileTypes: ['markdown'],
  96. defaultRendered: ['markdown']
  97. });
  98. factory.widgetCreated.connect((sender, widget) => {
  99. // Notify the widget tracker if restore data needs to update.
  100. widget.context.pathChanged.connect(() => {
  101. void tracker.save(widget);
  102. });
  103. // Handle the settings of new widgets.
  104. updateWidget(widget.content);
  105. void tracker.add(widget);
  106. });
  107. docRegistry.addWidgetFactory(factory);
  108. // Handle state restoration.
  109. void restorer.restore(tracker, {
  110. command: 'docmanager:open',
  111. args: widget => ({ path: widget.context.path, factory: FACTORY }),
  112. name: widget => widget.context.path
  113. });
  114. commands.addCommand(CommandIDs.markdownPreview, {
  115. label: 'Markdown Preview',
  116. execute: args => {
  117. const path = args['path'];
  118. if (typeof path !== 'string') {
  119. return;
  120. }
  121. return commands.execute('docmanager:open', {
  122. path,
  123. factory: FACTORY,
  124. options: args['options']
  125. });
  126. }
  127. });
  128. commands.addCommand(CommandIDs.markdownEditor, {
  129. execute: () => {
  130. const widget = tracker.currentWidget;
  131. if (!widget) {
  132. return;
  133. }
  134. const path = widget.context.path;
  135. return commands.execute('docmanager:open', {
  136. path,
  137. factory: 'Editor',
  138. options: {
  139. mode: 'split-right'
  140. }
  141. });
  142. },
  143. isVisible: () => {
  144. const widget = tracker.currentWidget;
  145. return (
  146. (widget && PathExt.extname(widget.context.path) === '.md') || false
  147. );
  148. },
  149. label: 'Show Markdown Editor'
  150. });
  151. app.contextMenu.addItem({
  152. command: CommandIDs.markdownEditor,
  153. selector: '.jp-RenderedMarkdown'
  154. });
  155. return tracker;
  156. }
  157. /**
  158. * Export the plugin as default.
  159. */
  160. export default plugin;
  161. //# sourceMappingURL=index.js.map