From ac8aa2c5a42d091f635ef76c86eaa7fdd8fffda0 Mon Sep 17 00:00:00 2001 From: 1ncounter <1ncounter.100@gmail.com> Date: Thu, 20 Jun 2024 10:34:50 +0800 Subject: [PATCH] fix: bugfix --- packages/react-renderer/src/router/index.ts | 2 +- .../extension/extensionHostService.ts | 16 ++--- .../src/services/package/managementService.ts | 55 +++++++++------- .../src/services/runtimeUtilService.ts | 66 ++++++++++--------- .../shared/src/types/specs/lowcode-spec.ts | 2 +- 5 files changed, 78 insertions(+), 63 deletions(-) diff --git a/packages/react-renderer/src/router/index.ts b/packages/react-renderer/src/router/index.ts index 1f2f8f6eb..352135a55 100644 --- a/packages/react-renderer/src/router/index.ts +++ b/packages/react-renderer/src/router/index.ts @@ -1,3 +1,3 @@ export * from './context'; export * from './plugin'; -export type * from '@alilc/lowcode-renderer-router'; +export type { Router, RouterHistory } from '@alilc/lowcode-renderer-router'; diff --git a/packages/renderer-core/src/services/extension/extensionHostService.ts b/packages/renderer-core/src/services/extension/extensionHostService.ts index b911b7d9d..cb55829af 100644 --- a/packages/renderer-core/src/services/extension/extensionHostService.ts +++ b/packages/renderer-core/src/services/extension/extensionHostService.ts @@ -57,17 +57,17 @@ export class ExtensionHostService implements IExtensionHostService { continue; } - await this.doSetupPlugin(plugin); + const pluginRuntime = plugin as IPluginRuntime; + + pluginRuntime.status = 'ready'; + this.pluginRuntimes.push(pluginRuntime); + + await this.doSetupPlugin(pluginRuntime); } } - private async doSetupPlugin(plugin: Plugin) { - const pluginRuntime = plugin as IPluginRuntime; - - this.pluginRuntimes.push({ - ...pluginRuntime, - status: 'ready', - }); + private async doSetupPlugin(pluginRuntime: IPluginRuntime) { + if (pluginRuntime.status === 'setup') return; const isSetup = (name: string) => { const setupPlugins = this.pluginRuntimes.filter((item) => item.status === 'setup'); diff --git a/packages/renderer-core/src/services/package/managementService.ts b/packages/renderer-core/src/services/package/managementService.ts index 0c96dca18..42695244a 100644 --- a/packages/renderer-core/src/services/package/managementService.ts +++ b/packages/renderer-core/src/services/package/managementService.ts @@ -33,12 +33,16 @@ export interface IPackageManagementService { setLibraryByPackageName(packageName: string, library: any): void; + getLibraryByComponentMap(componentMap: Spec.ComponentMap): any; + /** 解析组件映射 */ resolveComponentMaps(componentMaps: Spec.ComponentMap[]): void; + /** 获取组件映射对象,key = componentName value = component */ getComponentsNameRecord( componentMaps?: Spec.ComponentMap[], ): Record; + /** 通过组件名获取对应的组件 */ getComponent(componentName: string): C | LowCodeComponent | undefined; /** 注册组件 */ @@ -105,6 +109,33 @@ export class PackageManagementService implements IPackageManagementService { this.packageStore.set(packageName, library); } + getLibraryByComponentMap(componentMap: Spec.ComponentMap) { + if (this.packageStore.has(componentMap.package!)) { + const library = this.packageStore.get(componentMap.package!); + // export { exportName } from xxx exportName === global.libraryName.exportName + // export exportName from xxx exportName === global.libraryName.default || global.libraryName + // export { exportName as componentName } from package + // if exportName == null exportName === componentName; + // const componentName = exportName.subName, if exportName empty subName donot use + const paths = + componentMap.exportName && componentMap.subName ? componentMap.subName.split('.') : []; + const exportName = componentMap.exportName ?? componentMap.componentName; + + if (componentMap.destructuring) { + paths.unshift(exportName); + } + + let result = library; + for (const path of paths) { + result = result[path] || result; + } + + return result; + } + + return undefined; + } + resolveComponentMaps(componentMaps: Spec.ComponentMap[]) { for (const map of componentMaps) { if (map.devMode === 'lowCode') { @@ -114,28 +145,8 @@ export class PackageManagementService implements IPackageManagementService { this.componentsRecord[map.componentName] = packageInfo; } } else { - if (this.packageStore.has(map.package!)) { - const library = this.packageStore.get(map.package!); - // export { exportName } from xxx exportName === global.libraryName.exportName - // export exportName from xxx exportName === global.libraryName.default || global.libraryName - // export { exportName as componentName } from package - // if exportName == null exportName === componentName; - // const componentName = exportName.subName, if exportName empty subName donot use - const paths = map.exportName && map.subName ? map.subName.split('.') : []; - const exportName = map.exportName ?? map.componentName; - - if (map.destructuring) { - paths.unshift(exportName); - } - - let result = library; - for (const path of paths) { - result = result[path] || result; - } - - const recordName = map.componentName ?? map.exportName; - if (recordName && result) this.componentsRecord[recordName] = result; - } + const result = this.getLibraryByComponentMap(map); + if (map.componentName && result) this.componentsRecord[map.componentName] = result; } } } diff --git a/packages/renderer-core/src/services/runtimeUtilService.ts b/packages/renderer-core/src/services/runtimeUtilService.ts index 2e67ec09c..69f4422d7 100644 --- a/packages/renderer-core/src/services/runtimeUtilService.ts +++ b/packages/renderer-core/src/services/runtimeUtilService.ts @@ -1,4 +1,11 @@ -import { type AnyFunction, type Spec, createDecorator, Provide } from '@alilc/lowcode-shared'; +import { + type AnyFunction, + type Spec, + createDecorator, + Provide, + type PlainObject, +} from '@alilc/lowcode-shared'; +import { isPlainObject } from 'lodash-es'; import { IPackageManagementService } from './package'; import { ICodeRuntimeService } from './code-runtime'; import { ILifeCycleService, LifecyclePhase } from './lifeCycleService'; @@ -6,7 +13,7 @@ import { ISchemaService } from './schema'; export interface IRuntimeUtilService { add(utilItem: Spec.Util): void; - add(name: string, fn: AnyFunction): void; + add(name: string, target: AnyFunction | PlainObject): void; remove(name: string): void; } @@ -15,7 +22,7 @@ export const IRuntimeUtilService = createDecorator('rendere @Provide(IRuntimeUtilService) export class RuntimeUtilService implements IRuntimeUtilService { - private utilsMap: Map = new Map(); + private utilsMap: Map = new Map(); constructor( @ICodeRuntimeService private codeRuntimeService: ICodeRuntimeService, @@ -33,15 +40,25 @@ export class RuntimeUtilService implements IRuntimeUtilService { } add(utilItem: Spec.Util): void; - add(name: string, fn: AnyFunction): void; - add(name: Spec.Util | string, fn?: AnyFunction): void { + add(name: string, fn: AnyFunction | PlainObject): void; + add(name: Spec.Util | string, fn?: AnyFunction | PlainObject): void { if (typeof name === 'string') { - if (typeof fn === 'function') { - this.utilsMap.set(name, fn as AnyFunction); + if (fn) { + if (isPlainObject(fn)) { + if ((fn as PlainObject).destructuring) { + for (const key of Object.keys(fn)) { + this.add(key, (fn as PlainObject)[key]); + } + } else { + this.utilsMap.set(name, fn); + } + } else if (typeof fn === 'function') { + this.utilsMap.set(name, fn); + } } } else { - const fn = this.parseUtil(name); - if (fn) this.utilsMap.set(name.name, fn); + const util = this.parseUtil(name); + if (util) this.add(name.name, util); } } @@ -49,6 +66,15 @@ export class RuntimeUtilService implements IRuntimeUtilService { this.utilsMap.delete(name); } + private parseUtil(utilItem: Spec.Util) { + if (utilItem.type === 'function') { + const { content } = utilItem; + return this.codeRuntimeService.run(content.value); + } else { + return this.packageManagementService.getLibraryByComponentMap(utilItem.content); + } + } + private toExpose(): void { const exposed = new Proxy(Object.create(null), { get: (_, p: string) => { @@ -64,26 +90,4 @@ export class RuntimeUtilService implements IRuntimeUtilService { this.codeRuntimeService.getScope().set('utils', exposed); } - - private parseUtil(utilItem: Spec.Util) { - if (utilItem.type === 'function') { - const { content } = utilItem; - - return this.codeRuntimeService.run(content.value); - } else { - const { - content: { package: packageName, destructuring, exportName, subName }, - } = utilItem; - let library: any = this.packageManagementService.getLibraryByPackageName(packageName!); - - if (library) { - if (destructuring) { - const target = library[exportName!]; - library = subName ? target[subName] : target; - } - - return library; - } - } - } } diff --git a/packages/shared/src/types/specs/lowcode-spec.ts b/packages/shared/src/types/specs/lowcode-spec.ts index b66f30b35..03f69a8e7 100644 --- a/packages/shared/src/types/specs/lowcode-spec.ts +++ b/packages/shared/src/types/specs/lowcode-spec.ts @@ -329,7 +329,7 @@ export interface ComponentNodeProps { export interface NPMUtil { name: string; type: 'npm'; - content: Omit; + content: ComponentMap; } export interface FunctionUtil {