ProcessDesigner.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <template>
  2. <div class="my-process-designer">
  3. <div class="my-process-designer__header" style="display: table-row-group; z-index: 999">
  4. <slot name="control-header"></slot>
  5. <template v-if="!$slots['control-header']">
  6. <ElButtonGroup key="file-control">
  7. <XButton preIcon="ep:folder-opened" title="打开文件" @click="refFile.click()" />
  8. <el-tooltip effect="light" placement="bottom">
  9. <template #content>
  10. <div style="color: #409eff">
  11. <!-- <el-button link @click="downloadProcessAsXml()">下载为XML文件</el-button> -->
  12. <XTextButton title="下载为XML文件" @click="downloadProcessAsXml()" />
  13. <br />
  14. <!-- <el-button link @click="downloadProcessAsSvg()">下载为SVG文件</el-button> -->
  15. <XTextButton title="下载为SVG文件" @click="downloadProcessAsSvg()" />
  16. <br />
  17. <!-- <el-button link @click="downloadProcessAsBpmn()">下载为BPMN文件</el-button> -->
  18. <XTextButton title="下载为BPMN文件" @click="downloadProcessAsBpmn()" />
  19. </div>
  20. </template>
  21. <XButton title="下载文件" preIcon="ep:download" />
  22. </el-tooltip>
  23. <el-tooltip effect="light">
  24. <XButton preIcon="ep:view" title="浏览" />
  25. <template #content>
  26. <!-- <el-button link @click="previewProcessXML">预览XML</el-button> -->
  27. <XTextButton title="预览XML" @click="previewProcessXML" />
  28. <br />
  29. <!-- <el-button link @click="previewProcessJson">预览JSON</el-button> -->
  30. <XTextButton title="预览JSON" @click="previewProcessJson" />
  31. </template>
  32. </el-tooltip>
  33. <el-tooltip
  34. v-if="props.simulation"
  35. effect="light"
  36. :content="simulationStatus ? '退出模拟' : '开启模拟'"
  37. >
  38. <XButton preIcon="ep:cpu" title="模拟" @click="processSimulation" />
  39. </el-tooltip>
  40. </ElButtonGroup>
  41. <ElButtonGroup key="align-control">
  42. <el-tooltip effect="light" content="向左对齐">
  43. <!-- <el-button
  44. class="align align-left"
  45. icon="el-icon-s-data"
  46. @click="elementsAlign('left')"
  47. /> -->
  48. <XButton
  49. preIcon="fa:align-left"
  50. class="align align-bottom"
  51. @click="elementsAlign('left')"
  52. />
  53. </el-tooltip>
  54. <el-tooltip effect="light" content="向右对齐">
  55. <!-- <el-button
  56. class="align align-right"
  57. icon="el-icon-s-data"
  58. @click="elementsAlign('right')"
  59. /> -->
  60. <XButton
  61. preIcon="fa:align-left"
  62. class="align align-top"
  63. @click="elementsAlign('right')"
  64. />
  65. </el-tooltip>
  66. <el-tooltip effect="light" content="向上对齐">
  67. <!-- <el-button
  68. class="align align-top"
  69. icon="el-icon-s-data"
  70. @click="elementsAlign('top')"
  71. /> -->
  72. <XButton
  73. preIcon="fa:align-left"
  74. class="align align-left"
  75. @click="elementsAlign('top')"
  76. />
  77. </el-tooltip>
  78. <el-tooltip effect="light" content="向下对齐">
  79. <!-- <el-button
  80. class="align align-bottom"
  81. icon="el-icon-s-data"
  82. @click="elementsAlign('bottom')"
  83. /> -->
  84. <XButton
  85. preIcon="fa:align-left"
  86. class="align align-right"
  87. @click="elementsAlign('bottom')"
  88. />
  89. </el-tooltip>
  90. <el-tooltip effect="light" content="水平居中">
  91. <!-- <el-button
  92. class="align align-center"
  93. icon="el-icon-s-data"
  94. @click="elementsAlign('center')"
  95. /> -->
  96. <!-- class="align align-center" -->
  97. <XButton
  98. preIcon="fa:align-left"
  99. class="align align-center"
  100. @click="elementsAlign('center')"
  101. />
  102. </el-tooltip>
  103. <el-tooltip effect="light" content="垂直居中">
  104. <!-- <el-button
  105. class="align align-middle"
  106. icon="el-icon-s-data"
  107. @click="elementsAlign('middle')"
  108. /> -->
  109. <XButton
  110. preIcon="fa:align-left"
  111. class="align align-middle"
  112. @click="elementsAlign('middle')"
  113. />
  114. </el-tooltip>
  115. </ElButtonGroup>
  116. <ElButtonGroup key="scale-control">
  117. <el-tooltip effect="light" content="缩小视图">
  118. <!-- <el-button
  119. :disabled="defaultZoom < 0.2"
  120. icon="el-icon-zoom-out"
  121. @click="processZoomOut()"
  122. /> -->
  123. <XButton
  124. preIcon="ep:zoom-out"
  125. @click="processZoomOut()"
  126. :disabled="defaultZoom < 0.2"
  127. />
  128. </el-tooltip>
  129. <el-button>{{ Math.floor(defaultZoom * 10 * 10) + '%' }}</el-button>
  130. <el-tooltip effect="light" content="放大视图">
  131. <!-- <el-button
  132. :disabled="defaultZoom > 4"
  133. icon="el-icon-zoom-in"
  134. @click="processZoomIn()"
  135. /> -->
  136. <XButton preIcon="ep:zoom-in" @click="processZoomIn()" :disabled="defaultZoom > 4" />
  137. </el-tooltip>
  138. <el-tooltip effect="light" content="重置视图并居中">
  139. <!-- <el-button icon="el-icon-c-scale-to-original" @click="processReZoom()" /> -->
  140. <XButton preIcon="ep:scale-to-original" @click="processReZoom()" />
  141. </el-tooltip>
  142. </ElButtonGroup>
  143. <ElButtonGroup key="stack-control">
  144. <el-tooltip effect="light" content="撤销">
  145. <!-- <el-button :disabled="!revocable" icon="el-icon-refresh-left" @click="processUndo()" /> -->
  146. <XButton preIcon="ep:refresh-left" @click="processUndo()" :disabled="!revocable" />
  147. </el-tooltip>
  148. <el-tooltip effect="light" content="恢复">
  149. <!-- <el-button
  150. :disabled="!recoverable"
  151. icon="el-icon-refresh-right"
  152. @click="processRedo()"
  153. /> -->
  154. <XButton preIcon="ep:refresh-right" @click="processRedo()" :disabled="!recoverable" />
  155. </el-tooltip>
  156. <el-tooltip effect="light" content="重新绘制">
  157. <!-- <el-button icon="el-icon-refresh" @click="processRestart" /> -->
  158. <XButton preIcon="ep:refresh" @click="processRestart()" />
  159. </el-tooltip>
  160. </ElButtonGroup>
  161. <XButton
  162. preIcon="ep:plus"
  163. title="保存模型"
  164. @click="processSave"
  165. :type="props.headerButtonType"
  166. :disabled="simulationStatus"
  167. />
  168. </template>
  169. <!-- 用于打开本地文件-->
  170. <input
  171. type="file"
  172. id="files"
  173. ref="refFile"
  174. style="display: none"
  175. accept=".xml, .bpmn"
  176. @change="importLocalFile"
  177. />
  178. </div>
  179. <div class="my-process-designer__container">
  180. <div
  181. class="my-process-designer__canvas"
  182. ref="bpmnCanvas"
  183. id="bpmnCanvas"
  184. style="width: 1680px; height: 800px"
  185. ></div>
  186. <!-- <div id="js-properties-panel" class="panel"></div> -->
  187. <!-- <div class="my-process-designer__canvas" ref="bpmn-canvas"></div> -->
  188. </div>
  189. <Dialog
  190. title="预览"
  191. v-model="previewModelVisible"
  192. width="80%"
  193. :scroll="true"
  194. max-height="600px"
  195. >
  196. <!-- append-to-body -->
  197. <div v-highlight>
  198. <code class="hljs">
  199. <!-- 高亮代码块 -->
  200. {{ previewResult }}
  201. </code>
  202. </div>
  203. </Dialog>
  204. </div>
  205. </template>
  206. <script setup lang="ts" name="MyProcessDesigner">
  207. // import 'bpmn-js/dist/assets/diagram-js.css' // 左边工具栏以及编辑节点的样式
  208. // import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css'
  209. // import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-codes.css'
  210. // import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css'
  211. // import 'bpmn-js-properties-panel/dist/assets/bpmn-js-properties-panel.css' // 右侧框样式
  212. import { ElMessage, ElMessageBox } from 'element-plus'
  213. import BpmnModeler from 'bpmn-js/lib/Modeler'
  214. import DefaultEmptyXML from './plugins/defaultEmpty'
  215. // 翻译方法
  216. import customTranslate from './plugins/translate/customTranslate'
  217. import translationsCN from './plugins/translate/zh'
  218. // 模拟流转流程
  219. import tokenSimulation from 'bpmn-js-token-simulation'
  220. // 标签解析构建器
  221. // import bpmnPropertiesProvider from "bpmn-js-properties-panel/lib/provider/bpmn";
  222. // import propertiesPanelModule from 'bpmn-js-properties-panel'
  223. // import propertiesProviderModule from 'bpmn-js-properties-panel/lib/provider/camunda'
  224. // 标签解析 Moddle
  225. import camundaModdleDescriptor from './plugins/descriptor/camundaDescriptor.json'
  226. import activitiModdleDescriptor from './plugins/descriptor/activitiDescriptor.json'
  227. import flowableModdleDescriptor from './plugins/descriptor/flowableDescriptor.json'
  228. // 标签解析 Extension
  229. import camundaModdleExtension from './plugins/extension-moddle/camunda'
  230. import activitiModdleExtension from './plugins/extension-moddle/activiti'
  231. import flowableModdleExtension from './plugins/extension-moddle/flowable'
  232. // 引入json转换与高亮
  233. // import xml2js from 'xml-js'
  234. // import xml2js from 'fast-xml-parser'
  235. import { XmlNode, XmlNodeType, parseXmlString } from 'steady-xml'
  236. // 代码高亮插件
  237. // import hljs from 'highlight.js/lib/highlight'
  238. // import 'highlight.js/styles/github-gist.css'
  239. // hljs.registerLanguage('xml', 'highlight.js/lib/languages/xml')
  240. // hljs.registerLanguage('json', 'highlight.js/lib/languages/json')
  241. // const eventName = reactive({
  242. // name: ''
  243. // })
  244. const bpmnCanvas = ref()
  245. const refFile = ref()
  246. const emit = defineEmits([
  247. 'destroy',
  248. 'init-finished',
  249. 'save',
  250. 'commandStack-changed',
  251. 'input',
  252. 'change',
  253. 'canvas-viewbox-changed',
  254. // eventName.name
  255. 'element-click'
  256. ])
  257. const props = defineProps({
  258. value: String, // xml 字符串
  259. // valueWatch: true, // xml 字符串的 watch 状态
  260. processId: String, // 流程 key 标识
  261. processName: String, // 流程 name 名字
  262. formId: Number, // 流程 form 表单编号
  263. translations: {
  264. // 自定义的翻译文件
  265. type: Object,
  266. default: () => {}
  267. },
  268. additionalModel: [Object, Array], // 自定义model
  269. moddleExtension: {
  270. // 自定义moddle
  271. type: Object,
  272. default: () => {}
  273. },
  274. onlyCustomizeAddi: {
  275. type: Boolean,
  276. default: false
  277. },
  278. onlyCustomizeModdle: {
  279. type: Boolean,
  280. default: false
  281. },
  282. simulation: {
  283. type: Boolean,
  284. default: true
  285. },
  286. keyboard: {
  287. type: Boolean,
  288. default: true
  289. },
  290. prefix: {
  291. type: String,
  292. default: 'camunda'
  293. },
  294. events: {
  295. type: Array,
  296. default: () => ['element.click']
  297. },
  298. headerButtonSize: {
  299. type: String,
  300. default: 'small',
  301. validator: (value: string) => ['default', 'medium', 'small', 'mini'].indexOf(value) !== -1
  302. },
  303. headerButtonType: {
  304. type: String,
  305. default: 'primary',
  306. validator: (value: string) =>
  307. ['default', 'primary', 'success', 'warning', 'danger', 'info'].indexOf(value) !== -1
  308. }
  309. })
  310. provide('configGlobal', props)
  311. let bpmnModeler: any = null
  312. const defaultZoom = ref(1)
  313. const previewModelVisible = ref(false)
  314. const simulationStatus = ref(false)
  315. const previewResult = ref('')
  316. const previewType = ref('xml')
  317. const recoverable = ref(false)
  318. const revocable = ref(false)
  319. const additionalModules = computed(() => {
  320. console.log(props.additionalModel, 'additionalModel')
  321. const Modules: any[] = []
  322. // 仅保留用户自定义扩展模块
  323. if (props.onlyCustomizeAddi) {
  324. if (Object.prototype.toString.call(props.additionalModel) == '[object Array]') {
  325. return props.additionalModel || []
  326. }
  327. return [props.additionalModel]
  328. }
  329. // 插入用户自定义扩展模块
  330. if (Object.prototype.toString.call(props.additionalModel) == '[object Array]') {
  331. Modules.push(...(props.additionalModel as any[]))
  332. } else {
  333. props.additionalModel && Modules.push(props.additionalModel)
  334. }
  335. // 翻译模块
  336. const TranslateModule = {
  337. translate: ['value', customTranslate(props.translations || translationsCN)]
  338. }
  339. Modules.push(TranslateModule)
  340. // 模拟流转模块
  341. if (props.simulation) {
  342. Modules.push(tokenSimulation)
  343. }
  344. // 根据需要的流程类型设置扩展元素构建模块
  345. // if (this.prefix === "bpmn") {
  346. // Modules.push(bpmnModdleExtension);
  347. // }
  348. console.log(props.prefix, 'props.prefix ')
  349. if (props.prefix === 'camunda') {
  350. Modules.push(camundaModdleExtension)
  351. }
  352. if (props.prefix === 'flowable') {
  353. Modules.push(flowableModdleExtension)
  354. }
  355. if (props.prefix === 'activiti') {
  356. Modules.push(activitiModdleExtension)
  357. }
  358. return Modules
  359. })
  360. const moddleExtensions = computed(() => {
  361. console.log(props.onlyCustomizeModdle, 'props.onlyCustomizeModdle')
  362. console.log(props.moddleExtension, 'props.moddleExtension')
  363. console.log(props.prefix, 'props.prefix')
  364. const Extensions: any = {}
  365. // 仅使用用户自定义模块
  366. if (props.onlyCustomizeModdle) {
  367. return props.moddleExtension || null
  368. }
  369. // 插入用户自定义模块
  370. if (props.moddleExtension) {
  371. for (let key in props.moddleExtension) {
  372. Extensions[key] = props.moddleExtension[key]
  373. }
  374. }
  375. // 根据需要的 "流程类型" 设置 对应的解析文件
  376. if (props.prefix === 'activiti') {
  377. Extensions.activiti = activitiModdleDescriptor
  378. }
  379. if (props.prefix === 'flowable') {
  380. Extensions.flowable = flowableModdleDescriptor
  381. }
  382. if (props.prefix === 'camunda') {
  383. Extensions.camunda = camundaModdleDescriptor
  384. }
  385. return Extensions
  386. })
  387. console.log(additionalModules, 'additionalModules()')
  388. console.log(moddleExtensions, 'moddleExtensions()')
  389. const initBpmnModeler = () => {
  390. if (bpmnModeler) return
  391. let data = document.getElementById('bpmnCanvas')
  392. console.log(data, 'data')
  393. console.log(props.keyboard, 'props.keyboard')
  394. console.log(additionalModules, 'additionalModules()')
  395. console.log(moddleExtensions, 'moddleExtensions()')
  396. bpmnModeler = new BpmnModeler({
  397. // container: this.$refs['bpmn-canvas'],
  398. // container: getCurrentInstance(),
  399. // container: needClass,
  400. // container: bpmnCanvas.value,
  401. container: data,
  402. // width: '100%',
  403. // 添加控制板
  404. // propertiesPanel: {
  405. // parent: '#js-properties-panel'
  406. // },
  407. keyboard: props.keyboard ? { bindTo: document } : null,
  408. // additionalModules: additionalModules.value,
  409. additionalModules: additionalModules.value,
  410. moddleExtensions: moddleExtensions.value
  411. // additionalModules: [
  412. // additionalModules.value
  413. // propertiesPanelModule,
  414. // propertiesProviderModule
  415. // propertiesProviderModule
  416. // ],
  417. // moddleExtensions: { camunda: moddleExtensions.value }
  418. })
  419. // bpmnModeler.createDiagram()
  420. console.log(bpmnModeler, 'bpmnModeler111111')
  421. emit('init-finished', bpmnModeler)
  422. initModelListeners()
  423. }
  424. const initModelListeners = () => {
  425. const EventBus = bpmnModeler.get('eventBus')
  426. console.log(EventBus, 'EventBus')
  427. // 注册需要的监听事件, 将. 替换为 - , 避免解析异常
  428. props.events.forEach((event: any) => {
  429. EventBus.on(event, function (eventObj) {
  430. let eventName = event.replace(/\./g, '-')
  431. // eventName.name = eventName
  432. let element = eventObj ? eventObj.element : null
  433. console.log(eventName, 'eventName')
  434. console.log(element, 'element')
  435. emit('element-click', element, eventObj)
  436. // emit(eventName, element, eventObj)
  437. })
  438. })
  439. // 监听图形改变返回xml
  440. EventBus.on('commandStack.changed', async (event) => {
  441. try {
  442. recoverable.value = bpmnModeler.get('commandStack').canRedo()
  443. revocable.value = bpmnModeler.get('commandStack').canUndo()
  444. let { xml } = await bpmnModeler.saveXML({ format: true })
  445. emit('commandStack-changed', event)
  446. emit('input', xml)
  447. emit('change', xml)
  448. } catch (e: any) {
  449. console.error(`[Process Designer Warn]: ${e.message || e}`)
  450. }
  451. })
  452. // 监听视图缩放变化
  453. bpmnModeler.on('canvas.viewbox.changed', ({ viewbox }) => {
  454. emit('canvas-viewbox-changed', { viewbox })
  455. const { scale } = viewbox
  456. defaultZoom.value = Math.floor(scale * 100) / 100
  457. })
  458. }
  459. /* 创建新的流程图 */
  460. const createNewDiagram = async (xml) => {
  461. console.log(xml, 'xml')
  462. // 将字符串转换成图显示出来
  463. let newId = props.processId || `Process_${new Date().getTime()}`
  464. let newName = props.processName || `业务流程_${new Date().getTime()}`
  465. let xmlString = xml || DefaultEmptyXML(newId, newName, props.prefix)
  466. try {
  467. // console.log(xmlString, 'xmlString')
  468. // console.log(this.bpmnModeler.importXML);
  469. let { warnings } = await bpmnModeler.importXML(xmlString)
  470. console.log(warnings, 'warnings')
  471. if (warnings && warnings.length) {
  472. warnings.forEach((warn) => console.warn(warn))
  473. }
  474. } catch (e: any) {
  475. console.error(`[Process Designer Warn]: ${e.message || e}`)
  476. }
  477. }
  478. // 下载流程图到本地
  479. const downloadProcess = async (type) => {
  480. try {
  481. // 按需要类型创建文件并下载
  482. if (type === 'xml' || type === 'bpmn') {
  483. const { err, xml } = await bpmnModeler.saveXML()
  484. // 读取异常时抛出异常
  485. if (err) {
  486. console.error(`[Process Designer Warn ]: ${err.message || err}`)
  487. }
  488. let { href, filename } = setEncoded(type.toUpperCase(), xml)
  489. downloadFunc(href, filename)
  490. } else {
  491. const { err, svg } = await bpmnModeler.saveSVG()
  492. // 读取异常时抛出异常
  493. if (err) {
  494. return console.error(err)
  495. }
  496. let { href, filename } = setEncoded('SVG', svg)
  497. downloadFunc(href, filename)
  498. }
  499. } catch (e: any) {
  500. console.error(`[Process Designer Warn ]: ${e.message || e}`)
  501. }
  502. // 文件下载方法
  503. function downloadFunc(href, filename) {
  504. if (href && filename) {
  505. let a = document.createElement('a')
  506. a.download = filename //指定下载的文件名
  507. a.href = href // URL对象
  508. a.click() // 模拟点击
  509. URL.revokeObjectURL(a.href) // 释放URL 对象
  510. }
  511. }
  512. }
  513. // 根据所需类型进行转码并返回下载地址
  514. const setEncoded = (type, data) => {
  515. const filename = 'diagram'
  516. const encodedData = encodeURIComponent(data)
  517. return {
  518. filename: `${filename}.${type}`,
  519. href: `data:application/${
  520. type === 'svg' ? 'text/xml' : 'bpmn20-xml'
  521. };charset=UTF-8,${encodedData}`,
  522. data: data
  523. }
  524. }
  525. // 加载本地文件
  526. const importLocalFile = () => {
  527. const file = refFile.value.files[0]
  528. const reader = new FileReader()
  529. reader.readAsText(file)
  530. reader.onload = function () {
  531. let xmlStr = this.result
  532. createNewDiagram(xmlStr)
  533. }
  534. }
  535. /* ------------------------------------------------ refs methods ------------------------------------------------------ */
  536. const downloadProcessAsXml = () => {
  537. downloadProcess('xml')
  538. }
  539. const downloadProcessAsBpmn = () => {
  540. downloadProcess('bpmn')
  541. }
  542. const downloadProcessAsSvg = () => {
  543. downloadProcess('svg')
  544. }
  545. const processSimulation = () => {
  546. simulationStatus.value = !simulationStatus.value
  547. console.log(bpmnModeler.get('toggleMode', 'strict'), "bpmnModeler.get('toggleMode')")
  548. props.simulation && bpmnModeler.get('toggleMode', 'strict').toggleMode()
  549. }
  550. const processRedo = () => {
  551. bpmnModeler.get('commandStack').redo()
  552. }
  553. const processUndo = () => {
  554. bpmnModeler.get('commandStack').undo()
  555. }
  556. const processZoomIn = (zoomStep = 0.1) => {
  557. let newZoom = Math.floor(defaultZoom.value * 100 + zoomStep * 100) / 100
  558. if (newZoom > 4) {
  559. throw new Error('[Process Designer Warn ]: The zoom ratio cannot be greater than 4')
  560. }
  561. defaultZoom.value = newZoom
  562. bpmnModeler.get('canvas').zoom(defaultZoom.value)
  563. }
  564. const processZoomOut = (zoomStep = 0.1) => {
  565. let newZoom = Math.floor(defaultZoom.value * 100 - zoomStep * 100) / 100
  566. if (newZoom < 0.2) {
  567. throw new Error('[Process Designer Warn ]: The zoom ratio cannot be less than 0.2')
  568. }
  569. defaultZoom.value = newZoom
  570. bpmnModeler.get('canvas').zoom(defaultZoom.value)
  571. }
  572. // const processZoomTo = (newZoom = 1) => {
  573. // if (newZoom < 0.2) {
  574. // throw new Error('[Process Designer Warn ]: The zoom ratio cannot be less than 0.2')
  575. // }
  576. // if (newZoom > 4) {
  577. // throw new Error('[Process Designer Warn ]: The zoom ratio cannot be greater than 4')
  578. // }
  579. // defaultZoom = newZoom
  580. // bpmnModeler.get('canvas').zoom(newZoom)
  581. // }
  582. const processReZoom = () => {
  583. defaultZoom.value = 1
  584. bpmnModeler.get('canvas').zoom('fit-viewport', 'auto')
  585. }
  586. const processRestart = () => {
  587. recoverable.value = false
  588. revocable.value = false
  589. createNewDiagram(null)
  590. }
  591. const elementsAlign = (align) => {
  592. const Align = bpmnModeler.get('alignElements')
  593. const Selection = bpmnModeler.get('selection')
  594. const SelectedElements = Selection.get()
  595. if (!SelectedElements || SelectedElements.length <= 1) {
  596. ElMessage.warning('请按住 Shift 键选择多个元素对齐')
  597. // alert('请按住 Ctrl 键选择多个元素对齐
  598. return
  599. }
  600. ElMessageBox.confirm('自动对齐可能造成图形变形,是否继续?', '警告', {
  601. confirmButtonText: '确定',
  602. cancelButtonText: '取消',
  603. type: 'warning'
  604. }).then(() => {
  605. Align.trigger(SelectedElements, align)
  606. })
  607. }
  608. /*----------------------------- 方法结束 ---------------------------------*/
  609. const previewProcessXML = () => {
  610. console.log(bpmnModeler.saveXML, 'bpmnModeler')
  611. bpmnModeler.saveXML({ format: true }).then(({ xml }) => {
  612. // console.log(xml, 'xml111111')
  613. previewResult.value = xml
  614. previewType.value = 'xml'
  615. previewModelVisible.value = true
  616. })
  617. }
  618. const previewProcessJson = () => {
  619. bpmnModeler.saveXML({ format: true }).then(({ xml }) => {
  620. // console.log(xml, 'xml')
  621. // const rootNode = parseXmlString(xml)
  622. // console.log(rootNode, 'rootNoderootNode')
  623. const rootNodes = new XmlNode(XmlNodeType.Root, parseXmlString(xml))
  624. // console.log(rootNodes, 'rootNodesrootNodesrootNodes')
  625. // console.log(rootNodes.parent.toJsObject(), 'rootNodes.toJSON()')
  626. // console.log(JSON.stringify(rootNodes.parent.toJsObject()), 'rootNodes.toJSON()')
  627. // console.log(JSON.stringify(rootNodes.parent.toJSON()), 'rootNodes.toJSON()')
  628. // const parser = new xml2js.XMLParser()
  629. // let jObj = parser.parse(xml)
  630. // console.log(jObj, 'jObjjObjjObjjObjjObj')
  631. // const builder = new xml2js.XMLBuilder(xml)
  632. // const xmlContent = builder
  633. // console.log(xmlContent, 'xmlContent')
  634. // console.log(xml2js, 'convertconvertconvert')
  635. previewResult.value = rootNodes.parent?.toJSON() as unknown as string
  636. // previewResult.value = jObj
  637. // previewResult.value = convert.xml2json(xml, {explicitArray : false},{ spaces: 2 })
  638. previewType.value = 'json'
  639. previewModelVisible.value = true
  640. })
  641. }
  642. /* ------------------------------------------------ 芋道源码 methods ------------------------------------------------------ */
  643. const processSave = async () => {
  644. console.log(bpmnModeler, 'bpmnModelerbpmnModelerbpmnModelerbpmnModeler')
  645. const { err, xml } = await bpmnModeler.saveXML()
  646. console.log(err, 'errerrerrerrerr')
  647. console.log(xml, 'xmlxmlxmlxmlxml')
  648. // 读取异常时抛出异常
  649. if (err) {
  650. // this.$modal.msgError('保存模型失败,请重试!')
  651. alert('保存模型失败,请重试!')
  652. return
  653. }
  654. // 触发 save 事件
  655. emit('save', xml)
  656. }
  657. /** 高亮显示 */
  658. // const highlightedCode = (previewType, previewResult) => {
  659. // console.log(previewType, 'previewType, previewResult')
  660. // console.log(previewResult, 'previewType, previewResult')
  661. // console.log(hljs.highlight, 'hljs.highlight')
  662. // const result = hljs.highlight(previewType, previewResult.value || '', true)
  663. // return result.value || '&nbsp;'
  664. // }
  665. onBeforeMount(() => {
  666. console.log(props, 'propspropspropsprops')
  667. })
  668. onMounted(() => {
  669. initBpmnModeler()
  670. createNewDiagram(props.value)
  671. })
  672. onBeforeUnmount(() => {
  673. // this.$once('hook:beforeDestroy', () => {
  674. // })
  675. if (bpmnModeler) bpmnModeler.destroy()
  676. emit('destroy', bpmnModeler)
  677. bpmnModeler = null
  678. })
  679. </script>