【bpmn.js 使用总结】七、指定 Palette 容器

11 篇文章 37 订阅

指定 Palette 容器

开始之前需要了解一下自定义 Palette

开始

  1. 去除默认工具栏
 // 去除默认工具栏
      const modules = BpmnModeler.prototype._modules
      const index = modules.findIndex(it => it.paletteProvider)
      modules.splice(index, 1)

      this.bpmnModeler = new BpmnModeler({..})
  1. 声明容器
<div ref="palette"></div>

<div class="canvas" ref="canvas"></div>
const canvas = this.$refs.canvas
const palette = this.$refs.palette
// 建模
this.bpmnModeler = new BpmnModeler({
  // 主要容器
  container: canvas,
  // 工具栏容器
  paletteContainer: palette

  // ...
})
  1. 让 bpmn 使用指定的容器

CustomPalette.js

// 注入需要的数据,其中有指定的容器
Palette.$inject = [
  'eventBus',
  'canvas',
  'elementFactory',
  'create',
  'config.paletteContainer', // canvas 容器
  'config.paletteEntries' // palette 容器
]

// 拿到容器
function Palette(
  // ...
  paletteContainer,
  paletteEntries
) {
  this._entries = paletteEntries
  this._paletteContainer = paletteContainer
  // ..
}

// 使用指定的容器
Palette.prototype._init = function() {
  // ...
  // var parentContainer = this._getParentContainer()
  // 获取传入的工具栏容器
  var container = (this._container = this._paletteContainer)
  // 未找到 使用默认
  if (!container) {
    container = this._container = domify(Palette.HTML_MARKUP)
  } else {
    // 为传入的工具栏容器 创建子元素
    addClasses(container, 'custom-palette')
    const entries = domQuery('.custom-palette-entries', container)
    const toggle = domQuery('.custom-palette-toggle', container)

    if (!entries) {
      container.appendChild(
        domify('<div class="custom-palette-entries"></div>')
      )
    }
    if (!toggle) {
      container.appendChild(domify('<div class="custom-palette-toggle"></div>'))
    }
  }
  // parentContainer.appendChild(container) // 这句会将 palette 加入 canvas
  // ...
}

Palette.HTML_MARKUP =
  '<div class="custom-palette">' +
  '<div class="custom-palette-entries"></div>' +
  '<div class="custom-palette-toggle"></div>' +
  '</div>'

其中容器的样式随便写,我修改成了 custom-palette 这个类名,需要自己去修改 css
这里我为了简单,直接找到 源码 css 直接全局替换 成我的类名修改后的 diagram-js.less

强烈建议拿官方源码直接改 Palette.js

以下是源码,是短短的两句,可以对比一下

Palette.prototype._init = function() {
  // ...

  var container = (this._container = domify(Palette.HTML_MARKUP))
  parentContainer.appendChild(container)

  // ...
}

最后

代码 CustomPalette.js

建议直接取官方代码修改,安全又保险!:Palette.js


目录:

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_pengliang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值