feat(editor,stage): 支持切换runtimeUrl

This commit is contained in:
roymondchen 2025-05-29 20:17:15 +08:00
parent 0196eb343a
commit 92534fc915
3 changed files with 42 additions and 5 deletions

View File

@ -128,7 +128,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { provide } from 'vue'; import { provide, watch } from 'vue';
import type { MApp } from '@tmagic/core'; import type { MApp } from '@tmagic/core';
@ -209,6 +209,22 @@ const stageOptions: StageOptions = {
stageOverlayService.set('stageOptions', stageOptions); stageOverlayService.set('stageOptions', stageOptions);
watch(
() => props.runtimeUrl,
(url) => {
if (!url) {
return;
}
const stage = editorService.get('stage');
if (!stage) {
return;
}
stage.reloadIframe(url);
},
);
provide('services', services); provide('services', services);
provide('codeOptions', props.codeOptions); provide('codeOptions', props.codeOptions);

View File

@ -236,6 +236,10 @@ export default class StageCore extends EventEmitter {
this.actionManager?.enableMultiSelect(); this.actionManager?.enableMultiSelect();
} }
public reloadIframe(url: string) {
this.renderer?.reloadIframe(url);
}
/** /**
* *
*/ */

View File

@ -172,14 +172,31 @@ export default class StageRender extends EventEmitter {
); );
} }
/** public reloadIframe(url: string) {
* if (this.renderType !== RenderType.IFRAME) return;
*/
public destroy(): void { const el = this.iframe?.parentElement;
this.destroyIframe();
this.runtimeUrl = url;
this.createIframe();
this.mount(el as HTMLDivElement);
this.runtime = null;
}
public destroyIframe() {
this.iframe?.removeEventListener('load', this.iframeLoadHandler); this.iframe?.removeEventListener('load', this.iframeLoadHandler);
this.contentWindow = null; this.contentWindow = null;
this.iframe?.remove(); this.iframe?.remove();
this.iframe = undefined; this.iframe = undefined;
}
/**
*
*/
public destroy(): void {
this.destroyIframe();
// @ts-ignore
globalThis.runtime = undefined;
this.removeAllListeners(); this.removeAllListeners();
} }