| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { getBusinessObject, getDi, is } from 'bpmn-js/lib/util/ModelUtil';
- import { forEach, isArray, isUndefined, omit, reduce } from 'min-dash';
- import { isLabel } from 'bpmn-js/lib/util/LabelUtil';
- import { buildBitUUID } from '../utils/uuidUtil';
- function copyProperties(source, target, properties) {
- if (!isArray(properties)) properties = [properties];
- forEach(properties, function (property: any) {
- if (!isUndefined(source[property])) target[property] = source[property];
- });
- }
- let LOW_PRIORITY = 750;
- /**
- * BPMN-specific copy & paste.
- *
- * @param {BpmnFactory} bpmnFactory
- * @param {EventBus} eventBus
- * @param {ModdleCopy} moddleCopy
- */
- export default function BpmnCopyPaste(bpmnFactory, eventBus, moddleCopy) {
- function copy(bo: any, clone?: any) {
- bo.id = `${bo.id.split('_')[0]}_${buildBitUUID()}`
- let targetBo = bpmnFactory.create(bo.$type);
- return moddleCopy.copyElement(bo, targetBo, null, clone);
- }
- eventBus.on('copyPaste.copyElement', LOW_PRIORITY, function (context) {
- let descriptor = context.descriptor,
- element = context.element,
- businessObject = getBusinessObject(element);
- if (isLabel(element)) return descriptor;
- let businessObjectCopy = (descriptor.businessObject = copy(businessObject, true));
- let diCopy = (descriptor.di = copy(getDi(element), true));
- diCopy.bpmnElement = businessObjectCopy;
- copyProperties(businessObjectCopy, descriptor, 'name');
- copyProperties(diCopy, descriptor, 'isExpanded');
- descriptor.wnType = element?.wnType || null;
- if (businessObject.default) {
- descriptor.default = businessObject.default.id;
- }
- });
- let referencesKey = '-bpmn-js-refs';
- function getReferences(cache) {
- return (cache[referencesKey] = cache[referencesKey] || {});
- }
- function setReferences(cache: any, references: any) {
- cache[referencesKey] = references;
- }
- function resolveReferences(descriptor, cache, references) {
- let businessObject = getBusinessObject(descriptor);
- if (descriptor.default) {
- references[descriptor.default] = {
- element: businessObject,
- property: 'default',
- };
- }
- if (descriptor.host) getBusinessObject(descriptor).attachedToRef = getBusinessObject(cache[descriptor.host]);
- return omit(
- references,
- reduce(
- references,
- function (array: any, reference: any, key: any) {
- let element = reference.element,
- property = reference.property;
- if (key === descriptor.id) {
- element.set(property, businessObject);
- array.push(descriptor.id);
- }
- return array;
- },
- [],
- ),
- );
- }
- eventBus.on('copyPaste.pasteElement', function (context) {
- let cache = context.cache,
- descriptor = context.descriptor,
- businessObject = descriptor.businessObject,
- di = descriptor.di;
- if (isLabel(descriptor)) {
- descriptor.businessObject = getBusinessObject(cache[descriptor.labelTarget]);
- descriptor.di = getDi(cache[descriptor.labelTarget]);
- return;
- }
- businessObject = descriptor.businessObject = copy(businessObject);
- di = descriptor.di = copy(di);
- di.bpmnElement = businessObject;
- copyProperties(descriptor, businessObject, ['isExpanded', 'name', 'wnType']); // 添加自定义的wnType
- descriptor.type = businessObject.$type;
- });
- eventBus.on('copyPaste.copyElement', LOW_PRIORITY, function (context) {
- let descriptor = context.descriptor,
- element = context.element;
- if (!is(element, 'bpmn:Participant')) return;
- let participantBo = getBusinessObject(element);
- if (participantBo.processRef) descriptor.processRef = copy(participantBo.processRef, true);
- });
- eventBus.on('copyPaste.pasteElement', function (context) {
- let descriptor = context.descriptor,
- processRef = descriptor.processRef;
- if (processRef) descriptor.processRef = copy(processRef);
- });
- eventBus.on('copyPaste.pasteElement', LOW_PRIORITY, function (context) {
- let cache = context.cache,
- descriptor = context.descriptor;
- setReferences(cache, resolveReferences(descriptor, cache, getReferences(cache)));
- });
- }
- BpmnCopyPaste.$inject = ['bpmnFactory', 'eventBus', 'moddleCopy'];
|