mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-09-20 13:39:58 +08:00
docs: 修改onRuntimeReady描述
This commit is contained in:
parent
05f9bb8141
commit
8e1e7ef0de
@ -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);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user