Browse Source

Refactor tooltip plugin and update shortcuts.

Afshin Darian 8 years ago
parent
commit
aff1fa4a7d
3 changed files with 164 additions and 65 deletions
  1. 4 6
      src/shortcuts/plugin.ts
  2. 79 1
      src/tooltip/index.ts
  3. 81 58
      src/tooltip/plugin.ts

+ 4 - 6
src/shortcuts/plugin.ts

@@ -337,16 +337,14 @@ const SHORTCUTS = [
     keys: ['Ctrl M']
   },
   {
-    command: TooltipCommandIDs.launch,
+    command: TooltipCommandIDs.launchNotebook,
     selector: '.jp-Notebook',
-    keys: ['Shift Tab'],
-    args: { notebook: true }
+    keys: ['Shift Tab']
   },
   {
-    command: TooltipCommandIDs.launch,
+    command: TooltipCommandIDs.launchConsole,
     selector: '.jp-ConsolePanel',
-    keys: ['Shift Tab'],
-    args: { notebook: false }
+    keys: ['Shift Tab']
   }
 ];
 

+ 79 - 1
src/tooltip/index.ts

@@ -1,6 +1,27 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  Kernel
+} from '@jupyterlab/services';
+
+import {
+  Token
+} from 'phosphor/lib/core/token';
+
+import  {
+  Widget
+} from 'phosphor/lib/ui/widget';
+
+import {
+  CodeEditor
+} from '../codeeditor';
+
+import {
+  IRenderMime
+} from '../rendermime';
+
+
 export * from './model';
 export * from './widget';
 
@@ -11,5 +32,62 @@ export * from './widget';
 export
 namespace CommandIDs {
   export
-  const launch = 'tooltip:launch';
+  const launchConsole = 'tooltip:launch-console';
+
+  export
+  const launchNotebook = 'tooltip:launch-notebook';
 };
+
+
+/* tslint:disable */
+/**
+ * The tooltip manager token.
+ */
+export
+const ITooltipManager = new Token<ITooltipManager>('jupyter.services.tooltip');
+/* tslint:enable */
+
+
+/**
+ * A manager to register tooltips with parent widgets.
+ */
+export
+interface ITooltipManager {
+  /**
+   * Invoke a tooltip.
+   */
+  invoke(options: ITooltipManager.IOptions): void;
+}
+
+
+/**
+ * A namespace for `ICompletionManager` interface specifications.
+ */
+export
+namespace ITooltipManager {
+  /**
+   * An interface for tooltip-compatible objects.
+   */
+  export
+  interface IOptions {
+    /**
+     * The referent anchor the tooltip follows.
+     */
+    readonly anchor: Widget;
+
+    /**
+     * The referent editor for the tooltip.
+     */
+    readonly editor: CodeEditor.IEditor;
+
+    /**
+     * The kernel the tooltip communicates with to populate itself.
+     */
+    readonly kernel: Kernel.IKernel;
+
+    /**
+     * The renderer the tooltip uses to render API responses.
+     */
+    readonly rendermime: IRenderMime;
+  }
+}

+ 81 - 58
src/tooltip/plugin.ts

@@ -30,84 +30,107 @@ import {
 } from '../rendermime';
 
 import {
-  CommandIDs, TooltipModel, TooltipWidget
+  CommandIDs, ITooltipManager, TooltipModel, TooltipWidget
 } from './';
 
 
 /**
- * The tooltip extension.
+ * The main tooltip service.
  */
