kunpolibrary/src/ui/ComponentExtendHelper.ts

60 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-02-20 11:27:28 +08:00
/**
* @Author: Gongxh
* @Date: 2024-12-26
* @Description:
*/
import { UIObjectFactory } from "fairygui-cc";
import { debug } from "../tool/log";
import { PropsHelper } from "./PropsHelper";
import { _uidecorator } from "./UIDecorator";
export class ComponentExtendHelper {
/** 已注册的组件集合 @internal */
private static _registeredComponents: Set<string> = new Set<string>();
2025-02-20 11:27:28 +08:00
public static register(): void {
for (const { ctor, res } of _uidecorator.getComponentMaps().values()) {
const componentKey = `${res.pkg}/${res.name}`;
if (this._registeredComponents.has(componentKey)) {
debug(`自定义组件已注册,跳过 组件名:${res.name} 包名:${res.pkg}`);
continue;
}
2025-02-20 11:27:28 +08:00
debug(`自定义组件注册 组件名:${res.name} 包名:${res.pkg}`);
this.registerComponent(ctor, res.pkg, res.name);
this._registeredComponents.add(componentKey);
2025-02-20 11:27:28 +08:00
}
}
/**
*
* @param ctor
* @param pkg
* @param name
*/
public static dynamicRegister(ctor: any, pkg: string, name: string): void {
const componentKey = `${pkg}/${name}`;
if (this._registeredComponents.has(componentKey)) {
debug(`自定义组件已注册,跳过 组件名:${name} 包名:${pkg}`);
return;
}
debug(`自定义组件注册 组件名:${name} 包名:${pkg}`);
this.registerComponent(ctor, pkg, name);
this._registeredComponents.add(componentKey);
}
2025-02-20 11:27:28 +08:00
/**
*
* @param info
2025-03-07 16:02:00 +08:00
* @internal
2025-02-20 11:27:28 +08:00
*/
private static registerComponent(ctor: any, pkg: string, name: string): void {
// 自定义组件扩展
const onConstruct = function (this: any): void {
PropsHelper.serializeProps(this, pkg, name);
2025-02-20 11:27:28 +08:00
this.onInit && this.onInit();
};
ctor.prototype.onConstruct = onConstruct;
// 自定义组件扩展
UIObjectFactory.setExtension(`ui://${pkg}/${name}`, ctor);
}
}