diff --git a/packages/stage/src/StageRender.ts b/packages/stage/src/StageRender.ts index eb822fd4..43fd47d5 100644 --- a/packages/stage/src/StageRender.ts +++ b/packages/stage/src/StageRender.ts @@ -72,20 +72,22 @@ export default class StageRender extends EventEmitter { * @param el 将页面挂载到该Dom节点上 */ public async mount(el: HTMLDivElement) { - if (this.iframe) { - if (!isSameDomain(this.runtimeUrl) && this.runtimeUrl) { - // 不同域,使用srcdoc发起异步请求,需要目标地址支持跨域 - let html = await fetch(this.runtimeUrl).then((res) => res.text()); - // 使用base, 解决相对路径或绝对路径的问题 - const base = `${location.protocol}//${getHost(this.runtimeUrl)}`; - html = html.replace('', `\n`); - this.iframe.srcdoc = html; - } - - el.appendChild(this.iframe); - } else { + if (!this.iframe) { throw Error('mount 失败'); } + + if (!isSameDomain(this.runtimeUrl) && this.runtimeUrl) { + // 不同域,使用srcdoc发起异步请求,需要目标地址支持跨域 + let html = await fetch(this.runtimeUrl).then((res) => res.text()); + // 使用base, 解决相对路径或绝对路径的问题 + const base = `${location.protocol}//${getHost(this.runtimeUrl)}`; + html = html.replace('', `\n`); + this.iframe.srcdoc = html; + } + + el.appendChild(this.iframe); + + this.postTmagicRuntimeReady(); } public getRuntime = (): Promise => { @@ -111,26 +113,34 @@ export default class StageRender extends EventEmitter { } private loadHandler = async () => { - this.contentWindow = this.iframe?.contentWindow as RuntimeWindow; + if (!this.contentWindow?.magic) { + this.postTmagicRuntimeReady(); + } - this.contentWindow.magic = this.getMagicApi(); + if (!this.contentWindow) return; if (this.render) { const el = await this.render(this.core); if (el) { - this.iframe?.contentDocument?.body?.appendChild(el); + this.contentWindow.document?.body?.appendChild(el); } } this.emit('onload'); + injectStyle(this.contentWindow.document, style); + }; + + private postTmagicRuntimeReady() { + this.contentWindow = this.iframe?.contentWindow as RuntimeWindow; + + this.contentWindow.magic = this.getMagicApi(); + this.contentWindow.postMessage( { tmagicRuntimeReady: true, }, '*', ); - - injectStyle(this.contentWindow.document, style); - }; + } } diff --git a/packages/ui-react/src/useApp.ts b/packages/ui-react/src/useApp.ts index e49b6cce..72cf8019 100644 --- a/packages/ui-react/src/useApp.ts +++ b/packages/ui-react/src/useApp.ts @@ -43,6 +43,7 @@ export default ({ config, methods }: UseAppOptions) => { useEffect(() => { const emitData = { + config, ...methods, }; diff --git a/runtime/react/src/playground/App.css b/runtime/react/src/playground/App.css index e6768580..027b5be7 100644 --- a/runtime/react/src/playground/App.css +++ b/runtime/react/src/playground/App.css @@ -10,7 +10,7 @@ body, overflow: auto; } -#app::-webkit-scrollbar { +::-webkit-scrollbar { width: 0; } diff --git a/runtime/vue2/src/playground/App.vue b/runtime/vue2/src/playground/App.vue index 3a773f7d..2764eadd 100644 --- a/runtime/vue2/src/playground/App.vue +++ b/runtime/vue2/src/playground/App.vue @@ -3,7 +3,7 @@