vue項(xiàng)目中使用bpmn-自定義platter的示例代碼
內(nèi)容概述
本系列“vue項(xiàng)目中使用bpmn-xxxx”分為七篇,均為自己使用過程中用到的實(shí)例,手工原創(chuàng),目前陸續(xù)更新中。主要包括vue項(xiàng)目中bpmn使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。如果轉(zhuǎn)載或通過爬蟲直接爬的,格式特別丑,請(qǐng)來原創(chuàng)看:我是作者原文
前情提要
經(jīng)過前四篇的學(xué)習(xí),我們能夠?qū)崿F(xiàn)bpmn基本繪圖、預(yù)覽、為節(jié)點(diǎn)加事件加顏色等效果,這一篇我們來說,如何自定義左側(cè)工具欄(platter),首先看一下自定義前后效果圖對(duì)比:

我們本次要實(shí)現(xiàn)的目標(biāo):基于左側(cè)platter原有元素類型,創(chuàng)建出一個(gè)新的自定義節(jié)點(diǎn)。暫且叫它“草莓蛋糕”節(jié)點(diǎn),類型是 bpmn:Task,樣式我們自己找一個(gè)好看的小草莓蛋糕圖案。所以,開動(dòng)吧~首先新建一個(gè)“customPalette”文件夾,里面放我們今天所有自定義的文件。
步驟1:建立platter類函數(shù),命名為CustomPalette.js
export default class CustomPalette {
constructor(create, elementFactory, palette) {
this.create = create;
this.elementFactory = elementFactory;
palette.registerProvider(this);
}
// 這個(gè)是繪制palette的核心,函數(shù)名不要變
getPaletteEntries() {
const elementFactory = this.elementFactory;
const create = this.create;
function dragEventFactory(type) {
return function (event) {
const taskShape = elementFactory.create('shape', {
type: type
});
create.start(event, taskShape);
};
}
return {
'create.cake': {
title: '我是自定義節(jié)點(diǎn)-草莓蛋糕', // 鼠標(biāo)懸浮到節(jié)點(diǎn)上顯示的文字
className: 'icon-custom bpmn-icon-cake', // 樣式名
action: { // 操作該節(jié)點(diǎn)時(shí)會(huì)觸發(fā)的事件,此時(shí)只注冊(cè)一個(gè)拖動(dòng)事件即可,否則拖動(dòng)時(shí)沒有效果
dragstart: dragEventFactory('bpmn:Task')
}
}
};
}
}
CustomPalette.$inject = [
'create',
'elementFactory',
'palette'
];
此時(shí),我們已經(jīng)注冊(cè)好了一個(gè)名稱為“create.cake”的節(jié)點(diǎn),拖動(dòng)它時(shí),畫布中會(huì)出現(xiàn)一個(gè)"bpmn:Task"類型的節(jié)點(diǎn),此時(shí)需要將該文件導(dǎo)出并在頁(yè)面中引入,否則沒有效果。
步驟2:在CustomPalette.js同級(jí),建立一個(gè)index.js文件將其導(dǎo)出
import CustomPalette from './CustomPalette';
export default {
__init__: ['customPalette']
customPalette: ['type', CustomPalette],
};
步驟3:頁(yè)面中(index.vue)引入index.js
import customModule from './customPalette';
export default {
mounted() {
this.containerEl = document.getElementById('container');
this.bpmnModeler = new BpmnModeler({
additionalModules: [ customModule ]
});
}
步驟4:為節(jié)點(diǎn)定義樣式
新建一個(gè)customPalette.scss文件,在該文件同級(jí)放一張“cake.png”的圖片,作為節(jié)點(diǎn)的背景圖寫入。背景圖引入的話,貌似只支持.png格式,親測(cè):jpg報(bào)錯(cuò)
.bpmn-icon-cake {
background-image: url('./cake.png');
}
.icon-custom {
background-size: 65%;
background-repeat: no-repeat;
background-position: center center;
}
并且在main.js中引入,注意,一定要在main.js中全局引入,否則不生效。
import 'customPalette/customPalette.scss';
此時(shí),我們便可以在左側(cè)工具欄中看到自定義的“草莓蛋糕”節(jié)點(diǎn)了,但是此時(shí)拖動(dòng)該節(jié)點(diǎn),右側(cè)只會(huì)產(chǎn)生一個(gè)“bpmn:Task”的節(jié)點(diǎn),只有一個(gè)框框。

我們希望的是,拖動(dòng)后畫布中也顯示自定義圖標(biāo),所以我們進(jìn)行下一步:自定義渲染
步驟5:畫布渲染自定義節(jié)點(diǎn)
此時(shí)需要我們新建一個(gè) CustomRenderer.js文件,作用:自定義 renderer。因?yàn)槲覀兪窃赽pmn原有的元素“bpmn:Task”基礎(chǔ)上進(jìn)行修改,所以我們需要對(duì)將BaseRenderer進(jìn)行繼承。
import BaseRenderer from 'diagram-js/lib/draw/BaseRenderer'; // 引入默認(rèn)的renderer
const HIGH_PRIORITY = 1500; // 最高優(yōu)先級(jí)
export default class CustomRenderer extends BaseRenderer {
// 繼承BaseRenderer
constructor(eventBus, bpmnRenderer) {
super(eventBus, HIGH_PRIORITY);
this.bpmnRenderer = bpmnRenderer;
}
canRender(element) {
return !element.labelTarget;
}
drawShape(parentNode, element) {
const shape = this.bpmnRenderer.drawShape(parentNode, element);
return shape;
}
getShapePath(shape) {
return this.bpmnRenderer.getShapePath(shape);
}
}
CustomRenderer.$inject = ['eventBus', 'bpmnRenderer'];
此時(shí), CustomRenderer.js文件大概結(jié)構(gòu)完成了,注意:HIGH_PRIORITY變量和canRender不可以刪掉,否則會(huì)有問題。重頭戲是里面的drawShape函數(shù)。
步驟6:書寫drawShape函數(shù)
我們?cè)贑ustomRenderer.js同級(jí)建立一個(gè)util文件,記錄自定義類型節(jié)點(diǎn)的一些屬性。
import cake from './cake.png';
// 自定義元素的類型,此時(shí)我們只需要自定義一種節(jié)點(diǎn),所以數(shù)組只有一個(gè)元素
const customElements = ['bpmn:Task'];
const customConfig = {
// 自定義元素的配置
cake: {
url: cake,
attr: {x: 0, y: 0, width: 50, height: 50}
}
};
export {customElements, customConfig};
現(xiàn)在我們來書寫drawShape函數(shù)
import { customElements, customConfig } from './util';
import { append as svgAppend, create as svgCreate } from 'tiny-svg';
...
drawShape(parentNode, element) {
const type = element.type; // 獲取到類型
// 所有節(jié)點(diǎn)都會(huì)走這個(gè)函數(shù),所以此時(shí)只限制,需要自定義的才去自定義,否則仍顯示bpmn默認(rèn)圖標(biāo)
if (customElements.includes(type)) {
const {url, attr} = customConfig['cake'];
const customIcon = svgCreate('image', {...attr, href: url});
element['width'] = attr.width;
element['height'] = attr.height;
svgAppend(parentNode, customIcon);
return customIcon;
}
const shape = this.bpmnRenderer.drawShape(parentNode, element);
return shape;
}
步驟7: 導(dǎo)出并使用CustomRenderer
修改之前導(dǎo)出CustomPalette的index.js文件
import CustomPalette from './CustomPalette';
import CustomRenderer from './CustomRenderer';
export default {
__init__: ['customPalette', 'customRenderer'],
customPalette: ['type', CustomPalette],
customRenderer: ['type', CustomRenderer]
};
注意:此時(shí)__init__內(nèi)的屬性名都不可以改,不要問為什么,因?yàn)楦牧藞?bào)錯(cuò)。
步驟3中已經(jīng)將該index.js引入到了頁(yè)面中,此時(shí)無需再次引入,此時(shí)我們來看看效果。

