index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 { ISettingRegistry } from '@jupyterlab/settingregistry';
  15. import {
  16. TableOfContents,
  17. ITableOfContentsRegistry,
  18. TableOfContentsRegistry as Registry,
  19. createLatexGenerator,
  20. createNotebookGenerator,
  21. createMarkdownGenerator,
  22. createPythonGenerator,
  23. createRenderedMarkdownGenerator
  24. } from '@jupyterlab/toc';
  25. /**
  26. * Activates the ToC extension.
  27. *
  28. * @private
  29. * @param app - Jupyter application
  30. * @param docmanager - document manager
  31. * @param editorTracker - editor tracker
  32. * @param labShell - Jupyter lab shell
  33. * @param restorer - application layout restorer
  34. * @param markdownViewerTracker - Markdown viewer tracker
  35. * @param notebookTracker - notebook tracker
  36. * @param rendermime - rendered MIME registry
  37. * @returns table of contents registry
  38. */
  39. async function activateTOC(
  40. app: JupyterFrontEnd,
  41. docmanager: IDocumentManager,
  42. editorTracker: IEditorTracker,
  43. labShell: ILabShell,
  44. restorer: ILayoutRestorer,
  45. markdownViewerTracker: IMarkdownViewerTracker,
  46. notebookTracker: INotebookTracker,
  47. rendermime: IRenderMimeRegistry,
  48. settingRegistry: ISettingRegistry
  49. ): Promise<ITableOfContentsRegistry> {
  50. // Create the ToC widget:
  51. const toc = new TableOfContents({ docmanager, rendermime });
  52. // Create the ToC registry:
  53. const registry = new Registry();
  54. // Add the ToC to the left area:
  55. toc.title.iconClass = 'jp-TableOfContents-icon jp-SideBar-tabIcon';
  56. toc.title.caption = 'Table of Contents';
  57. toc.id = 'table-of-contents';
  58. labShell.add(toc, 'left', { rank: 700 });
  59. // Add the ToC widget to the application restorer:
  60. restorer.add(toc, '@jupyterlab/toc:plugin');
  61. // Attempt to load plugin settings:
  62. let settings: ISettingRegistry.ISettings | undefined;
  63. try {
  64. settings = await settingRegistry.load('@jupyterlab/toc:plugin');
  65. } catch (error) {
  66. console.error(
  67. `Failed to load settings for the Table of Contents extension.\n\n${error}`
  68. );
  69. }
  70. // Create a notebook generator:
  71. const notebookGenerator = createNotebookGenerator(
  72. notebookTracker,
  73. toc,
  74. rendermime.sanitizer,
  75. settings
  76. );
  77. registry.add(notebookGenerator);
  78. // Create a Markdown generator:
  79. const markdownGenerator = createMarkdownGenerator(
  80. editorTracker,
  81. toc,
  82. rendermime.sanitizer
  83. );
  84. registry.add(markdownGenerator);
  85. // Create a rendered Markdown generator:
  86. const renderedMarkdownGenerator = createRenderedMarkdownGenerator(
  87. markdownViewerTracker,
  88. toc,
  89. rendermime.sanitizer
  90. );
  91. registry.add(renderedMarkdownGenerator);
  92. // Create a LaTeX generator:
  93. const latexGenerator = createLatexGenerator(editorTracker);
  94. registry.add(latexGenerator);
  95. // Create a Python generator:
  96. const pythonGenerator = createPythonGenerator(editorTracker);
  97. registry.add(pythonGenerator);
  98. // Update the ToC when the active widget changes:
  99. labShell.currentChanged.connect(onConnect);
  100. return registry;
  101. /**
  102. * Callback invoked when the active widget changes.
  103. *
  104. * @private
  105. */
  106. function onConnect() {
  107. let widget = app.shell.currentWidget;
  108. if (!widget) {
  109. return;
  110. }
  111. let generator = registry.find(widget);
  112. if (!generator) {
  113. // If the previously used widget is still available, stick with it.
  114. // Otherwise, set the current ToC widget to null.
  115. if (toc.current && toc.current.widget.isDisposed) {
  116. toc.current = null;
  117. }
  118. return;
  119. }
  120. toc.current = { widget, generator };
  121. }
  122. }
  123. /**
  124. * Initialization data for the ToC extension.
  125. *
  126. * @private
  127. */
  128. const extension: JupyterFrontEndPlugin<ITableOfContentsRegistry> = {
  129. id: '@jupyterlab/toc:plugin',
  130. autoStart: true,
  131. provides: ITableOfContentsRegistry,
  132. requires: [
  133. IDocumentManager,
  134. IEditorTracker,
  135. ILabShell,
  136. ILayoutRestorer,
  137. IMarkdownViewerTracker,
  138. INotebookTracker,
  139. IRenderMimeRegistry,
  140. ISettingRegistry
  141. ],
  142. activate: activateTOC
  143. };
  144. /**
  145. * Exports.
  146. */
  147. export default extension;