美文网首页GIS加油站
Vue中实现在线画流程图实现

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

作者: 牛老师讲GIS | 来源:发表于2024-07-10 21:51 被阅读0次

概述

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

实现效果

image.png

实现

1. 添加依赖

{
   "metaeditor-mxgraph": "^2.0.7"
}

2. 编辑器简介

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

3. 编辑器实现

<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大小,需要设置其样式为:

 .metagraph-container {
    position: relative;
    width: 100%;
    height: 100%;
    user-select: none;
}

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

4. 文档预览

<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>

相关文章

  • pdf.js的使用实例

    最近 vue项目要在移动端实现在线浏览pdf,所以想到用pdf.jspdf.js可以实现在线预览pdf文档,核心部...

  • mui+vue实现图片上传

    mui+vue实现图片上传 在线工具:http://mxdqh.top/ /* ...

  • ProcessOn 在线思维导图和流程图小工具

    ProcessOn 在线思维导图和流程图小工具 ProcessOn是一个在线作图工具平台,它可以在线画流程图、思维...

  • Vue实现原理

    vue实现原理 1、了解Object的属性defineProperty 2、vue中mvvm的实现: 数据变化更新...

  • SpringBoot整合Activiti项目实战

    SpringBoot整合Activiti实现流程图的在线绘制、流程规则配置、请假流程申请流程流转与业务处理 1、视...

  • Vue页面预览PDF文件

    vue-pdf,可实现在线预览 PDF 格式的文档,流程: 在搭建好的 Vue 项目中,引入 Vue-pdf np...

  • Vue的响应式浅析

    1 Vue如何实现响应式? Vue的响应式是建立在监听data中的数据. 2 在Vue2中响应式的实现 Vue通过...

  • 基于vue-cli3在vue中实现rem布局:postcss-p

    在vue中实现rem布局:postcss-pxtorem+amfe-flexible实现移动端适配环境参数:vue...

  • 杂题

    vue中双向绑定是如何实现的? vue是使用Object.defineProperty()来实现数据劫持的,它可以...

  • vuex

    官方推荐的数据框架:在vue的开发中 vue实现视图层的开发,vuex来实现数据层,实现数据共享 vuex是整个虚...

网友评论

    本文标题:Vue中实现在线画流程图实现

    本文链接:https://www.haomeiwen.com/subject/joethjtx.html