GeezerVison is showing github.com — feat: assets.components support reference field (#1355) · annlay/lowcode-engine@f9dcbaf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/editor-core/src/editor.ts +10-8Lines changed: 10 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ import { globalLocale } from './intl'; | 18 | 18 | import Preference from './utils/preference'; | 19 | 19 | import { obx } from './utils'; | 20 | 20 | import { AssetsJson, AssetLoader } from '@alilc/lowcode-utils'; | | 21 | +import { assetsTransform } from './utils/assets-transform'; | 21 | 22 |
| 22 | 23 | EventEmitter.defaultMaxListeners = 100; | 23 | 24 |
| @@ -124,7 +125,8 @@ export class Editor extends (EventEmitter as any) implements IEditor { | 124 | 125 | ); | 125 | 126 | } | 126 | 127 | } | 127 | | - this.context.set('assets', assets); | | 128 | + const innerAssets = assetsTransform(assets); | | 129 | + this.context.set('assets', innerAssets); | 128 | 130 | this.notifyGot('assets'); | 129 | 131 | } | 130 | 132 |
| @@ -145,7 +147,7 @@ export class Editor extends (EventEmitter as any) implements IEditor { | 145 | 147 | const x = this.context.get(keyOrType); | 146 | 148 | if (x !== undefined) { | 147 | 149 | fn(x); | 148 | | - return () => {}; | | 150 | + return () => { }; | 149 | 151 | } else { | 150 | 152 | this.setWait(keyOrType, fn); | 151 | 153 | return () => { | @@ -169,7 +171,7 @@ export class Editor extends (EventEmitter as any) implements IEditor { | 169 | 171 | const { hooks = [], lifeCycles } = this.config; | 170 | 172 |
| 171 | 173 | this.emit('editor.beforeInit'); | 172 | | - const init = (lifeCycles && lifeCycles.init) || ((): void => {}); | | 174 | + const init = (lifeCycles && lifeCycles.init) || ((): void => { }); | 173 | 175 |
| 174 | 176 | try { | 175 | 177 | await init(this); | @@ -231,11 +233,11 @@ export class Editor extends (EventEmitter as any) implements IEditor { | 231 | 233 |
| 232 | 234 | /* eslint-disable */ | 233 | 235 | 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 | + }> | 239 | 241 | >(); | 240 | 242 | /* eslint-enable */ | 241 | 243 |
| |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/types/src/assets.ts +13-3Lines changed: 13 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Snippet, ComponentMetadata } from './metadata'; | 2 | 2 | import { I18nData } from './i18n'; | | 3 | +import { Reference } from './npm'; | | 4 | +import { EitherOr } from './utils'; | 3 | 5 |
| 4 | 6 | export interface AssetItem { | 5 | 7 | type: AssetType; | @@ -103,11 +105,15 @@ export interface ComponentSort { | 103 | 105 | * 定义组件大包及 external 资源的信息 | 104 | 106 | * 应该被编辑器默认加载 | 105 | 107 | */ | 106 | | -export interface Package { | | 108 | +export type Package = EitherOr<{ | 107 | 109 | /** | 108 | | - * 包名 | | 110 | + * npm 包名 | 109 | 111 | */ | 110 | 112 | package: string; | | 113 | + /** | | 114 | + * 包唯一标识 | | 115 | + */ | | 116 | + id: string; | 111 | 117 | /** | 112 | 118 | * 包版本号 | 113 | 119 | */ | @@ -142,7 +148,7 @@ export interface Package { | 142 | 148 | * 组件描述导出名字,可以通过 window[exportName] 获取到组件描述的 Object 内容; | 143 | 149 | */ | 144 | 150 | exportName?: string; | 145 | | -} | | 151 | +}, 'package', 'id'>; | 146 | 152 |
| 147 | 153 | /** | 148 | 154 | * 组件分类 | @@ -208,6 +214,10 @@ export interface ComponentDescription extends ComponentMetadata { | 208 | 214 | * @todo 待补充文档 @jinchan | 209 | 215 | */ | 210 | 216 | keywords: string[]; | | 217 | + /** | | 218 | + * 替代 npm 字段的升级版本 | | 219 | + */ | | 220 | + reference?: Reference; | 211 | 221 | } | 212 | 222 |
| 213 | 223 | /** | |
Collapse file Copy file name to clipboardExpand all lines: packages/types/src/npm.ts +35-1Lines changed: 35 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | - | | 1 | +import { EitherOr } from './utils'; | 2 | 2 | /** | 3 | 3 | * npm 源引入完整描述对象 | 4 | 4 | */ | @@ -33,6 +33,40 @@ export interface NpmInfo { | 33 | 33 | main?: string; | 34 | 34 | } | 35 | 35 |
| | 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 | + | 36 | 70 | export interface LowCodeComponentType { | 37 | 71 | /** | 38 | 72 | * 研发模式 | |
Collapse file Copy file name to clipboardExpand all lines: packages/types/src/utils.ts +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,3 +15,37 @@ export type ExternalUtils = { | 15 | 15 |
| 16 | 16 | export type UtilItem = InternalUtils | ExternalUtils; | 17 | 17 | 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>; |
|