Vue3.0+bpmn.js+国际化(一)

目录

一、bpmn.js初始化

1、安装相关依赖

2、引入

  (1)在main.js文件引入css样式

  (2) 创建工作流页面,引入依赖

  (3) 创建div

  (4)初始化bpmn.js

二、bpmn.js 部分方法

1、保存

2、下载

三、国际化

1、翻译文件translationsGerman.js

2、封装国际化js

3、vue中引入国际化

四、Vue完整代码


一、bpmn.js初始化

1、安装相关依赖

 在package.json文件(最好不要使用最新版本的兼容会有点问题,使用下方给的版本)

 注:node版本比较新依赖参考这个【扩展】bpmn右侧属性面板(新版本)+ 国际化(二)

    "bpmn-js": "^7.3.1",
    "bpmn-js-bpmnlint": "^0.19.0",
    "bpmn-js-connectors-extension": "^0.4.6",
    "bpmn-js-properties-panel": "^0.37.2",
    "bpmn-js-token-simulation": "^0.27.0",
    "bpmnlint": "^7.8.0",
    "bpmnlint-utils": "^1.0.2",
    "camunda-bpmn-moddle": "^4.4.0",

2、引入

   (1)在main.js文件引入css样式

import 'bpmn-js/dist/assets/diagram-js.css'
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css'
import 'bpmn-js-properties-panel/dist/assets/bpmn-js-properties-panel.css'

  (2) 创建工作流页面,引入依赖

import BpmnModeler from 'bpmn-js/lib/Modeler'
import propertiesPanelModule from 'bpmn-js-properties-panel'
import propertiesProviderModule from 'bpmn-js-properties-panel/lib/provider/camunda'
import camundaModdleDescriptor from 'camunda-bpmn-moddle/resources/camunda'

  (3) 创建div

 <div>
    <div class="designer-container">
      <div class="button-box">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="primary" @click="handleUndo">撤销</el-button>
        <el-button type="success" @click="handleRedo">恢复</el-button>
        <el-button type="warning" @click="handleDownload">下载</el-button>
      </div>
      <div ref="container" class="containerBox" />
      <div id="js-properties-panel" class="panel" />
    </div>
  </div>

 (4)初始化bpmn.js

