Skip to content

Commit 3442071

Browse files
authored
Merge pull request #1919 from tbo47/typescript-improve
typescript improvments
2 parents ca9be83 + 9c59566 commit 3442071

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

src/Container.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import { HitCanvas, SceneCanvas } from './Canvas';
2+
import { SceneContext } from './Context';
13
import { Factory } from './Factory';
24
import { Node, NodeConfig } from './Node';
3-
import { getNumberValidator } from './Validators';
4-
5-
import { GetSet, IRect } from './types';
65
import { Shape } from './Shape';
7-
import { HitCanvas, SceneCanvas } from './Canvas';
8-
import { SceneContext } from './Context';
6+
import { GetSet, IRect } from './types';
7+
import { getNumberValidator } from './Validators';
98

109
export type ClipFuncOutput =
1110
| void
@@ -51,18 +50,11 @@ export abstract class Container<
5150
* });
5251
*/
5352
getChildren(filterFunc?: (item: Node) => boolean) {
54-
if (!filterFunc) {
55-
return this.children || [];
56-
}
57-
5853
const children = this.children || [];
59-
const results: Array<ChildType> = [];
60-
children.forEach(function (child) {
61-
if (filterFunc(child)) {
62-
results.push(child);
63-
}
64-
});
65-
return results;
54+
if (filterFunc) {
55+
return children.filter(filterFunc);
56+
}
57+
return children;
6658
}
6759
/**
6860
* determine if node has children
@@ -233,7 +225,7 @@ export abstract class Container<
233225
this._descendants((node) => {
234226
const valid = node._isMatch(selector);
235227
if (valid) {
236-
retArr.push(node as unknown as ChildNode);
228+
retArr.push(node as ChildNode);
237229
}
238230
if (valid && findOne) {
239231
return true;

src/Layer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class Layer extends Container<Group | Shape> {
114114
return this;
115115
}
116116
// extend Node.prototype.setZIndex
117-
setZIndex(index) {
117+
setZIndex(index: number) {
118118
super.setZIndex(index);
119119
const stage = this.getStage();
120120
if (stage && stage.content) {

src/Node.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { Util, Transform } from './Util';
2-
import { Factory } from './Factory';
3-
import { SceneCanvas, HitCanvas, Canvas } from './Canvas';
4-
import { Konva } from './Global';
1+
import { Canvas, HitCanvas, SceneCanvas } from './Canvas';
52
import { Container } from './Container';
6-
import { GetSet, Vector2d, IRect } from './types';
3+
import { Context } from './Context';
74
import { DD } from './DragAndDrop';
5+
import { Factory } from './Factory';
6+
import { Konva } from './Global';
7+
import { Layer } from './Layer';
8+
import { Shape } from './Shape';
9+
import { Stage } from './Stage';
10+
import { GetSet, IRect, Vector2d } from './types';
11+
import { Transform, Util } from './Util';
812
import {
13+
getBooleanValidator,
914
getNumberValidator,
1015
getStringValidator,
11-
getBooleanValidator,
1216
} from './Validators';
13-
import { Stage } from './Stage';
14-
import { Context } from './Context';
15-
import { Shape } from './Shape';
16-
import { Layer } from './Layer';
1717

1818
export type Filter = (this: Node, imageData: ImageData) => void;
1919

@@ -738,7 +738,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
738738
this.eventListeners[baseEvent] = [];
739739
}
740740

741-
this.eventListeners[baseEvent].push({ name , handler });
741+
this.eventListeners[baseEvent].push({ name, handler });
742742
}
743743

744744
return this;
@@ -894,13 +894,13 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
894894
* @example
895895
* var x = node.getAttr('x');
896896
*/
897-
getAttr(attr: string) {
897+
getAttr<T>(attr: string) {
898898
const method = 'get' + Util._capitalize(attr);
899899
if (Util._isFunction((this as any)[method])) {
900900
return (this as any)[method]();
901901
}
902902
// otherwise get directly
903-
return this.attrs[attr];
903+
return this.attrs[attr] as T | undefined;
904904
}
905905
/**
906906
* get ancestors
@@ -2284,7 +2284,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
22842284
* @example
22852285
* node.setAttr('x', 5);
22862286
*/
2287-
setAttr(attr, val) {
2287+
setAttr(attr: string, val) {
22882288
const func = this[SET + Util._capitalize(attr)];
22892289

22902290
if (Util._isFunction(func)) {
@@ -2301,7 +2301,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
23012301
drawNode?.batchDraw();
23022302
}
23032303
}
2304-
_setAttr(key, val) {
2304+
_setAttr(key: string, val) {
23052305
const oldVal = this.attrs[key];
23062306
if (oldVal === val && !Util.isObject(val)) {
23072307
return;

0 commit comments

Comments
 (0)