index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <!-- eslint-disable vue/no-mutating-props -->
  2. <template>
  3. <el-container
  4. v-loading="isView"
  5. class="fm2-container"
  6. :class="{ 'view-mode': isView }"
  7. >
  8. <el-header height="45px">
  9. <el-button-group style="margin: 5px">
  10. <el-button icon="el-icon-arrow-left" @click="handleUndo"
  11. >撤回</el-button
  12. >
  13. <el-button icon="el-icon-arrow-right" @click="handleRedo"
  14. >重做</el-button
  15. >
  16. <el-button icon="el-icon-circle-plus-outline" @click="handleZoom(0.2)"
  17. >放大</el-button
  18. >
  19. <el-button icon="el-icon-help" @click="handleZoom(0)">还原</el-button>
  20. <el-button icon="el-icon-remove-outline" @click="handleZoom(-0.2)"
  21. >缩小</el-button
  22. >
  23. <el-button icon="el-icon-upload2" @click="importXml">导入</el-button>
  24. <el-upload action="" :before-upload="openBpmn" style="display: none">
  25. <el-button ref="importBtn" size="mini" icon="el-icon-folder-opened" />
  26. </el-upload>
  27. <el-button icon="el-icon-download" @click="handleExportXmlAction"
  28. >XML</el-button
  29. >
  30. <el-button icon="el-icon-download" @click="handleExportSvgAction"
  31. >SVG
  32. </el-button>
  33. <el-button icon="el-icon-tickets" @click="handlePreviewXml"
  34. >预览</el-button
  35. >
  36. <el-button icon="el-icon-delete" @click="handleClear">清空</el-button>
  37. <el-button icon="el-icon-upload" @click="handleSave">保存 </el-button>
  38. </el-button-group>
  39. </el-header>
  40. <el-main class="fm2-main">
  41. <el-container class="center-container">
  42. <el-container class="containers">
  43. <div class="canvas" ref="canvas" />
  44. </el-container>
  45. <el-aside class="widget-config-container1">
  46. <bpmn-element ref="_bpmnElement" v-if="modeler" :modeler="modeler" />
  47. </el-aside>
  48. </el-container>
  49. </el-main>
  50. <el-dialog :visible.sync="xmlVisible" title="预览" fullscreen center>
  51. <vue-ace-editor
  52. v-model="process.xml"
  53. @init="editorInit"
  54. lang="xml"
  55. theme="chrome"
  56. width="100%"
  57. height="calc(100vh - 214px)"
  58. :options="{ wrap: true, readOnly: true }"
  59. >
  60. </vue-ace-editor>
  61. <span slot="footer">
  62. <el-button
  63. icon="el-icon-document"
  64. type="primary"
  65. v-clipboard:copy="process.xml"
  66. v-clipboard:success="onCopy"
  67. >复 制</el-button
  68. >
  69. <el-button icon="el-icon-close" @click="xmlVisible = false"
  70. >关闭</el-button
  71. >
  72. </span>
  73. </el-dialog>
  74. </el-container>
  75. </template>
  76. <script>
  77. // 汉化
  78. import translate from './translate/index';
  79. import Modeler from 'bpmn-js/lib/Modeler';
  80. import BpmnElement from './BpmnElement';
  81. // 引入flowable的节点文件
  82. import flowableModdle from './descriptors/flowable.json';
  83. import VueAceEditor from 'vue2-ace-editor';
  84. import newXml from './resources/newDiagram.js';
  85. import { Message } from 'element-ui';
  86. export default {
  87. name: 'VueBpmn',
  88. props: {
  89. isView: {
  90. type: Boolean,
  91. default: false
  92. },
  93. modelData: {
  94. type: Object,
  95. // eslint-disable-next-line vue/require-valid-default-prop
  96. default: {
  97. id: undefined,
  98. key: 'processId_1',
  99. name: 'processName_1',
  100. category: 'category_1',
  101. description: 'description_1'
  102. }
  103. }
  104. },
  105. components: { VueAceEditor, BpmnElement },
  106. data() {
  107. return {
  108. scale: 1, //流程图比例(用于放大缩小)
  109. modeler: null,
  110. process: {
  111. xml: '',
  112. svg: ''
  113. },
  114. xmlVisible: false,
  115. idTest: /^[a-z_][\w-.]*$/i,
  116. processCategory: [],
  117. taskCategory: []
  118. };
  119. },
  120. mounted() {
  121. this.modeler = new Modeler({
  122. container: this.$refs.canvas,
  123. additionalModules: [
  124. {
  125. translate: ['value', translate]
  126. }
  127. ],
  128. moddleExtensions: {
  129. flowable: flowableModdle
  130. }
  131. });
  132. // 新增流程定义
  133. this.createNewDiagram(this.modelData?.editor);
  134. },
  135. methods: {
  136. // init ace editor
  137. editorInit: function () {
  138. require('brace/ext/language_tools');
  139. require('brace/mode/xml');
  140. require('brace/theme/chrome');
  141. },
  142. getProcessElement() {
  143. return this.modeler.getDefinitions().rootElements[0];
  144. },
  145. getProcess() {
  146. const element = this.getProcessElement();
  147. return {
  148. id: element.id,
  149. name: element.name,
  150. category: element.$parent.targetNamespace
  151. };
  152. },
  153. // 初始化流程图
  154. createNewDiagram(xml) {
  155. if (!xml) {
  156. // 初始化XML文本
  157. this.process.xml = newXml(
  158. this.modelData.key,
  159. this.modelData.name,
  160. this.modelData.category,
  161. this.modelData.description
  162. );
  163. } else {
  164. this.process.xml = xml;
  165. }
  166. // 将字符串转换成图显示出来
  167. this.modeler.importXML(this.process.xml, (err) => {
  168. if (err) {
  169. console.error(err);
  170. } else {
  171. // this.adjustPalette()
  172. }
  173. });
  174. },
  175. // 当图发生改变的时候会调用这个函数,这个data就是图的xml
  176. setEncoded(type, data) {
  177. // 把xml转换为URI,下载要用到的
  178. const encodedData = encodeURIComponent(data);
  179. if (data) {
  180. if (type === 'xml') {
  181. // 获取到图的xml,保存就是把这个xml提交给后台
  182. this.process.xml = data;
  183. return {
  184. filename: this.process.name + '.bpmn20.xml',
  185. href: 'data:application/bpmn20-xml;charset=UTF-8,' + encodedData,
  186. data: data
  187. };
  188. }
  189. if (type === 'svg') {
  190. this.process.svg = data;
  191. return {
  192. filename: this.process.name + '.bpmn20.svg',
  193. href: 'data:application/text/xml;charset=UTF-8,' + encodedData,
  194. data: data
  195. };
  196. }
  197. }
  198. },
  199. // 导入
  200. importXml() {
  201. this.$refs.importBtn.$el.click();
  202. },
  203. // 导入文件选择完成
  204. openBpmn(file) {
  205. const reader = new FileReader();
  206. reader.readAsText(file, 'utf-8');
  207. reader.onload = () => {
  208. this.createNewDiagram(reader.result);
  209. };
  210. return false;
  211. },
  212. /**
  213. * 下载xml/svg
  214. * @param type 类型 svg / xml
  215. * @param data 数据
  216. * @param name 文件名称
  217. */
  218. download(type, data, name) {
  219. let dataTrack = '';
  220. const a = document.createElement('a');
  221. switch (type) {
  222. case 'xml':
  223. dataTrack = 'bpmn';
  224. break;
  225. case 'svg':
  226. dataTrack = 'svg';
  227. break;
  228. default:
  229. break;
  230. }
  231. name = name || `diagram.${dataTrack}`;
  232. a.setAttribute(
  233. 'href',
  234. `data:application/bpmn20-xml;charset=UTF-8,${encodeURIComponent(
  235. data
  236. )}`
  237. );
  238. a.setAttribute('target', '_blank');
  239. a.setAttribute('dataTrack', `diagram:download-${dataTrack}`);
  240. a.setAttribute('download', name);
  241. document.body.appendChild(a);
  242. a.click();
  243. URL.revokeObjectURL(a.href); // 释放URL 对象
  244. document.body.removeChild(a);
  245. },
  246. // 导出XML文件
  247. handleExportXmlAction() {
  248. const _this = this;
  249. this.modeler.saveXML({ format: true }, function (err, xml) {
  250. xml = _this.replaceLtAndGt(xml);
  251. _this.download('xml', xml, _this.getProcess().name + '.bpmn20.xml');
  252. });
  253. },
  254. // 导出SVG文件
  255. handleExportSvgAction() {
  256. const _this = this;
  257. this.modeler.saveSVG({ format: true }, function (err, svg) {
  258. _this.download('svg', svg, _this.getProcess().name + '.bpmn20.svg');
  259. });
  260. },
  261. // 预览
  262. handlePreviewXml() {
  263. this.modeler.saveXML({ format: true }, (err, xml) => {
  264. this.process.xml = this.replaceLtAndGt(xml);
  265. this.xmlVisible = true;
  266. });
  267. },
  268. // 清空
  269. handleClear() {
  270. this.createNewDiagram();
  271. },
  272. // 保存
  273. handleSave() {
  274. const _this = this;
  275. this.$refs._bpmnElement
  276. ?.validate()
  277. .then(() => {
  278. const processId = this.getProcess().id;
  279. if (!this.idTest.test(processId)) {
  280. Message.error('流程标识key格式不正确');
  281. return;
  282. }
  283. this.modeler.saveXML({ format: true }, (err, xml) => {
  284. xml = _this.replaceLtAndGt(xml);
  285. const process = _this.getProcess();
  286. _this.process.xml = xml;
  287. // eslint-disable-next-line vue/no-mutating-props
  288. _this.modelData.editor = xml;
  289. // eslint-disable-next-line vue/no-mutating-props
  290. _this.modelData.key = process.id;
  291. // eslint-disable-next-line vue/no-mutating-props
  292. _this.modelData.name = process.name;
  293. // eslint-disable-next-line vue/no-mutating-props
  294. _this.modelData.category = process.category;
  295. _this.$emit('save', _this.modelData);
  296. });
  297. })
  298. .catch((e) => console.error(e));
  299. },
  300. // 复制成功
  301. onCopy() {
  302. this.$message.success('内容复制成功');
  303. },
  304. // 前进
  305. handleRedo() {
  306. this.modeler.get('commandStack').redo();
  307. },
  308. // 后退
  309. handleUndo() {
  310. this.modeler.get('commandStack').undo();
  311. },
  312. // 流程图放大缩小
  313. handleZoom(radio) {
  314. const newScale = !radio
  315. ? 1.0 // 不输入radio则还原
  316. : this.scale + radio <= 0.2 // 最小缩小倍数
  317. ? 0.2
  318. : this.scale + radio;
  319. this.modeler.get('canvas').zoom(newScale);
  320. this.scale = newScale;
  321. },
  322. replaceLtAndGt(xml) {
  323. xml = xml.replace(/&lt;/g, '<');
  324. xml = xml.replace(/&gt;/g, '>');
  325. return xml;
  326. }
  327. }
  328. };
  329. </script>
  330. <style lang="scss">
  331. /*左边工具栏以及编辑节点的样式*/
  332. @import '../../../node_modules/bpmn-js/dist/assets/diagram-js.css';
  333. @import '../../../node_modules/bpmn-js/dist/assets/bpmn-font/css/bpmn.css';
  334. @import '../../../node_modules/bpmn-js/dist/assets/bpmn-font/css/bpmn-codes.css';
  335. @import '../../../node_modules/bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css';
  336. $primary-color: #409eff;
  337. $primary-background-color: #ecf5ff;
  338. .view-mode {
  339. .el-header,
  340. .el-aside,
  341. .djs-palette {
  342. display: none;
  343. }
  344. .el-loading-mask {
  345. background-color: initial;
  346. }
  347. .el-loading-spinner {
  348. display: none;
  349. }
  350. }
  351. .fm2-container {
  352. background: #fff;
  353. height: calc(100vh - 162px);
  354. border: 1px solid #e0e0e0;
  355. .el-container {
  356. height: 100% !important;
  357. }
  358. & > .el-container {
  359. background: #fff;
  360. }
  361. .fm2-main {
  362. position: relative;
  363. border-top: solid 1px #e4e7ed;
  364. & > .el-container {
  365. position: absolute;
  366. top: 0;
  367. bottom: 0;
  368. left: 0;
  369. right: 0;
  370. }
  371. }
  372. footer {
  373. height: 30px;
  374. line-height: 30px;
  375. border-top: 1px solid #e0e0e0;
  376. font-size: 12px;
  377. text-align: right;
  378. color: $primary-color;
  379. background: #fafafa;
  380. a {
  381. color: $primary-color;
  382. }
  383. }
  384. }
  385. .center-container {
  386. border-left: 1px solid #e0e0e0;
  387. border-right: 1px solid #e0e0e0;
  388. }
  389. .widget-config-container1 {
  390. position: relative;
  391. border-left: solid 1px #e4e7ed;
  392. .el-form-item__label {
  393. font-size: 13px;
  394. }
  395. }
  396. .containers {
  397. background-color: #ffffff;
  398. width: 100%;
  399. height: 100%;
  400. .canvas {
  401. width: 100%;
  402. height: 100%;
  403. }
  404. .bjs-powered-by {
  405. display: none;
  406. }
  407. }
  408. </style>