-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.ts
More file actions
41 lines (35 loc) · 1.19 KB
/
utils.ts
File metadata and controls
41 lines (35 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { GROUP } from '@/ids.js';
import { Node } from 'reactflow';
// we have to make sure that parent nodes are rendered before their children
export const sortNodes = (a: Node, b: Node): number => {
if (a.type === b.type) {
return 0;
}
return a.type === GROUP && b.type !== GROUP ? -1 : 1;
};
export const getId = (prefix = 'node') => `${prefix}_${Math.random() * 10000}`;
export const getNodePositionInsideParent = (
node: Partial<Node>,
groupNode: Node,
) => {
const position = node.position ?? { x: 0, y: 0 };
const nodeWidth = node.width ?? 0;
const nodeHeight = node.height ?? 0;
const groupWidth = groupNode.width ?? 0;
const groupHeight = groupNode.height ?? 0;
if (position.x < groupNode.position.x) {
position.x = 0;
} else if (position.x + nodeWidth > groupNode.position.x + groupWidth) {
position.x = groupWidth - nodeWidth;
} else {
position.x = position.x - groupNode.position.x;
}
if (position.y < groupNode.position.y) {
position.y = 0;
} else if (position.y + nodeHeight > groupNode.position.y + groupHeight) {
position.y = groupHeight - nodeHeight;
} else {
position.y = position.y - groupNode.position.y;
}
return position;
};