Explorar o código

Merge pull request #1560 from afshin/tooltip-dismiss

Dismiss tooltip on keydown and mousedown
Steven Silvester %!s(int64=8) %!d(string=hai) anos
pai
achega
69961684c1
Modificáronse 4 ficheiros con 29 adicións e 30 borrados
  1. 0 10
      src/shortcuts/plugin.ts
  2. 0 3
      src/tooltip/index.ts
  3. 7 17
      src/tooltip/plugin.ts
  4. 22 0
      src/tooltip/widget.ts

+ 0 - 10
src/shortcuts/plugin.ts

@@ -337,16 +337,6 @@ const SHORTCUTS = [
     selector: '.jp-ConsolePanel',
     keys: ['Shift Tab'],
     args: { notebook: false }
-  },
-  {
-    command: TooltipCommandIDs.remove,
-    selector: '.jp-Notebook.jp-Tooltip-anchor',
-    keys: ['Escape']
-  },
-  {
-    command: TooltipCommandIDs.remove,
-    selector: '.jp-CodeConsole.jp-Tooltip-anchor',
-    keys: ['Escape']
   }
 ];
 

+ 0 - 3
src/tooltip/index.ts

@@ -12,7 +12,4 @@ export
 namespace CommandIDs {
   export
   const launch = 'tooltip:launch';
-
-  export
-  const remove = 'tooltip:remove';
 };

+ 7 - 17
src/tooltip/plugin.ts

@@ -61,11 +61,6 @@ function activate(app: JupyterLab, consoles: IConsoleTracker, notebooks: INotebo
   // Add tooltip launch command.
   registry.addCommand(CommandIDs.launch, {
     execute: args => {
-      // If a tooltip is open, remove it and return.
-      if (tooltip) {
-        return app.commands.execute(CommandIDs.remove, void 0);
-      }
-
       const notebook = args['notebook'] as boolean;
       let anchor: Widget | null = null;
       let editor: CodeEditor.IEditor | null = null;
@@ -73,6 +68,13 @@ function activate(app: JupyterLab, consoles: IConsoleTracker, notebooks: INotebo
       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 (notebook) {
         parent = notebooks.currentWidget;
         if (parent) {
@@ -108,16 +110,4 @@ function activate(app: JupyterLab, consoles: IConsoleTracker, notebooks: INotebo
     }
   });
 
-  // Add tooltip remove command.
-  registry.addCommand(CommandIDs.remove, {
-    execute: () => {
-      if (!tooltip) {
-        return;
-      }
-
-      tooltip.model.dispose();
-      tooltip.dispose();
-      tooltip = null;
-    }
-  });
 }

+ 22 - 0
src/tooltip/widget.ts

@@ -102,6 +102,10 @@ class TooltipWidget extends Widget {
       return;
     }
     switch (event.type) {
+    case 'keydown':
+    case 'mousedown':
+      this._dismiss(event);
+      break;
     case 'scroll':
       this._evtScroll(event as MouseEvent);
       break;
@@ -122,6 +126,8 @@ class TooltipWidget extends Widget {
    * Handle `'after-attach'` messages.
    */
   protected onAfterAttach(msg: Message): void {
+    document.addEventListener('keydown', this, USE_CAPTURE);
+    document.addEventListener('mousedown', this, USE_CAPTURE);
     this.anchor.node.addEventListener('scroll', this, USE_CAPTURE);
     this.model.fetch();
   }
@@ -130,6 +136,8 @@ class TooltipWidget extends Widget {
    * Handle `before_detach` messages for the widget.
    */
   protected onBeforeDetach(msg: Message): void {
+    document.removeEventListener('keydown', this, USE_CAPTURE);
+    document.removeEventListener('mousedown', this, USE_CAPTURE);
     this.anchor.node.removeEventListener('scroll', this, USE_CAPTURE);
   }
 
@@ -141,6 +149,20 @@ class TooltipWidget extends Widget {
     super.onUpdateRequest(msg);
   }
 
+  /**
+   * Dismiss the tooltip if necessary.
+   */
+  private _dismiss(event: Event): void {
+    let target = event.target as HTMLElement;
+    while (target && target.parentElement) {
+      if (target === this.node) {
+        return;
+      }
+      target = target.parentElement;
+    }
+    this.dispose();
+  }
+
   /**
    * Handle scroll events for the widget
    */