Fix tests/lint, use element instead of event.target

This commit is contained in:
Daniel Imms
2018-12-09 09:59:33 -08:00
parent 7f18199985
commit 6b9fb48898
3 changed files with 14 additions and 22 deletions
+1 -1
View File
@@ -246,7 +246,7 @@ export interface ILinkifierAccessor {
} }
export interface IMouseHelper { export interface IMouseHelper {
getCoords(event: { pageX: number, pageY: number }, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number]; getCoords(event: { clientX: number, clientY: number }, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number];
getRawByteCoords(event: MouseEvent, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number): { x: number, y: number }; getRawByteCoords(event: MouseEvent, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number): { x: number, y: number };
} }
+7 -13
View File
@@ -37,34 +37,28 @@ describe('MouseHelper.getCoords', () => {
describe('when charMeasure is not initialized', () => { describe('when charMeasure is not initialized', () => {
it('should return null', () => { it('should return null', () => {
charMeasure = new MockCharMeasure(); charMeasure = new MockCharMeasure();
assert.equal(mouseHelper.getCoords({ pageX: 0, pageY: 0 }, document.createElement('div'), charMeasure, 1, 10, 10), null); assert.equal(mouseHelper.getCoords({ clientX: 0, clientY: 0 }, document.createElement('div'), charMeasure, 1, 10, 10), null);
});
});
describe('when pageX/pageY are not supported', () => {
it('should return null', () => {
assert.equal(mouseHelper.getCoords({ pageX: undefined, pageY: undefined }, document.createElement('div'), charMeasure, 1, 10, 10), null);
}); });
}); });
it('should return the cell that was clicked', () => { it('should return the cell that was clicked', () => {
let coords: [number, number]; let coords: [number, number];
coords = mouseHelper.getCoords({ pageX: CHAR_WIDTH / 2, pageY: CHAR_HEIGHT / 2 }, document.createElement('div'), charMeasure, 1, 10, 10); coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH / 2, clientY: CHAR_HEIGHT / 2 }, document.createElement('div'), charMeasure, 1, 10, 10);
assert.deepEqual(coords, [1, 1]); assert.deepEqual(coords, [1, 1]);
coords = mouseHelper.getCoords({ pageX: CHAR_WIDTH, pageY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 1, 10, 10); coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH, clientY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 1, 10, 10);
assert.deepEqual(coords, [1, 1]); assert.deepEqual(coords, [1, 1]);
coords = mouseHelper.getCoords({ pageX: CHAR_WIDTH, pageY: CHAR_HEIGHT + 1 }, document.createElement('div'), charMeasure, 1, 10, 10); coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH, clientY: CHAR_HEIGHT + 1 }, document.createElement('div'), charMeasure, 1, 10, 10);
assert.deepEqual(coords, [1, 2]); assert.deepEqual(coords, [1, 2]);
coords = mouseHelper.getCoords({ pageX: CHAR_WIDTH + 1, pageY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 1, 10, 10); coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH + 1, clientY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 1, 10, 10);
assert.deepEqual(coords, [2, 1]); assert.deepEqual(coords, [2, 1]);
}); });
it('should ensure the coordinates are returned within the terminal bounds', () => { it('should ensure the coordinates are returned within the terminal bounds', () => {
let coords: [number, number]; let coords: [number, number];
coords = mouseHelper.getCoords({ pageX: -1, pageY: -1 }, document.createElement('div'), charMeasure, 1, 10, 10); coords = mouseHelper.getCoords({ clientX: -1, clientY: -1 }, document.createElement('div'), charMeasure, 1, 10, 10);
assert.deepEqual(coords, [1, 1]); assert.deepEqual(coords, [1, 1]);
// Event are double the cols/rows // Event are double the cols/rows
coords = mouseHelper.getCoords({ pageX: CHAR_WIDTH * 20, pageY: CHAR_HEIGHT * 20 }, document.createElement('div'), charMeasure, 1, 10, 10); coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH * 20, clientY: CHAR_HEIGHT * 20 }, document.createElement('div'), charMeasure, 1, 10, 10);
assert.deepEqual(coords, [10, 10], 'coordinates should never come back as larger than the terminal'); assert.deepEqual(coords, [10, 10], 'coordinates should never come back as larger than the terminal');
}); });
}); });
+6 -8
View File
@@ -3,21 +3,19 @@
* @license MIT * @license MIT
*/ */
import { ICharMeasure } from '../Types'; import { ICharMeasure, IMouseHelper } from '../Types';
import { IRenderer } from '../renderer/Types'; import { IRenderer } from '../renderer/Types';
export class MouseHelper { export class MouseHelper implements IMouseHelper {
constructor(private _renderer: IRenderer) {} constructor(private _renderer: IRenderer) {}
public setRenderer(renderer: IRenderer): void { public setRenderer(renderer: IRenderer): void {
this._renderer = renderer; this._renderer = renderer;
} }
public static getCoordsRelativeToElement(event: {target: EventTarget, clientX: number, clientY: number}, element: HTMLElement): [number, number] { public static getCoordsRelativeToElement(event: {clientX: number, clientY: number}, element: HTMLElement): [number, number] {
let rect = event.target.getBoundingClientRect(); const rect = element.getBoundingClientRect();
let x = event.clientX - rect.left; return [event.clientX - rect.left, event.clientY - rect.top];
let y = event.clientY - rect.top;
return [x, y];
} }
/** /**
@@ -33,7 +31,7 @@ export class MouseHelper {
* apply an offset to the x value such that the left half of the cell will * apply an offset to the x value such that the left half of the cell will
* select that cell and the right half will select the next cell. * select that cell and the right half will select the next cell.
*/ */
public getCoords(event: {target: EventTarget, pageX: number, pageY: number}, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number] { public getCoords(event: {clientX: number, clientY: number}, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number] {
// Coordinates cannot be measured if charMeasure has not been initialized // Coordinates cannot be measured if charMeasure has not been initialized
if (!charMeasure.width || !charMeasure.height) { if (!charMeasure.width || !charMeasure.height) {
return null; return null;