vue項(xiàng)目中使用bpmn-自定義platter的示例代碼
內(nèi)容概述
本系列“vue項(xiàng)目中使用bpmn-xxxx”分為七篇,均為自己使用過(guò)程中用到的實(shí)例,手工原創(chuàng),目前陸續(xù)更新中。主要包括vue項(xiàng)目中bpmn使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。如果轉(zhuǎn)載或通過(guò)爬蟲(chóng)直接爬的,格式特別丑,請(qǐng)來(lái)原創(chuàng)看:我是作者原文
前情提要
經(jīng)過(guò)前四篇的學(xué)習(xí),我們能夠?qū)崿F(xiàn)bpmn基本繪圖、預(yù)覽、為節(jié)點(diǎn)加事件加顏色等效果,這一篇我們來(lái)說(shuō),如何自定義左側(cè)工具欄(platter),首先看一下自定義前后效果圖對(duì)比:
我們本次要實(shí)現(xiàn)的目標(biāo):基于左側(cè)platter原有元素類(lèi)型,創(chuàng)建出一個(gè)新的自定義節(jié)點(diǎn)。暫且叫它“草莓蛋糕”節(jié)點(diǎn),類(lèi)型是 bpmn:Task,樣式我們自己找一個(gè)好看的小草莓蛋糕圖案。所以,開(kāi)動(dòng)吧~首先新建一個(gè)“customPalette”文件夾,里面放我們今天所有自定義的文件。
步驟1:建立platter類(lèi)函數(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í)沒(méi)有效果 dragstart: dragEventFactory('bpmn:Task') } } }; } } CustomPalette.$inject = [ 'create', 'elementFactory', 'palette' ];
此時(shí),我們已經(jīng)注冊(cè)好了一個(gè)名稱(chēng)為“create.cake”的節(jié)點(diǎn),拖動(dòng)它時(shí),畫(huà)布中會(huì)出現(xiàn)一個(gè)"bpmn:Task"類(lèi)型的節(jié)點(diǎn),此時(shí)需要將該文件導(dǎo)出并在頁(yè)面中引入,否則沒(méi)有效果。
步驟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)的背景圖寫(xiě)入。背景圖引入的話(huà),貌似只支持.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)后畫(huà)布中也顯示自定義圖標(biāo),所以我們進(jìn)行下一步:自定義渲染
步驟5:畫(huà)布渲染自定義節(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ì)有問(wèn)題。重頭戲是里面的drawShape函數(shù)。
步驟6:書(shū)寫(xiě)drawShape函數(shù)
我們?cè)贑ustomRenderer.js同級(jí)建立一個(gè)util文件,記錄自定義類(lèi)型節(jié)點(diǎn)的一些屬性。
import cake from './cake.png'; // 自定義元素的類(lèi)型,此時(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)在我們來(lái)書(shū)寫(xiě)drawShape函數(shù)
import { customElements, customConfig } from './util'; import { append as svgAppend, create as svgCreate } from 'tiny-svg'; ... drawShape(parentNode, element) { const type = element.type; // 獲取到類(lèi)型 // 所有節(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)的屬性名都不可以改,不要問(wèn)為什么,因?yàn)楦牧藞?bào)錯(cuò)。
步驟3中已經(jīng)將該index.js引入到了頁(yè)面中,此時(shí)無(wú)需再次引入,此時(shí)我們來(lái)看看效果。
后續(xù)
項(xiàng)目目錄:index.vue是畫(huà)布主頁(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)文章
nuxt框架中對(duì)vuex進(jìn)行模塊化設(shè)置的實(shí)現(xiàn)方法
這篇文章主要介紹了nuxt框架中對(duì)vuex進(jìn)行模塊化設(shè)置的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Vue中直接操作數(shù)組索引不奏效的問(wèn)題解讀
這篇文章主要介紹了Vue中直接操作數(shù)組索引不奏效的問(wèn)題解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue使用axios進(jìn)行g(shù)et請(qǐng)求拼接參數(shù)的2種方式詳解
axios中post請(qǐng)求都是要求攜帶參數(shù)進(jìn)行請(qǐng)求,這篇文章主要給大家介紹了關(guān)于Vue使用axios進(jìn)行g(shù)et請(qǐng)求拼接參數(shù)的2種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01vue使用screenfull插件實(shí)現(xiàn)全屏功能
這篇文章主要為大家詳細(xì)介紹了vue使用screenfull插件實(shí)現(xiàn)全屏功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09vue.js 初體驗(yàn)之Chrome 插件開(kāi)發(fā)實(shí)錄
這篇文章主要介紹了vue.js 初體驗(yàn)之Chrome 插件開(kāi)發(fā)實(shí)錄 ,需要的朋友可以參考下2017-05-05深入理解Vue父子組件生命周期執(zhí)行順序及鉤子函數(shù)
本文通過(guò)實(shí)例代碼給大家介紹了Vue父子組件生命周期執(zhí)行順序及鉤子函數(shù)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08Vue 實(shí)時(shí)監(jiān)聽(tīng)窗口變化 windowresize的兩種方法
這篇文章主要介紹了Vue 實(shí)時(shí)監(jiān)聽(tīng)窗口變化 windowresize的兩種方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11Element?UI/Plus中全局修改el-table默認(rèn)樣式的解決方案
element ui官方封裝好的el-table組件,好用是挺好用的,但不可避免的是默認(rèn)的樣式,下面這篇文章主要給大家介紹了關(guān)于Element?UI/Plus中全局修改el-table默認(rèn)樣式的解決方案,需要的朋友可以參考下2023-02-02