Skip to content

Commit f9dcbaf

Browse files
feat: assets.components support reference field (alibaba#1355)
* feat: 资产包协议兼容低代码组件物料
1 parent de3443a commit f9dcbaf

File tree

5 files changed

+118
-12
lines changed

5 files changed

+118
-12
lines changed

packages/editor-core/src/editor.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { globalLocale } from './intl';
1818
import Preference from './utils/preference';
1919
import { obx } from './utils';
2020
import { AssetsJson, AssetLoader } from '@alilc/lowcode-utils';
21+
import { assetsTransform } from './utils/assets-transform';
2122

2223
EventEmitter.defaultMaxListeners = 100;
2324

@@ -124,7 +125,8 @@ export class Editor extends (EventEmitter as any) implements IEditor {
124125
);
125126
}
126127
}
127-
this.context.set('assets', assets);
128+
const innerAssets = assetsTransform(assets);
129+
this.context.set('assets', innerAssets);
128130
this.notifyGot('assets');
129131
}
130132

@@ -145,7 +147,7 @@ export class Editor extends (EventEmitter as any) implements IEditor {
145147
const x = this.context.get(keyOrType);
146148
if (x !== undefined) {
147149
fn(x);
148-
return () => {};
150+
return () => { };
149151
} else {
150152
this.setWait(keyOrType, fn);
151153
return () => {
@@ -169,7 +171,7 @@ export class Editor extends (EventEmitter as any) implements IEditor {
169171
const { hooks = [], lifeCycles } = this.config;
170172

171173
this.emit('editor.beforeInit');
172-
const init = (lifeCycles && lifeCycles.init) || ((): void => {});
174+
const init = (lifeCycles && lifeCycles.init) || ((): void => { });
173175

174176
try {
175177
await init(this);
@@ -231,11 +233,11 @@ export class Editor extends (EventEmitter as any) implements IEditor {
231233

232234
/* eslint-disable */
233235
private waits = new Map<
234-
KeyType,
235-
Array<{
236-
once?: boolean;
237-
resolve: (data: any) => void;
238-
}>
236+
KeyType,
237+
Array<{
238+
once?: boolean;
239+
resolve: (data: any) => void;
240+
}>
239241
>();
240242
/* eslint-enable */
241243

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { AssetsJson, ComponentDescription } from '@alilc/lowcode-types';
2+
3+
4+
export function assetsTransform(assets: AssetsJson) {
5+
const { components, packages } = assets;
6+
const packageMaps = (packages || []).reduce((acc, cur) => {
7+
const key = (cur.id || cur.package) as string;
8+
acc[key] = cur;
9+
return acc;
10+
}, {} as any);
11+
components.forEach((componentDesc) => {
12+
let { devMode, schema, reference } = componentDesc as ComponentDescription;
13+
if ((devMode as string) === 'lowcode') {
14+
devMode = 'lowCode';
15+
} else if (devMode === 'proCode') {
16+
devMode = 'proCode';
17+
}
18+
if (devMode) {
19+
(componentDesc as ComponentDescription).devMode = devMode;
20+
}
21+
if (devMode === 'lowCode' && !schema && reference) {
22+
(componentDesc as ComponentDescription).schema = packageMaps[reference.id as string].schema;
23+
}
24+
});
25+
return assets;
26+
}

packages/types/src/assets.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Snippet, ComponentMetadata } from './metadata';
22
import { I18nData } from './i18n';
3+
import { Reference } from './npm';
4+
import { EitherOr } from './utils';
35

46
export interface AssetItem {
57
type: AssetType;
@@ -103,11 +105,15 @@ export interface ComponentSort {
103105
* 定义组件大包及 external 资源的信息
104106
* 应该被编辑器默认加载
105107
*/
106-
export interface Package {
108+
export type Package = EitherOr<{
107109
/**
108-
* 包名
110+
* npm 包名
109111
*/
110112
package: string;
113+
/**
114+
* 包唯一标识
115+
*/
116+
id: string;
111117
/**
112118
* 包版本号
113119
*/
@@ -142,7 +148,7 @@ export interface Package {
142148
* 组件描述导出名字,可以通过 window[exportName] 获取到组件描述的 Object 内容;
143149
*/
144150
exportName?: string;
145-
}
151+
}, 'package', 'id'>;
146152

147153
/**
148154
* 组件分类
@@ -208,6 +214,10 @@ export interface ComponentDescription extends ComponentMetadata {
208214
* @todo 待补充文档 @jinchan
209215
*/
210216
keywords: string[];
217+
/**
218+
* 替代 npm 字段的升级版本
219+
*/
220+
reference?: Reference;
211221
}
212222

213223
/**

packages/types/src/npm.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import { EitherOr } from './utils';
22
/**
33
* npm 源引入完整描述对象
44
*/
@@ -33,6 +33,40 @@ export interface NpmInfo {
3333
main?: string;
3434
}
3535

36+
/**
37+
* 资源引用信息,Npm 的升级版本,
38+
*/
39+
export type Reference = EitherOr<{
40+
/**
41+
* 引用资源的 id 标识
42+
*/
43+
id: string;
44+
/**
45+
* 引用资源的包名
46+
*/
47+
package: string;
48+
/**
49+
* 引用资源的导出对象中的属性值名称
50+
*/
51+
exportName: string;
52+
/**
53+
* 引用 exportName 上的子对象
54+
*/
55+
subName: string;
56+
/**
57+
* 引用的资源主入口
58+
*/
59+
main?: string;
60+
/**
61+
* 是否从引用资源的导出对象中获取属性值
62+
*/
63+
destructuring?: boolean;
64+
/**
65+
* 资源版本号
66+
*/
67+
version: string;
68+
}, 'package', 'id'>;
69+
3670
export interface LowCodeComponentType {
3771
/**
3872
* 研发模式

packages/types/src/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,37 @@ export type ExternalUtils = {
1515

1616
export type UtilItem = InternalUtils | ExternalUtils;
1717
export type UtilsMap = UtilItem[];
18+
19+
type FilterOptional<T> = Pick<
20+
T,
21+
Exclude<
22+
{
23+
[K in keyof T]: T extends Record<K, T[K]> ? K : never;
24+
}[keyof T],
25+
undefined
26+
>
27+
>;
28+
29+
type FilterNotOptional<T> = Pick<
30+
T,
31+
Exclude<
32+
{
33+
[K in keyof T]: T extends Record<K, T[K]> ? never : K;
34+
}[keyof T],
35+
undefined
36+
>
37+
>;
38+
39+
type PartialEither<T, K extends keyof any> = { [P in Exclude<keyof FilterOptional<T>, K>]-?: T[P] } &
40+
{ [P in Exclude<keyof FilterNotOptional<T>, K>]?: T[P] } &
41+
{ [P in Extract<keyof T, K>]?: undefined };
42+
43+
type Object = {
44+
[name: string]: any;
45+
};
46+
47+
export type EitherOr<O extends Object, L extends string, R extends string> =
48+
(
49+
PartialEither<Pick<O, L | R>, L> |
50+
PartialEither<Pick<O, L | R>, R>
51+
) & Omit<O, L | R>;

0 commit comments

Comments
 (0)