index.js 4.2 KB

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