import { ComponentType, ClassType, createElement } from "react"; import { createRoot } from "react-dom/client"; import { message } from "antd"; import { UpdateOptions } from "./abstract-controller"; import AbstractController from "./abstract-controller"; import { ComponentBaseProps, IFilterConfigType } from "../interface"; const merge = (originalData: any, newData: any): any => { if (!originalData && newData) return newData; if (originalData && !newData) return originalData; Object.keys(newData).forEach(key => { const newValue = newData[key]; if (originalData.hasOwnProperty(key)) { if (Array.isArray(newValue)) { originalData[key] = newValue; } else if (newValue && typeof newValue === "object" && Object.keys(newValue).length > 0 && originalData[key] && typeof originalData[key] === "object" && !!originalData[key]) { merge(originalData[key], newValue); } else { originalData[key] = newValue; } } else { originalData[key] = newValue; } }); return originalData; } class ComponentUtil { public static async createAndRender(container: HTMLElement, Template: ClassType, props?: any): Promise { if (!container) throw new Error("create react node failed, container is null"); return new Promise((resolve) => { try { createRoot(container).render(createElement(Template, { ref: (instance: T) => resolve(instance), ...props, isDesignMode: false // 组件中传参是否是编辑页 })) } catch (e: unknown) { console.error('create react node failed ', e) resolve(null); } }); } } export class ComponentController extends AbstractController { private componentNode: ComponentType; // 组件实例 private componentType: string; // 组件类型 constructor({ componentType, componentNode }) { super(); this.componentType = componentType; this.componentNode = componentNode; } async create(container: HTMLElement, config: { [key: string]: any }): Promise { this.config = config; this.container = container; this.instance = await ComponentUtil.createAndRender(container, this.componentNode, config); } destroy(): void { this.instance?.destroy?.(); this.instance = null; this.config = null; } getConfig(): { [key: string]: any } | null { return this.config; } update(config: { [key: string]: any }, upOp?: UpdateOptions | undefined): void { this.config = merge(this.config, config); upOp = upOp || { reRender: true }; if (upOp.reRender) { this.instance?.updateConfig(this.config!); } } changeData(data: any) { if (!this.config) { return; } if (this.componentType === "BaseText" && typeof data !== 'string') { // 文本组件数据更新 message.warning('数据类型错误, 基础文本仅接受字符串类型的数据: ', data); this.config.data.staticData = ""; return; } if (data?.type === "initData") { // 组件初始化数据更新 this.config.data.initData = data; this.instance?.updateConfig(this.config); return; } if (data?.type === "searchParams") { // 组件数据搜索参数更新 this.config.data.searchParams = data?.searchParams; this.instance?.updateConfig(this.config); return; } if (data?.type === "initSearchParams") { // 外部传参,组件参数更新 this.config.initSearchParams = data?.initSearchParams; this.instance?.updateConfig(this.config); return; } if (data?.type === "wsData") { // ws组件订阅数据更新 this.config.data.wsData = data; this.instance?.updateConfig(this.config); return; } // 默认组件静态数据更新 this.config.data.staticData = data; this.instance?.updateConfig(this.config); } // 更新组件数据 public loadComponentData({ type, data }: { type: string, data: any }): void { switch (type) { case "initData": // 初始化数据加载逻辑 this.changeData({ ...data, type: "initData" }); break; case "wsData": // 订阅数据更新 this.changeData({ ...data, type: "wsData" }); break; default: break; } } // 设置滤镜效果 public updateFilter(filter: IFilterConfigType): void { if (this.config && (this.config as ComponentBaseProps).filter) (this.config as ComponentBaseProps)!.filter = filter; if (!this.container) return; if (filter?.enable) { this.container.style.filter = `blur(${filter.blur}px) brightness(${filter.brightness}) contrast(${filter.contrast}) opacity(${filter.opacity}) saturate(${filter.saturate}) hue-rotate(${filter.hueRotate}deg)` } else { this.container.style.filter = 'none'; } } } export default ComponentController;