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>
相关推荐
GISer_Jing8 分钟前
字符串操作&栈和队列
前端·javascript
黑土豆15 分钟前
TypeScript技术系列13:深入理解配置文件tsconfig.json
前端·javascript·typescript
zheshiyangyang21 分钟前
JavaScript---原型和原型链
开发语言·前端·javascript
momo_养身版1 小时前
Browser use — 利用 AI 操作浏览器 · 原理篇
前端·openai
悲且狂1 小时前
Vue环境搭建:vue+idea
前端·vue.js·intellij-idea
Allen Bright1 小时前
【XML基础-1】深入理解XML:介绍、语法规则与实际应用
xml·前端
大个个个个个儿1 小时前
vue3腾讯云直播 前端拉流(前端页面展示直播)
前端·javascript·腾讯云
momo_养身版1 小时前
Browser use — 利用 AI 操作浏览器 · 实践篇
前端·ai编程
看晴天了1 小时前
关于web应用开发赛道的备考
前端·node.js·ecmascript 6
LoveCan1 小时前
ant-design-vue自动计算a-table每一列的宽度的实现
前端·vue.js