From 18b86a1cbc4cb1d99f67599992985b733fa6e1a8 Mon Sep 17 00:00:00 2001 From: Bruno Ribeito Date: Wed, 8 Nov 2017 23:27:00 +0000 Subject: [PATCH] Do the selection even if there is already a selection --- src/Interfaces.ts | 1 + src/SelectionManager.ts | 26 +++++++++++++++++++++++--- src/handlers/Clipboard.ts | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Interfaces.ts b/src/Interfaces.ts index ea1d00a0..69e7fcb2 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -201,6 +201,7 @@ export interface ISelectionManager { enable(): void; setBuffer(buffer: IBuffer): void; setSelection(row: number, col: number, length: number): void; + isClickInSelection(event: MouseEvent): boolean; selectWordAtCursor(event: MouseEvent): void; } diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 1fccc8b1..3bf366ce 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -249,6 +249,22 @@ export class SelectionManager extends EventEmitter implements ISelectionManager this.emit('refresh', { start: this._model.finalSelectionStart, end: this._model.finalSelectionEnd }); } + /** + * Checks if the current click was inside the current selection + * @param event The mouse event + */ + public isClickInSelection(event: MouseEvent): boolean { + const coords = this._getMouseBufferCoords(event); + const start = this._model.finalSelectionStart; + const end = this._model.finalSelectionEnd; + + if (!start || !end) { + return false; + } + + return (start[1] < coords[1] && end[1] > coords[1]) || (start[1] === coords[1] && coords[0] > start[0]) || (end[1] === coords[1] && coords[0] < end[0]); + } + /** * Selects word at the current mouse event coordinates. * @param event The mouse event. @@ -256,10 +272,14 @@ export class SelectionManager extends EventEmitter implements ISelectionManager public selectWordAtCursor(event: MouseEvent): void { const coords = this._getMouseBufferCoords(event); if (coords) { - this._selectWordAt(coords, false); - this.refresh(true); + const wordPosition = this._getWordAt(coords, true); + if (wordPosition) { + this._model.selectionStart = [wordPosition.start, coords[1]]; + this._model.selectionStartLength = wordPosition.length; + this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]]; + this.refresh(true); + } } - } /** * Selects all text within the terminal. diff --git a/src/handlers/Clipboard.ts b/src/handlers/Clipboard.ts index 86a604f2..6814d269 100644 --- a/src/handlers/Clipboard.ts +++ b/src/handlers/Clipboard.ts @@ -108,7 +108,7 @@ export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextA export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager, shouldSelectWord: boolean): void { moveTextAreaUnderMouseCursor(ev, textarea); - if (shouldSelectWord && !selectionManager.hasSelection) + if (shouldSelectWord && (!selectionManager.hasSelection || !selectionManager.isClickInSelection(ev))) selectionManager.selectWordAtCursor(ev); // Get textarea ready to copy from the context menu