index.ts
1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { ConfigType, FetchComFlagTypeEnum, PublicComponentOptions } from './index.type';
const configModules = import.meta.globEager('./components/**/config.vue');
const viewModules = import.meta.globEager('./components/**/index.vue');
const datasourceModules = import.meta.globEager('./components/**/datasource.vue');
const createInstaceModules = import.meta.globEager('./components/**/config.ts');
const findModule = (componentName: string, module: Recordable) => {
for (const key in module) {
const urlSplit = key.split('/');
if (urlSplit[urlSplit.length - 2] === componentName) {
return module[key] as Record<'default', any>;
}
}
};
/**
* @description 获取组件
* @param {string} componentName 组件名称
* @param {FetchComFlagTypeEnum} flag 标识 0展示组件 1配置组件 2数据源组件
*/
export const fetchComponent = (componentName: string, flag: FetchComFlagTypeEnum) => {
const module =
flag === FetchComFlagTypeEnum.VIEW
? viewModules
: flag === FetchComFlagTypeEnum.CONFIG
? configModules
: datasourceModules;
return findModule(componentName, module);
};
export const fetchViewComponent = (configType: ConfigType) => {
const { key } = configType;
return fetchComponent(key, FetchComFlagTypeEnum.VIEW)?.default;
};
export const fetchConfigComponent = (configType: ConfigType) => {
const { key } = configType;
return fetchComponent(key, FetchComFlagTypeEnum.CONFIG)?.default;
};
export const fetchDatasourceComponent = (configType: ConfigType) => {
const { key } = configType;
return fetchComponent(key, FetchComFlagTypeEnum.DATASOURCE_FORM)?.default;
};
/**
* @description 获取目标组件配置信息
* @param targetData
*/
export const createComponent = (
targetData: ConfigType,
options?: Partial<PublicComponentOptions>
) => {
const { key } = targetData;
const module = findModule(key, createInstaceModules);
const instance = module?.default;
if (instance) {
return new instance(options || {});
}
};