当前位置:   article > 正文

vue中使用Drawflow连线插件,并对端口进行命名_vue-drawflow

vue-drawflow

效果如图

场景:项目中需要拖拽模块并连线,有输入端和输出端之分,不同模块不同端口才能相连

文档相关

点击前往------->原项目git地址

点击前往------->提供端口既可输出又可输出方案

点击前往----->查阅发现原项目无法对端口命名

public文件夹下创建drawflow文件夹,将原项目的对应文件放在drawflow文件夹中,并且在public文件下的html中对对应的文件进行引用,这样对应的js就挂载在window上

<link rel="stylesheet" href="./Drawflow/dist/drawflow.min.css">

<link rel="stylesheet" href="./Drawflow/docs/beautiful.css">

<script src="./Drawflow/dist/drawflow.min.js"></script>

 

  1. <template>
  2. <div>
  3. <div class="wrapper drawflow-box">
  4. <div class="col">
  5. <el-collapse accordion>
  6. <div v-for="(item,index) in listData" :key="index">
  7. <el-collapse-item :title="item.name">
  8. <el-row :gutter="12">
  9. <el-col :span="12" v-for="(data,key) in item.children" :key="key">
  10. <div class="drawflow-drag" draggable="true" @dragstart="drag($event)" :data-node="data.type">
  11. <i class="el-icon-platform-eleme"></i>
  12. <span>{{data.name}}</span>
  13. </div>
  14. </el-col>
  15. </el-row>
  16. </el-collapse-item>
  17. </div>
  18. </el-collapse>
  19. </div>
  20. <div class="col-right">
  21. <div class="menu">
  22. <ul>
  23. <li @click="changeModule($event);" class="selected">模块单元</li>
  24. <!-- <li @click="editor.changeModule('Home'); changeModule($event);" class="selected">模块单元</li> -->
  25. <!-- <li @click="editor.changeModule('Other'); changeModule($event);">Other Module</li> -->
  26. </ul>
  27. </div>
  28. <div id="drawflow" @drop="drop($event)" @dragover="allowDrop($event)">
  29. <div class="btn-export" @click="handleExport(editor.export())">导出</div>
  30. <div class="btn-clear" @click="editor.clearModuleSelected()">清除</div>
  31. <!-- <div class="btn-lock">
  32. <i id="lock" class="fas fa-lock" onclick="editor.editor_mode='fixed'; changeMode('lock');"></i>
  33. <i id="unlock" class="fas fa-lock-open" onclick="editor.editor_mode='edit'; changeMode('unlock');" style="display:none;"></i>
  34. </div> -->
  35. <div class="bar-zoom">
  36. <i class="icon-suoxiao" title="缩小" @click="editor.zoom_out()"></i>
  37. <i class="icon-fuwei" title="复位" @click="editor.zoom_reset()"></i>
  38. <i class="icon-fangda" title="放大" @click="editor.zoom_in()"></i>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. // Drawflow 连线插件
  47. export default {
  48. data() {
  49. return {
  50. active: "",
  51. editor: null,
  52. mobile_item_selec: "",
  53. mobile_last_move: null,
  54. transform: "",
  55. listData: [
  56. {
  57. type: "in",
  58. name: "输入模块",
  59. children: [
  60. {
  61. name: "输入模块一",
  62. type: "test",
  63. },
  64. {
  65. name: "输入模块二",
  66. type: "test",
  67. },
  68. {
  69. name: "输入模块三",
  70. type: "test",
  71. },
  72. ],
  73. },
  74. {
  75. type: "out",
  76. name: "输出单元",
  77. children: [
  78. {
  79. name: "输出单元一",
  80. type: "dbclick",
  81. },
  82. {
  83. name: "输出单元二",
  84. type: "dbclick",
  85. },
  86. {
  87. name: "输出单元三",
  88. type: "dbclick",
  89. },
  90. ],
  91. },
  92. ],
  93. };
  94. },
  95. mounted() {
  96. this.$nextTick(() => {
  97. this.init();
  98. //挂载方法事件
  99. window.showpopup = this.showpopup;
  100. window.closemodal = this.closemodal;
  101. });
  102. },
  103. methods: {
  104. init() {
  105. var id = document.getElementById("drawflow");
  106. this.editor = new window.Drawflow(id);
  107. this.editor.reroute = true;
  108. this.editor.reroute_fix_curvature = true;
  109. this.editor.force_first_input = false;
  110. const dataToImport = {
  111. drawflow: {
  112. Home: {
  113. data: {},
  114. },
  115. },
  116. };
  117. this.editor.start();
  118. this.editor.import(dataToImport);
  119. this.editor.addNode(
  120. "test",
  121. 3,
  122. 3,
  123. 180,
  124. 200,
  125. "test",
  126. {
  127. name: "",
  128. inputs: {
  129. input_1: { connections: [], name: "t1" },
  130. input_2: { connections: [], name: "t2" },
  131. input_3: { connections: [], name: "t3" },
  132. },
  133. outputs: {
  134. output_1: { connections: [], name: "t4" },
  135. output_2: { connections: [], name: "t5" },
  136. output_3: { connections: [], name: "t6" },
  137. },
  138. },
  139. '<div><div class="content-box">Multiple!</div></div>'
  140. );
  141. },
  142. //下面的所有方法都是从原项目html中直接搬过来,有问题去原项目看
  143. showpopup(e) {
  144. e.target.closest(".drawflow-node").style.zIndex = "9999";
  145. e.target.children[0].style.display = "block";
  146. //document.getElementById("modalfix").style.display = "block";
  147. //e.target.children[0].style.transform = 'translate('+translate.x+'px, '+translate.y+'px)';
  148. this.transform = this.editor.precanvas.style.transform;
  149. this.editor.precanvas.style.transform = "";
  150. this.editor.precanvas.style.left = this.editor.canvas_x + "px";
  151. this.editor.precanvas.style.top = this.editor.canvas_y + "px";
  152. //e.target.children[0].style.top = -editor.canvas_y - editor.container.offsetTop +'px';
  153. //e.target.children[0].style.left = -editor.canvas_x - editor.container.offsetLeft +'px';
  154. this.editor.editor_mode = "fixed";
  155. },
  156. closemodal(e) {
  157. e.target.closest(".drawflow-node").style.zIndex = "2";
  158. e.target.parentElement.parentElement.style.display = "none";
  159. //document.getElementById("modalfix").style.display = "none";
  160. this.editor.precanvas.style.transform = this.transform;
  161. this.editor.precanvas.style.left = "0px";
  162. this.editor.precanvas.style.top = "0px";
  163. this.editor.editor_mode = "edit";
  164. },
  165. allowDrop(ev) {
  166. ev.preventDefault();
  167. },
  168. drag(ev) {
  169. if (ev.type === "touchstart") {
  170. this.mobile_item_selec = ev.target
  171. .closest(".drag-drawflow")
  172. .getAttribute("data-node");
  173. } else {
  174. ev.dataTransfer.setData("node", ev.target.getAttribute("data-node"));
  175. }
  176. },
  177. drop(ev) {
  178. if (ev.type === "touchend") {
  179. var parentdrawflow = document
  180. .elementFromPoint(
  181. mobile_last_move.touches[0].clientX,
  182. mobile_last_move.touches[0].clientY
  183. )
  184. .closest("#drawflow");
  185. if (parentdrawflow != null) {
  186. this.addNodeToDrawFlow(
  187. mobile_item_selec,
  188. mobile_last_move.touches[0].clientX,
  189. mobile_last_move.touches[0].clientY
  190. );
  191. }
  192. mobile_item_selec = "";
  193. } else {
  194. ev.preventDefault();
  195. var data = ev.dataTransfer.getData("node");
  196. this.addNodeToDrawFlow(data, ev.clientX, ev.clientY);
  197. }
  198. },
  199. addNodeToDrawFlow(name, pos_x, pos_y) {
  200. let editor = this.editor;
  201. if (editor.editor_mode === "fixed") {
  202. return false;
  203. }
  204. pos_x =
  205. pos_x *
  206. (editor.precanvas.clientWidth /
  207. (editor.precanvas.clientWidth * editor.zoom)) -
  208. editor.precanvas.getBoundingClientRect().x *
  209. (editor.precanvas.clientWidth /
  210. (editor.precanvas.clientWidth * editor.zoom));
  211. pos_y =
  212. pos_y *
  213. (editor.precanvas.clientHeight /
  214. (editor.precanvas.clientHeight * editor.zoom)) -
  215. editor.precanvas.getBoundingClientRect().y *
  216. (editor.precanvas.clientHeight /
  217. (editor.precanvas.clientHeight * editor.zoom));
  218. switch (name) {
  219. case "test":
  220. var test = `
  221. <div>
  222. <div class="box" ondblclick="showpopup(event)">
  223. 双击可以打开!
  224. <div class="modal" style="display:none">
  225. <div class="modal-content">
  226. <span class="close" onclick="closemodal(event)">&times;</span>
  227. 插入文本
  228. <input type="text">
  229. </div>
  230. </div>
  231. </div>
  232. </div>
  233. `;
  234. editor.addNode(
  235. "test",
  236. 3,
  237. 3,
  238. pos_x,
  239. pos_y,
  240. "test",
  241. {
  242. name: "test",
  243. inputs: {
  244. input_1: { connections: [], name: "t1" },
  245. input_2: { connections: [], name: "t2" },
  246. input_3: { connections: [], name: "t3" },
  247. },
  248. outputs: {
  249. output_1: { connections: [], name: "t4" },
  250. output_2: { connections: [], name: "t5" },
  251. output_3: { connections: [], name: "t6" },
  252. },
  253. },
  254. test
  255. );
  256. break;
  257. case "multiple":
  258. var multiple = `
  259. <div>
  260. <div class="box">
  261. Multiple!
  262. </div>
  263. </div>
  264. `;
  265. editor.addNode(
  266. "multiple",
  267. 3,
  268. 4,
  269. pos_x,
  270. pos_y,
  271. "multiple",
  272. {},
  273. multiple
  274. );
  275. break;
  276. case "dbclick":
  277. var dbclick = `
  278. <div>
  279. <div class="title-box"><i class="fas fa-mouse"></i> Db Click</div>
  280. <div class="box dbclickbox" ondblclick="showpopup(event)">
  281. Db Click here
  282. <div class="modal" style="display:none">
  283. <div class="modal-content">
  284. <span class="close" onclick="closemodal(event)">&times;</span>
  285. 插入文本
  286. <input type="text" df-name>
  287. </div>
  288. </div>
  289. </div>
  290. </div>
  291. `;
  292. editor.addNode("dbclick", 3, 3, pos_x, pos_y, "dbclick", {}, dbclick);
  293. break;
  294. default:
  295. }
  296. },
  297. changeModule(event) {
  298. var all = document.querySelectorAll(".menu ul li");
  299. for (var i = 0; i < all.length; i++) {
  300. all[i].classList.remove("selected");
  301. }
  302. event.target.classList.add("selected");
  303. },
  304. handleExport(val) {
  305. console.log(val);
  306. },
  307. },
  308. };
  309. </script>
  310. <style>
  311. .drawflow .drawflow-node .input::before {
  312. //通过操作标签上的属性,动态获取对应的文字
  313. content: attr(data-before);
  314. position: absolute;
  315. width: 64px;
  316. text-align: right;
  317. top: 0;
  318. left: -72px;
  319. font-size: 12px;
  320. color: #67C23A;
  321. }
  322. .drawflow .drawflow-node .output::before {
  323. content: attr(data-before);
  324. position: absolute;
  325. text-align: left;
  326. width: 64px;
  327. top: 0;
  328. left: 20px;
  329. font-size: 12px;
  330. color: #F56C6C;
  331. }
  332. </style>
  333. <style lang="scss">
  334. .drawflow-box {
  335. /** 样式调整 */
  336. .col {
  337. background: #fff;
  338. padding: 0 20px;
  339. box-sizing: border-box;
  340. .el-row {
  341. margin-top: 10px;
  342. .el-col {
  343. .drawflow-drag {
  344. width: 120px;
  345. height: 64px;
  346. border: 1px solid #ccc;
  347. margin: 0 auto;
  348. cursor: move;
  349. user-select: none;
  350. display: flex;
  351. justify-content: center;
  352. align-items: center;
  353. }
  354. }
  355. }
  356. }
  357. .test .drawflow_content_node {
  358. height: 100%;
  359. }
  360. .content-box {
  361. padding: 20px;
  362. }
  363. }
  364. </style>

 

修改 Drawflow/dist 文件夹下的drawflow.min.js文件对应的addNode方法(拖拽组件时执行)

添加1,2两处代码后,

 如果自定义节点类型中带了name属性,对应的端口就会出现对应的名称,如果没有name就按原项目逻辑执行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/536710
推荐阅读
相关标签
  

闽ICP备14008679号