刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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'];