export default {
  setup() {
    const container = ref(null)
    let bpmnModeler = null
    onMounted(async() => {
      init()
    })
   
    const init = () => {
      const customTranslateModule = {
        translate: ['value', customTranslate]
      }
      bpmnModeler = new BpmnModeler({
        container: container.value,
        propertiesPanel: {
          parent: '#js-properties-panel'
        },
        // 右侧属性面板
        additionalModules: [
          propertiesPanelModule,
          propertiesProviderModule,
        ],
        moddleExtensions: {
          camunda: camundaModdleDescriptor
        }
      })
      bpmnModeler.createDiagram(() => {
        bpmnModeler.get('canvas').zoom('fit-viewport')
      })
    }
   
    return {
      container,
      init,
    }
  }

二、bpmn.js 部分方法

1、保存

 const save = () => {
      bpmnModeler.saveXML({ format: true }, (err, data) => {
        const processInfo = {}
        processInfo.xml = data.replace(/camunda/ig, 'activiti')
        processInfo.processId = document.getElementById('camunda-id').value
        processInfo.processName = document.getElementById('camunda-name').innerHTML
        console.log(processInfo, err)
      })
}

2、下载

const handleDownload = () => {
      bpmnModeler.saveXML({ format: true }, (err, data) => {
        const dataTrack = 'bpmn'
        const a = document.createElement('a')
        const name = `diagram.${dataTrack}`
        a.setAttribute(
          'href',
          `data:application/bpmn20-xml;charset=UTF-8,${encodeURIComponent(data)}`
        )
        a.setAttribute('target', '_blank')
        a.setAttribute('dataTrack', `diagram:download-${dataTrack}`)
        a.setAttribute('download', name)
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
        console.log(err)
      })
    }

三、国际化

 1、翻译文件translationsGerman.js

export default {
  // Labels
  'Activate the global connect tool': '激活全局连接工具',
  'Append {type}': '追加 {type}',
  'Append EndEvent': '追加 结束事件 ',
  'Append Task': '追加 任务',
  'Append Gateway': '追加 网关',
  'Append Intermediate/Boundary Event': '追加 中间/边界 事件',
  'Add Lane above': '在上面添加道',
  'Divide into two Lanes': '分割成两个道',
  'Divide into three Lanes': '分割成三个道',
  'Add Lane below': '在下面添加道',
  'Append compensation activity': '追加补偿活动',
  'Change type': '修改类型',
  'Connect using Association': '使用关联连接',
  'Connect using Sequence/MessageFlow or Association': '使用顺序/消息流或者关联连接',
  'Connect using DataInputAssociation': '使用数据输入关联连接',
  'Remove': '移除',
  'Activate the hand tool': '激活抓手工具',
  'Activate the lasso tool': '激活套索工具',
  'Activate the create/remove space tool': '激活创建/删除空间工具',
  'Create expanded SubProcess': '创建扩展子过程',
  'Create IntermediateThrowEvent/BoundaryEvent': '创建中间抛出事件/边界事件',
  'Create Pool/Participant': '创建池/参与者',
  'Parallel Multi Instance': '并行多重事件',
  'Sequential Multi Instance': '时序多重事件',
  'DataObjectReference': '数据对象参考',
  'DataStoreReference': '数据存储参考',
  'Loop': '循环',
  'Ad-hoc': '即席',
  'Create {type}': '创建 {type}',
  'Create Task': '创建任务',
  'Create StartEvent': '创建开始事件',
  'Create EndEvent': '创建结束事件',
  'Create Group': '创建组',
  'Task': '任务',
  'Send Task': '发送任务',
  'Receive Task': '接收任务',
  'User Task': '用户任务',
  'Manual Task': '手工任务',
  'Business Rule Task': '业务规则任务',
  'Service Task': '服务任务',
  'Script Task': '脚本任务',
  'Call Activity': '调用活动',
  'Sub Process (collapsed)': '子流程(折叠的)',
  'Sub Process (expanded)': '子流程(展开的)',
  'Start Event': '开始事件',
  'StartEvent': '开始事件',
  'Intermediate Throw Event': '中间事件',
  'End Event': '结束事件',
  'EndEvent': '结束事件',
  'Create Gateway': '创建网关',
  'GateWay': '网关',
  'Create Intermediate/Boundary Event': '创建中间/边界事件',
  'Message Start Event': '消息开始事件',
  'Timer Start Event': '定时开始事件',
  'Conditional Start Event': '条件开始事件',
  'Signal Start Event': '信号开始事件',
  'Error Start Event': '错误开始事件',
  'Escalation Start Event': '升级开始事件',
  'Compensation Start Event': '补偿开始事件',
  'Message Start Event (non-interrupting)': '消息开始事件(非中断)',
  'Timer Start Event (non-interrupting)': '定时开始事件(非中断)',
  'Conditional Start Event (non-interrupting)': '条件开始事件(非中断)',
  'Signal Start Event (non-interrupting)': '信号开始事件(非中断)',
  'Escalation Start Event (non-interrupting)': '升级开始事件(非中断)',
  'Message Intermediate Catch Event': '消息中间捕获事件',
  'Message Intermediate Throw Event': '消息中间抛出事件',
  'Timer Intermediate Catch Event': '定时中间捕获事件',
  'Escalation Intermediate Throw Event': '升级中间抛出事件',
  'Conditional Intermediate Catch Event': '条件中间捕获事件',
  'Link Intermediate Catch Event': '链接中间捕获事件',
  'Link Intermediate Throw Event': '链接中间抛出事件',
  'Compensation Intermediate Throw Event': '补偿中间抛出事件',
  'Signal Intermediate Catch Event': '信号中间捕获事件',
  'Signal Intermediate Throw Event': '信号中间抛出事件',
  'Message End Event': '消息结束事件',
  'Escalation End Event': '定时结束事件',
  'Error End Event': '错误结束事件',
  'Cancel End Event': '取消结束事件',
  'Compensation End Event': '补偿结束事件',
  'Signal End Event': '信号结束事件',
  'Terminate End Event': '终止结束事件',
  'Message Boundary Event': '消息边界事件',
  'Message Boundary Event (non-interrupting)': '消息边界事件(非中断)',
  'Timer Boundary Event': '定时边界事件',
  'Timer Boundary Event (non-interrupting)': '定时边界事件(非中断)',
  'Escalation Boundary Event': '升级边界事件',
  'Escalation Boundary Event (non-interrupting)': '升级边界事件(非中断)',
  'Conditional Boundary Event': '条件边界事件',
  'Conditional Boundary Event (non-interrupting)': '条件边界事件(非中断)',
  'Error Boundary Event': '错误边界事件',
  'Cancel Boundary Event': '取消边界事件',
  'Signal Boundary Event': '信号边界事件',
  'Signal Boundary Event (non-interrupting)': '信号边界事件(非中断)',
  'Compensation Boundary Event': '补偿边界事件',
  'Exclusive Gateway': '互斥网关',
  'Parallel Gateway': '并行网关',
  'Inclusive Gateway': '相容网关',
  'Complex Gateway': '复杂网关',
  'Event based Gateway': '事件网关',
  'Transaction': '转运',
  'Sub Process': '子流程',
  'Event Sub Process': '事件子流程',
  'Collapsed Pool': '折叠池',
  'Expanded Pool': '展开池',
  // Errors
  'no parent for {element} in {parent}': '在{parent}里,{element}没有父类',
  'no shape type specified': '没有指定的形状类型',
  'flow elements must be children of pools/participants': '流元素必须是池/参与者的子类',
  'out of bounds release': 'out of bounds release',
  'more than {count} child lanes': '子道大于{count} ',
  'element required': '元素不能为空',
  'diagram not part of bpmn:Definitions': '流程图不符合bpmn规范',
  'no diagram to display': '没有可展示的流程图',
  'no process or collaboration to display': '没有可展示的流程/协作',
  'element {element} referenced by {referenced}#{property} not yet drawn': '由{referenced}#{property}引用的{element}元素仍未绘制',
  'already rendered {element}': '{element} 已被渲染',
  'failed to import {element}': '导入{element}失败',
  'Available process variables, identified in the diagram.': '图中标识的可用过程变量。',
  'No variables found.': '找不到变量。',
  // 属性面板的参数
  'Id': '编号',
  'Name': '名称',
  'General': '常规',
  'Details': '详情',
  'Message Name': '消息名称',
  'Message': '消息',
  'Initiator': '创建者',
  'Asynchronous Continuations': '持续异步',
  'Asynchronous Before': '异步前',
  'Asynchronous After': '异步后',
  'Job Configuration': '工作配置',
  'Exclusive': '排除',
  'Job Priority': '工作优先级',
  'Retry Time Cycle': '重试时间周期',
  'Documentation': '文档',
  'Element Documentation': '元素文档',
  'History Configuration': '历史配置',
  'History Time To Live': '历史的生存时间',
  'Forms': '表单',
  'Form Key': '表单key',
  'Form Fields': '表单字段',
  'Business Key': '业务key',
  'Form Field': '表单字段',
  'ID': '编号',
  'Type': '类型',
  'Label': '名称',
  'Default Value': '默认值',
  'Validation': '校验',
  'Add Constraint': '添加约束',
  'Config': '配置',
  'Properties': '属性',
  'Add Property': '添加属性',
  'Value': '值',
  'Add': '添加',
  'Values': '值',
  'Add Value': '添加值',
  'Listeners': '监听器',
  'Execution Listener': '执行监听',
  'Event Type': '事件类型',
  'Listener Type': '监听器类型',
  'Java Class': 'Java类',
  'Expression': '表达式',
  'Must provide a value': '必须提供一个值',
  'Delegate Expression': '代理表达式',
  'Script': '脚本',
  'Script Format': '脚本格式',
  'Script Type': '脚本类型',
  'Inline Script': '内联脚本',
  'External Script': '外部脚本',
  'Resource': '资源',
  'Field Injection': '字段注入',
  'Extensions': '扩展',
  'Input/Output': '输入/输出',
  'Input Parameters': '输入参数',
  'Output Parameters': '输出参数',
  'Parameters': '参数',
  'Output Parameter': '输出参数',
  'Timer Definition Type': '定时器定义类型',
  'Timer Definition': '定时器定义',
  'Date': '日期',
  'Duration': '持续',
  'Cycle': '循环',
  'Signal': '信号',
  'Signal Name': '信号名称',
  'Escalation': '升级',
  'Error': '错误',
  'Link Name': '链接名称',
  'Condition': '条件名称',
  'Variable Name': '变量名称',
  'Variable Event': '变量事件',
  'Specify more than one variable change event as a comma separated list.': '多个变量事件以逗号隔开',
  'Wait for Completion': '等待完成',
  'Activity Ref': '活动参考',
  'Version Tag': '版本标签',
  'Executable': '可执行文件',
  'External Task Configuration': '扩展任务配置',
  'Task Priority': '任务优先级',
  'External': '外部',
  'Connector': '连接器',
  'Must configure Connector': '必须配置连接器',
  'Connector Id': '连接器编号',
  'Implementation': '实现方式',
  'Field Injections': '字段注入',
  'Fields': '字段',
  'Result Variable': '结果变量',
  'Topic': '主题',
  'Configure Connector': '配置连接器',
  'Input Parameter': '输入参数',
  'Assignee': '代理人',
  'Candidate Users': '候选用户',
  'Candidate Groups': '候选组',
  'Due Date': '到期时间',
  'Follow Up Date': '跟踪日期',
  'Priority': '优先级',
  'The follow up date as an EL expression (e.g. ${someDate} or an ISO date (e.g. 2015-06-26T09:54:00)': '跟踪日期必须符合EL表达式,如: ${someDate} ,或者一个ISO标准日期,如:2015-06-26T09:54:00',
  'The due date as an EL expression (e.g. ${someDate} or an ISO date (e.g. 2015-06-26T09:54:00)': '跟踪日期必须符合EL表达式,如: ${someDate} ,或者一个ISO标准日期,如:2015-06-26T09:54:00',
  'Variables': '变量',
  'Candidate Starter Configuration': '候选开始配置',
  'Task Listener': '任务监听器',
  'Candidate Starter Groups': '候选开始组',
  'Candidate Starter Users': '候选开始用户',
  'Tasklist Configuration': '任务列表配置',
  'Startable': '启动',
  'Specify more than one group as a comma separated list.': '指定多个组,用逗号分隔',
  'Specify more than one user as a comma separated list.': '指定多个用户,用逗号分隔',
  'This maps to the process definition key.': '这会映射为流程定义的键',
  'CallActivity Type': '调用活动类型',
  'Condition Type': '条件类型',
  'Create UserTask': '创建用户任务',
  'Create CallActivity': '创建调用活动',
  'Called Element': '调用元素',
  'Create DataObjectReference': '创建数据对象引用',
  'Create DataStoreReference': '创建数据存储引用',
  'Multi Instance': '多实例',
  'Loop Cardinality': '实例数量',
  'Collection': '任务参与人列表',
  'Element Variable': '元素变量',
  'Completion Condition': '完成条件'
}

2、封装国际化js

import translations from './translationsGerman'

export default function customTranslate(template, replacements) {
  replacements = replacements || {}

  // Translate
  template = translations[template] || template

  // Replace
  return template.replace(/{([^}]+)}/g, function(_, key) {
    return replacements[key] || '{' + key + '}'
  })
}

