feat(bpmn-snapping): snap inside the target instead of target center

This commit is contained in:
Maciej Barelkowski
2019-06-07 10:45:58 +02:00
committed by Nico Rehwaldt
parent eadc1fb159
commit d80076a034
3 changed files with 52 additions and 79 deletions

View File

@ -11,6 +11,10 @@ import { some } from 'min-dash';
var HIGHER_PRIORITY = 1250;
var TARGET_BOUNDS_PADDING = 20;
var AXES = [ 'x', 'y' ];
/**
* Snap during connect.
@ -41,6 +45,10 @@ export default function BpmnConnectSnapping(eventBus, rules) {
target: target
});
if (target && connectionAttrs) {
snapInsideTarget(event, target);
}
if (target && isAnyType(connectionAttrs, [
'bpmn:Association',
'bpmn:DataInputAssociation',
@ -51,8 +59,6 @@ export default function BpmnConnectSnapping(eventBus, rules) {
// snap source
context.sourcePosition = mid(source);
// snap target
snapToPosition(event, mid(target));
} else if (isType(connectionAttrs, 'bpmn:MessageFlow')) {
if (is(source, 'bpmn:Event')) {
@ -80,6 +86,23 @@ BpmnConnectSnapping.$inject = [
'rules'
];
function snapInsideTarget(event, target) {
AXES.forEach(function(axis) {
var matchingTargetDimension = getDimensionForAxis(axis, target),
newCoordinate;
if (event[axis] < target[axis] + TARGET_BOUNDS_PADDING) {
newCoordinate = target[axis] + TARGET_BOUNDS_PADDING;
} else if (event[axis] > target[axis] + matchingTargetDimension - TARGET_BOUNDS_PADDING) {
newCoordinate = target[axis] + matchingTargetDimension - TARGET_BOUNDS_PADDING;
}
if (newCoordinate) {
setSnapped(event, axis, newCoordinate);
}
});
}
// helpers //////////
function snapToPosition(event, position) {
@ -95,4 +118,8 @@ function isAnyType(attrs, types) {
return some(types, function(type) {
return isType(attrs, type);
});
}
}
function getDimensionForAxis(axis, element) {
return axis === 'x' ? element.width : element.height;
}