windows部署騰訊tmagic-editor02-Runtime

創建editor項目

將上一教程中的hello-world復制過來,改名hello-editor

創建runtime項目

和hello-editor同級

pnpm create vite

在這里插入圖片描述
在這里插入圖片描述
刪除src/components/HelloWorld.vue

按鈕需要用的ts types依賴

pnpm add @tmagic/schema @tmagic/stage

在這里插入圖片描述

實現runtime

將hello-editor中的render函數實現移植到runtime項目中

<script setup lang="ts">
import {createApp,ref, computed} from 'vue';
import type StageCore from '@tmagic/stage';const value = ref({type: 'app',// 必須加上ID,這個id可能是數據庫生成的key,也可以是生成的uuidid: 1,items: [],
});const render = async ({renderer}:StageCore) => {const root = window.document.createElement('div');const page = value.value.items[0];if (!page.value) return root;const {width = 375, height = 1700} = page.value.style || {};root.id = `${page.value.id}`;root.style.cssText = `width: ${width}px;height: ${height}px;`;createApp({template: '<div v-for="node in config.items" :key="node.id" :id="node.id">hello world</div>',props: ['config'],},{config: page.value,},).mount(root);renderer.on('onload', () => {const style = window.document.createElement('style');// 隱藏滾動條,重置默認樣式style.innerHTML = `body {overflow: auto;}html,body {height: 100%; margin: 0;padding: 0;}html::-webkit-scrollbar {width: 0 !important;display: none;}`;renderer.iframe?.contentDocument?.head.appendChild(style);renderer.contentWindow?.magic?.onPageElUpdate(root);renderer.contentWindow?.magic?.onRuntimeReady({});});return root;
};
</script><template><div></div>
</template><style scoped>
</style>

新建ui-page.vue文件

<template><div v-if="config" :id="config.id" :style="style"><div v-for="node in config.items" :key="node.id" :id="node.id">hello world</div></div>
</template><script lang="ts" setup>
import { computed } from 'vue';const props = defineProps<{config: any;
}>();const style = computed(() => {const { width = 375, height = 1700 } = props.config.style || {};return {width: `${width}px`,height: `${height}px`,};
});
</script>

將以下代碼覆蓋到src/App.vue中

<template><uiPage :config="page"></uiPage>
</template><script lang="ts" setup>
import { ref } from 'vue';import uiPage from './ui-page.vue';const page = ref<any>();
</script>

修改vite.config.js
在這里插入圖片描述

啟動項目
在這里插入圖片描述

修改hello-editor

刪除render props,添加runtimeUrl,修改樣式app.vue

<template><m-editorv-model="value":runtime-url="runtimeUrl":component-group-list="componentGroupList"></m-editor>
</template><script lang="ts" setup>
import {ref, createApp, computed} from 'vue';
import {editorService} from '@tmagic/editor';const page = computed(() => editorService.get('page'));const value = ref({type: 'app',// 必須加上ID,這個id可能是數據庫生成的key,也可以是生成的uuidid: 1,items: [],
});const componentGroupList = ref([{title: '組件列表',items: [{icon: 'https://vfiles.gtimg.cn/vupload/20220614/9cc3091655207317835.png',text: 'HelloWorld',type: 'hello-world',},],},
]);const runtimeUrl = 'http://localhost:8078/';
</script><style>
#app {overflow: auto;
}html,body,#app {height: 100%; margin: 0;padding: 0;
}#app::-webkit-scrollbar {width: 0 !important;display: none;
}
</style>

在這里插入圖片描述
啟動hello-editor
在這里插入圖片描述

跨域問題

