Fix code lint complaints

This commit is contained in:
Surma
2018-06-26 15:11:07 +01:00
parent 579b8a494a
commit 5303afe9ad
6 changed files with 36 additions and 34 deletions

View File

@@ -24,17 +24,17 @@ interface SetTransformOpts {
allowChangeEvent?: boolean; allowChangeEvent?: boolean;
} }
function getDistance (a: Point, b?: Point): number { function getDistance(a: Point, b?: Point): number {
if (!b) return 0; if (!b) return 0;
return Math.sqrt((b.clientX - a.clientX) ** 2 + (b.clientY - a.clientY) ** 2); return Math.sqrt((b.clientX - a.clientX) ** 2 + (b.clientY - a.clientY) ** 2);
} }
function getMidpoint (a: Point, b?: Point): Point { function getMidpoint(a: Point, b?: Point): Point {
if (!b) return a; if (!b) return a;
return { return {
clientX: (a.clientX + b.clientX) / 2, clientX: (a.clientX + b.clientX) / 2,
clientY: (a.clientY + b.clientY) / 2 clientY: (a.clientY + b.clientY) / 2,
}; };
} }
@@ -42,15 +42,15 @@ function getMidpoint (a: Point, b?: Point): Point {
// Given that, better to use something everything supports. // Given that, better to use something everything supports.
let cachedSvg: SVGSVGElement; let cachedSvg: SVGSVGElement;
function getSVG (): SVGSVGElement { function getSVG(): SVGSVGElement {
return cachedSvg || (cachedSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')); return cachedSvg || (cachedSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'));
} }
function createMatrix (): SVGMatrix { function createMatrix(): SVGMatrix {
return getSVG().createSVGMatrix(); return getSVG().createSVGMatrix();
} }
function createPoint (): SVGPoint { function createPoint(): SVGPoint {
return getSVG().createSVGPoint(); return getSVG().createSVGPoint();
} }
@@ -79,9 +79,9 @@ export default class PinchZoom extends HTMLElement {
event.preventDefault(); event.preventDefault();
return true; return true;
}, },
move: previousPointers => { move: (previousPointers) => {
this._onPointerMove(previousPointers, pointerTracker.currentPointers); this._onPointerMove(previousPointers, pointerTracker.currentPointers);
} },
}); });
this.addEventListener('wheel', event => this._onWheel(event)); this.addEventListener('wheel', event => this._onWheel(event));
@@ -109,12 +109,12 @@ export default class PinchZoom extends HTMLElement {
setTransform (opts: SetTransformOpts = {}) { setTransform (opts: SetTransformOpts = {}) {
const { const {
scale = this.scale, scale = this.scale,
allowChangeEvent = false allowChangeEvent = false,
} = opts; } = opts;
let { let {
x = this.x, x = this.x,
y = this.y y = this.y,
} = opts; } = opts;
// If we don't have an element to position, just set the value as given. // If we don't have an element to position, just set the value as given.
@@ -144,7 +144,7 @@ export default class PinchZoom extends HTMLElement {
bottomRight.y = positioningElBounds.height + topLeft.y; bottomRight.y = positioningElBounds.height + topLeft.y;
// Calculate the intended position of _positioningEl. // Calculate the intended position of _positioningEl.
let matrix = createMatrix() const matrix = createMatrix()
.translate(x, y) .translate(x, y)
.scale(scale) .scale(scale)
// Undo current transform // Undo current transform
@@ -239,7 +239,7 @@ export default class PinchZoom extends HTMLElement {
this._applyChange({ this._applyChange({
scaleDiff, scaleDiff,
originX: event.clientX - thisRect.left, originX: event.clientX - thisRect.left,
originY: event.clientY - thisRect.top originY: event.clientY - thisRect.top,
}); });
} }
@@ -263,7 +263,7 @@ export default class PinchZoom extends HTMLElement {
this._applyChange({ this._applyChange({
originX, originY, scaleDiff, originX, originY, scaleDiff,
panX: newMidpoint.clientX - prevMidpoint.clientX, panX: newMidpoint.clientX - prevMidpoint.clientX,
panY: newMidpoint.clientY - prevMidpoint.clientY panY: newMidpoint.clientY - prevMidpoint.clientY,
}); });
} }
@@ -272,7 +272,7 @@ export default class PinchZoom extends HTMLElement {
const { const {
panX = 0, panY = 0, panX = 0, panY = 0,
originX = 0, originY = 0, originX = 0, originY = 0,
scaleDiff = 1 scaleDiff = 1,
} = opts; } = opts;
const matrix = createMatrix() const matrix = createMatrix()
@@ -290,7 +290,7 @@ export default class PinchZoom extends HTMLElement {
scale: matrix.a, scale: matrix.a,
x: matrix.e, x: matrix.e,
y: matrix.f, y: matrix.f,
allowChangeEvent: true allowChangeEvent: true,
}); });
} }
} }

View File

@@ -46,9 +46,9 @@ export default class TwoUp extends HTMLElement {
move: () => { move: () => {
this._pointerChange( this._pointerChange(
pointerTracker.startPointers[0], pointerTracker.startPointers[0],
pointerTracker.currentPointers[0] pointerTracker.currentPointers[0],
); );
} },
}); });
} }

View File

