RemoveElementBehavior.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import inherits from 'inherits-browser';
  2. import { is } from '../../../util/ModelUtil';
  3. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  4. import lineIntersect from './util/LineIntersect';
  5. /**
  6. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  7. * @typedef {import('../../rules/BpmnRules').default} BpmnRules
  8. * @typedef {import('../Modeling').default} Modeling
  9. */
  10. /**
  11. * @param {EventBus} eventBus
  12. * @param {BpmnRules} bpmnRules
  13. * @param {Modeling} modeling
  14. */
  15. export default function RemoveElementBehavior(eventBus, bpmnRules, modeling) {
  16. CommandInterceptor.call(this, eventBus);
  17. /**
  18. * Combine sequence flows when deleting an element
  19. * if there is one incoming and one outgoing
  20. * sequence flow
  21. */
  22. this.preExecute('shape.delete', function(e) {
  23. var shape = e.context.shape;
  24. // only handle [a] -> [shape] -> [b] patterns
  25. if (shape.incoming.length !== 1 || shape.outgoing.length !== 1) {
  26. return;
  27. }
  28. var inConnection = shape.incoming[0],
  29. outConnection = shape.outgoing[0];
  30. // only handle sequence flows
  31. if (!is(inConnection, 'bpmn:SequenceFlow') || !is(outConnection, 'bpmn:SequenceFlow')) {
  32. return;
  33. }
  34. if (bpmnRules.canConnect(inConnection.source, outConnection.target, inConnection)) {
  35. // compute new, combined waypoints
  36. var newWaypoints = getNewWaypoints(inConnection.waypoints, outConnection.waypoints);
  37. modeling.reconnectEnd(inConnection, outConnection.target, newWaypoints);
  38. }
  39. });
  40. }
  41. inherits(RemoveElementBehavior, CommandInterceptor);
  42. RemoveElementBehavior.$inject = [
  43. 'eventBus',
  44. 'bpmnRules',
  45. 'modeling'
  46. ];
  47. // helpers //////////////////////
  48. function getDocking(point) {
  49. return point.original || point;
  50. }
  51. function getNewWaypoints(inWaypoints, outWaypoints) {
  52. var intersection = lineIntersect(
  53. getDocking(inWaypoints[inWaypoints.length - 2]),
  54. getDocking(inWaypoints[inWaypoints.length - 1]),
  55. getDocking(outWaypoints[1]),
  56. getDocking(outWaypoints[0]));
  57. if (intersection) {
  58. return [].concat(
  59. inWaypoints.slice(0, inWaypoints.length - 1),
  60. [ intersection ],
  61. outWaypoints.slice(1));
  62. } else {
  63. return [
  64. getDocking(inWaypoints[0]),
  65. getDocking(outWaypoints[outWaypoints.length - 1])
  66. ];
  67. }
  68. }