修改editor-runtime下的vite.config.js

 server: {port: 8078, //指定端口號headers:{'Access-Control-Allow-Origin': '*',}},

runtime與editor通信

到這里項目就可以正常訪問了,但是會發現添加組件沒有反應。

這是因為在runtime中無法直接獲取到editor中的dsl,所以需要通過editor注入到window的magic api來交互

如出現在runtime中出現magic為undefined, 可以嘗試在App.vue中通過監聽message,來準備獲取magic注入時機,然后調用magic.onRuntimeReady,示例代碼如下

window.addEventListener('message', ({ data }) => {if (!data.tmagicRuntimeReady) {return;}window.magic?.onRuntimeReady({// ...});
});

注意:這里可能會出現editor拋出message的時候,runtime還沒有執行到監聽message的情況 編輯器只在iframe onload事件中拋出message 如果出現runtime中接收不到message的情況,可以嘗試在onMounted的時候調用magic.onRuntimeReady

修改main.js為main.ts

import { createApp } from 'vue'
import type { Magic } from '@tmagic/stage';
import './style.css';
import App from './App.vue';declare global {interface Window {magic?: Magic;}
}
createApp(App).mount('#app')

新增tsconfig.json

{"compilerOptions": {"target": "esnext","module": "esnext","strict": true,"jsx": "preserve","moduleResolution": "node","skipLibCheck": true,"esModuleInterop": true,"allowSyntheticDefaultImports": true,"forceConsistentCasingInFileNames": true,"useDefineForClassFields": true,"sourceMap": true,"baseUrl": ".","types": ["webpack-env"],"paths": {"@/*": ["src/*"]},"lib": ["esnext","dom","dom.iterable","scripthost"]},"include": ["src/**/*.ts","src/**/*.tsx","src/**/*.vue","tests/**/*.ts","tests/**/*.tsx"],"exclude": ["node_modules"]
}

src下新增shims-vue.d.ts

/* eslint-disable */
declare module '*.vue' {import type { DefineComponent } from 'vue'const component: DefineComponent<{}, {}, any>export default component
}

修改runtime下的app.vue

<template><uiPage :config="page"></uiPage>
</template><script lang="ts" setup>
import type { RemoveData, UpdateData } from '@tmagic/stage';
import type { Id, MApp, MNode } from '@tmagic/schema';
import { ref,reactive } from 'vue';
import uiPage from './ui-page.vue';
const root = ref<MApp>();const page = ref<any>();window.magic?.onRuntimeReady({/** 當編輯器的dsl對象變化時會調用 */updateRootConfig(config: MApp) {root.value = config;},/** 當編輯器的切換頁面時會調用 */updatePageId(id: Id) {page.value = root.value?.items?.find((item) => item.id === id);},/** 新增組件時調用 */add({ config }: UpdateData) {const parent = config.type === 'page' ? root.value : page.value;parent.items?.push(config);},/** 更新組件時調用 */update({ config }: UpdateData) {const index = page.value.items?.findIndex((child: MNode) => child.id === config.id);page.value.items.splice(index, 1, reactive(config));},/** 刪除組件時調用 */remove({ id }: RemoveData) {const index = page.value.items?.findIndex((child: MNode) => child.id === id);page.value.items.splice(index, 1);},
});
</script>

同步頁面dom給編輯器

由于組件渲染在runtime中,對于編輯器來說是個黑盒,并不知道哪個dom節點才是頁面(對于dsl的解析渲染可能是千奇百怪的),所以需要將頁面的dom節點同步給編輯器
修改runtime下的app.vue

const pageComp = ref<InstanceType<typeof uiPage>>();watch(page, async () => {// page配置變化后,需要等dom更新await nextTick();window?.magic?.onPageElUpdate(pageComp.value?.$el);
});

以上就是一個簡單runtime實現,以及與編輯的交互,這是一個不完善的實現(會發現組件再畫布中無法自由拖動,是因為沒有完整的解析style),但是其中已經幾乎覆蓋所有需要關心的內容

當前教程中實現了一個簡單的page,tmagic提供了一個比較完善的實現,將在下一節介紹

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/13082.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/13082.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/13082.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【C語言】5.C語言函數(2)

文章目錄 7.嵌套調?和鏈式訪問7.1 嵌套調?7.2 鏈式訪問 8.函數的聲明和定義8.1 單個?件8.2 多個?件8.3 static 和 extern8.3.1 static 修飾局部變量8.3.2 static 修飾全局變量8.3.3 static 修飾函數 7.嵌套調?和鏈式訪問 7.1 嵌套調? 嵌套調用就是函數之間的互相調用。…

Docker安裝Mosquitto

在物聯網項目中&#xff0c;我們經常用到MQTT協議&#xff0c;用MQTT協議做交互就需要部署一個MQTT服務&#xff0c;而mosquitto是一個常用的MQTT應用服務&#xff0c; Mosquitto是一個實現了消息推送協議MQTT v3.1的開源消息代理軟件。MQTT&#xff08;Message Queuing Teleme…

python的幾個關于文本文件的demo腳本

部分來自WeTab AI PRO 1.在文末添加一行文字 def add_endline(filename, texts): # 文本末尾增加一行with open(filename, a) as file:file.write(f\n{texts})file.close() 當使用 open() 函數打開文件時&#xff0c;第二個參數指定了文件的打開模式。常見的文件打開模式包…

【LeetCode】每日一題 2024_5_14 完成所有任務需要的最少輪數(哈希)

文章目錄 LeetCode&#xff1f;啟動&#xff01;&#xff01;&#xff01;題目&#xff1a;完成所有任務需要的最少輪數題目描述代碼與解題思路 每天進步一點點 LeetCode&#xff1f;啟動&#xff01;&#xff01;&#xff01; 題目&#xff1a;完成所有任務需要的最少輪數 題…

拿到測試點如何跑

首先你要知道你測試點文件的位置,然后你要創建一個接收結果的文件,將你代碼中的std::cin替換成infile,std::cout替換成outfile即可 #include <fstream> int main() {// 打開輸入文件std::ifstream infile("C:\\Users\\Downloads\\P4779_1.in");// 打開輸出文件…

OpenCV 圖像退化與增強

退化 濾波 img_averagingcv2.blur(img2,(3,3)) #均值濾波 img_median cv2.medianBlur(img2,3) #中值濾波高斯模糊 result cv2.GaussianBlur(source, (11,11), 0)高斯噪聲 def add_noise_Guass(img, mean0, var0.01): # 添加高斯噪聲img np.array(img / 255, dtypefloat…

麒麟 V10 安裝docker2

1. 查看系統版本 2.安裝docker-ce 添加源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 安裝docker yum install docker-ce --allowerasing 重啟docker systemctl start docker 3.安裝nvidia-container-runtime 添…

el-tooltip 提示框樣式修改?

【element-plus el-tooltip官網地址&#xff1a; Tooltip 文字提示 | Element Plus】 <el-tooltippopper-class"Tooltip":content"content"placement"top"effect"light" ><span class"content">{{ content }}&l…

【TypeScript的JSX簡介以及使用方法】

TypeScript 是 JavaScript 的一個超集&#xff0c;它添加了靜態類型檢查和面向對象編程的特性。JSX 是一種 JavaScript 的語法擴展&#xff0c;主要用于 React 組件的聲明性渲染。TypeScript 完美地支持 JSX&#xff0c;并允許你為 React 組件和它們的 props 添加類型注解。 T…

C中Mysql的基本api接口

一、初始化參數返回值 二、鏈接服務器三、執行SQL語句注意事項 四、獲取結果集4.1mysql_affected_rows和mysql_num_rows4.2mysql_store_result與mysql_free_result注意事項注意事項整體的工作流程 4.3mysql_use_result&#xff08;&#xff09;4.4mysql_field_count&#xff08…

001 側邊欄 地址增刪改查 默認地址代碼沒完善

文章目錄 user_index.htmlmyaccount_style.cssmyaccount_scripts.jsaddress_edit.htmlReceiverAddressReceiverAddressControllerReceiverAddressServiceImplIReceiverAddressServiceRFshopAppApplicationServletInitializerpom.xmlReceiverAddressMapper.xmlReceiverAddressMa…

文件存儲解決方案-阿里云OSS

文章目錄 1.菜單分級顯示問題1.問題引出1.蘋果燈&#xff0c;放到節能燈下面也就是id大于1272.查看菜單&#xff0c;并沒有出現蘋果燈3.放到燈具下面id42&#xff0c;就可以顯示 2.問題分析和解決1.判斷可能出現問題的位置2.找到遞歸返回樹形菜單數據的位置3.這里出現問題的原因…

Golang 的 unmarshal 踩坑指南

文章目錄 1. 寫在最前面2. 字段區分出空字段還是未設置字段2.1 問題描述2.2 解決 3. 字段支持多種類型 & 按需做不同類型處理3.1 問題描述3.2 解決 4. 碎碎念5. 參考資料 1. 寫在最前面 筆者最近在實現將內部通知系統的數據定義轉化為產品定義的對外提供的數據結構。 舉例…

算法學習筆記(5.0)-基于比較的高效排序算法-歸并排序

##時間復雜度O(nlogn) 目錄 ##時間復雜度O(nlogn) ##遞歸實現歸并排序 ##原理 ##圖例 ##代碼實現 ##非遞歸實現歸并排序 ##釋 #代碼實現 ##遞歸實現歸并排序 ##原理 是一種基于分治策略的基礎排序算法。 1.劃分階段&#xff1a;通過不斷遞歸地將數組從中點處分開&…

Java 開發 框架安全:Spring 命令執行漏洞.(CVE-2022-22965)

什么叫 Spring 框架. Spring 框架是一個用于構建企業級應用程序的開源框架。它提供了一種全面的編程和配置模型&#xff0c;可以簡化應用程序的開發過程。Spring 框架的核心特性包括依賴注入&#xff08;Dependency Injection&#xff09;、面向切面編程&#xff08;Aspect-Or…

【SpringBoot筆記43】SpringBoot應用程序集成spring-boot-admin監控工具

這篇文章,主要介紹SpringBoot應用程序如何集成spring-boot-admin監控工具。 目錄 一、spring-boot-admin監控工具 1.1、創建admin-client客戶端 (1)引入依賴

DeepSpeed

文章目錄 一、關于 DeepSpeed1、DeepSpeed 是什么2、深度學習訓練和推理的極致速度和規模3、DeepSpeed 的四大創新支柱1&#xff09;DeepSpeed 訓練2&#xff09;DeepSpeed 推理3&#xff09;DeepSpeed 壓縮4&#xff09;DeepSpeed4Science 4、DeepSpeed 軟件套件DeepSpeed 庫推…

React 第二十七章 Hook useCallback

useCallback 是 React 提供的一個 Hook 函數&#xff0c;用于優化性能。它的作用是返回一個記憶化的函數&#xff0c;當依賴發生變化時&#xff0c;才會重新創建并返回新的函數。 在 React 中&#xff0c;當一個組件重新渲染時&#xff0c;所有的函數都會被重新創建。這可能會…

青少年軟件編程(Python)等級考試試卷(五級)2024年3月

2024.03 電子學會 青少年軟件編程&#xff08;Python&#xff09;等級考試試卷&#xff08;五級&#xff09; 一、單選題 1.以下代碼的輸出結果是? ) nums list(range(100, 201)) print(nums[::10]) A.[100,110,120,130,140,150,160,170,180,190,200] B.[100,101,1…

QML筆記八

QML與C交互 QML中調用C功能、使用QML或者Quick中的C接口、使用C實現自定義的QML對象 注&#xff1a; 只有QObject的派生類才能與QML交互 QML引擎集成Qt元對象系統&#xff0c;QObject的派生子類的屬性、方法、信號都可以在QML中訪問 C類可以被注冊為一個QML實例 C類可以被注冊為…