-const plugin: JupyterLabPlugin<void> = {
-  activate,
+const service: JupyterLabPlugin<ITooltipManager> = {
   id: 'jupyter.extensions.tooltip',
   autoStart: true,
-  requires: [IConsoleTracker, INotebookTracker]
+  provides: ITooltipManager,
+  activate: (app: JupyterLab): ITooltipManager => {
+    let tooltip: TooltipWidget | null = null;
+    return {
+      invoke(options: ITooltipManager.IOptions): void {
+        const { anchor, editor, kernel, rendermime  } = options;
+        const model = new TooltipModel({ editor, kernel, rendermime });
+
+        if (tooltip) {
+          tooltip.model.dispose();
+          tooltip.dispose();
+          tooltip = null;
+        }
+        tooltip = new TooltipWidget({ anchor, model });
+        Widget.attach(tooltip, document.body);
+      }
+    };
+  }
 };
 
 
 /**
- * Export the plugin as default.
+ * The console tooltip plugin.
  */
-export default plugin;
+const consolePlugin: JupyterLabPlugin<void> = {
+  id: 'jupyter.extensions.tooltip',
+  autoStart: true,
+  requires: [ITooltipManager, IConsoleTracker],
+  activate: (app: JupyterLab, manager: ITooltipManager, consoles: IConsoleTracker): void => {
+    // Add tooltip launch command.
+    app.commands.addCommand(CommandIDs.launchConsole, {
+      execute: () => {
+        const parent = consoles.currentWidget;
+
+        if (!parent) {
+          return;
+        }
 
+        const anchor = parent.console;
+        const editor = parent.console.prompt.editor;
+        const kernel = parent.console.session.kernel;
+        const rendermime = parent.console.rendermime;
 
-/**
- * Activate the tooltip.
- */
-function activate(app: JupyterLab, consoles: IConsoleTracker, notebooks: INotebookTracker): void {
-  const registry = app.commands;
-  let tooltip: TooltipWidget = null;
-
-  // Add tooltip launch command.
-  registry.addCommand(CommandIDs.launch, {
-    execute: args => {
-      const notebook = !!(args && args['notebook']);
-      let anchor: Widget | null = null;
-      let editor: CodeEditor.IEditor | null = null;
-      let kernel: Kernel.IKernel | null = null;
-      let rendermime: IRenderMime | null = null;
-      let parent: NotebookPanel | ConsolePanel | null = null;
-
-      // If a tooltip is open, remove it.
-      if (tooltip) {
-        tooltip.model.dispose();
-        tooltip.dispose();
-        tooltip = null;
+        // If all components necessary for rendering exist, create a tooltip.
+        if (!!editor && !!kernel && !!rendermime) {
+          manager.invoke({ anchor, editor, kernel, rendermime });
+        }
       }
+    });
 
-      if (notebook) {
-        parent = notebooks.currentWidget;
-        if (parent) {
-          anchor = parent.notebook;
-          editor = parent.notebook.activeCell.editor;
-          kernel = parent.kernel;
-          rendermime = parent.rendermime;
-        }
-      } else {
-        parent = consoles.currentWidget;
-        if (parent) {
-          anchor = parent.console;
-          editor = parent.console.prompt.editor;
-          kernel = parent.console.session.kernel;
-          rendermime = parent.console.rendermime;
+  }
+};
+
+
+/**
+ * The notebook tooltip plugin.
+ */
+const notebookPlugin: JupyterLabPlugin<void> = {
+  id: 'jupyter.extensions.tooltip',
+  autoStart: true,
+  requires: [ITooltipManager, INotebookTracker],
+  activate: (app: JupyterLab, manager: ITooltipManager, notebooks: INotebookTracker): void => {
+    // Add tooltip launch command.
+    app.commands.addCommand(CommandIDs.launchNotebook, {
+      execute: () => {
+        const parent = notebooks.currentWidget;
+
+        if (!parent) {
+          return;
         }
-      }
 
-      // If all components necessary for rendering exist, create a tooltip.
-      let ready = !!editor && !!kernel && !!rendermime;
+        const anchor = parent.notebook;
+        const editor = parent.notebook.activeCell.editor;
+        const kernel = parent.kernel;
+        const rendermime = parent.rendermime;
 
-      if (!ready) {
-        return;
+        // If all components necessary for rendering exist, create a tooltip.
+        if (!!editor && !!kernel && !!rendermime) {
+          manager.invoke({ anchor, editor, kernel, rendermime });
+        }
       }
+    });
 
-      const model = new TooltipModel({ editor, kernel, rendermime });
-
-      tooltip = new TooltipWidget({ anchor, model });
-      Widget.attach(tooltip, document.body);
+  }
+};
 
-      // Make sure the parent notebook/console still has the focus.
-      parent.activate();
-    }
-  });
 
-}
+/**
+ * Export the plugins as default.
+ */
+const plugins: JupyterLabPlugin<any>[] = [
+  service, consolePlugin, notebookPlugin
+];
+export default plugins;