index.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 { TabBarSvg } from '@jupyterlab/ui-components';
  10. import { each } from '@phosphor/algorithm';
  11. import { Widget } from '@phosphor/widgets';
  12. /**
  13. * The default tab manager extension.
  14. */
  15. const plugin: JupyterFrontEndPlugin<void> = {
  16. id: '@jupyterlab/tabmanager-extension:plugin',
  17. activate: (
  18. app: JupyterFrontEnd,
  19. labShell: ILabShell | null,
  20. restorer: ILayoutRestorer | null
  21. ): void => {
  22. const { shell } = app;
  23. const tabs = new TabBarSvg<Widget>({
  24. kind: 'tabManager',
  25. orientation: 'vertical'
  26. });
  27. const header = document.createElement('header');
  28. if (restorer) {
  29. restorer.add(tabs, 'tab-manager');
  30. }
  31. tabs.id = 'tab-manager';
  32. tabs.title.iconClass = 'jp-TabIcon jp-SideBar-tabIcon';
  33. tabs.title.caption = 'Open Tabs';
  34. header.textContent = 'Open Tabs';
  35. tabs.node.insertBefore(header, tabs.contentNode);
  36. shell.add(tabs, 'left', { rank: 600 });
  37. void app.restored.then(() => {
  38. const populate = () => {
  39. tabs.clearTabs();
  40. each(shell.widgets('main'), widget => {
  41. tabs.addTab(widget.title);
  42. });
  43. };
  44. // Connect signal handlers.
  45. tabs.tabActivateRequested.connect((sender, tab) => {
  46. shell.activateById(tab.title.owner.id);
  47. });
  48. tabs.tabCloseRequested.connect((sender, tab) => {
  49. tab.title.owner.close();
  50. populate();
  51. });
  52. // If available, connect to the shell's layout modified signal.
  53. if (labShell) {
  54. labShell.layoutModified.connect(() => {
  55. populate();
  56. });
  57. // when current tab changes, make sure it is activated
  58. labShell.currentChanged.connect((sender, args) => {
  59. if (args.newValue) {
  60. shell.activateById(args.newValue.id);
  61. }
  62. });
  63. }
  64. // Populate the tab manager.
  65. populate();
  66. });
  67. },
  68. autoStart: true,
  69. optional: [ILabShell, ILayoutRestorer]
  70. };
  71. /**
  72. * Export the plugin as default.
  73. */
  74. export default plugin;