windows部署腾讯tmagic-editor03-DSL 解析渲染

2024-05-16 05:52

本文主要是介绍windows部署腾讯tmagic-editor03-DSL 解析渲染,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

创建项目

将上一教程中的editor-runtime和hello-editor复制过来

概念

在这里插入图片描述

实现

创建hello-ui目录
在这里插入图片描述

渲染节点

在hello-ui下创建 Component.vue 文件

由于节点的type是由业务自行定义的,所以需要使用动态组件渲染,在vue下可以使用component组件来实现

component 是通过is参数来决定哪个组件被渲染,所以将type与组件做绑定

例如有组件 HelloWorld,可以将组件全局注册

app.component('hello-world', HelloWorld);

然后将’hello-world’作为type,那么is="hello-world"就会渲染 HelloWorld 组件

为了让组件渲染出来的dom能被编辑器识别到,还需要将节点的id作为dom的id

<template><component v-if="config" :is="type" :id="`${id}`" :style="style" :config="config"><slot></slot></component>
</template><script lang=ts setup>
import { computed } from 'vue';import type { MNode } from '@tmagic/schema';// 将节点作品参数传入组件中
const props = defineProps<{config: MNode;
}>();const type = computed(() => {if (!props.config.type || ['page', 'container'].includes(props.config.type)) return 'div';return props.config.type;
});const id = computed(() => props.config.id);const fillBackgroundImage = (value: string) => {if (value && !/^url/.test(value) && !/^linear-gradient/.test(value)) {return `url(${value})`;}return value;
};const style = computed(() => {if (!props.config.style) {return {};}const results: Record<string, any> = {};const whiteList = ['zIndex', 'opacity', 'fontWeight'];Object.entries(props.config.style).forEach(([key, value]) => {if (key === 'backgroundImage') {value && (results[key] = fillBackgroundImage(value));} else if (key === 'transform' && typeof value !== 'string') {results[key] = Object.entries(value as Record<string, string>).map(([transformKey, transformValue]) => {let defaultValue = 0;if (transformKey === 'scale') {defaultValue = 1;}return `${transformKey}(${transformValue || defaultValue})`;}).join(' ');} else if (!whiteList.includes(key) && value && /^[-]?[0-9]*[.]?[0-9]*$/.test(value)) {results[key] = `${value}px`;} else {results[key] = value;}});return results;
});
</script>

接下来就需要解析节点的样式,在tmagic/editor中默认会将样式配置保存到节点的style属性中,如果自行定义到了其他属性,则已实际为准
解析style需要注意几个地方

数字

css中的数值有些是需要单位的,例如px,有些是不需要的,例如opacity
在tmagic/editor中,默认都是不带单位的,所以需要将需要单位的地方补齐单位
这里做补齐px处理,如果需要做屏幕大小适应, 可以使用rem或者vw,这个可以根据自身需求处理。

url

css中的url需要是用url(),所以当值为url时,需要转为url(xxx)

transform

transform属性可以指定为关键字值none 或一个或多个transform-function值。

渲染容器

容器与普通节点的区别,就是需要多一个items的解析

新增Container.vue文件

<template><Component :config="config"><Component v-for="item in config.items" :key="item.id" :config="item"></Component></Component>
</template><script lang="ts" setup>
import type { MContainer } from '@tmagic/schema';import Component from './Component.vue';defineProps<{config: MContainer;
}>();
</script>

渲染页面

页面就是容器,之所以单独存在,是页面会自己的方法,例如reload等
Page.vue文件

<template><Container :config="config"></Container>
</template><script lang="ts" setup>
import type { MPage } from '@tmagic/schema';import Container from './Container.vue';defineProps<{config: MPage;
}>();defineExpose({reload() {window.location.reload();}
});
</script>

在runtime中使用 hello-ui

删除editor-runtime/src/ui-page.vue

将App.vue中的ui-page改成hello-ui中的Page

<template><Page v-if="page" :config="page" ref="pageComp"></Page>
</template><script lang="ts" setup>
import type { RemoveData, UpdateData } from '@tmagic/stage';
import type { Id, MApp, MNode } from '@tmagic/schema';
import { ref,reactive,watch,nextTick } from 'vue';
import { Page } from 'hello-ui';
const pageComp = ref<InstanceType<typeof Page>>();
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);},
});
watch(page, async () => {// page配置变化后,需要等dom更新await nextTick();window?.magic?.onPageElUpdate(pageComp.value?.$el);
});
</script>

在editor-runtime/vue.config.js中加上配置

configureWebpack: {resolve: {alias: {'hello-ui': path.resolve(__dirname, '../hello-ui'),vue$: path.resolve(__dirname, './node_modules/vue'),},},},

添加HelloWorld组件

在hello-ui下新增HelloWorld.vue

<template><div>hollo-world</div>
</template><script lang="ts" setup>
import type { MNode } from '@tmagic/schema';defineProps<{config: MNode;
}>();
</script>

在editor-runtime main.ts中注册HelloWorld

import { createApp } from 'vue'
import type { Magic } from '@tmagic/stage';
import './style.css';
import App from './App.vue';
// eslint-disable-next-line
import { HelloWorld } from 'hello-ui';declare global {interface Window {magic?: Magic;}
}
const app = createApp(App)
app.component('hello-world', HelloWorld);
app.mount('#app')

放在外层不好使,需要将hello-ui放到src
在这里插入图片描述
在这里插入图片描述
这样就实现了组件的渲染展示

这篇关于windows部署腾讯tmagic-editor03-DSL 解析渲染的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994056

相关文章

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

SQL 外键Foreign Key全解析

《SQL外键ForeignKey全解析》外键是数据库表中的一列(或一组列),用于​​建立两个表之间的关联关系​​,外键的值必须匹配另一个表的主键(PrimaryKey)或唯一约束(UniqueCo... 目录1. 什么是外键?​​ ​​​​2. 外键的语法​​​​3. 外键的约束行为​​​​4. 多列外键​

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

Maven 插件配置分层架构深度解析

《Maven插件配置分层架构深度解析》:本文主要介绍Maven插件配置分层架构深度解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Maven 插件配置分层架构深度解析引言:当构建逻辑遇上复杂配置第一章 Maven插件配置的三重境界1.1 插件配置的拓扑

ubuntu如何部署Dify以及安装Docker? Dify安装部署指南

《ubuntu如何部署Dify以及安装Docker?Dify安装部署指南》Dify是一个开源的大模型应用开发平台,允许用户快速构建和部署基于大语言模型的应用,ubuntu如何部署Dify呢?详细请... Dify是个不错的开源LLM应用开发平台,提供从 Agent 构建到 AI workflow 编排、RA

ubuntu16.04如何部署dify? 在Linux上安装部署Dify的技巧

《ubuntu16.04如何部署dify?在Linux上安装部署Dify的技巧》随着云计算和容器技术的快速发展,Docker已经成为现代软件开发和部署的重要工具之一,Dify作为一款优秀的云原生应用... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。它

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

全解析CSS Grid 的 auto-fill 和 auto-fit 内容自适应

《全解析CSSGrid的auto-fill和auto-fit内容自适应》:本文主要介绍了全解析CSSGrid的auto-fill和auto-fit内容自适应的相关资料,详细内容请阅读本文,希望能对你有所帮助... css  Grid 的 auto-fill 和 auto-fit/* 父元素 */.gri