From 1fa525d64acbb0f79209cf44c56380113f493461 Mon Sep 17 00:00:00 2001 From: liujuping Date: Thu, 8 Dec 2022 14:57:02 +0800 Subject: [PATCH] fix: init --- .../designer/src/builtin-simulator/host.ts | 2 +- packages/designer/src/designer/designer.ts | 4 ++-- packages/editor-core/src/editor.ts | 20 +++++++++++++++++-- .../src/layouts/workbench.less | 1 + packages/shell/src/document-model.ts | 2 +- packages/shell/src/material.ts | 8 ++++++-- packages/shell/src/project.ts | 3 ++- packages/utils/src/asset.ts | 3 +-- .../workspace/src/editor-window/context.ts | 13 ++++++++++-- packages/workspace/src/index.ts | 1 + packages/workspace/src/resource.ts | 4 ++++ 11 files changed, 48 insertions(+), 13 deletions(-) diff --git a/packages/designer/src/builtin-simulator/host.ts b/packages/designer/src/builtin-simulator/host.ts index 0e18b0637..c7d2ea544 100644 --- a/packages/designer/src/builtin-simulator/host.ts +++ b/packages/designer/src/builtin-simulator/host.ts @@ -418,7 +418,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost { if (!iframe || this._iframe === iframe) { return; } diff --git a/packages/designer/src/designer/designer.ts b/packages/designer/src/designer/designer.ts index 2b0c3afad..4eeee1e0a 100644 --- a/packages/designer/src/designer/designer.ts +++ b/packages/designer/src/designer/designer.ts @@ -399,12 +399,12 @@ export class Designer { const { components, packages } = incrementalAssets; components && this.buildComponentMetasMap(components); if (packages) { - await this.project.simulator!.setupComponents(packages); + await this.project.simulator?.setupComponents(packages); } if (components) { // 合并 assets - let assets = this.editor.get('assets'); + let assets = this.editor.get('assets') || {}; let newAssets = megreAssets(assets, incrementalAssets); // 对于 assets 存在需要二次网络下载的过程,必须 await 等待结束之后,再进行事件触发 await this.editor.set('assets', newAssets); diff --git a/packages/editor-core/src/editor.ts b/packages/editor-core/src/editor.ts index 6c01bcd7d..5738ab2fc 100644 --- a/packages/editor-core/src/editor.ts +++ b/packages/editor-core/src/editor.ts @@ -113,10 +113,25 @@ export class Editor extends (EventEmitter as any) implements IEditor { if (remoteComponentDescriptions && remoteComponentDescriptions.length) { await Promise.all( remoteComponentDescriptions.map(async (component: any) => { - const { exportName, url } = component; + const { exportName, url, npm } = component; await (new AssetLoader()).load(url); if (window[exportName]) { - assets.components = assets.components.concat(window[exportName].components || []); + if (Array.isArray(window[exportName])) { + (window[exportName] as any).forEach((d: any, i: number) => { + assets.components = assets.components.concat({ + npm: { + ...npm, + exportName: i.toString(), + subName: i.toString(), + }, + ...d.components, + } || []); + }); + } + assets.components = assets.components.concat({ + npm, + ...window[exportName].components, + } || []); assets.componentList = assets.componentList.concat(window[exportName].componentList || []); } return window[exportName]; @@ -124,6 +139,7 @@ export class Editor extends (EventEmitter as any) implements IEditor { ); } } + this.context.set('assets', assets); this.notifyGot('assets'); } diff --git a/packages/editor-skeleton/src/layouts/workbench.less b/packages/editor-skeleton/src/layouts/workbench.less index 3c2b7e578..68098e941 100644 --- a/packages/editor-skeleton/src/layouts/workbench.less +++ b/packages/editor-skeleton/src/layouts/workbench.less @@ -376,6 +376,7 @@ body { } .lc-main-area { flex: 1; + background-color: #edeff3; } .lc-bottom-area { height: var(--bottom-area-height); diff --git a/packages/shell/src/document-model.ts b/packages/shell/src/document-model.ts index 3f8f4e70e..146b3c8f6 100644 --- a/packages/shell/src/document-model.ts +++ b/packages/shell/src/document-model.ts @@ -36,7 +36,7 @@ type PropChangeOptions = { oldValue: any; }; -const Events = { +export const Events = { IMPORT_SCHEMA: 'shell.document.importSchema', }; diff --git a/packages/shell/src/material.ts b/packages/shell/src/material.ts index 6ffd1f0cb..78d5ee4fb 100644 --- a/packages/shell/src/material.ts +++ b/packages/shell/src/material.ts @@ -21,7 +21,7 @@ export default class Material { private readonly [innerEditorSymbol]: Editor; // private readonly [designerSymbol]: Designer; - get [editorSymbol]() { + get [editorSymbol](): Editor { if (this.workspaceMode) { return this[innerEditorSymbol]; } @@ -33,7 +33,7 @@ export default class Material { return this[innerEditorSymbol]; } - get [designerSymbol]() { + get [designerSymbol](): Designer { return this[editorSymbol].get('designer')!; } @@ -66,6 +66,10 @@ export default class Material { return this[editorSymbol].get('assets'); } + async asyncGetAssets() { + return await this[editorSymbol].get('assets'); + } + /** * 加载增量的「资产包」结构,该增量包会与原有的合并 * @param incrementalAssets diff --git a/packages/shell/src/project.ts b/packages/shell/src/project.ts index 4ccd32a41..39a029c91 100644 --- a/packages/shell/src/project.ts +++ b/packages/shell/src/project.ts @@ -6,7 +6,7 @@ import { } from '@alilc/lowcode-designer'; import { RootSchema, ProjectSchema, IEditor } from '@alilc/lowcode-types'; import { globalContext } from '@alilc/lowcode-editor-core'; -import DocumentModel from './document-model'; +import DocumentModel, { Events } from './document-model'; import SimulatorHost from './simulator-host'; import { editorSymbol, projectSymbol, simulatorHostSymbol, simulatorRendererSymbol, documentSymbol } from './symbols'; @@ -129,6 +129,7 @@ export default class Project { */ importSchema(schema?: ProjectSchema) { this[projectSymbol].load(schema, true); + // this[editorSymbol].emit(Events.IMPORT_SCHEMA, schema); } /** diff --git a/packages/utils/src/asset.ts b/packages/utils/src/asset.ts index 23254fc74..b94b66ea8 100644 --- a/packages/utils/src/asset.ts +++ b/packages/utils/src/asset.ts @@ -53,10 +53,9 @@ export function megreAssets(assets: AssetsJson, incrementalAssets: AssetsJson): } if (incrementalAssets.components) { - assets.components = [...assets.components, ...incrementalAssets.components]; + assets.components = [...(assets.components || []), ...incrementalAssets.components]; } - megreAssetsComponentList(assets, incrementalAssets, 'componentList'); megreAssetsComponentList(assets, incrementalAssets, 'bizComponentList'); diff --git a/packages/workspace/src/editor-window/context.ts b/packages/workspace/src/editor-window/context.ts index cfa33b63e..0990e6523 100644 --- a/packages/workspace/src/editor-window/context.ts +++ b/packages/workspace/src/editor-window/context.ts @@ -9,6 +9,15 @@ export class EditorWindow { this.init(); } + async importSchema(schema: any) { + const newSchema = await this.resource.import(schema); + + Object.keys(newSchema).forEach(key => { + const view = this.editorViews.get(key); + view?.project.importSchema(newSchema[key]); + }); + } + async init() { await this.initViewTypes(); await this.execViewTypesInit(); @@ -17,7 +26,7 @@ export class EditorWindow { initViewTypes = async () => { const editorViews = this.resource.editorViews; - for (let i = editorViews.length - 1; i >= 0; i--) { + for (let i = 0; i < editorViews.length; i++) { const name = editorViews[i].name; await this.initViewType(name); if (!this.editorView) { @@ -28,7 +37,7 @@ export class EditorWindow { execViewTypesInit = async () => { const editorViews = this.resource.editorViews; - for (let i = editorViews.length - 1; i >= 0; i--) { + for (let i = 0; i < editorViews.length; i++) { const name = editorViews[i].name; this.changeViewType(name); await this.editorViews.get(name)?.init(); diff --git a/packages/workspace/src/index.ts b/packages/workspace/src/index.ts index cd4025258..e013b975d 100644 --- a/packages/workspace/src/index.ts +++ b/packages/workspace/src/index.ts @@ -67,6 +67,7 @@ export interface ResourceOptions { editorViews?: EditorViewOptions[]; init: (ctx: any) => Promise; dispose: (ctx: any) => Promise; + import: (ctx: any) => Promise; } export interface EditorViewOptions { diff --git a/packages/workspace/src/resource.ts b/packages/workspace/src/resource.ts index 613468f2d..4b2a2d176 100644 --- a/packages/workspace/src/resource.ts +++ b/packages/workspace/src/resource.ts @@ -19,6 +19,10 @@ export class Resource { this.options.init(ctx); } + async import(schema: any) { + return await this.options.import?.(schema); + } + getEditorView(name: string) { return this.editorViewMap.get(name); }