fix(stage,runtime,ui-react): tmagicRuntimeReady时机修改

fix #198
This commit is contained in:
roymondchen 2022-07-29 12:48:09 +08:00 committed by jia000
parent 0a1cf060a5
commit 9858327eb8
5 changed files with 152 additions and 145 deletions

View File

@ -72,7 +72,10 @@ export default class StageRender extends EventEmitter {
* @param el Dom节点上 * @param el Dom节点上
*/ */
public async mount(el: HTMLDivElement) { public async mount(el: HTMLDivElement) {
if (this.iframe) { if (!this.iframe) {
throw Error('mount 失败');
}
if (!isSameDomain(this.runtimeUrl) && this.runtimeUrl) { if (!isSameDomain(this.runtimeUrl) && this.runtimeUrl) {
// 不同域使用srcdoc发起异步请求需要目标地址支持跨域 // 不同域使用srcdoc发起异步请求需要目标地址支持跨域
let html = await fetch(this.runtimeUrl).then((res) => res.text()); let html = await fetch(this.runtimeUrl).then((res) => res.text());
@ -83,9 +86,8 @@ export default class StageRender extends EventEmitter {
} }
el.appendChild<HTMLIFrameElement>(this.iframe); el.appendChild<HTMLIFrameElement>(this.iframe);
} else {
throw Error('mount 失败'); this.postTmagicRuntimeReady();
}
} }
public getRuntime = (): Promise<Runtime> => { public getRuntime = (): Promise<Runtime> => {
@ -111,26 +113,34 @@ export default class StageRender extends EventEmitter {
} }
private loadHandler = async () => { 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) { if (this.render) {
const el = await this.render(this.core); const el = await this.render(this.core);
if (el) { if (el) {
this.iframe?.contentDocument?.body?.appendChild(el); this.contentWindow.document?.body?.appendChild(el);
} }
} }
this.emit('onload'); 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( this.contentWindow.postMessage(
{ {
tmagicRuntimeReady: true, tmagicRuntimeReady: true,
}, },
'*', '*',
); );
}
injectStyle(this.contentWindow.document, style);
};
} }

View File

@ -43,6 +43,7 @@ export default ({ config, methods }: UseAppOptions) => {
useEffect(() => { useEffect(() => {
const emitData = { const emitData = {
config,
...methods, ...methods,
}; };

View File

@ -10,7 +10,7 @@ body,
overflow: auto; overflow: auto;
} }
#app::-webkit-scrollbar { ::-webkit-scrollbar {
width: 0; width: 0;
} }

View File

@ -3,7 +3,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { computed, defineComponent, nextTick, onMounted, provide, reactive, ref, watch } from 'vue'; import { computed, defineComponent, nextTick, provide, reactive, ref, watch } from 'vue';
import Core from '@tmagic/core'; import Core from '@tmagic/core';
import type { Id, MApp, MNode } from '@tmagic/schema'; import type { Id, MApp, MNode } from '@tmagic/schema';
@ -39,7 +39,6 @@ export default defineComponent({
page && window.magic.onPageElUpdate(page); page && window.magic.onPageElUpdate(page);
}); });
onMounted(() => {
window.magic?.onRuntimeReady({ window.magic?.onRuntimeReady({
getApp() { getApp() {
return app; return app;
@ -104,7 +103,6 @@ export default defineComponent({
parent.items.splice(index, 1); parent.items.splice(index, 1);
}, },
}); });
});
return { return {
pageConfig, pageConfig,
@ -114,6 +112,10 @@ export default defineComponent({
</script> </script>
<style lang="scss"> <style lang="scss">
::-webkit-scrollbar {
width: 0;
}
html, html,
body, body,
#app { #app {
@ -124,10 +126,6 @@ body,
#app { #app {
position: relative; position: relative;
overflow: auto; overflow: auto;
&::-webkit-scrollbar {
width: 0;
}
} }
.magic-ui-container { .magic-ui-container {

View File

@ -3,7 +3,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { computed, defineComponent, nextTick, onMounted, provide, reactive, ref, watch } from 'vue'; import { computed, defineComponent, nextTick, provide, reactive, ref, watch } from 'vue';
import Core from '@tmagic/core'; import Core from '@tmagic/core';
import type { Id, MApp, MNode } from '@tmagic/schema'; import type { Id, MApp, MNode } from '@tmagic/schema';
@ -39,7 +39,6 @@ export default defineComponent({
page && window.magic.onPageElUpdate(page); page && window.magic.onPageElUpdate(page);
}); });
onMounted(() => {
window.magic?.onRuntimeReady({ window.magic?.onRuntimeReady({
getApp() { getApp() {
return app; return app;
@ -104,7 +103,6 @@ export default defineComponent({
parent.items.splice(index, 1); parent.items.splice(index, 1);
}, },
}); });
});
return { return {
pageConfig, pageConfig,
@ -114,6 +112,10 @@ export default defineComponent({
</script> </script>
<style lang="scss"> <style lang="scss">
::-webkit-scrollbar {
width: 0;
}
html, html,
body, body,
#app { #app {
@ -124,10 +126,6 @@ body,
#app { #app {
position: relative; position: relative;
overflow: auto; overflow: auto;
&::-webkit-scrollbar {
width: 0;
}
} }
.magic-ui-container { .magic-ui-container {