From 27f2df41e7c50325733c672c5693fdb6b9c57531 Mon Sep 17 00:00:00 2001 From: tbo47 Date: Thu, 10 Apr 2025 13:02:31 +0000 Subject: [PATCH] typescript improvements --- src/Container.ts | 28 ++++++++++------------------ src/Layer.ts | 2 +- src/Node.ts | 30 +++++++++++++++--------------- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/Container.ts b/src/Container.ts index c70026fa..6bcf95a7 100644 --- a/src/Container.ts +++ b/src/Container.ts @@ -1,11 +1,10 @@ -import { Factory } from './Factory'; -import { Node, NodeConfig } from './Node'; -import { getNumberValidator } from './Validators'; - -import { GetSet, IRect } from './types'; -import { Shape } from './Shape'; import { HitCanvas, SceneCanvas } from './Canvas'; import { SceneContext } from './Context'; +import { Factory } from './Factory'; +import { Node, NodeConfig } from './Node'; +import { Shape } from './Shape'; +import { GetSet, IRect } from './types'; +import { getNumberValidator } from './Validators'; export type ClipFuncOutput = | void @@ -51,18 +50,11 @@ export abstract class Container< * }); */ getChildren(filterFunc?: (item: Node) => boolean) { - if (!filterFunc) { - return this.children || []; - } - const children = this.children || []; - const results: Array = []; - children.forEach(function (child) { - if (filterFunc(child)) { - results.push(child); - } - }); - return results; + if (filterFunc) { + return children.filter(filterFunc); + } + return children; } /** * determine if node has children @@ -233,7 +225,7 @@ export abstract class Container< this._descendants((node) => { const valid = node._isMatch(selector); if (valid) { - retArr.push(node as unknown as ChildNode); + retArr.push(node as ChildNode); } if (valid && findOne) { return true; diff --git a/src/Layer.ts b/src/Layer.ts index 2ffc12f3..5a13b2d5 100644 --- a/src/Layer.ts +++ b/src/Layer.ts @@ -114,7 +114,7 @@ export class Layer extends Container { return this; } // extend Node.prototype.setZIndex - setZIndex(index) { + setZIndex(index: number) { super.setZIndex(index); const stage = this.getStage(); if (stage && stage.content) { diff --git a/src/Node.ts b/src/Node.ts index bd0b96a1..058bb880 100644 --- a/src/Node.ts +++ b/src/Node.ts @@ -1,19 +1,19 @@ -import { Util, Transform } from './Util'; -import { Factory } from './Factory'; -import { SceneCanvas, HitCanvas, Canvas } from './Canvas'; -import { Konva } from './Global'; +import { Canvas, HitCanvas, SceneCanvas } from './Canvas'; import { Container } from './Container'; -import { GetSet, Vector2d, IRect } from './types'; +import { Context } from './Context'; import { DD } from './DragAndDrop'; +import { Factory } from './Factory'; +import { Konva } from './Global'; +import { Layer } from './Layer'; +import { Shape } from './Shape'; +import { Stage } from './Stage'; +import { GetSet, IRect, Vector2d } from './types'; +import { Transform, Util } from './Util'; import { + getBooleanValidator, getNumberValidator, getStringValidator, - getBooleanValidator, } from './Validators'; -import { Stage } from './Stage'; -import { Context } from './Context'; -import { Shape } from './Shape'; -import { Layer } from './Layer'; export type Filter = (this: Node, imageData: ImageData) => void; @@ -738,7 +738,7 @@ export abstract class Node { this.eventListeners[baseEvent] = []; } - this.eventListeners[baseEvent].push({ name , handler }); + this.eventListeners[baseEvent].push({ name, handler }); } return this; @@ -894,13 +894,13 @@ export abstract class Node { * @example * var x = node.getAttr('x'); */ - getAttr(attr: string) { + getAttr(attr: string) { const method = 'get' + Util._capitalize(attr); if (Util._isFunction((this as any)[method])) { return (this as any)[method](); } // otherwise get directly - return this.attrs[attr]; + return this.attrs[attr] as T | undefined; } /** * get ancestors @@ -2284,7 +2284,7 @@ export abstract class Node { * @example * node.setAttr('x', 5); */ - setAttr(attr, val) { + setAttr(attr: string, val) { const func = this[SET + Util._capitalize(attr)]; if (Util._isFunction(func)) { @@ -2301,7 +2301,7 @@ export abstract class Node { drawNode?.batchDraw(); } } - _setAttr(key, val) { + _setAttr(key: string, val) { const oldVal = this.attrs[key]; if (oldVal === val && !Util.isObject(val)) { return;