Skip to content

Commit

Permalink
fix: bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
1ncounter committed Jun 20, 2024
1 parent 61bc8e6 commit ac8aa2c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 63 deletions.
2 changes: 1 addition & 1 deletion packages/react-renderer/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
55 changes: 33 additions & 22 deletions packages/renderer-core/src/services/package/managementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<C = unknown>(
componentMaps?: Spec.ComponentMap[],
): Record<string, C | LowCodeComponent>;

/** 通过组件名获取对应的组件 */
getComponent<C = unknown>(componentName: string): C | LowCodeComponent | undefined;
/** 注册组件 */
Expand Down Expand Up @@ -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') {
Expand All @@ -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;
}
}
}
Expand Down
66 changes: 35 additions & 31 deletions packages/renderer-core/src/services/runtimeUtilService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
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';
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;
}
Expand All @@ -15,7 +22,7 @@ export const IRuntimeUtilService = createDecorator<IRuntimeUtilService>('rendere

@Provide(IRuntimeUtilService)
export class RuntimeUtilService implements IRuntimeUtilService {
private utilsMap: Map<string, AnyFunction> = new Map();
private utilsMap: Map<string, any> = new Map();

constructor(
@ICodeRuntimeService private codeRuntimeService: ICodeRuntimeService,
Expand All @@ -33,22 +40,41 @@ 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);
}
}

remove(name: string): void {
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) => {
Expand All @@ -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;
}
}
}
}
2 changes: 1 addition & 1 deletion packages/shared/src/types/specs/lowcode-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export interface ComponentNodeProps {
export interface NPMUtil {
name: string;
type: 'npm';
content: Omit<ComponentMap, 'componentName'>;
content: ComponentMap;
}

export interface FunctionUtil {
Expand Down

0 comments on commit ac8aa2c

Please sign in to comment.