Vue中實(shí)現(xiàn)在線畫(huà)流程圖的方法
概述
最近在調(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è)還沒(méi)修復(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)在線畫(huà)流程圖實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue畫(huà)流程圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)在v-html的html字符串中綁定事件
今天小編就為大家分享一篇vue實(shí)現(xiàn)在v-html的html字符串中綁定事件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Vue實(shí)現(xiàn)模糊查詢的簡(jiǎn)單示例
在Vue中實(shí)現(xiàn)模糊查詢,你可以使用JavaScript的filter和includes方法,結(jié)合Vue的v-for指令,本文給大家舉了一個(gè)簡(jiǎn)單的示例,并通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Vue監(jiān)聽(tīng)使用方法和過(guò)濾器實(shí)現(xiàn)
這篇文章主要介紹了Vue監(jiān)聽(tīng)使用方法和過(guò)濾器實(shí)現(xiàn),過(guò)濾器為頁(yè)面中數(shù)據(jù)進(jìn)行強(qiáng)化,具有局部過(guò)濾器和全局過(guò)濾器2022-06-06
Vue2實(shí)現(xiàn)自適應(yīng)屏幕大小的兩種方法詳解
這篇文章主要為大家詳細(xì)介紹了Vue2實(shí)現(xiàn)自適應(yīng)屏幕大小的兩種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

