浏览代码

Allow to set custom position for Tooltip (#10590)

Michał Krassowski 3 年之前
父节点
当前提交
119895e674
共有 1 个文件被更改,包括 23 次插入4 次删除
  1. 23 4
      packages/tooltip/src/widget.ts

+ 23 - 4
packages/tooltip/src/widget.ts

@@ -59,6 +59,7 @@ export class Tooltip extends Widget {
     this.addClass(TOOLTIP_CLASS);
     this.hide();
     this._editor = options.editor;
+    this._position = options.position;
     this._rendermime = options.rendermime;
 
     const mimeType = this._rendermime.preferredMimeType(options.bundle, 'any');
@@ -184,10 +185,9 @@ export class Tooltip extends Widget {
   }
 
   /**
-   * Set the geometry of the tooltip widget.
+   * Find the position of the first character of the current token.
    */
-  private _setGeometry(): void {
-    // Find the start of the current token for hover box placement.
+  private _getTokenPosition(): CodeEditor.IPosition | undefined {
     const editor = this._editor;
     const cursor = editor.getCursorPosition();
     const end = editor.getOffsetAt(cursor);
@@ -200,12 +200,22 @@ export class Tooltip extends Widget {
     const tokens = line.substring(0, end).split(/\W+/);
     const last = tokens[tokens.length - 1];
     const start = last ? end - last.length : end;
-    const position = editor.getPositionAt(start);
+    return editor.getPositionAt(start);
+  }
+
+  /**
+   * Set the geometry of the tooltip widget.
+   */
+  private _setGeometry(): void {
+    // determine position for hover box placement
+    const position = this._position ? this._position : this._getTokenPosition();
 
     if (!position) {
       return;
     }
 
+    const editor = this._editor;
+
     const anchor = editor.getCoordinateForPosition(position) as ClientRect;
     const style = window.getComputedStyle(this.node);
     const paddingLeft = parseInt(style.paddingLeft!, 10) || 0;
@@ -225,6 +235,7 @@ export class Tooltip extends Widget {
 
   private _content: IRenderMime.IRenderer | null = null;
   private _editor: CodeEditor.IEditor;
+  private _position: CodeEditor.IPosition | undefined;
   private _rendermime: IRenderMimeRegistry;
 }
 
@@ -255,5 +266,13 @@ export namespace Tooltip {
      * The rendermime instance used by the tooltip model.
      */
     rendermime: IRenderMimeRegistry;
+
+    /**
+     * Position at which the tooltip should be placed.
+     *
+     * If not given, the position of the first character
+     * in the current token will be used.
+     */
+    position?: CodeEditor.IPosition;
   }
 }