extension.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ILabShell,
  5. ILayoutRestorer,
  6. JupyterFrontEnd,
  7. JupyterFrontEndPlugin
  8. } from '@jupyterlab/application';
  9. import { IDocumentManager } from '@jupyterlab/docmanager';
  10. import { IEditorTracker } from '@jupyterlab/fileeditor';
  11. import { IMarkdownViewerTracker } from '@jupyterlab/markdownviewer';
  12. import { INotebookTracker } from '@jupyterlab/notebook';
  13. import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
  14. import { TableOfContents } from './toc';
  15. import {
  16. createLatexGenerator,
  17. createNotebookGenerator,
  18. createMarkdownGenerator,
  19. createRenderedMarkdownGenerator
  20. } from './generators';
  21. import { ITableOfContentsRegistry, Registry } from './registry';
  22. import '../style/index.css';
  23. /**
  24. * Activates the ToC extension.
  25. *
  26. * @private
  27. * @param app - Jupyter application
  28. * @param docmanager - document manager
  29. * @param editorTracker - editor tracker
  30. * @param labShell - Jupyter lab shell
  31. * @param restorer - application layout restorer
  32. * @param markdownViewerTracker - Markdown viewer tracker
  33. * @param notebookTracker - notebook tracker
  34. * @param rendermime - rendered MIME registry
  35. * @returns table of contents registry
  36. */
  37. function activateTOC(
  38. app: JupyterFrontEnd,
  39. docmanager: IDocumentManager,
  40. editorTracker: IEditorTracker,
  41. labShell: ILabShell,
  42. restorer: ILayoutRestorer,
  43. markdownViewerTracker: IMarkdownViewerTracker,
  44. notebookTracker: INotebookTracker,
  45. rendermime: IRenderMimeRegistry
  46. ): ITableOfContentsRegistry {
  47. // Create the ToC widget:
  48. const toc = new TableOfContents({ docmanager, rendermime });
  49. // Create the ToC registry:
  50. const registry = new Registry();
  51. // Add the ToC to the left area:
  52. toc.title.iconClass = 'jp-TableOfContents-icon jp-SideBar-tabIcon';
  53. toc.title.caption = 'Table of Contents';
  54. toc.id = 'table-of-contents';
  55. labShell.add(toc, 'left', { rank: 700 });
  56. // Add the ToC widget to the application restorer:
  57. restorer.add(toc, 'juputerlab-toc');
  58. // Create a notebook generator:
  59. const notebookGenerator = createNotebookGenerator(
  60. notebookTracker,
  61. toc,
  62. rendermime.sanitizer
  63. );
  64. registry.add(notebookGenerator);
  65. // Create an markdown generator:
  66. const markdownGenerator = createMarkdownGenerator(
  67. editorTracker,
  68. toc,
  69. rendermime.sanitizer
  70. );
  71. registry.add(markdownGenerator);
  72. // Create an rendered markdown generator:
  73. const renderedMarkdownGenerator = createRenderedMarkdownGenerator(
  74. markdownViewerTracker,
  75. toc,
  76. rendermime.sanitizer
  77. );
  78. registry.add(renderedMarkdownGenerator);
  79. // Create a LaTeX generator:
  80. const latexGenerator = createLatexGenerator(editorTracker);
  81. registry.add(latexGenerator);
  82. // Update the ToC when the active widget changes:
  83. labShell.currentChanged.connect(onConnect);
  84. return registry;
  85. /**
  86. * Callback invoked when the active widget changes.
  87. *
  88. * @private
  89. */
  90. function onConnect() {
  91. let widget = app.shell.currentWidget;
  92. if (!widget) {
  93. return;
  94. }
  95. let generator = registry.find(widget);
  96. if (!generator) {
  97. // If the previously used widget is still available, stick with it.
  98. // Otherwise, set the current ToC widget to null.
  99. if (toc.current && toc.current.widget.isDisposed) {
  100. toc.current = null;
  101. }
  102. return;
  103. }
  104. toc.current = { widget, generator };
  105. }
  106. }
  107. /**
  108. * Initialization data for the ToC extension.
  109. *
  110. * @private
  111. */
  112. const extension: JupyterFrontEndPlugin<ITableOfContentsRegistry> = {
  113. id: 'jupyterlab-toc',
  114. autoStart: true,
  115. provides: ITableOfContentsRegistry,
  116. requires: [
  117. IDocumentManager,
  118. IEditorTracker,
  119. ILabShell,
  120. ILayoutRestorer,
  121. IMarkdownViewerTracker,
  122. INotebookTracker,
  123. IRenderMimeRegistry
  124. ],
  125. activate: activateTOC
  126. };
  127. /**
  128. * Exports.
  129. */
  130. export default extension;