2019-05-02 07:55:11 +00:00
|
|
|
import inherits from 'inherits';
|
|
|
|
|
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
|
|
|
|
|
|
|
import { pointsAligned } from 'diagram-js/lib/util/Geometry';
|
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
import {
|
|
|
|
assign
|
|
|
|
} from 'min-dash';
|
|
|
|
|
2019-05-02 07:55:11 +00:00
|
|
|
var HIGH_PRIORITY = 3000;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Snaps connections with Manhattan layout.
|
|
|
|
*/
|
|
|
|
export default function LayoutConnectionBehavior(eventBus, gridSnapping, modeling) {
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
|
|
|
this._gridSnapping = gridSnapping;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.postExecuted([
|
|
|
|
'connection.create',
|
|
|
|
'connection.layout'
|
|
|
|
], HIGH_PRIORITY, function(event) {
|
|
|
|
var context = event.context,
|
|
|
|
connection = context.connection,
|
2019-05-29 09:37:31 +00:00
|
|
|
hints = context.hints || {},
|
2019-05-02 07:55:11 +00:00
|
|
|
waypoints = connection.waypoints;
|
2019-05-29 09:37:31 +00:00
|
|
|
|
2019-11-30 23:18:19 +00:00
|
|
|
if (hints.connectionStart || hints.connectionEnd || hints.createElementsBehavior === false) {
|
2019-05-29 09:37:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-05-02 07:55:11 +00:00
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
if (!hasMiddleSegments(waypoints)) {
|
|
|
|
return;
|
2019-05-02 07:55:11 +00:00
|
|
|
}
|
2019-06-17 08:23:16 +00:00
|
|
|
|
|
|
|
modeling.updateWaypoints(connection, self.snapMiddleSegments(waypoints));
|
2019-05-02 07:55:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
LayoutConnectionBehavior.$inject = [
|
|
|
|
'eventBus',
|
|
|
|
'gridSnapping',
|
|
|
|
'modeling'
|
|
|
|
];
|
|
|
|
|
|
|
|
inherits(LayoutConnectionBehavior, CommandInterceptor);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Snap middle segments of a given connection.
|
|
|
|
*
|
|
|
|
* @param {Array<Point>} waypoints
|
|
|
|
*
|
|
|
|
* @returns {Array<Point>}
|
|
|
|
*/
|
|
|
|
LayoutConnectionBehavior.prototype.snapMiddleSegments = function(waypoints) {
|
2019-06-17 08:23:16 +00:00
|
|
|
var gridSnapping = this._gridSnapping,
|
|
|
|
snapped;
|
2019-05-02 07:55:11 +00:00
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
waypoints = waypoints.slice();
|
2019-05-02 07:55:11 +00:00
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
for (var i = 1; i < waypoints.length - 2; i++) {
|
2019-05-02 07:55:11 +00:00
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
snapped = snapSegment(gridSnapping, waypoints[i], waypoints[i + 1]);
|
2019-05-02 07:55:11 +00:00
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
waypoints[i] = snapped[0];
|
|
|
|
waypoints[i + 1] = snapped[1];
|
|
|
|
}
|
2019-05-02 07:55:11 +00:00
|
|
|
|
|
|
|
return waypoints;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// helpers //////////
|
|
|
|
|
|
|
|
/**
|
2019-09-09 10:21:33 +00:00
|
|
|
* Check whether a connection has a middle segments.
|
2019-05-02 07:55:11 +00:00
|
|
|
*
|
|
|
|
* @param {Array} waypoints
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function hasMiddleSegments(waypoints) {
|
|
|
|
return waypoints.length > 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-09 10:21:33 +00:00
|
|
|
* Check whether an alignment is horizontal.
|
2019-05-02 07:55:11 +00:00
|
|
|
*
|
|
|
|
* @param {string} aligned
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function horizontallyAligned(aligned) {
|
|
|
|
return aligned === 'h';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-09 10:21:33 +00:00
|
|
|
* Check whether an alignment is vertical.
|
2019-05-02 07:55:11 +00:00
|
|
|
*
|
|
|
|
* @param {string} aligned
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function verticallyAligned(aligned) {
|
|
|
|
return aligned === 'v';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get middle segments from a given connection.
|
|
|
|
*
|
|
|
|
* @param {Array} waypoints
|
|
|
|
*
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2019-06-17 08:23:16 +00:00
|
|
|
function snapSegment(gridSnapping, segmentStart, segmentEnd) {
|
2019-05-02 07:55:11 +00:00
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
var aligned = pointsAligned(segmentStart, segmentEnd);
|
|
|
|
|
|
|
|
var snapped = {};
|
|
|
|
|
|
|
|
if (horizontallyAligned(aligned)) {
|
|
|
|
|
|
|
|
// snap horizontally
|
|
|
|
snapped.y = gridSnapping.snapValue(segmentStart.y);
|
2019-05-02 07:55:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 08:23:16 +00:00
|
|
|
if (verticallyAligned(aligned)) {
|
|
|
|
|
|
|
|
// snap vertically
|
|
|
|
snapped.x = gridSnapping.snapValue(segmentStart.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('x' in snapped || 'y' in snapped) {
|
|
|
|
segmentStart = assign({}, segmentStart, snapped);
|
|
|
|
segmentEnd = assign({}, segmentEnd, snapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ segmentStart, segmentEnd ];
|
|
|
|
}
|