后續(xù)
項(xiàng)目目錄:index.vue是畫布主頁(yè)面,同級(jí)customPalette文件夾下共有6個(gè)文件,結(jié)構(gòu)如下:

總結(jié)
到此這篇關(guān)于vue項(xiàng)目中使用bpmn-自定義platter的示例代碼的文章就介紹到這了,更多相關(guān)vue自定義platter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)單談?wù)刅ue3中的ref和reactive
vue3中實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的方法是就是使用ref和reactive,所謂響應(yīng)式就是界面和數(shù)據(jù)同步,能實(shí)現(xiàn)實(shí)時(shí)更新,下面這篇文章主要給大家介紹了關(guān)于Vue3中ref和reactive的相關(guān)資料,需要的朋友可以參考下2023-04-04
vue 框架下自定義滾動(dòng)條(easyscroll)實(shí)現(xiàn)方法
這篇文章主要介紹了vue 框架下自定義滾動(dòng)條(easyscroll)實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
vue 實(shí)現(xiàn)的樹形菜的實(shí)例代碼
這篇文章主要介紹了vue 實(shí)現(xiàn)的樹形菜單,需要的朋友可以參考下2018-03-03
vue拖拽組件 vuedraggable API options實(shí)現(xiàn)盒子之間相互拖拽排序
這篇文章主要介紹了vue拖拽組件 vuedraggable API options實(shí)現(xiàn)盒子之間相互拖拽排序克隆clone,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vue中如何使用embed標(biāo)簽PDF預(yù)覽
這篇文章主要介紹了vue中如何使用embed標(biāo)簽PDF預(yù)覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)4
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)的相關(guān)資料,介紹了Array的變異方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
element?el-upload文件上傳覆蓋第一個(gè)文件的實(shí)現(xiàn)
這篇文章主要介紹了element?el-upload文件上傳覆蓋第一個(gè)文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

