vue2中使用jsplumb完成流程图

2023-12-09 00:52

本文主要是介绍vue2中使用jsplumb完成流程图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

之前的几个demo都是在vue3中写的,虽然可以直接拿去复用。

但是根据有些看客反馈,想用一个vue2版本的,毕竟很多人开发功能的时间都不是特别富裕。大多时候还是用现成的demo更好一些。

这里我就写一个简易版本的demo,可以实现绘制,并且删除连接线和节点等功能,篇幅也不大,适合应急的朋友,哈哈

代码

1.剥离公共的配置  config.js

export const readyConfig = {Container: 'plumbBox',anchor: ['Bottom', 'Top', 'Left', 'Right'],connector: 'Straight',endpoint: 'Blank',PaintStyle: { stroke: '#8b8c8d', strokeWidth: 2, outlineStroke: 'transparent', outlineWidth: 10 },Overlays: [['Arrow', { width: 10, length: 5, location: 0.5, direction: 1 }]],endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 }
}
export const sourceConfig = {filter: '.pointNode',filterExclude: false,allowLoopback: true,maxConnections: -1,Container: 'plumbBox',anchor: ['Bottom', 'Top', 'Left', 'Right'],connector: 'Straight',endpoint: 'Blank',PaintStyle: { stroke: '#8b8c8d', strokeWidth: 2, outlineStroke: 'transparent', outlineWidth: 10 },Overlays: [['Arrow', { width: 10, length: 5, location: 0.5, direction: 1 }]],endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 }
}
export const targetConfig = {filter: '.pointNode',filterExclude: false,allowLoopback: true,maxConnections: -1,Container: 'plumbBox',anchor: ['Bottom', 'Top', 'Left', 'Right'],connector: 'Straight',endpoint: 'Blank',PaintStyle: { stroke: '#8b8c8d', strokeWidth: 2, outlineStroke: 'transparent', outlineWidth: 10 },Overlays: [['Arrow', { width: 10, length: 5, location: 0.5, direction: 1 }]],endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 }
}

其实这些配置都大差不差,之所以分别罗列,是让大家搞明白,画布层级的配置,起点节点的配置以及终点节点的配置都是可以单独去配置,具体配置项可以看我之前的文档。

2.使用简易数据 data.js

export const leftMenuList = [{name: "节点1",id: "app1",},{name: "节点2",id: "app2",},{name: "节点3",id: "app3",},{name: "节点4",id: "app4",},
]

 这里的数据其实是左侧的数据

3.组件部分

