docs: 修改onRuntimeReady描述

This commit is contained in:
roymondchen 2022-07-29 16:56:12 +08:00 committed by jia000
parent 05f9bb8141
commit 8e1e7ef0de

View File

@ -149,12 +149,23 @@ devServer: {
这是因为在runtime中无法直接获取到editor中的dsl所以需要通过editor注入到window的magic api来交互 这是因为在runtime中无法直接获取到editor中的dsl所以需要通过editor注入到window的magic api来交互
在App.vue中通过监听message来准备获取magic注入时机然后调用magic.onRuntimeReady示例代码如下 > 如出现在runtime中出现magic为undefined 可以尝试在App.vue中通过监听message来准备获取magic注入时机然后调用magic.onRuntimeReady示例代码如下
```js
window.addEventListener('message', ({ data }) => {
if (!data.tmagicRuntimeReady) {
return;
}
window.magic?.onRuntimeReady({
// ...
});
});
```
> 这里可能会出现editor抛出message的时候runtime还没有执行到监听message的情况 > 这里可能会出现editor抛出message的时候runtime还没有执行到监听message的情况
> 编辑器只在iframe onload事件中抛出message > 编辑器只在iframe onload事件中抛出message
> 如果出现runtime中接收不到message的情况可以尝试在onMounted的时候调用magic.onRuntimeReady > 如果出现runtime中接收不到message的情况可以尝试在onMounted的时候调用magic.onRuntimeReady
```ts ```ts
import type { Magic } from '@tmagic/stage'; import type { Magic } from '@tmagic/stage';
@ -165,47 +176,40 @@ declare global {
} }
``` ```
```ts ```ts
import type { RemoveData, UpdateData } from '@tmagic/stage'; import type { RemoveData, UpdateData } from '@tmagic/stage';
import type { Id, MApp, MNode } from '@tmagic/schema'; import type { Id, MApp, MNode } from '@tmagic/schema';
const root = ref<MApp>(); const root = ref<MApp>();
window.addEventListener('message', ({ data }) => { window.magic?.onRuntimeReady({
if (!data.tmagicRuntimeReady) { /** 当编辑器的dsl对象变化时会调用 */
return; updateRootConfig(config: MApp) {
} root.value = config;
},
window.magic?.onRuntimeReady({ /** 当编辑器的切换页面时会调用 */
/** 当编辑器的dsl对象变化时会调用 */ updatePageId(id: Id) {
updateRootConfig(config: MApp) { page.value = root.value?.items?.find((item) => item.id === id);
root.value = config; },
},
/** 当编辑器的切换页面时会调用 */ /** 新增组件时调用 */
updatePageId(id: Id) { add({ config }: UpdateData) {
page.value = root.value?.items?.find((item) => item.id === id); const parent = config.type === 'page' ? root.value : page.value;
}, parent.items?.push(config);
},
/** 新组件时调用 */ /** 新组件时调用 */
add({ config }: UpdateData) { update({ config }: UpdateData) {
const parent = config.type === 'page' ? root.value : page.value; const index = page.value.items?.findIndex((child: MNode) => child.id === config.id);
parent.items?.push(config); page.value.items.splice(index, 1, reactive(config));
}, },
/** 更新组件时调用 */ /** 删除组件时调用 */
update({ config }: UpdateData) { remove({ id }: RemoveData) {
const index = page.value.items?.findIndex((child: MNode) => child.id === config.id); const index = page.value.items?.findIndex((child: MNode) => child.id === id);
page.value.items.splice(index, 1, reactive(config)); page.value.items.splice(index, 1);
}, },
/** 删除组件时调用 */
remove({ id }: RemoveData) {
const index = page.value.items?.findIndex((child: MNode) => child.id === id);
page.value.items.splice(index, 1);
},
});
}); });
``` ```