plugin.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. JupyterLab, JupyterLabPlugin
  5. } from '../application';
  6. import {
  7. InstanceTracker
  8. } from '../common/instancetracker';
  9. import {
  10. IDocumentRegistry
  11. } from '../docregistry';
  12. import {
  13. ILayoutRestorer
  14. } from '../layoutrestorer';
  15. import {
  16. IRenderMime
  17. } from '../rendermime';
  18. import {
  19. IStateDB
  20. } from '../statedb';
  21. import {
  22. MarkdownWidget, MarkdownWidgetFactory
  23. } from './widget';
  24. /**
  25. * The class name for all main area portrait tab icons.
  26. */
  27. const PORTRAIT_ICON_CLASS = 'jp-MainAreaPortraitIcon';
  28. /**
  29. * The class name for the text editor icon from the default theme.
  30. */
  31. const TEXTEDITOR_ICON_CLASS = 'jp-ImageTextEditor';
  32. /**
  33. * The name of the factory that creates markdown widgets.
  34. */
  35. const FACTORY = 'Rendered Markdown';
  36. /**
  37. * The markdown handler extension.
  38. */
  39. export
  40. const plugin: JupyterLabPlugin<void> = {
  41. id: 'jupyter.extensions.rendered-markdown',
  42. requires: [IDocumentRegistry, IRenderMime, IStateDB, ILayoutRestorer],
  43. activate: (app: JupyterLab, registry: IDocumentRegistry, rendermime: IRenderMime, state: IStateDB, layout: ILayoutRestorer) => {
  44. const factory = new MarkdownWidgetFactory({
  45. name: FACTORY,
  46. fileExtensions: ['.md'],
  47. rendermime
  48. });
  49. const tracker = new InstanceTracker<MarkdownWidget>();
  50. // Handle state restoration.
  51. layout.restore(tracker, {
  52. namespace: 'rendered-markdown',
  53. command: 'file-operations:open',
  54. args: widget => ({ path: widget.context.path, factory: FACTORY }),
  55. name: widget => widget.context.path
  56. });
  57. let icon = `${PORTRAIT_ICON_CLASS} ${TEXTEDITOR_ICON_CLASS}`;
  58. // Sync tracker with currently focused widget.
  59. app.shell.currentChanged.connect((sender, args) => {
  60. tracker.sync(args.newValue);
  61. });
  62. factory.widgetCreated.connect((sender, widget) => {
  63. widget.title.icon = icon;
  64. // Notify the instance tracker if restore data needs to update.
  65. widget.context.pathChanged.connect(() => { tracker.save(widget); });
  66. tracker.add(widget);
  67. });
  68. registry.addWidgetFactory(factory);
  69. },
  70. autoStart: true
  71. };