feat: make appHelper observable (#2186)

* feat: make appHelper observable
This commit is contained in:
eightHundreds 2023-06-15 09:59:53 +08:00 committed by GitHub
parent f81954fda5
commit 48acc2d2e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,6 +14,7 @@ import {
createModuleEventBus, createModuleEventBus,
IEventBus, IEventBus,
} from '@alilc/lowcode-editor-core'; } from '@alilc/lowcode-editor-core';
import { import {
ISimulatorHost, ISimulatorHost,
Component, Component,
@ -265,6 +266,8 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
@obx.ref private _contentDocument?: Document; @obx.ref private _contentDocument?: Document;
@obx.ref private _appHelper?: any;
get contentDocument() { get contentDocument() {
return this._contentDocument; return this._contentDocument;
} }
@ -310,13 +313,19 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
this.designer = designer; this.designer = designer;
this.scroller = this.designer.createScroller(this.viewport); this.scroller = this.designer.createScroller(this.viewport);
this.autoRender = !engineConfig.get('disableAutoRender', false); this.autoRender = !engineConfig.get('disableAutoRender', false);
this._appHelper = engineConfig.get('appHelper');
this.componentsConsumer = new ResourceConsumer<Asset | undefined>(() => this.componentsAsset); this.componentsConsumer = new ResourceConsumer<Asset | undefined>(() => this.componentsAsset);
this.injectionConsumer = new ResourceConsumer(() => { this.injectionConsumer = new ResourceConsumer(() => {
return { return {
appHelper: engineConfig.get('appHelper'), appHelper: this._appHelper,
}; };
}); });
engineConfig.onGot('appHelper', (data) => {
// appHelper被config.set修改后触发injectionConsumer.consume回调
this._appHelper = data;
});
this.i18nConsumer = new ResourceConsumer(() => this.project.i18n); this.i18nConsumer = new ResourceConsumer(() => this.project.i18n);
transactionManager.onStartTransaction(() => { transactionManager.onStartTransaction(() => {
@ -384,6 +393,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
purge(): void { purge(): void {
// todo // todo
} }
mountViewport(viewport: HTMLElement | null) { mountViewport(viewport: HTMLElement | null) {
@ -494,7 +504,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
if (Object.keys(this.asyncLibraryMap).length > 0) { if (Object.keys(this.asyncLibraryMap).length > 0) {
// 加载异步 Library // 加载异步 Library
await renderer.loadAsyncLibrary(this.asyncLibraryMap); await renderer.loadAsyncLibrary(this.asyncLibraryMap);
Object.keys(this.asyncLibraryMap).forEach(key => { Object.keys(this.asyncLibraryMap).forEach((key) => {
delete this.asyncLibraryMap[key]; delete this.asyncLibraryMap[key];
}); });
} }
@ -521,7 +531,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
if (Object.keys(this.asyncLibraryMap).length > 0) { if (Object.keys(this.asyncLibraryMap).length > 0) {
// 加载异步 Library // 加载异步 Library
await this.renderer?.loadAsyncLibrary(this.asyncLibraryMap); await this.renderer?.loadAsyncLibrary(this.asyncLibraryMap);
Object.keys(this.asyncLibraryMap).forEach(key => { Object.keys(this.asyncLibraryMap).forEach((key) => {
delete this.asyncLibraryMap[key]; delete this.asyncLibraryMap[key];
}); });
} }
@ -680,7 +690,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
const x = new Event('click'); const x = new Event('click');
x.initEvent('click', true); x.initEvent('click', true);
this._iframe?.dispatchEvent(x); this._iframe?.dispatchEvent(x);
const target = e.target; const { target } = e;
const customizeIgnoreSelectors = engineConfig.get('customizeIgnoreSelectors'); const customizeIgnoreSelectors = engineConfig.get('customizeIgnoreSelectors');
// TODO: need more elegant solution to ignore click events of components in designer // TODO: need more elegant solution to ignore click events of components in designer
@ -1497,7 +1507,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
handleAccept({ container }: DropContainer, e: ILocateEvent): boolean { handleAccept({ container }: DropContainer, e: ILocateEvent): boolean {
const { dragObject } = e; const { dragObject } = e;
const document = this.currentDocument!; const document = this.currentDocument!;
const focusNode = document.focusNode; const { focusNode } = document;
if (isRootNode(container) || container.contains(focusNode)) { if (isRootNode(container) || container.contains(focusNode)) {
return document.checkNesting(focusNode!, dragObject as any); return document.checkNesting(focusNode!, dragObject as any);
} }