-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @license MIT | ||
*/ | ||
|
||
import { CharMeasure } from './utils/CharMeasure'; | ||
import { CircularList } from './utils/CircularList'; | ||
import * as Mouse from './utils/Mouse'; | ||
|
||
export class SelectionManager { | ||
private _selectionStart: [number, number]; | ||
private _selectionEnd: [number, number]; | ||
|
||
private _buffer: CircularList<any>; | ||
private _rowContainer: HTMLElement; | ||
private _charMeasure: CharMeasure; | ||
|
||
private _mouseMoveListener: EventListener; | ||
|
||
constructor(buffer: CircularList<any>, rowContainer: HTMLElement, charMeasure: CharMeasure) { | ||
this._rowContainer = rowContainer; | ||
this._buffer = buffer; | ||
this._charMeasure = charMeasure; | ||
this._attachListeners(); | ||
} | ||
|
||
private _attachListeners() { | ||
this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event); | ||
|
||
this._buffer.on('trim', amount => this._onTrim(amount)); | ||
this._rowContainer.addEventListener('mousedown', event => this._onMouseDown(event)); | ||
this._rowContainer.addEventListener('mouseup', event => this._onMouseUp(event)); | ||
} | ||
|
||
public get selectionText(): string { | ||
if (!this._selectionStart || !this._selectionEnd) { | ||
return null; | ||
} | ||
return ''; | ||
} | ||
|
||
private _onTrim(amount: number) { | ||
console.log('trimmed: ' + amount); | ||
} | ||
|
||
private _onMouseDown(event: MouseEvent) { | ||
this._selectionStart = Mouse.getCoords(event, this._rowContainer, this._charMeasure); | ||
if (this._selectionStart) { | ||
this._rowContainer.addEventListener('mousemove', this._mouseMoveListener); | ||
} | ||
} | ||
|
||
private _onMouseMove(event: MouseEvent) { | ||
this._selectionEnd = Mouse.getCoords(event, this._rowContainer, this._charMeasure); | ||
} | ||
|
||
private _onMouseUp(event: MouseEvent) { | ||
console.log('mouseup'); | ||
console.log('start', this._selectionStart); | ||
console.log('end', this._selectionEnd); | ||
if (!this._selectionStart) { | ||
return; | ||
} | ||
this._rowContainer.removeEventListener('mousemove', this._mouseMoveListener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @license MIT | ||
*/ | ||
|
||
import { CharMeasure } from './CharMeasure'; | ||
|
||
export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure): [number, number] { | ||
// ignore browsers without pageX for now | ||
if (event.pageX == null) { | ||
return null; | ||
} | ||
|
||
let x = event.pageX; | ||
let y = event.pageY; | ||
let el = rowContainer; | ||
|
||
// should probably check offsetParent | ||
// but this is more portable | ||
while (el && el !== self.document.documentElement) { | ||
x -= el.offsetLeft; | ||
y -= el.offsetTop; | ||
el = 'offsetParent' in el ? <HTMLElement>el.offsetParent : <HTMLElement>el.parentElement; | ||
} | ||
|
||
// convert to cols/rows | ||
x = Math.ceil(x / charMeasure.width); | ||
y = Math.ceil(y / charMeasure.height); | ||
|
||
return [x, y]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters