ProcessDesigner.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <template>
  2. <div class="my-process-designer">
  3. <div class="my-process-designer__header" style="z-index: 999; display: table-row-group">
  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 lang="ts" setup>
  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. defineOptions({ name: 'MyProcessDesigner' })
  245. const bpmnCanvas = ref()
  246. const refFile = ref()
  247. const emit = defineEmits([
  248. 'destroy',
  249. 'init-finished',
  250. 'save',
  251. 'commandStack-changed',
  252. 'input',
  253. 'change',
  254. 'canvas-viewbox-changed',
  255. // eventName.name
  256. 'element-click'
  257. ])
  258. const props = defineProps({
  259. value: String, // xml 字符串
  260. // valueWatch: true, // xml 字符串的 watch 状态
  261. processId: String, // 流程 key 标识
  262. processName: String, // 流程 name 名字
  263. formId: Number, // 流程 form 表单编号
  264. translations: {
  265. // 自定义的翻译文件
  266. type: Object,
  267. default: () => {}
  268. },
  269. additionalModel: [Object, Array], // 自定义model
  270. moddleExtension: {
  271. // 自定义moddle
  272. type: Object,
  273. default: () => {}
  274. },
  275. onlyCustomizeAddi: {
  276. type: Boolean,
  277. default: false
  278. },
  279. onlyCustomizeModdle: {
  280. type: Boolean,
  281. default: false
  282. },
  283. simulation: {
  284. type: Boolean,
  285. default: true
  286. },
  287. keyboard: {
  288. type: Boolean,
  289. default: true
  290. },
  291. prefix: {
  292. type: String,
  293. default: 'camunda'
  294. },
  295. events: {
  296. type: Array,
  297. default: () => ['element.click']
  298. },
  299. headerButtonSize: {
  300. type: String,
  301. default: 'small',
  302. validator: (value: string) => ['default', 'medium', 'small', 'mini'].indexOf(value) !== -1
  303. },
  304. headerButtonType: {
  305. type: String,
  306. default: 'primary',
  307. validator: (value: string) =>
  308. ['default', 'primary', 'success', 'warning', 'danger', 'info'].indexOf(value) !== -1
  309. }
  310. })
  311. provide('configGlobal', props)
  312. let bpmnModeler: any = null
  313. const defaultZoom = ref(1)
  314. const previewModelVisible = ref(false)
  315. const simulationStatus = ref(false)
  316. const previewResult = ref('')
  317. const previewType = ref('xml')
  318. const recoverable = ref(false)
  319. const revocable = ref(false)
  320. const additionalModules = computed(() => {
  321. console.log(props.additionalModel, 'additionalModel')
  322. const Modules: any[] = []
  323. // 仅保留用户自定义扩展模块
  324. if (props.onlyCustomizeAddi) {
  325. if (Object.prototype.toString.call(props.additionalModel) == '[object Array]') {
  326. return props.additionalModel || []
  327. }
  328. return [props.additionalModel]
  329. }
  330. // 插入用户自定义扩展模块
  331. if (Object.prototype.toString.call(props.additionalModel) == '[object Array]') {
  332. Modules.push(...(props.additionalModel as any[]))
  333. } else {
  334. props.additionalModel && Modules.push(props.additionalModel)
  335. }
  336. // 翻译模块
  337. const TranslateModule = {
  338. translate: ['value', customTranslate(props.translations || translationsCN)]
  339. }
  340. Modules.push(TranslateModule)
  341. // 模拟流转模块
  342. if (props.simulation) {
  343. Modules.push(tokenSimulation)
  344. }
  345. // 根据需要的流程类型设置扩展元素构建模块
  346. // if (this.prefix === "bpmn") {
  347. // Modules.push(bpmnModdleExtension);
  348. // }
  349. console.log(props.prefix, 'props.prefix ')
  350. if (props.prefix === 'camunda') {
  351. Modules.push(camundaModdleExtension)
  352. }
  353. if (props.prefix === 'flowable') {
  354. Modules.push(flowableModdleExtension)
  355. }
  356. if (props.prefix === 'activiti') {
  357. Modules.push(activitiModdleExtension)
  358. }
  359. return Modules
  360. })
  361. const moddleExtensions = computed(() => {
  362. console.log(props.onlyCustomizeModdle, 'props.onlyCustomizeModdle')
  363. console.log(props.moddleExtension, 'props.moddleExtension')
  364. console.log(props.prefix, 'props.prefix')
  365. const Extensions: any = {}
  366. // 仅使用用户自定义模块
  367. if (props.onlyCustomizeModdle) {
  368. return props.moddleExtension || null
  369. }
  370. // 插入用户自定义模块
  371. if (props.moddleExtension) {
  372. for (let key in props.moddleExtension) {
  373. Extensions[key] = props.moddleExtension[key]
  374. }
  375. }
  376. // 根据需要的 "流程类型" 设置 对应的解析文件
  377. if (props.prefix === 'activiti') {
  378. Extensions.activiti = activitiModdleDescriptor
  379. }
  380. if (props.prefix === 'flowable') {
  381. Extensions.flowable = flowableModdleDescriptor
  382. }
  383. if (props.prefix === 'camunda') {
  384. Extensions.camunda = camundaModdleDescriptor
  385. }
  386. return Extensions
  387. })
  388. console.log(additionalModules, 'additionalModules()')
  389. console.log(moddleExtensions, 'moddleExtensions()')
  390. const initBpmnModeler = () => {
  391. if (bpmnModeler) return
  392. let data = document.getElementById('bpmnCanvas')
  393. console.log(data, 'data')
  394. console.log(props.keyboard, 'props.keyboard')
  395. console.log(additionalModules, 'additionalModules()')
  396. console.log(moddleExtensions, 'moddleExtensions()')
  397. bpmnModeler = new BpmnModeler({
  398. // container: this.$refs['bpmn-canvas'],
  399. // container: getCurrentInstance(),
  400. // container: needClass,
  401. // container: bpmnCanvas.value,
  402. container: data,
  403. // width: '100%',
  404. // 添加控制板
  405. // propertiesPanel: {
  406. // parent: '#js-properties-panel'
  407. // },
  408. keyboard: props.keyboard ? { bindTo: document } : null,
  409. // additionalModules: additionalModules.value,
  410. additionalModules: additionalModules.value,
  411. moddleExtensions: moddleExtensions.value
  412. // additionalModules: [
  413. // additionalModules.value
  414. // propertiesPanelModule,
  415. // propertiesProviderModule
  416. // propertiesProviderModule
  417. // ],
  418. // moddleExtensions: { camunda: moddleExtensions.value }
  419. })
  420. // bpmnModeler.createDiagram()
  421. // console.log(bpmnModeler, 'bpmnModeler111111')
  422. emit('init-finished', bpmnModeler)
  423. initModelListeners()
  424. }
  425. const initModelListeners = () => {
  426. const EventBus = bpmnModeler.get('eventBus')
  427. console.log(EventBus, 'EventBus')
  428. // 注册需要的监听事件, 将. 替换为 - , 避免解析异常
  429. props.events.forEach((event: any) => {
  430. EventBus.on(event, function (eventObj) {
  431. let eventName = event.replace(/\./g, '-')
  432. // eventName.name = eventName
  433. let element = eventObj ? eventObj.element : null
  434. console.log(eventName, 'eventName')
  435. console.log(element, 'element')
  436. emit('element-click', element, eventObj)
  437. // emit(eventName, element, eventObj)
  438. })
  439. })
  440. // 监听图形改变返回xml
  441. EventBus.on('commandStack.changed', async (event) => {
  442. try {
  443. recoverable.value = bpmnModeler.get('commandStack').canRedo()
  444. revocable.value = bpmnModeler.get('commandStack').canUndo()
  445. let { xml } = await bpmnModeler.saveXML({ format: true })
  446. emit('commandStack-changed', event)
  447. emit('input', xml)
  448. emit('change', xml)
  449. } catch (e: any) {
  450. console.error(`[Process Designer Warn]: ${e.message || e}`)
  451. }
  452. })
  453. // 监听视图缩放变化
  454. bpmnModeler.on('canvas.viewbox.changed', ({ viewbox }) => {
  455. emit('canvas-viewbox-changed', { viewbox })
  456. const { scale } = viewbox
  457. defaultZoom.value = Math.floor(scale * 100) / 100
  458. })
  459. }
  460. /* 创建新的流程图 */
  461. const createNewDiagram = async (xml) => {
  462. console.log(xml, 'xml')
  463. // 将字符串转换成图显示出来
  464. let newId = props.processId || `Process_${new Date().getTime()}`
  465. let newName = props.processName || `业务流程_${new Date().getTime()}`
  466. let xmlString = xml || DefaultEmptyXML(newId, newName, props.prefix)
  467. try {
  468. // console.log(xmlString, 'xmlString')
  469. // console.log(this.bpmnModeler.importXML);
  470. let { warnings } = await bpmnModeler.importXML(xmlString)
  471. console.log(warnings, 'warnings')
  472. if (warnings && warnings.length) {
  473. warnings.forEach((warn) => console.warn(warn))
  474. }
  475. } catch (e: any) {
  476. console.error(`[Process Designer Warn]: ${e.message || e}`)
  477. }
  478. }
  479. // 下载流程图到本地
  480. const downloadProcess = async (type) => {
  481. try {
  482. // 按需要类型创建文件并下载
  483. if (type === 'xml' || type === 'bpmn') {
  484. const { err, xml } = await bpmnModeler.saveXML()
  485. // 读取异常时抛出异常
  486. if (err) {
  487. console.error(`[Process Designer Warn ]: ${err.message || err}`)
  488. }
  489. let { href, filename } = setEncoded(type.toUpperCase(), xml)
  490. downloadFunc(href, filename)
  491. } else {
  492. const { err, svg } = await bpmnModeler.saveSVG()
  493. // 读取异常时抛出异常
  494. if (err) {
  495. return console.error(err)
  496. }
  497. let { href, filename } = setEncoded('SVG', svg)
  498. downloadFunc(href, filename)
  499. }
  500. } catch (e: any) {
  501. console.error(`[Process Designer Warn ]: ${e.message || e}`)
  502. }
  503. // 文件下载方法
  504. function downloadFunc(href, filename) {
  505. if (href && filename) {
  506. let a = document.createElement('a')
  507. a.download = filename //指定下载的文件名
  508. a.href = href // URL对象
  509. a.click() // 模拟点击
  510. URL.revokeObjectURL(a.href) // 释放URL 对象
  511. }
  512. }
  513. }
  514. // 根据所需类型进行转码并返回下载地址
  515. const setEncoded = (type, data) => {
  516. const filename = 'diagram'
  517. const encodedData = encodeURIComponent(data)
  518. return {
  519. filename: `${filename}.${type}`,
  520. href: `data:application/${
  521. type === 'svg' ? 'text/xml' : 'bpmn20-xml'
  522. };charset=UTF-8,${encodedData}`,
  523. data: data
  524. }
  525. }
  526. // 加载本地文件
  527. const importLocalFile = () => {
  528. const file = refFile.value.files[0]
  529. const reader = new FileReader()
  530. reader.readAsText(file)
  531. reader.onload = function () {
  532. let xmlStr = this.result
  533. createNewDiagram(xmlStr)
  534. }
  535. }
  536. /* ------------------------------------------------ refs methods ------------------------------------------------------ */
  537. const downloadProcessAsXml = () => {
  538. downloadProcess('xml')
  539. }
  540. const downloadProcessAsBpmn = () => {
  541. downloadProcess('bpmn')
  542. }
  543. const downloadProcessAsSvg = () => {
  544. downloadProcess('svg')
  545. }
  546. const processSimulation = () => {
  547. simulationStatus.value = !simulationStatus.value
  548. console.log(bpmnModeler.get('toggleMode', 'strict'), "bpmnModeler.get('toggleMode')")
  549. props.simulation && bpmnModeler.get('toggleMode', 'strict').toggleMode()
  550. }
  551. const processRedo = () => {
  552. bpmnModeler.get('commandStack').redo()
  553. }
  554. const processUndo = () => {
  555. bpmnModeler.get('commandStack').undo()
  556. }
  557. const processZoomIn = (zoomStep = 0.1) => {
  558. let newZoom = Math.floor(defaultZoom.value * 100 + zoomStep * 100) / 100
  559. if (newZoom > 4) {
  560. throw new Error('[Process Designer Warn ]: The zoom ratio cannot be greater than 4')
  561. }
  562. defaultZoom.value = newZoom
  563. bpmnModeler.get('canvas').zoom(defaultZoom.value)
  564. }
  565. const processZoomOut = (zoomStep = 0.1) => {
  566. let newZoom = Math.floor(defaultZoom.value * 100 - zoomStep * 100) / 100
  567. if (newZoom < 0.2) {
  568. throw new Error('[Process Designer Warn ]: The zoom ratio cannot be less than 0.2')
  569. }
  570. defaultZoom.value = newZoom
  571. bpmnModeler.get('canvas').zoom(defaultZoom.value)
  572. }
  573. // const processZoomTo = (newZoom = 1) => {
  574. // if (newZoom < 0.2) {
  575. // throw new Error('[Process Designer Warn ]: The zoom ratio cannot be less than 0.2')
  576. // }
  577. // if (newZoom > 4) {
  578. // throw new Error('[Process Designer Warn ]: The zoom ratio cannot be greater than 4')
  579. // }
  580. // defaultZoom = newZoom
  581. // bpmnModeler.get('canvas').zoom(newZoom)
  582. // }
  583. const processReZoom = () => {
  584. defaultZoom.value = 1
  585. bpmnModeler.get('canvas').zoom('fit-viewport', 'auto')
  586. }
  587. const processRestart = () => {
  588. recoverable.value = false
  589. revocable.value = false
  590. createNewDiagram(null)
  591. }
  592. const elementsAlign = (align) => {
  593. const Align = bpmnModeler.get('alignElements')
  594. const Selection = bpmnModeler.get('selection')
  595. const SelectedElements = Selection.get()
  596. if (!SelectedElements || SelectedElements.length <= 1) {
  597. ElMessage.warning('请按住 Shift 键选择多个元素对齐')
  598. // alert('请按住 Ctrl 键选择多个元素对齐
  599. return
  600. }
  601. ElMessageBox.confirm('自动对齐可能造成图形变形,是否继续?', '警告', {
  602. confirmButtonText: '确定',
  603. cancelButtonText: '取消',
  604. type: 'warning'
  605. }).then(() => {
  606. Align.trigger(SelectedElements, align)
  607. })
  608. }
  609. /*----------------------------- 方法结束 ---------------------------------*/
  610. const previewProcessXML = () => {
  611. console.log(bpmnModeler.saveXML, 'bpmnModeler')
  612. bpmnModeler.saveXML({ format: true }).then(({ xml }) => {
  613. // console.log(xml, 'xml111111')
  614. previewResult.value = xml
  615. previewType.value = 'xml'
  616. previewModelVisible.value = true
  617. })
  618. }
  619. const previewProcessJson = () => {
  620. bpmnModeler.saveXML({ format: true }).then(({ xml }) => {
  621. // console.log(xml, 'xml')
  622. // const rootNode = parseXmlString(xml)
  623. // console.log(rootNode, 'rootNoderootNode')
  624. const rootNodes = new XmlNode(XmlNodeType.Root, parseXmlString(xml))
  625. // console.log(rootNodes, 'rootNodesrootNodesrootNodes')
  626. // console.log(rootNodes.parent.toJsObject(), 'rootNodes.toJSON()')
  627. // console.log(JSON.stringify(rootNodes.parent.toJsObject()), 'rootNodes.toJSON()')
  628. // console.log(JSON.stringify(rootNodes.parent.toJSON()), 'rootNodes.toJSON()')
  629. // const parser = new xml2js.XMLParser()
  630. // let jObj = parser.parse(xml)
  631. // console.log(jObj, 'jObjjObjjObjjObjjObj')
  632. // const builder = new xml2js.XMLBuilder(xml)
  633. // const xmlContent = builder
  634. // console.log(xmlContent, 'xmlContent')
  635. // console.log(xml2js, 'convertconvertconvert')
  636. previewResult.value = rootNodes.parent?.toJSON() as unknown as string
  637. // previewResult.value = jObj
  638. // previewResult.value = convert.xml2json(xml, {explicitArray : false},{ spaces: 2 })
  639. previewType.value = 'json'
  640. previewModelVisible.value = true
  641. })
  642. }
  643. /* ------------------------------------------------ 纽森源码 methods ------------------------------------------------------ */
  644. const processSave = async () => {
  645. // console.log(bpmnModeler, 'bpmnModelerbpmnModelerbpmnModelerbpmnModeler')
  646. const { err, xml } = await bpmnModeler.saveXML()
  647. // console.log(err, 'errerrerrerrerr')
  648. // console.log(xml, 'xmlxmlxmlxmlxml')
  649. // 读取异常时抛出异常
  650. if (err) {
  651. // this.$modal.msgError('保存模型失败,请重试!')
  652. alert('保存模型失败,请重试!')
  653. return
  654. }
  655. // 触发 save 事件
  656. emit('save', xml)
  657. }
  658. /** 高亮显示 */
  659. // const highlightedCode = (previewType, previewResult) => {
  660. // console.log(previewType, 'previewType, previewResult')
  661. // console.log(previewResult, 'previewType, previewResult')
  662. // console.log(hljs.highlight, 'hljs.highlight')
  663. // const result = hljs.highlight(previewType, previewResult.value || '', true)
  664. // return result.value || '&nbsp;'
  665. // }
  666. onBeforeMount(() => {
  667. console.log(props, 'propspropspropsprops')
  668. })
  669. onMounted(() => {
  670. initBpmnModeler()
  671. createNewDiagram(props.value)
  672. })
  673. onBeforeUnmount(() => {
  674. // this.$once('hook:beforeDestroy', () => {
  675. // })
  676. if (bpmnModeler) bpmnModeler.destroy()
  677. emit('destroy', bpmnModeler)
  678. bpmnModeler = null
  679. })
  680. </script>