-
Notifications
You must be signed in to change notification settings - Fork 45
/
ListNodeLayoutHandler.ts
140 lines (123 loc) · 5.28 KB
/
ListNodeLayoutHandler.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { Box, Node, Rect, boxToRect, rectToBox } from 'reactflow';
import { Diagram, NodeData } from '../DiagramRenderer.types';
import { ListNodeData } from '../node/ListNode.types';
import { DiagramNodeType } from '../node/NodeTypes.types';
import { ILayoutEngine, INodeLayoutHandler } from './LayoutEngine.types';
const defaultWidth = 150;
const defaultHeight = 70;
export class ListNodeLayoutHandler implements INodeLayoutHandler<ListNodeData> {
public canHandle(node: Node<NodeData, DiagramNodeType>) {
return node.type === 'listNode';
}
public handle(
layoutEngine: ILayoutEngine,
previousDiagram: Diagram | null,
node: Node<ListNodeData, 'listNode'>,
visibleNodes: Node<NodeData, DiagramNodeType>[],
directChildren: Node<NodeData, DiagramNodeType>[],
forceWidth?: number
) {
const nodeIndex = this.findNodeIndex(visibleNodes, node.id);
const nodeElement = document.getElementById(`${node.id}-listNode-${nodeIndex}`)?.children[0];
const borderWidth = nodeElement ? parseFloat(window.getComputedStyle(nodeElement).borderWidth) : 0;
if (directChildren.length > 0) {
this.handleParentNode(layoutEngine, previousDiagram, node, visibleNodes, directChildren, borderWidth, forceWidth);
} else {
this.handleLeafNode(previousDiagram, node, visibleNodes, borderWidth, forceWidth);
}
}
handleLeafNode(
_previousDiagram: Diagram | null,
node: Node<ListNodeData, 'listNode'>,
visibleNodes: Node<NodeData, DiagramNodeType>[],
borderWidth: number,
forceWidth?: number
) {
const labelElement = document.getElementById(`${node.id}-label-${this.findNodeIndex(visibleNodes, node.id)}`);
const nodeWidth = (labelElement?.getBoundingClientRect().width ?? 0) + borderWidth * 2;
const nodeHeight = (labelElement?.getBoundingClientRect().height ?? 0) + borderWidth * 2;
node.width = forceWidth ?? this.getNodeOrMinWidth(nodeWidth);
node.height = this.getNodeOrMinHeight(nodeHeight);
}
private handleParentNode(
layoutEngine: ILayoutEngine,
previousDiagram: Diagram | null,
node: Node<ListNodeData, 'listNode'>,
visibleNodes: Node<NodeData, DiagramNodeType>[],
directChildren: Node<NodeData, DiagramNodeType>[],
borderWidth: number,
forceWidth?: number
) {
layoutEngine.layoutNodes(previousDiagram, visibleNodes, directChildren, forceWidth);
const nodeIndex = this.findNodeIndex(visibleNodes, node.id);
const labelElement = document.getElementById(`${node.id}-label-${nodeIndex}`);
if (!forceWidth) {
const widerWidth = directChildren.reduce<number>(
(widerWidth, child) => Math.max(child.width ?? 0, widerWidth),
labelElement?.getBoundingClientRect().width ?? 0
);
layoutEngine.layoutNodes(previousDiagram, visibleNodes, directChildren, widerWidth);
}
directChildren.forEach((child, index) => {
child.position = {
x: borderWidth,
y: borderWidth + (labelElement?.getBoundingClientRect().height ?? 0),
};
const previousSibling = directChildren[index - 1];
if (previousSibling) {
child.position = { ...child.position, y: previousSibling.position.y + (previousSibling.height ?? 0) };
}
});
const childrenFootprint = this.getChildrenFootprint(directChildren);
const labelOnlyWidth = labelElement?.getBoundingClientRect().width ?? 0;
const nodeWidth = Math.max(childrenFootprint.width, labelOnlyWidth) + borderWidth * 2;
const nodeHeight = (labelElement?.getBoundingClientRect().height ?? 0) + childrenFootprint.height + borderWidth * 2;
node.width = forceWidth ?? this.getNodeOrMinWidth(nodeWidth);
node.height = this.getNodeOrMinHeight(nodeHeight);
}
private findNodeIndex(nodes: Node<NodeData>[], nodeId: string): number {
return nodes.findIndex((node) => node.id === nodeId);
}
private getChildrenFootprint(children: Node<NodeData>[]): Rect {
const footPrint: Box = children.reduce(
(currentFootPrint, node) => {
const { x, y } = node.position;
const nodeBox = rectToBox({
x,
y,
width: node.width ?? 0,
height: node.height ?? 0,
});
return this.getBoundsOfBoxes(currentFootPrint, nodeBox);
},
{ x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity }
);
return boxToRect(footPrint);
}
private getBoundsOfBoxes(box1: Box, box2: Box): Box {
return {
x: Math.min(box1.x, box2.x),
y: Math.min(box1.y, box2.y),
x2: Math.max(box1.x2, box2.x2),
y2: Math.max(box1.y2, box2.y2),
};
}
private getNodeOrMinWidth(nodeWidth: number | undefined): number {
return Math.max(nodeWidth ?? -Infinity, defaultWidth);
}
private getNodeOrMinHeight(nodeHeight: number | undefined): number {
return Math.max(nodeHeight ?? -Infinity, defaultHeight);
}
}