]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/SelectionManager.ts
Merge pull request #926 from ficristo/search-fix
[mirror_xterm.js.git] / src / SelectionManager.ts
index 78494ae19304c195e0770a412de889a1eeec1433..f6fb44da58cf51bc033b7fb17fc653c83aa24157 100644 (file)
@@ -91,9 +91,12 @@ export class SelectionManager extends EventEmitter {
    */
   private _refreshAnimationFrame: number;
 
-  private _bufferTrimListener: any;
+  /**
+   * Whether selection is enabled.
+   */
+  private _enabled = true;
+
   private _mouseMoveListener: EventListener;
-  private _mouseDownListener: EventListener;
   private _mouseUpListener: EventListener;
 
   constructor(
@@ -114,10 +117,16 @@ export class SelectionManager extends EventEmitter {
    * Initializes listener variables.
    */
   private _initListeners() {
-    this._bufferTrimListener = (amount: number) => this._onTrim(amount);
     this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event);
-    this._mouseDownListener = event => this._onMouseDown(<MouseEvent>event);
     this._mouseUpListener = event => this._onMouseUp(<MouseEvent>event);
+
+    this._rowContainer.addEventListener('mousedown', event => this._onMouseDown(<MouseEvent>event));
+
+    // Only adjust the selection on trim, shiftElements is rarely used (only in
+    // reverseIndex) and delete in a splice is only ever used when the same
+    // number of elements was just added. Given this is could actually be
+    // beneficial to leave the selection as is for these cases.
+    this._buffer.on('trim', (amount: number) => this._onTrim(amount));
   }
 
   /**
@@ -126,20 +135,14 @@ export class SelectionManager extends EventEmitter {
    */
   public disable() {
     this.clearSelection();
-    this._buffer.off('trim', this._bufferTrimListener);
-    this._rowContainer.removeEventListener('mousedown', this._mouseDownListener);
+    this._enabled = false;
   }
 
   /**
    * Enable the selection manager.
    */
   public enable() {
-    // Only adjust the selection on trim, shiftElements is rarely used (only in
-    // reverseIndex) and delete in a splice is only ever used when the same
-    // number of elements was just added. Given this is could actually be
-    // beneficial to leave the selection as is for these cases.
-    this._buffer.on('trim', this._bufferTrimListener);
-    this._rowContainer.addEventListener('mousedown', this._mouseDownListener);
+    this._enabled = true;
   }
 
   /**
@@ -277,6 +280,10 @@ export class SelectionManager extends EventEmitter {
    */
   private _getMouseBufferCoords(event: MouseEvent): [number, number] {
     const coords = Mouse.getCoords(event, this._rowContainer, this._charMeasure, this._terminal.cols, this._terminal.rows, true);
+    if (!coords) {
+      return null;
+    }
+
     // Convert to 0-based
     coords[0]--;
     coords[1]--;
@@ -310,26 +317,45 @@ export class SelectionManager extends EventEmitter {
    * @param event The mousedown event.
    */
   private _onMouseDown(event: MouseEvent) {
+    // If we have selection, we want the context menu on right click even if the
+    // terminal is in mouse mode.
+    if (event.button === 2 && this.hasSelection) {
+      event.stopPropagation();
+      return;
+    }
+
     // Only action the primary button
     if (event.button !== 0) {
       return;
     }
 
+    // Allow selection when using a specific modifier key, even when disabled
+    if (!this._enabled) {
+      const shouldForceSelection = Browser.isMac && event.altKey;
+
+      if (!shouldForceSelection) {
+        return;
+      }
+
+      // Don't send the mouse down event to the current process, we want to select
+      event.stopPropagation();
+    }
+
     // Tell the browser not to start a regular selection
     event.preventDefault();
 
     // Reset drag scroll state
     this._dragScrollAmount = 0;
 
-    if (event.shiftKey) {
-      this._onShiftClick(event);
+    if (this._enabled && event.shiftKey) {
+      this._onIncrementalClick(event);
     } else {
       if (event.detail === 1) {
-          this._onSingleClick(event);
+        this._onSingleClick(event);
       } else if (event.detail === 2) {
-          this._onDoubleClick(event);
+        this._onDoubleClick(event);
       } else if (event.detail === 3) {
-          this._onTripleClick(event);
+        this._onTripleClick(event);
       }
     }
 
@@ -358,11 +384,11 @@ export class SelectionManager extends EventEmitter {
   }
 
   /**
-   * Performs a shift click, setting the selection end position to the mouse
+   * Performs an incremental click, setting the selection end position to the mouse
    * position.
    * @param event The mouse event.
    */
-  private _onShiftClick(event: MouseEvent): void {
+  private _onIncrementalClick(event: MouseEvent): void {
     if (this._model.selectionStart) {
       this._model.selectionEnd = this._getMouseBufferCoords(event);
     }
@@ -377,15 +403,25 @@ export class SelectionManager extends EventEmitter {
     this._model.selectionStartLength = 0;
     this._model.isSelectAllActive = false;
     this._activeSelectionMode = SelectionMode.NORMAL;
+
+    // Initialize the new selection
     this._model.selectionStart = this._getMouseBufferCoords(event);
-    if (this._model.selectionStart) {
-      this._model.selectionEnd = null;
-      // If the mouse is over the second half of a wide character, adjust the
-      // selection to cover the whole character
-      const char = this._buffer.get(this._model.selectionStart[1])[this._model.selectionStart[0]];
-      if (char[LINE_DATA_WIDTH_INDEX] === 0) {
-        this._model.selectionStart[0]++;
-      }
+    if (!this._model.selectionStart) {
+      return;
+    }
+    this._model.selectionEnd = null;
+
+    // Ensure the line exists
+    const line = this._buffer.get(this._model.selectionStart[1]);
+    if (!line) {
+      return;
+    }
+
+    // If the mouse is over the second half of a wide character, adjust the
+    // selection to cover the whole character
+    const char = line[this._model.selectionStart[0]];
+    if (char[LINE_DATA_WIDTH_INDEX] === 0) {
+      this._model.selectionStart[0]++;
     }
   }
 
@@ -426,6 +462,10 @@ export class SelectionManager extends EventEmitter {
 
     // Set the initial selection end based on the mouse coordinates
     this._model.selectionEnd = this._getMouseBufferCoords(event);
+    if (!this._model.selectionEnd) {
+      this.refresh(true);
+      return;
+    }
 
     // Select the entire line if line select mode is active.
     if (this._activeSelectionMode === SelectionMode.LINE) {
@@ -461,8 +501,8 @@ export class SelectionManager extends EventEmitter {
 
     // Only draw here if the selection changes.
     if (!previousSelectionEnd ||
-        previousSelectionEnd[0] !== this._model.selectionEnd[0] ||
-        previousSelectionEnd[1] !== this._model.selectionEnd[1]) {
+      previousSelectionEnd[0] !== this._model.selectionEnd[0] ||
+      previousSelectionEnd[1] !== this._model.selectionEnd[1]) {
       this.refresh(true);
     }
   }
@@ -585,7 +625,7 @@ export class SelectionManager extends EventEmitter {
 
     const start = startIndex + charOffset - leftWideCharCount;
     const length = Math.min(endIndex - startIndex + leftWideCharCount + rightWideCharCount + 1/*include endIndex char*/, this._terminal.cols);
-    return {start, length};
+    return { start, length };
   }
 
   /**