@@ -11,6 +11,6 @@ interface Window {
declare namespace JSX { declare namespace JSX {
interface IntrinsicElements { interface IntrinsicElements {
"two-up": HTMLAttributes 'two-up': HTMLAttributes;
} }
} }

View File

@@ -32,9 +32,10 @@ const isPointerEvent = (event: any): event is PointerEvent =>
const noop = () => {}; const noop = () => {};
type StartCallback = ((pointer: Pointer, event: TouchEvent | PointerEvent | MouseEvent) => boolean); export type InputEvent = TouchEvent | PointerEvent | MouseEvent;
type MoveCallback = ((previousPointers: Pointer[], event: TouchEvent | PointerEvent | MouseEvent) => void); type StartCallback = ((pointer: Pointer, event: InputEvent) => boolean);
type EndCallback = ((pointer: Pointer, event: TouchEvent | PointerEvent | MouseEvent) => void); type MoveCallback = ((previousPointers: Pointer[], event: InputEvent) => void);
type EndCallback = ((pointer: Pointer, event: InputEvent) => void);
interface PointerTrackerCallbacks { interface PointerTrackerCallbacks {
/** /**
@@ -95,7 +96,7 @@ export class PointerTracker {
const { const {
start = () => true, start = () => true,
move = noop, move = noop,
end = noop end = noop,
} = callbacks; } = callbacks;
this._startCallback = start; this._startCallback = start;
@@ -120,7 +121,7 @@ export class PointerTracker {
* @param event Related event * @param event Related event
* @returns Whether the pointer is being tracked. * @returns Whether the pointer is being tracked.
*/ */
private _triggerPointerStart (pointer: Pointer, event: PointerEvent | MouseEvent | TouchEvent): boolean { private _triggerPointerStart (pointer: Pointer, event: InputEvent): boolean {
if (!this._startCallback(pointer, event)) return false; if (!this._startCallback(pointer, event)) return false;
this.currentPointers.push(pointer); this.currentPointers.push(pointer);
this.startPointers.push(pointer); this.startPointers.push(pointer);
@@ -193,7 +194,7 @@ export class PointerTracker {
* @param event Related event * @param event Related event
*/ */
@bind @bind
private _triggerPointerEnd (pointer: Pointer, event: PointerEvent | MouseEvent | TouchEvent): boolean { private _triggerPointerEnd (pointer: Pointer, event: InputEvent): boolean {
const index = this.currentPointers.findIndex(p => p.id === pointer.id); const index = this.currentPointers.findIndex(p => p.id === pointer.id);
// Not a pointer we're interested in? // Not a pointer we're interested in?
if (index === -1) return false; if (index === -1) return false;

View File

@@ -1,4 +1,4 @@
import {Encoder} from './codec'; import { Encoder } from './codec';
import mozjpeg_enc from '../../../codecs/mozjpeg_enc/mozjpeg_enc'; import mozjpeg_enc from '../../../codecs/mozjpeg_enc/mozjpeg_enc';
// Using require() so TypeScript doesnt complain about this not being a module. // Using require() so TypeScript doesnt complain about this not being a module.
@@ -19,13 +19,13 @@ export class MozJpegEncoder implements Encoder {
private emscriptenModule: Promise<EmscriptenWasm.Module>; private emscriptenModule: Promise<EmscriptenWasm.Module>;
private api: Promise<ModuleAPI>; private api: Promise<ModuleAPI>;
constructor() { constructor() {
this.emscriptenModule = new Promise(resolve => { this.emscriptenModule = new Promise((resolve) => {
const m = mozjpeg_enc({ const m = mozjpeg_enc({
// Just to be safe, dont automatically invoke any wasm functions // Just to be safe, dont automatically invoke any wasm functions
noInitialRun: false, noInitialRun: false,
locateFile(url: string): string { locateFile(url: string): string {
// Redirect the request for the wasm binary to whatever webpack gave us. // Redirect the request for the wasm binary to whatever webpack gave us.
if(url.endsWith('.wasm')) { if (url.endsWith('.wasm')) {
return wasmBinaryUrl; return wasmBinaryUrl;
} }
return url; return url;
@@ -38,12 +38,14 @@ export class MozJpegEncoder implements Encoder {
// TODO(surma@): File a bug with Emscripten on this. // TODO(surma@): File a bug with Emscripten on this.
delete (m as any).then; delete (m as any).then;
resolve(m); resolve(m);
} },
}); });
}); });
this.api = (async () => { this.api = (async () => {
// Not sure why, but TypeScript complains that I am using `emscriptenModule` before its getting assigned, which is clearly not true :shrug: Using `any` // Not sure why, but TypeScript complains that I am using
// `emscriptenModule` before its getting assigned, which is clearly not
// true :shrug: Using `any`
const m = await (this as any).emscriptenModule; const m = await (this as any).emscriptenModule;
return { return {
version: m.cwrap('version', 'number', []), version: m.cwrap('version', 'number', []),

View File

@@ -16,12 +16,12 @@ export function bind(target: any, propertyKey: string, descriptor: PropertyDescr
// define an instance property pointing to the bound function. // define an instance property pointing to the bound function.
// This effectively "caches" the bound prototype method as an instance property. // This effectively "caches" the bound prototype method as an instance property.
get() { get() {
let bound = descriptor.value.bind(this); const bound = descriptor.value.bind(this);
Object.defineProperty(this, propertyKey, { Object.defineProperty(this, propertyKey, {
value: bound value: bound,
}); });
return bound; return bound;
} },
}; };
} }
@@ -37,9 +37,8 @@ export async function bitmapToImageData(bitmap: ImageBitmap): Promise<ImageData>
// Draw image onto canvas // Draw image onto canvas
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
if (!ctx) { if (!ctx) {
throw new Error("Could not create canvas context"); throw new Error('Could not create canvas context');
} }
ctx.drawImage(bitmap, 0, 0); ctx.drawImage(bitmap, 0, 0);
return ctx.getImageData(0, 0, bitmap.width, bitmap.height); return ctx.getImageData(0, 0, bitmap.width, bitmap.height);
} }