import { removePluginDuplicates } from './remove-plugin-duplicates'; // 自动注册到全局的插件 // export class PluginManager { plugin = {}; registeredPlugin: Record = {}; constructor() { this.getGlobalPlugin(); } getGlobalPlugin() { const plugin = window.CN_UI_PLUGIN || []; for (const { name, plugin: pluginFun, plugins: pluginFuns } of plugin) { if (Array.isArray(this.plugin[name])) { this.plugin[name] = removePluginDuplicates( this.plugin[name].concat(pluginFuns ?? pluginFun), ); } else { this.plugin[name] = pluginFuns ?? pluginFun; } } } registerPlugin(pluginList) { const registerPlugin = pluginList?.map?.((plugin) => { return { name: plugin.componentName, plugins: removePluginDuplicates( plugin.plugins.map((item) => ({ name: item?.name, method: item.sourceCode, lowCodeConfig: item.lowCode, })), ), }; }); const registerComponentMap: Record = {}; if (Array.isArray(window.CN_UI_PLUGIN)) { window.CN_UI_PLUGIN.forEach((item) => { if (!registerComponentMap[item.name]) { registerComponentMap[item.name] = { name: item.name, plugins: item.plugins ?? [], }; } else { registerComponentMap[item.name].plugins.push(...item.plugins); } }); } registerPlugin.forEach((item) => { if (!registerComponentMap[item.name]) { registerComponentMap[item.name] = { name: item.name, plugins: item.plugins ?? [], }; } else { registerComponentMap[item.name].plugins.push(...item.plugins); } }); // 去重并转换为数组 const registerDuplicatesPlugin = []; Object.values(registerComponentMap).forEach((item) => { registerDuplicatesPlugin.push({ name: item.name, plugins: removePluginDuplicates(item.plugins), }); }); window.CN_UI_PLUGIN = registerDuplicatesPlugin; // 注册即生效,后续需要删除 loadPlugins 和 loadFullPlugins 能力 this.getGlobalPlugin(); this.loadFullPlugins(); } loadPlugins(componentName) { if (Array.isArray(componentName)) { componentName.forEach((name) => { this.registeredPlugin[name] = this.plugin[name] || []; }); } else { this.registeredPlugin[componentName] = this.plugin[componentName] || []; } } loadFullPlugins() { this.registeredPlugin = { ...this.plugin }; } // getPluginsByComponentName(componentName) { getPluginsByComponentName(componentName: string | string[]) { if (Array.isArray(componentName)) { return componentName.reduce((plugin, name) => { return plugin.concat(this.registeredPlugin[name] || []); }, []); // .map((item) => item.method); } return this.registeredPlugin[componentName] ?? []; } // getPluginsByPluginName(pluginName) { getPluginByPluginName(pluginName, componentName?) { const componentPlugin = this.plugin[componentName] ?? []; const fullPluginList = Object.values(this.plugin).flat(); const pluginList = componentName ? componentPlugin : fullPluginList; return pluginList?.find?.((item) => item.name === pluginName); } }