index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { getBusinessObject, getDi, is } from 'bpmn-js/lib/util/ModelUtil';
  2. import { forEach, isArray, isUndefined, omit, reduce } from 'min-dash';
  3. import { isLabel } from 'bpmn-js/lib/util/LabelUtil';
  4. import { buildBitUUID } from '../utils/uuidUtil';
  5. function copyProperties(source, target, properties) {
  6. if (!isArray(properties)) properties = [properties];
  7. forEach(properties, function (property: any) {
  8. if (!isUndefined(source[property])) target[property] = source[property];
  9. });
  10. }
  11. let LOW_PRIORITY = 750;
  12. /**
  13. * BPMN-specific copy & paste.
  14. *
  15. * @param {BpmnFactory} bpmnFactory
  16. * @param {EventBus} eventBus
  17. * @param {ModdleCopy} moddleCopy
  18. */
  19. export default function BpmnCopyPaste(bpmnFactory, eventBus, moddleCopy) {
  20. function copy(bo: any, clone?: any) {
  21. bo.id = `${bo.id.split('_')[0]}_${buildBitUUID()}`
  22. let targetBo = bpmnFactory.create(bo.$type);
  23. return moddleCopy.copyElement(bo, targetBo, null, clone);
  24. }
  25. eventBus.on('copyPaste.copyElement', LOW_PRIORITY, function (context) {
  26. let descriptor = context.descriptor,
  27. element = context.element,
  28. businessObject = getBusinessObject(element);
  29. if (isLabel(element)) return descriptor;
  30. let businessObjectCopy = (descriptor.businessObject = copy(businessObject, true));
  31. let diCopy = (descriptor.di = copy(getDi(element), true));
  32. diCopy.bpmnElement = businessObjectCopy;
  33. copyProperties(businessObjectCopy, descriptor, 'name');
  34. copyProperties(diCopy, descriptor, 'isExpanded');
  35. descriptor.wnType = element?.wnType || null;
  36. if (businessObject.default) {
  37. descriptor.default = businessObject.default.id;
  38. }
  39. });
  40. let referencesKey = '-bpmn-js-refs';
  41. function getReferences(cache) {
  42. return (cache[referencesKey] = cache[referencesKey] || {});
  43. }
  44. function setReferences(cache: any, references: any) {
  45. cache[referencesKey] = references;
  46. }
  47. function resolveReferences(descriptor, cache, references) {
  48. let businessObject = getBusinessObject(descriptor);
  49. if (descriptor.default) {
  50. references[descriptor.default] = {
  51. element: businessObject,
  52. property: 'default',
  53. };
  54. }
  55. if (descriptor.host) getBusinessObject(descriptor).attachedToRef = getBusinessObject(cache[descriptor.host]);
  56. return omit(
  57. references,
  58. reduce(
  59. references,
  60. function (array: any, reference: any, key: any) {
  61. let element = reference.element,
  62. property = reference.property;
  63. if (key === descriptor.id) {
  64. element.set(property, businessObject);
  65. array.push(descriptor.id);
  66. }
  67. return array;
  68. },
  69. [],
  70. ),
  71. );
  72. }
  73. eventBus.on('copyPaste.pasteElement', function (context) {
  74. let cache = context.cache,
  75. descriptor = context.descriptor,
  76. businessObject = descriptor.businessObject,
  77. di = descriptor.di;
  78. if (isLabel(descriptor)) {
  79. descriptor.businessObject = getBusinessObject(cache[descriptor.labelTarget]);
  80. descriptor.di = getDi(cache[descriptor.labelTarget]);
  81. return;
  82. }
  83. businessObject = descriptor.businessObject = copy(businessObject);
  84. di = descriptor.di = copy(di);
  85. di.bpmnElement = businessObject;
  86. copyProperties(descriptor, businessObject, ['isExpanded', 'name', 'wnType']); // 添加自定义的wnType
  87. descriptor.type = businessObject.$type;
  88. });
  89. eventBus.on('copyPaste.copyElement', LOW_PRIORITY, function (context) {
  90. let descriptor = context.descriptor,
  91. element = context.element;
  92. if (!is(element, 'bpmn:Participant')) return;
  93. let participantBo = getBusinessObject(element);
  94. if (participantBo.processRef) descriptor.processRef = copy(participantBo.processRef, true);
  95. });
  96. eventBus.on('copyPaste.pasteElement', function (context) {
  97. let descriptor = context.descriptor,
  98. processRef = descriptor.processRef;
  99. if (processRef) descriptor.processRef = copy(processRef);
  100. });
  101. eventBus.on('copyPaste.pasteElement', LOW_PRIORITY, function (context) {
  102. let cache = context.cache,
  103. descriptor = context.descriptor;
  104. setReferences(cache, resolveReferences(descriptor, cache, getReferences(cache)));
  105. });
  106. }
  107. BpmnCopyPaste.$inject = ['bpmnFactory', 'eventBus', 'moddleCopy'];