Merge pull request #756 from rndD/fix-any

Fix any in TS: part 1
This commit is contained in:
Anton Lavrenov
2019-10-11 07:58:10 -05:00
committed by GitHub
6 changed files with 33 additions and 22 deletions

View File

@@ -2,7 +2,7 @@ import { glob } from './Global';
import { Layer } from './Layer'; import { Layer } from './Layer';
import { IFrame, AnimationFn } from './types'; import { IFrame, AnimationFn } from './types';
var now = (function() { var now = (function(): () => number {
if (glob.performance && glob.performance.now) { if (glob.performance && glob.performance.now) {
return function() { return function() {
return glob.performance.now(); return glob.performance.now();

View File

@@ -1,6 +1,6 @@
import { Util } from './Util'; import { Util } from './Util';
import { SceneContext, HitContext, Context } from './Context'; import { SceneContext, HitContext, Context } from './Context';
import { glob, Konva } from './Global'; import { Konva } from './Global';
import { Factory } from './Factory'; import { Factory } from './Factory';
import { getNumberValidator } from './Validators'; import { getNumberValidator } from './Validators';

View File

@@ -2,6 +2,7 @@ import { Util } from './Util';
import { Animation } from './Animation'; import { Animation } from './Animation';
import { Node } from './Node'; import { Node } from './Node';
import { Konva } from './Global'; import { Konva } from './Global';
import { Line } from './shapes/Line';
var blacklist = { var blacklist = {
node: 1, node: 1,
@@ -249,7 +250,7 @@ export class Tween {
this.onReset = config.onReset; this.onReset = config.onReset;
} }
_addAttr(key, end) { _addAttr(key, end) {
var node = this.node as any, var node = this.node,
nodeId = node._id, nodeId = node._id,
start, start,
diff, diff,
@@ -280,11 +281,11 @@ export class Tween {
if (end.length > start.length) { if (end.length > start.length) {
// so in this case we will increase number of starting points // so in this case we will increase number of starting points
trueStart = start; trueStart = start;
start = Util._prepareArrayForTween(start, end, node.closed()); start = Util._prepareArrayForTween(start, end, (node as Line).closed());
} else { } else {
// in this case we will increase number of eding points // in this case we will increase number of eding points
trueEnd = end; trueEnd = end;
end = Util._prepareArrayForTween(end, start, node.closed()); end = Util._prepareArrayForTween(end, start, (node as Line).closed());
} }
} }

View File

@@ -1,6 +1,6 @@
import { glob, Konva } from './Global'; import { glob, Konva } from './Global';
import { Node } from './Node'; import { Node } from './Node';
import { IRect } from './types'; import { IRect, RGB, RGBA } from './types';
export type Point = { export type Point = {
x: number; x: number;
@@ -500,7 +500,7 @@ export const Util = {
/* /*
* cherry-picked utilities from underscore.js * cherry-picked utilities from underscore.js
*/ */
_isElement(obj: any) { _isElement(obj: any): obj is Element {
return !!(obj && obj.nodeType == 1); return !!(obj && obj.nodeType == 1);
}, },
_isFunction(obj: any) { _isFunction(obj: any) {
@@ -512,21 +512,21 @@ export const Util = {
_isArray(obj: any) { _isArray(obj: any) {
return Object.prototype.toString.call(obj) === OBJECT_ARRAY; return Object.prototype.toString.call(obj) === OBJECT_ARRAY;
}, },
_isNumber(obj: any) { _isNumber(obj: any): obj is number {
return ( return (
Object.prototype.toString.call(obj) === OBJECT_NUMBER && Object.prototype.toString.call(obj) === OBJECT_NUMBER &&
!isNaN(obj) && !isNaN(obj) &&
isFinite(obj) isFinite(obj)
); );
}, },
_isString(obj: any) { _isString(obj: any): obj is string {
return Object.prototype.toString.call(obj) === OBJECT_STRING; return Object.prototype.toString.call(obj) === OBJECT_STRING;
}, },
_isBoolean(obj: any) { _isBoolean(obj: any): obj is boolean {
return Object.prototype.toString.call(obj) === OBJECT_BOOLEAN; return Object.prototype.toString.call(obj) === OBJECT_BOOLEAN;
}, },
// arrays are objects too // arrays are objects too
isObject(val: any) { isObject(val: any): val is Object {
return val instanceof Object; return val instanceof Object;
}, },
isValidSelector(selector: any) { isValidSelector(selector: any) {
@@ -616,7 +616,7 @@ export const Util = {
_rgbToHex(r: number, g: number, b: number) { _rgbToHex(r: number, g: number, b: number) {
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}, },
_hexToRgb(hex: string) { _hexToRgb(hex: string): RGB {
hex = hex.replace(HASH, EMPTY_STRING); hex = hex.replace(HASH, EMPTY_STRING);
var bigint = parseInt(hex, 16); var bigint = parseInt(hex, 16);
return { return {
@@ -658,7 +658,7 @@ export const Util = {
* var rgb = Konva.Util.getRGB('#0000ff'); * var rgb = Konva.Util.getRGB('#0000ff');
* var rgb = Konva.Util.getRGB('rgb(0,0,255)'); * var rgb = Konva.Util.getRGB('rgb(0,0,255)');
*/ */
getRGB(color: string) { getRGB(color: string): RGB {
var rgb; var rgb;
// color string // color string
if (color in COLORS) { if (color in COLORS) {
@@ -690,7 +690,7 @@ export const Util = {
}, },
// convert any color string to RGBA object // convert any color string to RGBA object
// from https://github.com/component/color-parser // from https://github.com/component/color-parser
colorToRGBA(str: string) { colorToRGBA(str: string): RGBA {
str = str || 'black'; str = str || 'black';
return ( return (
Util._namedColorToRBA(str) || Util._namedColorToRBA(str) ||
@@ -715,7 +715,7 @@ export const Util = {
}; };
}, },
// Parse rgb(n, n, n) // Parse rgb(n, n, n)
_rgbColorToRGBA(str: string) { _rgbColorToRGBA(str: string): RGBA {
if (str.indexOf('rgb(') === 0) { if (str.indexOf('rgb(') === 0) {
str = str.match(/rgb\(([^)]+)\)/)[1]; str = str.match(/rgb\(([^)]+)\)/)[1];
var parts = str.split(/ *, */).map(Number); var parts = str.split(/ *, */).map(Number);
@@ -728,7 +728,7 @@ export const Util = {
} }
}, },
// Parse rgba(n, n, n, n) // Parse rgba(n, n, n, n)
_rgbaColorToRGBA(str: string) { _rgbaColorToRGBA(str: string): RGBA {
if (str.indexOf('rgba(') === 0) { if (str.indexOf('rgba(') === 0) {
str = str.match(/rgba\(([^)]+)\)/)[1]; str = str.match(/rgba\(([^)]+)\)/)[1];
var parts = str.split(/ *, */).map(Number); var parts = str.split(/ *, */).map(Number);
@@ -741,7 +741,7 @@ export const Util = {
} }
}, },
// Parse #nnnnnn // Parse #nnnnnn
_hex6ColorToRGBA(str: string) { _hex6ColorToRGBA(str: string): RGBA {
if (str[0] === '#' && str.length === 7) { if (str[0] === '#' && str.length === 7) {
return { return {
r: parseInt(str.slice(1, 3), 16), r: parseInt(str.slice(1, 3), 16),
@@ -752,7 +752,7 @@ export const Util = {
} }
}, },
// Parse #nnn // Parse #nnn
_hex3ColorToRGBA(str: string) { _hex3ColorToRGBA(str: string): RGBA {
if (str[0] === '#' && str.length === 4) { if (str[0] === '#' && str.length === 4) {
return { return {
r: parseInt(str[1] + str[1], 16), r: parseInt(str[1] + str[1], 16),
@@ -763,7 +763,7 @@ export const Util = {
} }
}, },
// Code adapted from https://github.com/Qix-/color-convert/blob/master/conversions.js#L244 // Code adapted from https://github.com/Qix-/color-convert/blob/master/conversions.js#L244
_hslColorToRGBA(str: string) { _hslColorToRGBA(str: string): RGBA {
// Check hsl() format // Check hsl() format
if (/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.test(str)) { if (/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.test(str)) {
// Extract h, s, l // Extract h, s, l

View File

@@ -35,7 +35,7 @@ export function alphaComponent(val: number) {
export function getNumberValidator() { export function getNumberValidator() {
if (Konva.isUnminified) { if (Konva.isUnminified) {
return function(val: any, attr: string) { return function<T>(val: T, attr: string): T|void {
if (!Util._isNumber(val)) { if (!Util._isNumber(val)) {
Util.warn( Util.warn(
_formatValue(val) + _formatValue(val) +
@@ -50,7 +50,7 @@ export function getNumberValidator() {
} }
export function getNumberOrAutoValidator() { export function getNumberOrAutoValidator() {
if (Konva.isUnminified) { if (Konva.isUnminified) {
return function(val: any, attr: string) { return function<T extends string>(val: T, attr: string): T|void {
var isNumber = Util._isNumber(val); var isNumber = Util._isNumber(val);
var isAuto = val === 'auto'; var isAuto = val === 'auto';

View File

@@ -21,7 +21,7 @@ export interface IRect {
export interface IFrame { export interface IFrame {
time: number; time: number;
timeDiff: number; timeDiff: number;
lastTime: any; lastTime: number;
frameRate: number; frameRate: number;
} }
@@ -48,3 +48,13 @@ export enum KonvaNodeEvent {
dragmove = 'dragmove', dragmove = 'dragmove',
dragend = 'dragend' dragend = 'dragend'
} }
export interface RGB {
r: number;
g: number;
b: number;
}
export interface RGBA extends RGB {
a: number;
}