From c287bad37bfc62f8fe5b8d9274f160d42f386123 Mon Sep 17 00:00:00 2001 From: "mario.gk" Date: Thu, 13 Aug 2020 20:00:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=20AC=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor-preset-vision/src/bundle/bundle.ts | 17 ++++++++++++++ .../src/bundle/prototype.ts | 12 +++++----- .../editor-preset-vision/src/bundle/trunk.ts | 22 ++++++++++++++++--- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/packages/editor-preset-vision/src/bundle/bundle.ts b/packages/editor-preset-vision/src/bundle/bundle.ts index 3331ca2f9..8b0ce27d5 100644 --- a/packages/editor-preset-vision/src/bundle/bundle.ts +++ b/packages/editor-preset-vision/src/bundle/bundle.ts @@ -2,6 +2,7 @@ import lg from '@ali/vu-logger'; import { ComponentClass, ComponentType } from 'react'; import Prototype, { isPrototype } from './prototype'; import { designer } from '../editor'; +import trunk from './trunk'; function basename(name: string) { return name ? (/[^\/]+$/.exec(name) || [''])[0] : ''; @@ -65,6 +66,22 @@ export default class Bundle { this.recursivelyRegisterPrototypes(prototype, item); } }); + + // invoke prototype mocker while the prototype does not exist + Object.keys(this.viewsMap).forEach((viewName) => { + const test = this; + // console.log(test, viewName); + if (!this.prototypeList.find((proto) => proto.getComponentName() === viewName)) { + const mockedPrototype = trunk.mockComponentPrototype(this.viewsMap[viewName]); + if (mockedPrototype) { + mockedPrototype.setView(this.viewsMap[viewName]); + this.registerPrototype(mockedPrototype); + if (!mockedPrototype.getPackageName()) { + mockedPrototype.setPackageName((this.viewsMap[viewName] as any)._packageName_); + } + } + } + }); } getFromMeta(componentName: string): Prototype { diff --git a/packages/editor-preset-vision/src/bundle/prototype.ts b/packages/editor-preset-vision/src/bundle/prototype.ts index b1406c987..7499f899e 100644 --- a/packages/editor-preset-vision/src/bundle/prototype.ts +++ b/packages/editor-preset-vision/src/bundle/prototype.ts @@ -213,8 +213,8 @@ class Prototype { static addGlobalExtraActions = addGlobalExtraActions; static removeGlobalPropsConfigure = removeGlobalPropsConfigure; static overridePropsConfigure = overridePropsConfigure; - static create(config: OldPrototypeConfig | ComponentMetadata | ComponentMeta, lookup: boolean = false) { - return new Prototype(config, lookup); + static create(config: OldPrototypeConfig | ComponentMetadata | ComponentMeta, extraConfigs: any = null, lookup: boolean = false) { + return new Prototype(config, extraConfigs, lookup); } readonly isPrototype = true; @@ -224,7 +224,7 @@ class Prototype { return this.meta.npm?.package; } - constructor(input: OldPrototypeConfig | ComponentMetadata | ComponentMeta, lookup: boolean = false) { + constructor(input: OldPrototypeConfig | ComponentMetadata | ComponentMeta, extraConfigs: any = null, lookup: boolean = false) { if (lookup) { this.meta = designer.getComponentMeta(input.componentName); this.options = this.meta.getMetadata(); @@ -279,15 +279,15 @@ class Prototype { private category?: string; getCategory() { - if (this.category != null) { - return this.category; + if (this.options.category != null) { + return this.options.category; } return this.meta.getMetadata().tags?.[0] || '*'; } setCategory(category: string) { - this.category = category; + this.options.category = category; } getIcon() { diff --git a/packages/editor-preset-vision/src/bundle/trunk.ts b/packages/editor-preset-vision/src/bundle/trunk.ts index 831919a09..4ae5ed640 100644 --- a/packages/editor-preset-vision/src/bundle/trunk.ts +++ b/packages/editor-preset-vision/src/bundle/trunk.ts @@ -3,11 +3,13 @@ import { EventEmitter } from 'events'; import { registerSetter, RegisteredSetter, getSetter } from '@ali/lowcode-editor-core'; import Bundle from './bundle'; import { CustomView } from '@ali/lowcode-types'; +import Prototype from './prototype'; export class Trunk { private trunk: any[] = []; private emitter: EventEmitter = new EventEmitter(); private metaBundle = new Bundle(); + private componentPrototypeMocker: any; isReady() { return this.getList().length > 0; @@ -25,7 +27,13 @@ export class Trunk { getList(): any[] { const list = this.trunk.reduceRight((prev, cur) => prev.concat(cur.getList()), []); - return Array.from(new Set(list)); + const result: Prototype[] = []; + list.forEach((item: Prototype) => { + if (!result.find(r => r.options.componentName === item.options.componentName)) { + result.push(item); + } + }); + return result; } getPrototype(name: string) { @@ -98,8 +106,16 @@ export class Trunk { console.warn('Trunk.afterLoadBundle is deprecated'); } - registerComponentPrototypeMocker() { - console.warn('Trunk.registerComponentPrototypeMocker is deprecated'); + registerComponentPrototypeMocker(mocker: any) { + this.componentPrototypeMocker = mocker; + } + + mockComponentPrototype(bundle: any) { + if (!this.componentPrototypeMocker) { + lg.error('ERROR: no component prototypeMocker is set'); + } + return this.componentPrototypeMocker + && this.componentPrototypeMocker.mockPrototype(bundle); } setPackages() {