AutoPlaceBehavior.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { getNewShapePosition } from '../../auto-place/BpmnAutoPlaceUtil';
  2. import { getMid } from 'diagram-js/lib/layout/LayoutUtil';
  3. import { is } from '../../../util/ModelUtil';
  4. var HIGH_PRIORITY = 2000;
  5. export default function AutoPlaceBehavior(eventBus, gridSnapping) {
  6. eventBus.on('autoPlace', HIGH_PRIORITY, function(context) {
  7. var source = context.source,
  8. sourceMid = getMid(source),
  9. shape = context.shape;
  10. var position = getNewShapePosition(source, shape);
  11. [ 'x', 'y' ].forEach(function(axis) {
  12. var options = {};
  13. // do not snap if x/y equal
  14. if (position[ axis ] === sourceMid[ axis ]) {
  15. return;
  16. }
  17. if (position[ axis ] > sourceMid[ axis ]) {
  18. options.min = position[ axis ];
  19. } else {
  20. options.max = position[ axis ];
  21. }
  22. if (is(shape, 'bpmn:TextAnnotation')) {
  23. if (isHorizontal(axis)) {
  24. options.offset = -shape.width / 2;
  25. } else {
  26. options.offset = -shape.height / 2;
  27. }
  28. }
  29. position[ axis ] = gridSnapping.snapValue(position[ axis ], options);
  30. });
  31. // must be returned to be considered by auto place
  32. return position;
  33. });
  34. }
  35. AutoPlaceBehavior.$inject = [
  36. 'eventBus',
  37. 'gridSnapping'
  38. ];
  39. // helpers //////////
  40. function isHorizontal(axis) {
  41. return axis === 'x';
  42. }