import {
is
} from 'bpmn-js/lib/util/ModelUtil';
import {
getBusinessObject
} from 'bpmn-js/lib/util/ModelUtil';
import {
add as collectionAdd,
remove as collectionRemove
} from 'diagram-js/lib/util/Collections';
import {
getExtensionElementsList
} from 'bpmn-js/lib/properties-panel/util/ExtensionElementsUtil';
import {
isArray
} from 'min-dash';
// 示例:向元素添加自定义扩展属性
function addCustomExtension(element, bpmnFactory, extensionType) {
const businessObject = getBusinessObject(element);
return bpmnFactory.create(extensionType, {
values: []
}).then(extensionElements => {
return getExtensionElementsList(businessObject, extensionType, true).then(extensionElementsList => {
if (!extensionElementsList) {
// 如果没有扩展元素,则创建一个新的扩展元素集合
return bpmnFactory.create('bpmn:ExtensionElements', {
values: [extensionElements]
}).then(extensionElements => {
return bpmnFactory.create('bpmn:Documentation', {
text: '自定义扩展属性'
}).then(documentation => {
// 将文档添加到扩展元素集合中
collectionAdd(extensionElements.get('values'), documentation);
// 将扩展元素集合添加到业务对象中
return businessObject.set('extensionElements', extensionElements);
});
});
}
// 如果已经有扩展元素,直接添加新的扩展元素
collectionAdd(extensionElementsList, extensionElements);
return businessObject.set('extensionElements', businessObject.get('extensionElements'));
});
});
}
// 示例:从元素中移除自定义扩展属性
function removeCustomExtension(element, bpmnFactory, extensionType) {
const businessObject = getBusinessObject(element);
return getExtensionElementsList(businessObject, extensionType).then(extensionElementsList => {
if (isArray(extensionElementsList)) {
// 移除所有的自定义扩展属性
extensionElementsList.forEach(extensionElements => {
collectionRemove(extensionElements.get('values'), extensionElements => {
return is(extensionElements, 'bpmn:ExtensionElements') &&
!extensionElements.get('values').length;
});
});
// 移除扩展元素集合
businessObject.set('extensionElements', null);
}
});
}
// 使用示例
// 假设 element, bpmnFactory 已经定义且初始化
addCustomExtension(
评论已关闭