Skip to content

Commit

Permalink
fix: asTRBL supported for flow elements
Browse files Browse the repository at this point in the history
Related to bpmn-js: #2239
  • Loading branch information
abdul99ahad committed Oct 15, 2024
1 parent 5144004 commit 9230d53
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
31 changes: 31 additions & 0 deletions lib/layout/LayoutUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { isConnection } from '../util/ModelUtil';
* @typedef {import('../util/Types').Point} Point
* @typedef {import('../util/Types').Rect} Rect
* @typedef {import('../util/Types').RectTRBL} RectTRBL
* @typedef {import('../util/Types').ConnectionTRBL} ConnectionTRBL

Check failure on line 24 in lib/layout/LayoutUtil.js

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Trailing spaces not allowed

Check failure on line 24 in lib/layout/LayoutUtil.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04)

Trailing spaces not allowed

Check failure on line 24 in lib/layout/LayoutUtil.js

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Trailing spaces not allowed

Check failure on line 24 in lib/layout/LayoutUtil.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04)

Trailing spaces not allowed
*/

/**
Expand Down Expand Up @@ -51,6 +52,36 @@ export function roundPoint(point) {
}


/**
* Convert the given connection bounds to a { top, left, bottom, right } descriptor.
*
* @param {Point|Connection} bounds
*
* @return {ConnectionTRBL}
*/
export function asConnectionTRBL(bounds) {
const waypoints = bounds.waypoints;
const points = waypoints.reduce((acc, point, index, array) => {
acc.x += point.x;
acc.y += point.y;

// Check if it's the last element
if (index === array.length - 1) {
acc.x /= array.length;
acc.y /= array.length;
}

return acc;
}, { x: 0, y: 0 });

return {
top: points.y,
right: points.x + (bounds.width || 0),
bottom: points.y,
left: points.x
};
}

/**
* Convert the given bounds to a { top, left, bottom, right } descriptor.
*
Expand Down
6 changes: 5 additions & 1 deletion lib/util/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ export type Dimensions = {

export type Rect = Dimensions & Point;

export type RectTRBL = {
export type TRBL = {
top: number;
right: number;
bottom: number;
left: number;
};

export type RectTRBL = TRBL

export type ConnectionTRBL = TRBL;

export type Axis = 'x' | 'y';

export type Direction = 'n' | 'w' | 's' | 'e' | 'nw' | 'ne' | 'sw' | 'se';
Expand Down
52 changes: 51 additions & 1 deletion test/spec/layout/LayoutUtilSpec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {
filterRedundantWaypoints,
getOrientation,
getMid
getMid,
asTRBL,
asConnectionTRBL
} from 'lib/layout/LayoutUtil';

import {
create,
} from 'lib/model';

function rect(x, y, width, height) {
return { x: x, y: y, width: width, height: height };
Expand Down Expand Up @@ -173,4 +178,49 @@ describe('layout/LayoutUtil', function() {
});
});

describe('#asTRBL', function() {

it('should return top, right, bottom and left when shape', function() {

// given
var shape = create('shape', {
x: 10,
y: 20,
width: 100,
height: 100
});

// then
expect(asTRBL(shape)).to.deep.equal({
top: 20,
right: 110,
bottom: 120,
left: 10
});
});

it('should return top, right, bottom and left when flow element', function() {

// given
var waypoints = [ { x: 10, y: 10 }, { x: 100, y: 100 } ];

// when
var connection = create('connection', {
waypoints: waypoints,
width: 20,
height: 5
});

// then
expect(asConnectionTRBL(connection)).to.deep.equal({
top: 55,
right: 75,
bottom: 55,
left: 55
});

});

});

});

0 comments on commit 9230d53

Please sign in to comment.