<template><div class="flowBox"><div class="leftMenu"><h4 @click="checkInfo">左侧菜单</h4><draggable @start="start" @end="end" :sort="false"><divv-for="(item, index) in leftMenuList":key="item.id"@mousedown="(el) => downNode(el, item)"class="leftNode">{{ item.name }}</div><h4>操作提示</h4><hr /><p>左侧拖拽至右侧画布</p><p>右键节点是删除节点,左键线条是删除线条</p></draggable></div><div class="plumbBox" id="plumbBox"><divv-for="(item, index) in dataInfo":key="item.id":id="item.id":style="nodeStyle(item)"@click.right="deleteNode($event, item)">{{ item.name }}<div class="pointNode"></div></div></div></div>
</template>
<script>
import { jsPlumb } from "jsplumb";
import { readyConfig, sourceConfig, targetConfig } from "./config";
import { leftMenuList } from "./data";
import { cloneDeep } from "lodash";
import draggable from "vuedraggable";
export default {name: "flowChart",components: { draggable },data() {return {//节点列表leftMenuList: leftMenuList,dataInfo: [],//plumb实例PlumbInit: null,//关系列表renderList: [],//画布信息(大小和位置)canvasInfo: null,//鼠标位置精准判定nodePositionDiff: null,//选中的左侧列表节点ativeNodeItem: null,//线条的信息deleteLineInfo: null,};},mounted() {this.jsPlumbInit();this.readyPlumbDataFun("once");},methods: {//初始化jsPlumbInit() {this.PlumbInit = jsPlumb.getInstance();this.PlumbInit.importDefaults(readyConfig);},//组织渲染用的数据readyPlumbDataFun(flag) {this.renderList = [];this.PlumbInit.deleteEveryConnection();this.PlumbInit.deleteEveryEndpoint();this.$nextTick(() => {this.dataInfo = cloneDeep(this.dataInfo);//根据数据创建关联信息(连线关系)this.dataInfo.forEach((item) => {if (item.to && item.to.length > 0) {item.to.forEach((item1) => {let nodeConfig = Object.assign({source: item.id,target: item1,},readyConfig);this.renderList.push(nodeConfig);});}this.makeNodeConfig(item);});this.readyPlumbNodeFun(flag);});},//配置节点的具体信息makeNodeConfig(item) {this.PlumbInit.setSourceEnabled(item.id, true);this.PlumbInit.setTargetEnabled(item.id, true);this.PlumbInit.makeSource(item.id, sourceConfig);this.PlumbInit.makeTarget(item.id, targetConfig);this.PlumbInit.setDraggable(item.id, true);this.PlumbInit.draggable(item.id, {containment: "parent",stop: function (el) {item.left = el.pos[0];item.top = el.pos[1];},});},//渲染页面关系readyPlumbNodeFun(flag) {this.PlumbInit.ready(() => {this.renderList.forEach((item) => {this.PlumbInit.connect(item);});if (flag !== "once") {return;}//连线事件this.PlumbInit.bind("connection", (info) => {const sourceNode = this.dataInfo.find((item) => item.id === info.sourceId);if (sourceNode.to.includes(info.targetId)) {return false;}console.log("调用了几次");sourceNode.to.push(info.targetId);// this.$nextTick(() => {//   this.readyPlumbDataFun()// })return true;});//点击线条this.PlumbInit.bind("click", (con) => {this.deleteLineInfo = {source: con.sourceId,target: con.targetId,};this.deleteLine(this.deleteLineInfo);});});},//plumbNode的样式nodeStyle(item) {return {position: "absolute",left: item.left + "px",top: item.top + "px",width: "200px",height: "40px",lineHeight: "40px",textAlign: "center",borderLeft: "2px solid blue",borderRadius: "4%",cursor: "pointer",boxShadow: "#eee 3px 3px 3px 3px",};},//拖动开始start() {},//拖动结束end(e) {this.refreshCanvas();// 判断位置this.judgePosition(this.ativeNodeItem,this.canvasInfo,e.originalEvent.x,e.originalEvent.y);},//添加节点addNode(positionInfo, nodeInfo) {if (this.dataInfo.find((item) => item.id === nodeInfo.id)) {this.$message.error("该节点已经存在");return;}this.dataInfo.push({name: nodeInfo.name,id: nodeInfo.id,left: positionInfo.left,top: positionInfo.top,to: [],});this.$nextTick(() => {this.readyPlumbDataFun();});},//删除节点deleteNode($event, nodeInfo) {$event.returnValue = false;let index = this.dataInfo.findIndex((item) => item.id === nodeInfo.id);this.dataInfo.splice(index, 1);this.readyPlumbDataFun();},//删除线deleteLine(deleteLineInfo) {let dataInfo = cloneDeep(this.dataInfo);let node = dataInfo.find((val) => val.id === deleteLineInfo.source);let index = node.to.findIndex((val) => val === deleteLineInfo.target);node.to.splice(index, 1);this.dataInfo = null;this.dataInfo = dataInfo;this.readyPlumbDataFun();},checkInfo() {console.log(this.dataInfo, "dataInfo");console.log(this.renderList, "渲染关系");},//获取画布信息refreshCanvas() {this.canvasInfo = document.querySelector("#plumbBox").getBoundingClientRect();},//判断节点拖拽位置judgePosition(dragNodeInfo, plumbBoxPositionInfo, x, y) {if (x - this.nodePositionDiff.leftDiff < plumbBoxPositionInfo.left ||x + 200 - this.nodePositionDiff.leftDiff > plumbBoxPositionInfo.right ||y - this.nodePositionDiff.topDiff < plumbBoxPositionInfo.top ||y + 40 - this.nodePositionDiff.topDiff > plumbBoxPositionInfo.bottom) {this.$message.error("节点不能拖拽至画布之外");} else {const positionInfo = {top: y - plumbBoxPositionInfo.top - this.nodePositionDiff.topDiff,left: x - plumbBoxPositionInfo.left - this.nodePositionDiff.leftDiff,};this.addNode(positionInfo, dragNodeInfo);}},//鼠标抬起时,距离判定downNode(el, nodeItem) {this.ativeNodeItem = nodeItem;const mousedownPositionInfo = { x: el.clientX, y: el.clientY };// 被拖拽节点初始的位置信息const moveBoxBeforePosition = {x: el.target.getBoundingClientRect().x,y: el.target.getBoundingClientRect().y,};this.nodePositionDiff = {leftDiff: mousedownPositionInfo.x - moveBoxBeforePosition.x,topDiff: mousedownPositionInfo.y - moveBoxBeforePosition.y,};console.log(this.nodePositionDiff, "位置判定");},},
};
</script>
<style scoped>
.flowBox {display: flex;height: 100%;
}.leftMenu {width: 300px;/* height: 100%; */border: 1px solid #1a1919;
}h4 {margin-top: 10px;margin-left: 110px;
}.leftNode {width: 200px;height: 40px;line-height: 40px;text-align: center;margin: 30px;border: dashed 1px #362c2c;cursor: move;
}.plumbBox {flex: 1;/* height: 100%; */position: relative;
}
.pointNode {border-radius: 50%;width: 10px;height: 10px;background: royalblue;position: absolute;bottom: -5px;left: 95px;
}
</style>

各个函数的注释都写好了,操作方法也在其中,最主要的是根据插件暴露的api去领悟其中的每一个方法。

效果

这篇关于vue2中使用jsplumb完成流程图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/471909

相关文章

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi