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