plugin.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Kernel
  5. } from '@jupyterlab/services';
  6. import {
  7. Widget
  8. } from 'phosphor/lib/ui/widget';
  9. import {
  10. JupyterLab, JupyterLabPlugin
  11. } from '../application';
  12. import {
  13. BaseCellWidget
  14. } from '../cells';
  15. import {
  16. CodeEditor
  17. } from '../codeeditor';
  18. import {
  19. ConsolePanel, IConsoleTracker
  20. } from '../console';
  21. import {
  22. INotebookTracker, NotebookPanel
  23. } from '../notebook';
  24. import {
  25. IRenderMime
  26. } from '../rendermime';
  27. import {
  28. TooltipModel
  29. } from './model';
  30. import {
  31. TooltipWidget
  32. } from './widget';
  33. /**
  34. * The tooltip extension.
  35. */
  36. const plugin: JupyterLabPlugin<void> = {
  37. activate,
  38. id: 'jupyter.extensions.tooltip',
  39. autoStart: true,
  40. requires: [IConsoleTracker, INotebookTracker]
  41. };
  42. /**
  43. * Export the plugin as default.
  44. */
  45. export default plugin;
  46. /**
  47. * Activate the tooltip.
  48. */
  49. function activate(app: JupyterLab, consoles: IConsoleTracker, notebooks: INotebookTracker): void {
  50. const launch = 'tooltip:launch';
  51. const remove = 'tooltip:remove';
  52. const keymap = app.keymap;
  53. const registry = app.commands;
  54. let tooltip: TooltipWidget = null;
  55. let id = 0;
  56. // Add tooltip launch command.
  57. registry.addCommand(launch, {
  58. execute: args => {
  59. let notebook = args['notebook'] as boolean;
  60. let cell: BaseCellWidget | null = null;
  61. let editor: CodeEditor.IEditor | null = null;
  62. let kernel: Kernel.IKernel | null = null;
  63. let rendermime: IRenderMime | null = null;
  64. let parent: NotebookPanel | ConsolePanel | null = null;
  65. if (tooltip) {
  66. return app.commands.execute(remove, void 0);
  67. }
  68. if (notebook) {
  69. parent = notebooks.currentWidget;
  70. if (parent) {
  71. cell = parent.notebook.activeCell;
  72. editor = cell.editor;
  73. kernel = parent.kernel;
  74. rendermime = parent.rendermime;
  75. }
  76. } else {
  77. parent = consoles.currentWidget;
  78. if (parent) {
  79. cell = parent.console.prompt;
  80. editor = cell.editor;
  81. kernel = parent.console.session.kernel;
  82. rendermime = parent.console.rendermime;
  83. }
  84. }
  85. // If all components necessary for rendering exist, create a tooltip.
  86. let ready = !!editor && !!kernel && !!rendermime;
  87. if (ready) {
  88. tooltip = new TooltipWidget({
  89. anchor: cell,
  90. model: new TooltipModel({ editor, kernel, rendermime })
  91. });
  92. tooltip.id = `tooltip-${++id}`;
  93. Widget.attach(tooltip, document.body);
  94. // Make sure the parent notebook/console still has the focus.
  95. parent.activate();
  96. }
  97. }
  98. });
  99. // Add tooltip remove command.
  100. registry.addCommand(remove, {
  101. execute: () => {
  102. if (tooltip) {
  103. tooltip.model.dispose();
  104. tooltip.dispose();
  105. tooltip = null;
  106. }
  107. }
  108. });
  109. // Add notebook tooltip key binding.
  110. keymap.addBinding({
  111. args: { notebook: true },
  112. command: launch,
  113. keys: ['Shift Tab'],
  114. selector: '.jp-Notebook'
  115. });
  116. // Add console tooltip key binding.
  117. keymap.addBinding({
  118. args: { notebook: false },
  119. command: launch,
  120. keys: ['Shift Tab'],
  121. selector: '.jp-ConsolePanel'
  122. });
  123. // Add tooltip removal key binding.
  124. keymap.addBinding({
  125. command: remove,
  126. keys: ['Escape'],
  127. selector: `.jp-Cell.jp-Tooltip-anchor, .jp-Tooltip`
  128. });
  129. }