Vue中实现在线画流程图实现

概述

最近在调研一些在线文档的实现,包括文档编辑器、在线思维导图、在线流程图等,前面的文章基于语雀编辑器的在线文档编辑与查看实现了文档编辑器。在本文,分享在Vue框架下基于metaeditor-mxgraph实现在线流程图。

实现效果

实现

1. 添加依赖

json 复制代码
{
   "metaeditor-mxgraph": "^2.0.7"
}

2. 编辑器简介

metaeditor-mxgraph,图元编辑器,支持独立的流程图编辑器,以及 DrawIO 嵌入方案。文档地址为:https://npm.io/package/metaeditor-mxgraph。

3. 编辑器实现

vue 复制代码
<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>

需要注意的是,编辑器默认是绝对定位的,想要跟随设定dom大小,需要设置其样式为:

css 复制代码
 .metagraph-container {
    position: relative;
    width: 100%;
    height: 100%;
    user-select: none;
}

设置完样式后,菜单的位置会出错,这个还没修复,使用时请注意。

4. 文档预览

vue 复制代码
<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>
相关推荐
foxhuli22922 分钟前
禁止ifrmare标签上的文件,实现自动下载功能,并且隐藏工具栏
前端
青皮桔1 小时前
CSS实现百分比水柱图
前端·css
影子信息1 小时前
vue 前端动态导入文件 import.meta.glob
前端·javascript·vue.js
青阳流月1 小时前
1.vue权衡的艺术
前端·vue.js·开源
RunsenLIu1 小时前
基于Vue.js + Node.js + MySQL实现的图书销售管理系统
vue.js·mysql·node.js
样子20181 小时前
Vue3 之dialog弹框简单制作
前端·javascript·vue.js·前端框架·ecmascript
kevin_水滴石穿1 小时前
Vue 中报错 TypeError: crypto$2.getRandomValues is not a function
前端·javascript·vue.js
翻滚吧键盘1 小时前
vue文本插值
javascript·vue.js·ecmascript
华子w9089258591 小时前
基于 SpringBoot+Vue.js+ElementUI 的 “花开富贵“ 花园管理系统设计与实现7000字论文
vue.js·spring boot·elementui
孤水寒月2 小时前
给自己网站增加一个免费的AI助手,纯HTML
前端·人工智能·html