mirror of
https://github.com/bpmn-io/bpmn-js.git
synced 2025-08-26 04:32:44 +08:00
feat(bpmn-snapping): snap inside the target instead of target center
This commit is contained in:

committed by
Nico Rehwaldt

parent
eadc1fb159
commit
d80076a034
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user