Skip to content

Commit

Permalink
feat(resize): keep connection dockings if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac authored and philippfromme committed Apr 1, 2020
1 parent 459a0ec commit e73bc8b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
59 changes: 56 additions & 3 deletions lib/features/modeling/cmd/helper/AnchorsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,69 @@ import {
getNewAttachPoint
} from '../../../../util/AttachUtil';

import {
getOrientation
} from '../../../../layout/LayoutUtil';

import {
filter,
map
} from 'min-dash';


export function getResizedSourceAnchor(connection, shape, oldBounds) {

var waypoints = safeGetWaypoints(connection),
waypointsInsideNewBounds = getWaypointsInsideBounds(waypoints, shape),
oldAnchor = waypoints[0];

// new anchor is the last waypoint enclosed be resized source
if (waypointsInsideNewBounds.length) {
return waypointsInsideNewBounds[ waypointsInsideNewBounds.length - 1 ];
}

return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, shape);
}


export function getResizedTargetAnchor(connection, shape, oldBounds) {

var waypoints = safeGetWaypoints(connection),
waypointsInsideNewBounds = getWaypointsInsideBounds(waypoints, shape),
oldAnchor = waypoints[waypoints.length - 1];

// new anchor is the first waypoint enclosed be resized target
if (waypointsInsideNewBounds.length) {
return waypointsInsideNewBounds[ 0 ];
}

return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, shape);
}


export function getMovedSourceAnchor(connection, source, moveDelta) {
return getResizedSourceAnchor(connection, source, substractPosition(source, moveDelta));

var waypoints = safeGetWaypoints(connection),
oldBounds = subtract(source, moveDelta),
oldAnchor = waypoints[ 0 ];

return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, source);
}


export function getMovedTargetAnchor(connection, target, moveDelta) {
return getResizedTargetAnchor(connection, target, substractPosition(target, moveDelta));

var waypoints = safeGetWaypoints(connection),
oldBounds = subtract(target, moveDelta),
oldAnchor = waypoints[ waypoints.length - 1 ];

return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, target);
}


// helpers //////////////////////

function substractPosition(bounds, delta) {
function subtract(bounds, delta) {
return {
x: bounds.x - delta.x,
y: bounds.y - delta.y,
Expand All @@ -60,3 +91,25 @@ function safeGetWaypoints(connection) {

return waypoints;
}

function getWaypointsInsideBounds(waypoints, bounds) {
var originalWaypoints = map(waypoints, getOriginal);

return filter(originalWaypoints, function(waypoint) {
return isInsideBounds(waypoint, bounds);
});
}

/**
* Checks if point is inside bounds, incl. edges.
*
* @param {Point} point
* @param {Bounds} bounds
*/
function isInsideBounds(point, bounds) {
return getOrientation(bounds, point, 1) === 'intersect';
}

function getOriginal(point) {
return point.original || point;
}
20 changes: 17 additions & 3 deletions test/spec/features/modeling/ResizeShapeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ describe('features/modeling - resize shape', function() {
modeling.resizeShape(shape1, { x: 50, y: 100, width: 50, height: 100 });

// then
expect(connection.waypoints).to.deep.eql([
{ x: 100, y: 150, original: { x: 75, y: 150 } },
{ x: 200, y: 150, original: { x: 250, y: 150 } }
expect(connection).to.have.waypoints([
{ x: 100, y: 150 },
{ x: 200, y: 150 }
]);
}));

Expand Down Expand Up @@ -429,6 +429,20 @@ describe('features/modeling - resize shape', function() {
expect(connectionB.waypoints).to.eql(layoutedWaypoints);
}));


it('when connection cannot end within resized shape', inject(
function(modeling) {

// when
modeling.resizeShape(parentShape, { x: 400, y: 50, width: 350, height: 200 });

// then
expect(connectionA.waypoints).to.eql([
{ x: 150, y: 150 },
{ x: 419, y: 239 }
]);
}
));
});

});
Expand Down
2 changes: 1 addition & 1 deletion test/spec/features/replace/ReplaceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ describe('features/replace', function() {

expect(connection).to.have.waypoints([
{ x: 160, y: 160 },
{ x: 280, y: 160 }
{ x: 290, y: 160 }
]);
}));

Expand Down

0 comments on commit e73bc8b

Please sign in to comment.