From f9dcbaf61824df2a1901758d3ef15e6a1b865be5 Mon Sep 17 00:00:00 2001 From: wangwei Date: Tue, 13 Dec 2022 10:31:44 +0800 Subject: [PATCH] feat: assets.components support reference field (#1355) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 资产包协议兼容低代码组件物料 --- packages/editor-core/src/editor.ts | 18 +++++----- .../editor-core/src/utils/assets-transform.ts | 26 ++++++++++++++ packages/types/src/assets.ts | 16 +++++++-- packages/types/src/npm.ts | 36 ++++++++++++++++++- packages/types/src/utils.ts | 34 ++++++++++++++++++ 5 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 packages/editor-core/src/utils/assets-transform.ts diff --git a/packages/editor-core/src/editor.ts b/packages/editor-core/src/editor.ts index f3b52c61d..892cdb719 100644 --- a/packages/editor-core/src/editor.ts +++ b/packages/editor-core/src/editor.ts @@ -18,6 +18,7 @@ import { globalLocale } from './intl'; import Preference from './utils/preference'; import { obx } from './utils'; import { AssetsJson, AssetLoader } from '@alilc/lowcode-utils'; +import { assetsTransform } from './utils/assets-transform'; EventEmitter.defaultMaxListeners = 100; @@ -124,7 +125,8 @@ export class Editor extends (EventEmitter as any) implements IEditor { ); } } - this.context.set('assets', assets); + const innerAssets = assetsTransform(assets); + this.context.set('assets', innerAssets); this.notifyGot('assets'); } @@ -145,7 +147,7 @@ export class Editor extends (EventEmitter as any) implements IEditor { const x = this.context.get(keyOrType); if (x !== undefined) { fn(x); - return () => {}; + return () => { }; } else { this.setWait(keyOrType, fn); return () => { @@ -169,7 +171,7 @@ export class Editor extends (EventEmitter as any) implements IEditor { const { hooks = [], lifeCycles } = this.config; this.emit('editor.beforeInit'); - const init = (lifeCycles && lifeCycles.init) || ((): void => {}); + const init = (lifeCycles && lifeCycles.init) || ((): void => { }); try { await init(this); @@ -231,11 +233,11 @@ export class Editor extends (EventEmitter as any) implements IEditor { /* eslint-disable */ private waits = new Map< - KeyType, - Array<{ - once?: boolean; - resolve: (data: any) => void; - }> + KeyType, + Array<{ + once?: boolean; + resolve: (data: any) => void; + }> >(); /* eslint-enable */ diff --git a/packages/editor-core/src/utils/assets-transform.ts b/packages/editor-core/src/utils/assets-transform.ts new file mode 100644 index 000000000..d4e735c03 --- /dev/null +++ b/packages/editor-core/src/utils/assets-transform.ts @@ -0,0 +1,26 @@ +import { AssetsJson, ComponentDescription } from '@alilc/lowcode-types'; + + +export function assetsTransform(assets: AssetsJson) { + const { components, packages } = assets; + const packageMaps = (packages || []).reduce((acc, cur) => { + const key = (cur.id || cur.package) as string; + acc[key] = cur; + return acc; + }, {} as any); + components.forEach((componentDesc) => { + let { devMode, schema, reference } = componentDesc as ComponentDescription; + if ((devMode as string) === 'lowcode') { + devMode = 'lowCode'; + } else if (devMode === 'proCode') { + devMode = 'proCode'; + } + if (devMode) { + (componentDesc as ComponentDescription).devMode = devMode; + } + if (devMode === 'lowCode' && !schema && reference) { + (componentDesc as ComponentDescription).schema = packageMaps[reference.id as string].schema; + } + }); + return assets; +} \ No newline at end of file diff --git a/packages/types/src/assets.ts b/packages/types/src/assets.ts index 581af679b..588f56230 100644 --- a/packages/types/src/assets.ts +++ b/packages/types/src/assets.ts @@ -1,5 +1,7 @@ import { Snippet, ComponentMetadata } from './metadata'; import { I18nData } from './i18n'; +import { Reference } from './npm'; +import { EitherOr } from './utils'; export interface AssetItem { type: AssetType; @@ -103,11 +105,15 @@ export interface ComponentSort { * 定义组件大包及 external 资源的信息 * 应该被编辑器默认加载 */ -export interface Package { +export type Package = EitherOr<{ /** - * 包名 + * npm 包名 */ package: string; + /** + * 包唯一标识 + */ + id: string; /** * 包版本号 */ @@ -142,7 +148,7 @@ export interface Package { * 组件描述导出名字,可以通过 window[exportName] 获取到组件描述的 Object 内容; */ exportName?: string; -} +}, 'package', 'id'>; /** * 组件分类 @@ -208,6 +214,10 @@ export interface ComponentDescription extends ComponentMetadata { * @todo 待补充文档 @jinchan */ keywords: string[]; + /** + * 替代 npm 字段的升级版本 + */ + reference?: Reference; } /** diff --git a/packages/types/src/npm.ts b/packages/types/src/npm.ts index f805158e5..78de0795b 100644 --- a/packages/types/src/npm.ts +++ b/packages/types/src/npm.ts @@ -1,4 +1,4 @@ - +import { EitherOr } from './utils'; /** * npm 源引入完整描述对象 */ @@ -33,6 +33,40 @@ export interface NpmInfo { main?: string; } +/** + * 资源引用信息,Npm 的升级版本, + */ +export type Reference = EitherOr<{ + /** + * 引用资源的 id 标识 + */ + id: string; + /** + * 引用资源的包名 + */ + package: string; + /** + * 引用资源的导出对象中的属性值名称 + */ + exportName: string; + /** + * 引用 exportName 上的子对象 + */ + subName: string; + /** + * 引用的资源主入口 + */ + main?: string; + /** + * 是否从引用资源的导出对象中获取属性值 + */ + destructuring?: boolean; + /** + * 资源版本号 + */ + version: string; +}, 'package', 'id'>; + export interface LowCodeComponentType { /** * 研发模式 diff --git a/packages/types/src/utils.ts b/packages/types/src/utils.ts index b8ac406ef..ffe611508 100644 --- a/packages/types/src/utils.ts +++ b/packages/types/src/utils.ts @@ -15,3 +15,37 @@ export type ExternalUtils = { export type UtilItem = InternalUtils | ExternalUtils; export type UtilsMap = UtilItem[]; + +type FilterOptional = Pick< + T, + Exclude< + { + [K in keyof T]: T extends Record ? K : never; + }[keyof T], + undefined + > +>; + +type FilterNotOptional = Pick< + T, + Exclude< + { + [K in keyof T]: T extends Record ? never : K; + }[keyof T], + undefined + > +>; + +type PartialEither = { [P in Exclude, K>]-?: T[P] } & + { [P in Exclude, K>]?: T[P] } & + { [P in Extract]?: undefined }; + +type Object = { + [name: string]: any; +}; + +export type EitherOr = + ( + PartialEither, L> | + PartialEither, R> + ) & Omit;