3、vue中引入国际化

import customTranslate from '@/utils/customTranslate'



//初始化中加入国际化
  const customTranslateModule = {
        translate: ['value', customTranslate]
      }
      bpmnModeler = new BpmnModeler({
        container: container.value,
        propertiesPanel: {
          parent: '#js-properties-panel'
        },
        // 右侧属性面板
        additionalModules: [
          propertiesPanelModule,
          propertiesProviderModule,
          // 国际化
          customTranslateModule
        ],
        moddleExtensions: {
          camunda: camundaModdleDescriptor
        }
      })
      bpmnModeler.createDiagram(() => {
        bpmnModeler.get('canvas').zoom('fit-viewport')
   })

四、Vue完整代码

<template>
  <div>
    <div class="designer-container">
      <div class="button-box">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="warning" @click="handleDownload">下载</el-button>
      </div>
      <div ref="container" class="containerBox" />
      <div id="js-properties-panel" class="panel" />
    </div>
  </div>
</template>

<script>
import { onMounted, ref, reactive } from 'vue'
import BpmnModeler from 'bpmn-js/lib/Modeler'
import propertiesPanelModule from 'bpmn-js-properties-panel'
import propertiesProviderModule from 'bpmn-js-properties-panel/lib/provider/camunda'
import camundaModdleDescriptor from 'camunda-bpmn-moddle/resources/camunda'
// BPMN国际化
import customTranslate from '@/utils/customTranslate'
export default {
  setup() {
    const container = ref(null)
    let bpmnModeler = null
    onMounted(async() => {
      init()
    })
    const init = () => {
      const customTranslateModule = {
        translate: ['value', customTranslate]
      }
      bpmnModeler = new BpmnModeler({
        container: container.value,
        propertiesPanel: {
          parent: '#js-properties-panel'
        },
        // 右侧属性面板
        additionalModules: [
          propertiesPanelModule,
          propertiesProviderModule,
          // 国际化
          customTranslateModule
        ],
        moddleExtensions: {
          camunda: camundaModdleDescriptor
        }
      })
      bpmnModeler.createDiagram(() => {
        bpmnModeler.get('canvas').zoom('fit-viewport')
      })
    }
    const save= () => {
      bpmnModeler.saveXML({ format: true }, (err, data) => {
        const processInfo = {}
        processInfo.xml = data.replace(/camunda/ig, 'activiti')
        processInfo.processId = document.getElementById('camunda-id').value
        processInfo.processName = document.getElementById('camunda-name').innerHTML
        console.log(processInfo, err)
      })
    }
    const handleDownload = () => {
      bpmnModeler.saveXML({ format: true }, (err, data) => {
        const dataTrack = 'bpmn'
        const a = document.createElement('a')
        const name = `diagram.${dataTrack}`
        a.setAttribute(
          'href',
          `data:application/bpmn20-xml;charset=UTF-8,${encodeURIComponent(data)}`
        )
        a.setAttribute('target', '_blank')
        a.setAttribute('dataTrack', `diagram:download-${dataTrack}`)
        a.setAttribute('download', name)
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
        console.log(err)
      })
    }

    return {
      container,
      save,
      init,
      handleDownload
    }
  }
}
</script>
<style lang="scss">
.designer-container {
  width: 100%;
  height: calc(100vh - 97px);
  background: #fff;
position: relative;
  .containerBox {
    height: 100%;
  }
}

.bjs-powered-by {
  display: none;
}

.button-box{
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  position: absolute;
}

.panel {
  width: 400px;
  position: absolute;
  top: 1px;
  right: 1px;
  height: 100%;
  overflow: auto;
}
</style>

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值