Merge pull request #1919 from tbo47/typescript-improve
Some checks failed
Test Browser / build (20.x) (push) Has been cancelled
Test NodeJS / build (23.x) (push) Has been cancelled

typescript improvments
This commit is contained in:
Anton Lavrenov 2025-04-10 17:59:50 -05:00 committed by GitHub
commit 3442071bdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 34 deletions

View File

@ -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<ChildType> = [];
children.forEach(function (child) {
if (filterFunc(child)) {
results.push(child);
if (filterFunc) {
return children.filter(filterFunc);
}
});
return results;
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;

View File

@ -114,7 +114,7 @@ export class Layer extends Container<Group | Shape> {
return this;
}
// extend Node.prototype.setZIndex
setZIndex(index) {
setZIndex(index: number) {
super.setZIndex(index);
const stage = this.getStage();
if (stage && stage.content) {

View File

@ -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<Config extends NodeConfig = NodeConfig> {
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<Config extends NodeConfig = NodeConfig> {
* @example
* var x = node.getAttr('x');
*/
getAttr(attr: string) {
getAttr<T>(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<Config extends NodeConfig = NodeConfig> {
* @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<Config extends NodeConfig = NodeConfig> {
drawNode?.batchDraw();
}
}
_setAttr(key, val) {
_setAttr(key: string, val) {
const oldVal = this.attrs[key];
if (oldVal === val && !Util.isObject(val)) {
return;