32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
|
|
|
||
|
|
import { connectPoints } from 'diagram-js/lib/layout/ManhattanLayout';
|
||
|
|
|
||
|
|
function ManhattanLayoutPlugin(eventBus, modeling) {
|
||
|
|
eventBus.on('connection.changed', (event) => {
|
||
|
|
var connection = event.element;
|
||
|
|
|
||
|
|
if (connection.source && connection.target) {
|
||
|
|
const x0 = connection.source.x + connection.source.width / 2;
|
||
|
|
const x1 = connection.target.x + connection.target.width / 2;
|
||
|
|
const y0 = connection.source.y + connection.source.height / 2;
|
||
|
|
const y1 = connection.target.y + connection.target.height / 2;
|
||
|
|
const x2 = (Math.abs(x0-x1) < 5) ? x0 : x1;
|
||
|
|
const y2 = (Math.abs(y0-y1) < 5) ? y0 : y1;
|
||
|
|
const waypoints = connectPoints(
|
||
|
|
{ x: x0, y: y0 },
|
||
|
|
{ x: x2, y: y2 });
|
||
|
|
|
||
|
|
const hasChanged = JSON.stringify(connection.waypoints) != JSON.stringify(waypoints);
|
||
|
|
|
||
|
|
if (hasChanged) {
|
||
|
|
modeling.updateWaypoints(connection, waypoints)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// export as module
|
||
|
|
export default {
|
||
|
|
__init__: [ 'manhattanLayoutPlugin' ],
|
||
|
|
manhattanLayoutPlugin: [ 'type', ManhattanLayoutPlugin ]
|
||
|
|
};
|