]>
Commit | Line | Data |
---|---|---|
70fda994 DI |
1 | /** |
2 | * @license MIT | |
3 | */ | |
4 | ||
dc165175 DI |
5 | import * as Mouse from './utils/Mouse'; |
6 | import * as Browser from './utils/Browser'; | |
70fda994 DI |
7 | import { CharMeasure } from './utils/CharMeasure'; |
8 | import { CircularList } from './utils/CircularList'; | |
b594407c | 9 | import { EventEmitter } from './EventEmitter'; |
ad3ae67e | 10 | import { ITerminal } from './Interfaces'; |
f7d6ab5f | 11 | import { SelectionModel } from './SelectionModel'; |
70fda994 | 12 | |
0dc3dd03 DI |
13 | /** |
14 | * The number of pixels the mouse needs to be above or below the viewport in | |
15 | * order to scroll at the maximum speed. | |
16 | */ | |
b8129910 | 17 | const DRAG_SCROLL_MAX_THRESHOLD = 50; |
0dc3dd03 DI |
18 | |
19 | /** | |
20 | * The maximum scrolling speed | |
21 | */ | |
b8129910 | 22 | const DRAG_SCROLL_MAX_SPEED = 15; |
0dc3dd03 DI |
23 | |
24 | /** | |
25 | * The number of milliseconds between drag scroll updates. | |
26 | */ | |
b8129910 | 27 | const DRAG_SCROLL_INTERVAL = 50; |
0dc3dd03 | 28 | |
9f271de8 | 29 | /** |
f380153f DI |
30 | * The amount of time before mousedown events are no longer stacked to create |
31 | * double/triple click events. | |
9f271de8 DI |
32 | */ |
33 | const CLEAR_MOUSE_DOWN_TIME = 400; | |
34 | ||
f380153f DI |
35 | /** |
36 | * The number of pixels in each direction that the mouse must move before | |
37 | * mousedown events are no longer stacked to create double/triple click events. | |
38 | */ | |
39 | const CLEAR_MOUSE_DISTANCE = 10; | |
40 | ||
7f2e94c3 DI |
41 | /** |
42 | * A string containing all characters that are considered word separated by the | |
43 | * double click to select work logic. | |
44 | */ | |
ca03f892 | 45 | const WORD_SEPARATORS = ' ()[]{}\'"'; |
7f2e94c3 | 46 | |
2621be81 DI |
47 | // TODO: Move these constants elsewhere, they belong in a buffer or buffer |
48 | // data/line class. | |
54e7f65d DI |
49 | const LINE_DATA_CHAR_INDEX = 1; |
50 | const LINE_DATA_WIDTH_INDEX = 2; | |
51 | ||
2012c8c9 DI |
52 | const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160); |
53 | const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g'); | |
54 | ||
320fb55d DI |
55 | /** |
56 | * Represents a position of a word on a line. | |
57 | */ | |
4b170ca4 DI |
58 | interface IWordPosition { |
59 | start: number; | |
60 | length: number; | |
61 | } | |
62 | ||
320fb55d DI |
63 | /** |
64 | * A selection mode, this drives how the selection behaves on mouse move. | |
65 | */ | |
66 | enum SelectionMode { | |
67 | NORMAL, | |
68 | WORD, | |
69 | LINE | |
70 | } | |
71 | ||
6075498c DI |
72 | /** |
73 | * A class that manages the selection of the terminal. With help from | |
74 | * SelectionModel, SelectionManager handles with all logic associated with | |
75 | * dealing with the selection, including handling mouse interaction, wide | |
76 | * characters and fetching the actual text within the selection. Rendering is | |
77 | * not handled by the SelectionManager but a 'refresh' event is fired when the | |
78 | * selection is ready to be redrawn. | |
79 | */ | |
b594407c | 80 | export class SelectionManager extends EventEmitter { |
fd91c5e1 | 81 | protected _model: SelectionModel; |
9f271de8 DI |
82 | |
83 | /** | |
84 | * The amount to scroll every drag scroll update (depends on how far the mouse | |
85 | * drag is above or below the terminal). | |
86 | */ | |
0dc3dd03 | 87 | private _dragScrollAmount: number; |
70fda994 | 88 | |
2621be81 | 89 | /** |
320fb55d | 90 | * The current selection mode. |
2621be81 | 91 | */ |
320fb55d | 92 | private _activeSelectionMode: SelectionMode; |
4b170ca4 | 93 | |
d0b603d0 DI |
94 | /** |
95 | * A setInterval timer that is active while the mouse is down whose callback | |
96 | * scrolls the viewport when necessary. | |
97 | */ | |
98 | private _dragScrollIntervalTimer: NodeJS.Timer; | |
99 | ||
b81c165b DI |
100 | /** |
101 | * The animation frame ID used for refreshing the selection. | |
102 | */ | |
13c401cb DI |
103 | private _refreshAnimationFrame: number; |
104 | ||
ab40908f | 105 | private _bufferTrimListener: any; |
70fda994 | 106 | private _mouseMoveListener: EventListener; |
ab40908f DI |
107 | private _mouseDownListener: EventListener; |
108 | private _mouseUpListener: EventListener; | |
70fda994 | 109 | |
ad3ae67e DI |
110 | constructor( |
111 | private _terminal: ITerminal, | |
112 | private _buffer: CircularList<any>, | |
113 | private _rowContainer: HTMLElement, | |
ad3ae67e DI |
114 | private _charMeasure: CharMeasure |
115 | ) { | |
b594407c | 116 | super(); |
ab40908f DI |
117 | this._initListeners(); |
118 | this.enable(); | |
9f271de8 | 119 | |
f7d6ab5f | 120 | this._model = new SelectionModel(_terminal); |
320fb55d | 121 | this._activeSelectionMode = SelectionMode.NORMAL; |
70fda994 DI |
122 | } |
123 | ||
d0b603d0 DI |
124 | /** |
125 | * Initializes listener variables. | |
126 | */ | |
ab40908f DI |
127 | private _initListeners() { |
128 | this._bufferTrimListener = (amount: number) => this._onTrim(amount); | |
70fda994 | 129 | this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event); |
ab40908f DI |
130 | this._mouseDownListener = event => this._onMouseDown(<MouseEvent>event); |
131 | this._mouseUpListener = event => this._onMouseUp(<MouseEvent>event); | |
ab40908f | 132 | } |
70fda994 | 133 | |
ab40908f DI |
134 | /** |
135 | * Disables the selection manager. This is useful for when terminal mouse | |
136 | * are enabled. | |
137 | */ | |
138 | public disable() { | |
4405f5e1 | 139 | this.clearSelection(); |
ab40908f DI |
140 | this._buffer.off('trim', this._bufferTrimListener); |
141 | this._rowContainer.removeEventListener('mousedown', this._mouseDownListener); | |
ab40908f DI |
142 | } |
143 | ||
144 | /** | |
145 | * Enable the selection manager. | |
146 | */ | |
147 | public enable() { | |
f380153f DI |
148 | // Only adjust the selection on trim, shiftElements is rarely used (only in |
149 | // reverseIndex) and delete in a splice is only ever used when the same | |
150 | // number of elements was just added. Given this is could actually be | |
151 | // beneficial to leave the selection as is for these cases. | |
ab40908f DI |
152 | this._buffer.on('trim', this._bufferTrimListener); |
153 | this._rowContainer.addEventListener('mousedown', this._mouseDownListener); | |
70fda994 DI |
154 | } |
155 | ||
9a86eeb3 DI |
156 | /** |
157 | * Sets the active buffer, this should be called when the alt buffer is | |
158 | * switched in or out. | |
159 | * @param buffer The active buffer. | |
160 | */ | |
161 | public setBuffer(buffer: CircularList<any>): void { | |
162 | this._buffer = buffer; | |
645a5920 | 163 | this.clearSelection(); |
9a86eeb3 DI |
164 | } |
165 | ||
1343b83f DI |
166 | /** |
167 | * Gets whether there is an active text selection. | |
168 | */ | |
169 | public get hasSelection(): boolean { | |
7b469407 DI |
170 | const start = this._model.finalSelectionStart; |
171 | const end = this._model.finalSelectionEnd; | |
172 | if (!start || !end) { | |
173 | return false; | |
174 | } | |
175 | return start[0] !== end[0] || start[1] !== end[1]; | |
1343b83f DI |
176 | } |
177 | ||
9f271de8 DI |
178 | /** |
179 | * Gets the text currently selected. | |
180 | */ | |
70fda994 | 181 | public get selectionText(): string { |
f7d6ab5f DI |
182 | const start = this._model.finalSelectionStart; |
183 | const end = this._model.finalSelectionEnd; | |
e29ab294 | 184 | if (!start || !end) { |
293ae18a | 185 | return ''; |
70fda994 | 186 | } |
43c796a7 | 187 | |
43c796a7 | 188 | // Get first row |
293ae18a | 189 | const startRowEndCol = start[1] === end[1] ? end[0] : null; |
32b34cbe | 190 | let result: string[] = []; |
ec61f3ac | 191 | result.push(this._translateBufferLineToString(this._buffer.get(start[1]), true, start[0], startRowEndCol)); |
43c796a7 DI |
192 | |
193 | // Get middle rows | |
293ae18a | 194 | for (let i = start[1] + 1; i <= end[1] - 1; i++) { |
346b5177 DI |
195 | const bufferLine = this._buffer.get(i); |
196 | const lineText = this._translateBufferLineToString(bufferLine, true); | |
197 | if (bufferLine.isWrapped) { | |
198 | result[result.length - 1] += lineText; | |
199 | } else { | |
200 | result.push(lineText); | |
201 | } | |
32b34cbe | 202 | } |
43c796a7 DI |
203 | |
204 | // Get final row | |
293ae18a | 205 | if (start[1] !== end[1]) { |
346b5177 DI |
206 | const bufferLine = this._buffer.get(end[1]); |
207 | const lineText = this._translateBufferLineToString(bufferLine, true, 0, end[0]); | |
208 | if (bufferLine.isWrapped) { | |
209 | result[result.length - 1] += lineText; | |
210 | } else { | |
211 | result.push(lineText); | |
212 | } | |
32b34cbe | 213 | } |
2b243182 | 214 | |
2012c8c9 DI |
215 | // Format string by replacing non-breaking space chars with regular spaces |
216 | // and joining the array into a multi-line string. | |
217 | const formattedResult = result.map(line => { | |
218 | return line.replace(ALL_NON_BREAKING_SPACE_REGEX, ' '); | |
cfe22e6f | 219 | }).join(Browser.isMSWindows ? '\r\n' : '\n'); |
2012c8c9 DI |
220 | |
221 | return formattedResult; | |
32b34cbe DI |
222 | } |
223 | ||
5d33727a DI |
224 | /** |
225 | * Clears the current terminal selection. | |
226 | */ | |
227 | public clearSelection(): void { | |
4405f5e1 | 228 | this._model.clearSelection(); |
5d33727a DI |
229 | this._removeMouseDownListeners(); |
230 | this.refresh(); | |
231 | } | |
232 | ||
d0b603d0 DI |
233 | /** |
234 | * Translates a buffer line to a string, with optional start and end columns. | |
235 | * Wide characters will count as two columns in the resulting string. This | |
236 | * function is useful for getting the actual text underneath the raw selection | |
237 | * position. | |
238 | * @param line The line being translated. | |
239 | * @param trimRight Whether to trim whitespace to the right. | |
240 | * @param startCol The column to start at. | |
241 | * @param endCol The column to end at. | |
242 | */ | |
ec61f3ac | 243 | private _translateBufferLineToString(line: any, trimRight: boolean, startCol: number = 0, endCol: number = null): string { |
32b34cbe | 244 | // TODO: This function should live in a buffer or buffer line class |
ec61f3ac DI |
245 | |
246 | // Get full line | |
247 | let lineString = ''; | |
54e7f65d DI |
248 | let widthAdjustedStartCol = startCol; |
249 | let widthAdjustedEndCol = endCol; | |
ec61f3ac | 250 | for (let i = 0; i < line.length; i++) { |
54e7f65d DI |
251 | const char = line[i]; |
252 | lineString += char[LINE_DATA_CHAR_INDEX]; | |
253 | // Adjust start and end cols for wide characters if they affect their | |
254 | // column indexes | |
255 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { | |
256 | if (startCol >= i) { | |
257 | widthAdjustedStartCol--; | |
258 | } | |
259 | if (endCol >= i) { | |
260 | widthAdjustedEndCol--; | |
261 | } | |
262 | } | |
ec61f3ac DI |
263 | } |
264 | ||
54e7f65d DI |
265 | // Calculate the final end col by trimming whitespace on the right of the |
266 | // line if needed. | |
d3865ad2 | 267 | let finalEndCol = widthAdjustedEndCol || line.length; |
ec61f3ac DI |
268 | if (trimRight) { |
269 | const rightWhitespaceIndex = lineString.search(/\s+$/); | |
a53a0acd DI |
270 | if (rightWhitespaceIndex !== -1) { |
271 | finalEndCol = Math.min(finalEndCol, rightWhitespaceIndex); | |
272 | } | |
ec61f3ac | 273 | // Return the empty string if only trimmed whitespace is selected |
54e7f65d | 274 | if (finalEndCol <= widthAdjustedStartCol) { |
ec61f3ac DI |
275 | return ''; |
276 | } | |
277 | } | |
278 | ||
54e7f65d | 279 | return lineString.substring(widthAdjustedStartCol, finalEndCol); |
70fda994 DI |
280 | } |
281 | ||
207c4cf9 | 282 | /** |
13c401cb | 283 | * Queues a refresh, redrawing the selection on the next opportunity. |
63d63c27 DI |
284 | * @param isNewSelection Whether the selection should be registered as a new |
285 | * selection on Linux. | |
207c4cf9 | 286 | */ |
63d63c27 | 287 | public refresh(isNewSelection?: boolean): void { |
8811d96a | 288 | // Queue the refresh for the renderer |
13c401cb DI |
289 | if (!this._refreshAnimationFrame) { |
290 | this._refreshAnimationFrame = window.requestAnimationFrame(() => this._refresh()); | |
291 | } | |
8811d96a | 292 | |
dc165175 DI |
293 | // If the platform is Linux and the refresh call comes from a mouse event, |
294 | // we need to update the selection for middle click to paste selection. | |
63d63c27 | 295 | if (Browser.isLinux && isNewSelection) { |
dc165175 DI |
296 | const selectionText = this.selectionText; |
297 | if (selectionText.length) { | |
298 | this.emit('newselection', this.selectionText); | |
299 | } | |
8811d96a | 300 | } |
13c401cb DI |
301 | } |
302 | ||
303 | /** | |
304 | * Fires the refresh event, causing consumers to pick it up and redraw the | |
305 | * selection state. | |
306 | */ | |
307 | private _refresh(): void { | |
308 | this._refreshAnimationFrame = null; | |
f7d6ab5f | 309 | this.emit('refresh', { start: this._model.finalSelectionStart, end: this._model.finalSelectionEnd }); |
25152e44 DI |
310 | } |
311 | ||
312 | /** | |
313 | * Selects all text within the terminal. | |
314 | */ | |
315 | public selectAll(): void { | |
f7d6ab5f | 316 | this._model.isSelectAllActive = true; |
25152e44 | 317 | this.refresh(); |
207c4cf9 DI |
318 | } |
319 | ||
320 | /** | |
321 | * Handle the buffer being trimmed, adjust the selection position. | |
322 | * @param amount The amount the buffer is being trimmed. | |
323 | */ | |
70fda994 | 324 | private _onTrim(amount: number) { |
f7d6ab5f DI |
325 | const needsRefresh = this._model.onTrim(amount); |
326 | if (needsRefresh) { | |
207c4cf9 | 327 | this.refresh(); |
207c4cf9 | 328 | } |
70fda994 DI |
329 | } |
330 | ||
d0b603d0 DI |
331 | /** |
332 | * Gets the 0-based [x, y] buffer coordinates of the current mouse event. | |
333 | * @param event The mouse event. | |
334 | */ | |
0dc3dd03 | 335 | private _getMouseBufferCoords(event: MouseEvent): [number, number] { |
b05814e6 | 336 | const coords = Mouse.getCoords(event, this._rowContainer, this._charMeasure, this._terminal.cols, this._terminal.rows, true); |
ad3ae67e DI |
337 | // Convert to 0-based |
338 | coords[0]--; | |
339 | coords[1]--; | |
340 | // Convert viewport coords to buffer coords | |
341 | coords[1] += this._terminal.ydisp; | |
342 | return coords; | |
b36d8780 DI |
343 | } |
344 | ||
d0b603d0 DI |
345 | /** |
346 | * Gets the amount the viewport should be scrolled based on how far out of the | |
347 | * terminal the mouse is. | |
348 | * @param event The mouse event. | |
349 | */ | |
0dc3dd03 DI |
350 | private _getMouseEventScrollAmount(event: MouseEvent): number { |
351 | let offset = Mouse.getCoordsRelativeToElement(event, this._rowContainer)[1]; | |
352 | const terminalHeight = this._terminal.rows * this._charMeasure.height; | |
353 | if (offset >= 0 && offset <= terminalHeight) { | |
354 | return 0; | |
355 | } | |
356 | if (offset > terminalHeight) { | |
357 | offset -= terminalHeight; | |
358 | } | |
359 | ||
360 | offset = Math.min(Math.max(offset, -DRAG_SCROLL_MAX_THRESHOLD), DRAG_SCROLL_MAX_THRESHOLD); | |
361 | offset /= DRAG_SCROLL_MAX_THRESHOLD; | |
362 | return (offset / Math.abs(offset)) + Math.round(offset * (DRAG_SCROLL_MAX_SPEED - 1)); | |
363 | } | |
364 | ||
e63fdf58 DI |
365 | /** |
366 | * Handles te mousedown event, setting up for a new selection. | |
367 | * @param event The mousedown event. | |
368 | */ | |
70fda994 | 369 | private _onMouseDown(event: MouseEvent) { |
0dc3dd03 DI |
370 | // Only action the primary button |
371 | if (event.button !== 0) { | |
372 | return; | |
373 | } | |
374 | ||
2a0f44b0 DI |
375 | // Tell the browser not to start a regular selection |
376 | event.preventDefault(); | |
377 | ||
b8129910 DI |
378 | // Reset drag scroll state |
379 | this._dragScrollAmount = 0; | |
380 | ||
db8ded2a DI |
381 | if (event.shiftKey) { |
382 | this._onShiftClick(event); | |
383 | } else { | |
9bd52820 | 384 | if (event.detail === 1) { |
db8ded2a | 385 | this._onSingleClick(event); |
9bd52820 | 386 | } else if (event.detail === 2) { |
db8ded2a | 387 | this._onDoubleClick(event); |
9bd52820 | 388 | } else if (event.detail === 3) { |
db8ded2a DI |
389 | this._onTripleClick(event); |
390 | } | |
9f271de8 | 391 | } |
e29ab294 | 392 | |
b8129910 | 393 | this._addMouseDownListeners(); |
8811d96a | 394 | this.refresh(true); |
b8129910 DI |
395 | } |
396 | ||
397 | /** | |
398 | * Adds listeners when mousedown is triggered. | |
399 | */ | |
400 | private _addMouseDownListeners(): void { | |
e29ab294 DI |
401 | // Listen on the document so that dragging outside of viewport works |
402 | this._rowContainer.ownerDocument.addEventListener('mousemove', this._mouseMoveListener); | |
403 | this._rowContainer.ownerDocument.addEventListener('mouseup', this._mouseUpListener); | |
2621be81 | 404 | this._dragScrollIntervalTimer = setInterval(() => this._dragScroll(), DRAG_SCROLL_INTERVAL); |
b8129910 DI |
405 | } |
406 | ||
407 | /** | |
408 | * Removes the listeners that are registered when mousedown is triggered. | |
409 | */ | |
410 | private _removeMouseDownListeners(): void { | |
411 | this._rowContainer.ownerDocument.removeEventListener('mousemove', this._mouseMoveListener); | |
412 | this._rowContainer.ownerDocument.removeEventListener('mouseup', this._mouseUpListener); | |
413 | clearInterval(this._dragScrollIntervalTimer); | |
414 | this._dragScrollIntervalTimer = null; | |
9f271de8 DI |
415 | } |
416 | ||
d0b603d0 DI |
417 | /** |
418 | * Performs a shift click, setting the selection end position to the mouse | |
419 | * position. | |
420 | * @param event The mouse event. | |
421 | */ | |
db8ded2a DI |
422 | private _onShiftClick(event: MouseEvent): void { |
423 | if (this._model.selectionStart) { | |
424 | this._model.selectionEnd = this._getMouseBufferCoords(event); | |
425 | } | |
426 | } | |
427 | ||
d0b603d0 DI |
428 | /** |
429 | * Performs a single click, resetting relevant state and setting the selection | |
430 | * start position. | |
431 | * @param event The mouse event. | |
432 | */ | |
9f271de8 | 433 | private _onSingleClick(event: MouseEvent): void { |
f7d6ab5f DI |
434 | this._model.selectionStartLength = 0; |
435 | this._model.isSelectAllActive = false; | |
320fb55d | 436 | this._activeSelectionMode = SelectionMode.NORMAL; |
f7d6ab5f DI |
437 | this._model.selectionStart = this._getMouseBufferCoords(event); |
438 | if (this._model.selectionStart) { | |
439 | this._model.selectionEnd = null; | |
54e7f65d DI |
440 | // If the mouse is over the second half of a wide character, adjust the |
441 | // selection to cover the whole character | |
442 | const char = this._buffer.get(this._model.selectionStart[1])[this._model.selectionStart[0]]; | |
443 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { | |
444 | this._model.selectionStart[0]++; | |
445 | } | |
70fda994 DI |
446 | } |
447 | } | |
448 | ||
d0b603d0 DI |
449 | /** |
450 | * Performs a double click, selecting the current work. | |
451 | * @param event The mouse event. | |
452 | */ | |
9f271de8 DI |
453 | private _onDoubleClick(event: MouseEvent): void { |
454 | const coords = this._getMouseBufferCoords(event); | |
455 | if (coords) { | |
320fb55d | 456 | this._activeSelectionMode = SelectionMode.WORD; |
9f271de8 DI |
457 | this._selectWordAt(coords); |
458 | } | |
459 | } | |
460 | ||
d0b603d0 DI |
461 | /** |
462 | * Performs a triple click, selecting the current line and activating line | |
463 | * select mode. | |
464 | * @param event The mouse event. | |
465 | */ | |
9f271de8 DI |
466 | private _onTripleClick(event: MouseEvent): void { |
467 | const coords = this._getMouseBufferCoords(event); | |
468 | if (coords) { | |
320fb55d | 469 | this._activeSelectionMode = SelectionMode.LINE; |
9f271de8 DI |
470 | this._selectLineAt(coords[1]); |
471 | } | |
472 | } | |
473 | ||
e63fdf58 DI |
474 | /** |
475 | * Handles the mousemove event when the mouse button is down, recording the | |
476 | * end of the selection and refreshing the selection. | |
477 | * @param event The mousemove event. | |
478 | */ | |
70fda994 | 479 | private _onMouseMove(event: MouseEvent) { |
2621be81 DI |
480 | // Record the previous position so we know whether to redraw the selection |
481 | // at the end. | |
482 | const previousSelectionEnd = this._model.selectionEnd ? [this._model.selectionEnd[0], this._model.selectionEnd[1]] : null; | |
483 | ||
484 | // Set the initial selection end based on the mouse coordinates | |
485 | this._model.selectionEnd = this._getMouseBufferCoords(event); | |
486 | ||
487 | // Select the entire line if line select mode is active. | |
320fb55d | 488 | if (this._activeSelectionMode === SelectionMode.LINE) { |
5bc11121 DI |
489 | if (this._model.selectionEnd[1] < this._model.selectionStart[1]) { |
490 | this._model.selectionEnd[0] = 0; | |
491 | } else { | |
492 | this._model.selectionEnd[0] = this._terminal.cols; | |
493 | } | |
320fb55d | 494 | } else if (this._activeSelectionMode === SelectionMode.WORD) { |
4b170ca4 DI |
495 | this._selectToWordAt(this._model.selectionEnd); |
496 | } | |
497 | ||
2621be81 | 498 | // Determine the amount of scrolling that will happen. |
0dc3dd03 | 499 | this._dragScrollAmount = this._getMouseEventScrollAmount(event); |
2621be81 | 500 | |
0dc3dd03 | 501 | // If the cursor was above or below the viewport, make sure it's at the |
2621be81 | 502 | // start or end of the viewport respectively. |
0dc3dd03 | 503 | if (this._dragScrollAmount > 0) { |
f7d6ab5f | 504 | this._model.selectionEnd[0] = this._terminal.cols - 1; |
0dc3dd03 | 505 | } else if (this._dragScrollAmount < 0) { |
f7d6ab5f | 506 | this._model.selectionEnd[0] = 0; |
0dc3dd03 | 507 | } |
54e7f65d DI |
508 | |
509 | // If the character is a wide character include the cell to the right in the | |
5bc11121 DI |
510 | // selection. Note that selections at the very end of the line will never |
511 | // have a character. | |
71477874 DI |
512 | if (this._model.selectionEnd[1] < this._buffer.length) { |
513 | const char = this._buffer.get(this._model.selectionEnd[1])[this._model.selectionEnd[0]]; | |
514 | if (char && char[2] === 0) { | |
515 | this._model.selectionEnd[0]++; | |
516 | } | |
54e7f65d DI |
517 | } |
518 | ||
2621be81 DI |
519 | // Only draw here if the selection changes. |
520 | if (!previousSelectionEnd || | |
521 | previousSelectionEnd[0] !== this._model.selectionEnd[0] || | |
522 | previousSelectionEnd[1] !== this._model.selectionEnd[1]) { | |
dc165175 | 523 | this.refresh(true); |
2621be81 | 524 | } |
70fda994 DI |
525 | } |
526 | ||
d0b603d0 DI |
527 | /** |
528 | * The callback that occurs every DRAG_SCROLL_INTERVAL ms that does the | |
529 | * scrolling of the viewport. | |
530 | */ | |
0dc3dd03 DI |
531 | private _dragScroll() { |
532 | if (this._dragScrollAmount) { | |
533 | this._terminal.scrollDisp(this._dragScrollAmount, false); | |
534 | // Re-evaluate selection | |
535 | if (this._dragScrollAmount > 0) { | |
f7d6ab5f | 536 | this._model.selectionEnd = [this._terminal.cols - 1, this._terminal.ydisp + this._terminal.rows]; |
0dc3dd03 | 537 | } else { |
f7d6ab5f | 538 | this._model.selectionEnd = [0, this._terminal.ydisp]; |
0dc3dd03 DI |
539 | } |
540 | this.refresh(); | |
541 | } | |
542 | } | |
543 | ||
e63fdf58 | 544 | /** |
b8129910 | 545 | * Handles the mouseup event, removing the mousedown listeners. |
e63fdf58 DI |
546 | * @param event The mouseup event. |
547 | */ | |
70fda994 | 548 | private _onMouseUp(event: MouseEvent) { |
b8129910 | 549 | this._removeMouseDownListeners(); |
70fda994 | 550 | } |
597c6939 | 551 | |
cb6533f3 DI |
552 | /** |
553 | * Converts a viewport column to the character index on the buffer line, the | |
554 | * latter takes into account wide characters. | |
d3865ad2 | 555 | * @param coords The coordinates to find the 2 index for. |
cb6533f3 | 556 | */ |
11cec31d | 557 | private _convertViewportColToCharacterIndex(bufferLine: any, coords: [number, number]): number { |
cb6533f3 | 558 | let charIndex = coords[0]; |
d9991b25 | 559 | for (let i = 0; coords[0] >= i; i++) { |
11cec31d | 560 | const char = bufferLine[i]; |
cb6533f3 DI |
561 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { |
562 | charIndex--; | |
563 | } | |
564 | } | |
565 | return charIndex; | |
566 | } | |
567 | ||
597c6939 | 568 | /** |
4b170ca4 | 569 | * Gets positional information for the word at the coordinated specified. |
597c6939 DI |
570 | * @param coords The coordinates to get the word at. |
571 | */ | |
4b170ca4 | 572 | private _getWordAt(coords: [number, number]): IWordPosition { |
11cec31d DI |
573 | const bufferLine = this._buffer.get(coords[1]); |
574 | const line = this._translateBufferLineToString(bufferLine, false); | |
cb6533f3 DI |
575 | |
576 | // Get actual index, taking into consideration wide characters | |
11cec31d | 577 | let endIndex = this._convertViewportColToCharacterIndex(bufferLine, coords); |
cb6533f3 | 578 | let startIndex = endIndex; |
cb6533f3 DI |
579 | |
580 | // Record offset to be used later | |
581 | const charOffset = coords[0] - startIndex; | |
11cec31d DI |
582 | let leftWideCharCount = 0; |
583 | let rightWideCharCount = 0; | |
cb6533f3 | 584 | |
cb6533f3 | 585 | if (line.charAt(startIndex) === ' ') { |
11cec31d | 586 | // Expand until non-whitespace is hit |
cb6533f3 DI |
587 | while (startIndex > 0 && line.charAt(startIndex - 1) === ' ') { |
588 | startIndex--; | |
589 | } | |
590 | while (endIndex < line.length && line.charAt(endIndex + 1) === ' ') { | |
591 | endIndex++; | |
592 | } | |
cb6533f3 | 593 | } else { |
11cec31d DI |
594 | // Expand until whitespace is hit. This algorithm works by scanning left |
595 | // and right from the starting position, keeping both the index format | |
596 | // (line) and the column format (bufferLine) in sync. When a wide | |
597 | // character is hit, it is recorded and the column index is adjusted. | |
d9991b25 DI |
598 | let startCol = coords[0]; |
599 | let endCol = coords[0]; | |
600 | // Consider the initial position, skip it and increment the wide char | |
601 | // variable | |
11cec31d | 602 | if (bufferLine[startCol][LINE_DATA_WIDTH_INDEX] === 0) { |
d9991b25 DI |
603 | leftWideCharCount++; |
604 | startCol--; | |
605 | } | |
11cec31d | 606 | if (bufferLine[endCol][LINE_DATA_WIDTH_INDEX] === 2) { |
d9991b25 DI |
607 | rightWideCharCount++; |
608 | endCol++; | |
609 | } | |
cb6533f3 | 610 | // Expand the string in both directions until a space is hit |
7f2e94c3 | 611 | while (startIndex > 0 && !this._isCharWordSeparator(line.charAt(startIndex - 1))) { |
11cec31d DI |
612 | if (bufferLine[startCol - 1][LINE_DATA_WIDTH_INDEX] === 0) { |
613 | // If the next character is a wide char, record it and skip the column | |
d9991b25 DI |
614 | leftWideCharCount++; |
615 | startCol--; | |
cb6533f3 DI |
616 | } |
617 | startIndex--; | |
d9991b25 | 618 | startCol--; |
cb6533f3 | 619 | } |
7f2e94c3 | 620 | while (endIndex + 1 < line.length && !this._isCharWordSeparator(line.charAt(endIndex + 1))) { |
11cec31d DI |
621 | if (bufferLine[endCol + 1][LINE_DATA_WIDTH_INDEX] === 2) { |
622 | // If the next character is a wide char, record it and skip the column | |
d9991b25 DI |
623 | rightWideCharCount++; |
624 | endCol++; | |
cb6533f3 DI |
625 | } |
626 | endIndex++; | |
d9991b25 | 627 | endCol++; |
cb6533f3 | 628 | } |
597c6939 | 629 | } |
11cec31d | 630 | |
4b170ca4 DI |
631 | const start = startIndex + charOffset - leftWideCharCount; |
632 | const length = Math.min(endIndex - startIndex + leftWideCharCount + rightWideCharCount + 1/*include endIndex char*/, this._terminal.cols); | |
633 | return {start, length}; | |
634 | } | |
635 | ||
636 | /** | |
637 | * Selects the word at the coordinates specified. | |
638 | * @param coords The coordinates to get the word at. | |
639 | */ | |
640 | protected _selectWordAt(coords: [number, number]): void { | |
641 | const wordPosition = this._getWordAt(coords); | |
642 | this._model.selectionStart = [wordPosition.start, coords[1]]; | |
643 | this._model.selectionStartLength = wordPosition.length; | |
644 | } | |
645 | ||
646 | /** | |
647 | * Sets the selection end to the word at the coordinated specified. | |
648 | * @param coords The coordinates to get the word at. | |
649 | */ | |
650 | private _selectToWordAt(coords: [number, number]): void { | |
651 | const wordPosition = this._getWordAt(coords); | |
652 | this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]]; | |
597c6939 | 653 | } |
9f271de8 | 654 | |
7f2e94c3 DI |
655 | /** |
656 | * Gets whether the character is considered a word separator by the select | |
657 | * word logic. | |
658 | * @param char The character to check. | |
659 | */ | |
660 | private _isCharWordSeparator(char: string): boolean { | |
661 | return WORD_SEPARATORS.indexOf(char) >= 0; | |
662 | } | |
663 | ||
d0b603d0 DI |
664 | /** |
665 | * Selects the line specified. | |
666 | * @param line The line index. | |
667 | */ | |
fd91c5e1 | 668 | protected _selectLineAt(line: number): void { |
f7d6ab5f DI |
669 | this._model.selectionStart = [0, line]; |
670 | this._model.selectionStartLength = this._terminal.cols; | |
9f271de8 | 671 | } |
70fda994 | 672 | } |