Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AutoPlaceUtil Types #844

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions lib/features/auto-place/AutoPlaceUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var DEFAULT_MAX_DISTANCE = 250;
* @param {Shape} source
* @param {Shape} element
* @param {Point} position
* @param {(element: Element, position: Point, connectedAtPosition: Element) => Element} getNextPosition
* @param {(element: Element, position: Point, connectedAtPosition: Element) => Point} getNextPosition
*
* @return {Point}
*/
Expand Down Expand Up @@ -92,10 +92,14 @@ export function generateGetNextPosition(nextPositionDirection) {
}

/**
* Return target at given position, if defined.
* Return connected element at given position and within given bounds. Takes
* connected elements from host and attachers into account, too.
*
* This takes connected elements from host and attachers
* into account, too.
* @param {Shape} source
* @param {Point} position
* @param {Shape} element
*
* @return {Shape|undefined}
*/
export function getConnectedAtPosition(source, position, element) {

Expand All @@ -106,7 +110,7 @@ export function getConnectedAtPosition(source, position, element) {
height: element.height
};

var closure = getAutoPlaceClosure(source, element);
var closure = getAutoPlaceClosure(source);

return find(closure, function(target) {

Expand All @@ -131,7 +135,7 @@ export function getConnectedAtPosition(source, position, element) {
* @param {(connection: Connection) => boolean} [hints.filter]
* @param {(connection: Connection) => number} [hints.getWeight]
* @param {number} [hints.maxDistance]
* @param {string} [hints.reference]
* @param {'start'|'center'|'end'} [hints.reference]
*
* @return {number}
*/
Expand Down Expand Up @@ -255,13 +259,13 @@ export function getConnectedDistance(source, hints) {
}

/**
* Returns all connected elements around the given source.
* Returns all elements connected to given source.
*
* This includes:
*
* - connected elements
* - host connected elements
* - attachers connected elements
* - elements connected to source
* - elements connected to host if source is an attacher
* - elements connected to attachers if source is a host
*
* @param {Shape} source
*
Expand All @@ -284,6 +288,13 @@ function getAutoPlaceClosure(source) {
return allConnected;
}

/**
* Get all connected elements.
*
* @param {Shape} element
*
* @returns {Shape[]}
*/
function getConnected(element) {
return getTargets(element).concat(getSources(element));
}
Expand Down
29 changes: 29 additions & 0 deletions lib/features/auto-place/AutoPlaceUtil.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Diagram from '../../Diagram';

import ElementFactory from '../../core/ElementFactory';

import {
findFreePosition,
generateGetNextPosition,
getConnectedDistance
} from './AutoPlaceUtil';

const diagram = new Diagram();

const elementFactory = diagram.get<ElementFactory>('elementFactory');

const source = elementFactory.createShape(),
element = elementFactory.createShape();

const getNextPosition = generateGetNextPosition({ x: 100, y: 100 });

findFreePosition(source, element, { x: 100, y: 100 }, getNextPosition);

getConnectedDistance(source, {
defaultDistance: 100,
direction: 'right',
filter: (connection) => true,
getWeight: (connection) => 1,
maxDistance: 100,
reference: 'center'
});
3 changes: 2 additions & 1 deletion lib/layout/LayoutUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { isConnection } from '../util/ModelUtil';
* @typedef {import('../core/Types').ConnectionLike} Connection
*
* @typedef {import('../util/Types').DirectionTRBL} DirectionTRBL
* @typedef {import('../util/Types').Intersect} Intersect
* @typedef {import('../util/Types').Point} Point
* @typedef {import('../util/Types').Rect} Rect
* @typedef {import('../util/Types').RectTRBL} RectTRBL
Expand Down Expand Up @@ -186,7 +187,7 @@ export function getMid(element) {
* @param {Rect} reference
* @param {Point|number} padding
*
* @return {DirectionTRBL} the orientation; one of top, top-left, left, ..., bottom, right or intersect.
* @return {DirectionTRBL|Intersect} the orientation; one of top, top-left, left, ..., bottom, right or intersect.
*/
export function getOrientation(rect, reference, padding) {

Expand Down
4 changes: 3 additions & 1 deletion lib/util/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export type Axis = 'x' | 'y';

export type Direction = 'n' | 'w' | 's' | 'e' | 'nw' | 'ne' | 'sw' | 'se';

export type DirectionTRBL = 'top' | 'right' | 'bottom' | 'left' | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
export type DirectionTRBL = 'top' | 'right' | 'bottom' | 'left' | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';

export type Intersection = 'intersect';