refactor(stage): 根据cr意见优化stageRender构造函数传参

This commit is contained in:
oceanzhu 2022-10-25 14:12:05 +08:00 committed by roymondchen
parent 9787a9e379
commit 554e695ef5
3 changed files with 11 additions and 8 deletions

View File

@ -76,7 +76,7 @@ export default class StageCore extends EventEmitter {
this.containerHighlightDuration = config.containerHighlightDuration || 800;
this.containerHighlightType = config.containerHighlightType;
this.renderer = new StageRender(config.runtimeUrl, this.render);
this.renderer = new StageRender({ runtimeUrl: config.runtimeUrl, render: this.render.bind(this) });
this.mask = new StageMask(this.renderer.getDocument()?.documentElement);
this.dr = new StageDragResize({ core: this, container: this.mask.content, mask: this.mask });
this.multiDr = new StageMultiDragResize({ core: this, container: this.mask.content, mask: this.mask });

View File

@ -21,7 +21,7 @@ import { EventEmitter } from 'events';
import { getHost, injectStyle, isSameDomain } from '@tmagic/utils';
import style from './style.css?raw';
import type { Runtime, RuntimeWindow } from './types';
import type { Runtime, RuntimeWindow, StageRenderConfig } from './types';
export default class StageRender extends EventEmitter {
/** 组件的js、css执行的环境直接渲染为当前windowiframe渲染则为iframe.contentWindow */
@ -33,9 +33,9 @@ export default class StageRender extends EventEmitter {
public runtimeUrl?: string;
private render: () => Promise<HTMLElement | null>;
private render?: () => Promise<HTMLElement | null>;
constructor(runtimeUrl: string | undefined, render: () => Promise<HTMLElement | null>) {
constructor({ runtimeUrl, render }: StageRenderConfig) {
super();
this.runtimeUrl = runtimeUrl || '';
@ -119,9 +119,11 @@ export default class StageRender extends EventEmitter {
if (!this.contentWindow) return;
const el = await this.render();
if (el) {
this.contentWindow.document?.body?.appendChild(el);
if (this.render) {
const el = await this.render();
if (el) {
this.contentWindow.document?.body?.appendChild(el);
}
}
this.emit('onload');

View File

@ -54,7 +54,8 @@ export type StageCoreConfig = {
};
export interface StageRenderConfig {
core: StageCore;
runtimeUrl?: string;
render?: () => Promise<HTMLElement | null>;
}
export interface StageMaskConfig {