From 597c6939c82a080e0f9be0e05c773770e7a4f3fd Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 20 May 2017 00:17:23 -0700 Subject: [PATCH] Handle basic double click select --- src/SelectionManager.ts | 35 +++++++++++++++++++++++++++++++++-- src/xterm.css | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 95a2fa1..c8cd838 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -32,6 +32,7 @@ export class SelectionManager extends EventEmitter { this._buffer.on('trim', amount => this._onTrim(amount)); this._rowContainer.addEventListener('mousedown', event => this._onMouseDown(event)); this._rowContainer.addEventListener('mouseup', event => this._onMouseUp(event)); + this._rowContainer.addEventListener('dblclick', event => this._onDblclick(event)); } public get selectionText(): string { @@ -51,7 +52,7 @@ export class SelectionManager extends EventEmitter { if (start[1] !== end[1]) { result.push(this._translateBufferLineToString(this._buffer.get(end[1]), 0, end[1])); } - console.log('result: ' + result); + console.log('selectionText result: ' + result); return result.join('\n'); } @@ -62,7 +63,7 @@ export class SelectionManager extends EventEmitter { for (let i = startCol; i < endCol; i++) { result += line[i][1]; } - // TODO: Trim line? + // TODO: Trim line here instead of in handlers/Clipboard? return result; // TODO: Handle the double-width character case } @@ -138,4 +139,34 @@ export class SelectionManager extends EventEmitter { } this._rowContainer.removeEventListener('mousemove', this._mouseMoveListener); } + + private _onDblclick(event: MouseEvent) { + const coords = this._getMouseBufferCoords(event); + if (coords) { + this._selectWordAt(coords); + } + } + + /** + * Selects the word at the coordinates specified. Words are defined as all + * non-whitespace characters. + * @param coords The coordinates to get the word at. + */ + private _selectWordAt(coords: [number, number]): void { + // TODO: Handle double click and drag in both directions! + + const line = this._translateBufferLineToString(this._buffer.get(coords[1])); + // Expand the string in both directions until a space is hit + let startCol = coords[0]; + let endCol = coords[0]; + while (startCol > 0 && line.charAt(startCol - 1) !== ' ') { + startCol--; + } + while (endCol < line.length && line.charAt(endCol) !== ' ') { + endCol++; + } + this._selectionStart = [startCol, coords[1]]; + this._selectionEnd = [endCol, coords[1]]; + this.refresh(); + } } diff --git a/src/xterm.css b/src/xterm.css index 381dd53..602cc13 100644 --- a/src/xterm.css +++ b/src/xterm.css @@ -191,7 +191,7 @@ .terminal .xterm-selection div { position: absolute; - background-color: #777; + background-color: #555; } /* -- 2.39.2