Vue中實(shí)現(xiàn)在線畫流程圖的方法
概述
最近在調(diào)研一些在線文檔的實(shí)現(xiàn),包括文檔編輯器、在線思維導(dǎo)圖、在線流程圖等,前面的文章基于語(yǔ)雀編輯器的在線文檔編輯與查看實(shí)現(xiàn)了文檔編輯器。在本文,分享在Vue框架下基于metaeditor-mxgraph實(shí)現(xiàn)在線流程圖。
實(shí)現(xiàn)效果

實(shí)現(xiàn)
1. 添加依賴
{
"metaeditor-mxgraph": "^2.0.7"
}2. 編輯器簡(jiǎn)介
metaeditor-mxgraph,圖元編輯器,支持獨(dú)立的流程圖編輯器,以及 DrawIO 嵌入方案。文檔地址為:https://npm.io/package/metaeditor-mxgraph。
3. 編輯器實(shí)現(xiàn)
<template>
<div class="flow-chart" ref="flowChart"></div>
</template>
<script>
import 'metaeditor-mxgraph/assets/index.scss'
import { MetaEditor } from 'metaeditor-mxgraph'
const { MetaGraphEditor, getLanguage, stringToXml, xmlToString } = MetaEditor
export default {
props: {
chartData: {
type: Object,
default: () => null,
},
},
mounted() {
this.$nextTick(() => {
this.init()
})
},
watch: {
chartData() {
this.init()
},
},
unmounted() {
this.destroy()
},
methods: {
destroy() {
if (window.metaGraphEditor) window.metaGraphEditor.destroy()
window.metaGraphEditor = null
},
init() {
this.destroy()
const xml = stringToXml(this.chartData || '<mxGraphModel></mxGraphModel>')
const dom = this.$refs.flowChart
const metaGraphEditor = new MetaGraphEditor({
container: dom,
})
const lan = getLanguage('zh')
metaGraphEditor.init(lan, xml)
window.metaGraphEditor = metaGraphEditor
},
},
}
</script>
<style scoped lang="scss">
.flow-chart {
width: 100%;
height: 100%;
}
</style>需要注意的是,編輯器默認(rèn)是絕對(duì)定位的,想要跟隨設(shè)定dom大小,需要設(shè)置其樣式為:
.metagraph-container {
position: relative;
width: 100%;
height: 100%;
user-select: none;
}設(shè)置完樣式后,菜單的位置會(huì)出錯(cuò),這個(gè)還沒修復(fù),使用時(shí)請(qǐng)注意。
4. 文檔預(yù)覽
<template>
<div class="flow-chart" ref="flowChart"></div>
</template>
<script>
import 'metaeditor-mxgraph/assets/index.scss'
import { MetaEditor } from 'metaeditor-mxgraph'
const { MetaGraphViewer, stringToXml} = MetaEditor
export default {
props: {
chartData: {
type: Object,
default: () => null,
},
},
mounted() {
this.$nextTick(() => {
this.init()
})
},
watch: {
chartData() {
this.init()
},
},
methods: {
init() {
const xml = stringToXml(this.chartData || '<mxGraphModel></mxGraphModel>')
const dom = this.$refs.flowChart
const metaGraphEditor = new MetaGraphViewer({
xml: xml,
})
const { offsetWidth, offsetHeight } = dom
const svg = metaGraphEditor.renderSVGDom(null, 1, 1, {
width: offsetWidth,
height: offsetHeight,
})
dom.appendChild(svg)
},
},
}
</script>
<style scoped lang="scss">
.flow-chart {
width: 100%;
height: 100%;
}
</style>到此這篇關(guān)于Vue中實(shí)現(xiàn)在線畫流程圖實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue畫流程圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中使用vue-router切換頁(yè)面時(shí)滾動(dòng)條自動(dòng)滾動(dòng)到頂部的方法
這篇文章主要介紹了vue項(xiàng)目中在使用vue-router切換頁(yè)面的時(shí)候滾動(dòng)條自動(dòng)滾動(dòng)到頂部的實(shí)現(xiàn)方法,一般使用Window scrollTo() 方法實(shí)現(xiàn),本文給大家簡(jiǎn)單介紹了crollTop的使用,需要的朋友可以參考下2017-11-11
Vue Router4與Router3路由配置與區(qū)別說(shuō)明
這篇文章主要介紹了Vue Router4與Router3路由配置與區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-12-12
Vue3實(shí)現(xiàn)計(jì)算屬性的代碼詳解
計(jì)算屬性對(duì)于前端開發(fā)來(lái)說(shuō)算是經(jīng)常使用的一個(gè)能力了,本文將從代碼層面來(lái)給大家介紹下Vue3是如何實(shí)現(xiàn)計(jì)算屬性的,需要的朋友可以參考下2023-07-07
vue中實(shí)現(xiàn)代碼高亮與語(yǔ)法著色的方法介紹
在Vue的開發(fā)過(guò)程中,我們經(jīng)常需要展示代碼片段或者進(jìn)行代碼高亮與語(yǔ)法著色,Vue提供了多種方式來(lái)實(shí)現(xiàn)代碼高亮與語(yǔ)法著色,本文將為你詳細(xì)介紹這些方法,需要的朋友可以參考下2023-06-06
vue實(shí)現(xiàn)錄音并轉(zhuǎn)文字功能包括PC端web手機(jī)端web(實(shí)現(xiàn)過(guò)程)
vue實(shí)現(xiàn)錄音并轉(zhuǎn)文字功能,包括PC端,手機(jī)端和企業(yè)微信自建應(yīng)用端,本文通過(guò)實(shí)例代碼介紹vue實(shí)現(xiàn)錄音并轉(zhuǎn)文字功能包括PC端web手機(jī)端web,感興趣的朋友跟隨小編一起看看吧2024-08-08
Vue.js如何利用v-for循環(huán)生成動(dòng)態(tài)標(biāo)簽
這篇文章主要給大家介紹了關(guān)于Vue.js如何利用v-for循環(huán)生成動(dòng)態(tài)標(biāo)簽的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01

