]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/SelectionManager.ts
Create new entries when shifting
[mirror_xterm.js.git] / src / SelectionManager.ts
CommitLineData
70fda994
DI
1/**
2 * @license MIT
3 */
4
5import { CharMeasure } from './utils/CharMeasure';
6import { CircularList } from './utils/CircularList';
7import * as Mouse from './utils/Mouse';
8
9export class SelectionManager {
10 private _selectionStart: [number, number];
11 private _selectionEnd: [number, number];
12
13 private _buffer: CircularList<any>;
14 private _rowContainer: HTMLElement;
15 private _charMeasure: CharMeasure;
16
17 private _mouseMoveListener: EventListener;
18
19 constructor(buffer: CircularList<any>, rowContainer: HTMLElement, charMeasure: CharMeasure) {
20 this._rowContainer = rowContainer;
21 this._buffer = buffer;
22 this._charMeasure = charMeasure;
23 this._attachListeners();
24 }
25
26 private _attachListeners() {
27 this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event);
28
29 this._buffer.on('trim', amount => this._onTrim(amount));
30 this._rowContainer.addEventListener('mousedown', event => this._onMouseDown(event));
31 this._rowContainer.addEventListener('mouseup', event => this._onMouseUp(event));
32 }
33
34 public get selectionText(): string {
35 if (!this._selectionStart || !this._selectionEnd) {
36 return null;
37 }
38 return '';
39 }
40
41 private _onTrim(amount: number) {
b36d8780
DI
42 // TODO: Somehow map the selection coordinates with the list that is constantly being trimmed
43 // Maybe we need an ID in the CircularList that starts from 0 for the first entry and increments
70fda994
DI
44 console.log('trimmed: ' + amount);
45 }
46
b36d8780
DI
47 private _getMouseBufferCoords(event: MouseEvent) {
48 // TODO: Take into account the current terminal viewport when fetching coordinates
49 return Mouse.getCoords(event, this._rowContainer, this._charMeasure);
50 }
51
70fda994 52 private _onMouseDown(event: MouseEvent) {
b36d8780 53 this._selectionStart = this._getMouseBufferCoords(event);
70fda994
DI
54 if (this._selectionStart) {
55 this._rowContainer.addEventListener('mousemove', this._mouseMoveListener);
56 }
57 }
58
59 private _onMouseMove(event: MouseEvent) {
b36d8780 60 this._selectionEnd = this._getMouseBufferCoords(event);
70fda994
DI
61 }
62
63 private _onMouseUp(event: MouseEvent) {
64 console.log('mouseup');
65 console.log('start', this._selectionStart);
66 console.log('end', this._selectionEnd);
67 if (!this._selectionStart) {
68 return;
69 }
70 this._rowContainer.removeEventListener('mousemove', this._mouseMoveListener);
71 }
72}