-
Notifications
You must be signed in to change notification settings - Fork 45
/
FreeFormNodeLayoutHandler.ts
247 lines (224 loc) · 10.1 KB
/
FreeFormNodeLayoutHandler.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
* 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 { Node } from 'reactflow';
import { NodeData } from '../DiagramRenderer.types';
import { ImageNodeData } from '../node/ImageNode.types';
import { DiagramNodeType } from '../node/NodeTypes.types';
import { RectangularNodeData } from '../node/RectangularNode.types';
import { ILayoutEngine, INodeLayoutHandler } from './LayoutEngine.types';
import { computePreviousPosition, computePreviousSize } from './bounds';
import { RawDiagram } from './layout.types';
import { getBorderNodeExtent } from './layoutBorderNodes';
import {
applyRatioOnNewNodeSizeValue,
computeNodesBox,
findNodeIndex,
getChildNodePosition,
getEastBorderNodeFootprintHeight,
getHeaderFootprint,
getNodeOrMinHeight,
getNodeOrMinWidth,
getNorthBorderNodeFootprintWidth,
getSouthBorderNodeFootprintWidth,
getWestBorderNodeFootprintHeight,
setBorderNodesPosition,
} from './layoutNode';
import { rectangularNodePadding } from './layoutParams';
export class FreeFormNodeLayoutHandler implements INodeLayoutHandler<RectangularNodeData | ImageNodeData> {
public canHandle(node: Node<NodeData, DiagramNodeType>) {
return node.type === 'rectangularNode' || node.type === 'imageNode';
}
public handle(
layoutEngine: ILayoutEngine,
previousDiagram: RawDiagram | null,
node: Node<RectangularNodeData | ImageNodeData, 'rectangularNode' | 'imageNode'>,
visibleNodes: Node<NodeData, DiagramNodeType>[],
directChildren: Node<NodeData, DiagramNodeType>[],
newlyAddedNode: Node<NodeData, DiagramNodeType> | undefined,
forceWidth?: number
) {
const nodeIndex = findNodeIndex(visibleNodes, node.id);
const nodeElement = document.getElementById(`${node.id}-rectangularNode-${nodeIndex}`)?.children[0];
const borderWidth = nodeElement ? parseFloat(window.getComputedStyle(nodeElement).borderWidth) : 0;
if (directChildren.length > 0) {
this.handleParentNode(
layoutEngine,
previousDiagram,
node,
visibleNodes,
directChildren,
newlyAddedNode,
borderWidth,
forceWidth
);
} else {
this.handleLeafNode(previousDiagram, node, visibleNodes, borderWidth, forceWidth);
}
}
private handleParentNode(
layoutEngine: ILayoutEngine,
previousDiagram: RawDiagram | null,
node: Node<RectangularNodeData | ImageNodeData, 'rectangularNode' | 'imageNode'>,
visibleNodes: Node<NodeData, DiagramNodeType>[],
directChildren: Node<NodeData, DiagramNodeType>[],
newlyAddedNode: Node<NodeData, DiagramNodeType> | undefined,
borderWidth: number,
forceWidth?: number
) {
layoutEngine.layoutNodes(previousDiagram, visibleNodes, directChildren, newlyAddedNode);
const nodeIndex = findNodeIndex(visibleNodes, node.id);
const labelElement = document.getElementById(`${node.id}-label-${nodeIndex}`);
const withHeader: boolean = node.data.insideLabel?.isHeader ?? false;
const displayHeaderSeparator: boolean = node.data.insideLabel?.displayHeaderSeparator ?? false;
const borderNodes = directChildren.filter((node) => node.data.isBorderNode);
const directNodesChildren = directChildren.filter((child) => !child.data.isBorderNode);
// Update children position to be under the label and at the right padding.
directNodesChildren.forEach((child, index) => {
const previousNode = (previousDiagram?.nodes ?? []).find((previouseNode) => previouseNode.id === child.id);
const previousPosition = computePreviousPosition(previousNode, child);
const createdNode = newlyAddedNode?.id === child.id ? newlyAddedNode : undefined;
if (!!createdNode) {
// WARN: this prevent the created to overlep the TOP header. It is a quick fix but a proper solution should be implemented.
const headerHeightFootprint = labelElement
? getHeaderFootprint(labelElement, withHeader, displayHeaderSeparator)
: 0;
child.position = createdNode.position;
if (child.position.y < borderWidth + headerHeightFootprint) {
child.position = { ...child.position, y: borderWidth + headerHeightFootprint };
}
} else if (previousPosition) {
// WARN: this prevent the moved node to overlep the TOP header or appear outside of its container. It is a quick fix but a proper solution should be implemented.
const headerHeightFootprint = labelElement
? getHeaderFootprint(labelElement, withHeader, displayHeaderSeparator)
: 0;
child.position = previousPosition;
if (child.position.y < borderWidth + headerHeightFootprint) {
child.position = { ...child.position, y: borderWidth + headerHeightFootprint };
}
if (child.position.x < borderWidth + rectangularNodePadding) {
child.position = { ...child.position, x: borderWidth + rectangularNodePadding };
}
} else {
child.position = getChildNodePosition(
visibleNodes,
child,
labelElement,
withHeader,
displayHeaderSeparator,
borderWidth
);
const previousSibling = directNodesChildren[index - 1];
if (previousSibling) {
child.position = getChildNodePosition(
visibleNodes,
child,
labelElement,
withHeader,
displayHeaderSeparator,
borderWidth,
previousSibling
);
}
}
});
// Update node to layout size
// WARN: We suppose label are always on top of children (that wrong)
const childrenContentBox = computeNodesBox(visibleNodes, directNodesChildren); // WARN: The current content box algorithm does not take the margin of direct children (it should)
const directChildrenAwareNodeWidth = childrenContentBox.x + childrenContentBox.width + rectangularNodePadding;
const northBorderNodeFootprintWidth = getNorthBorderNodeFootprintWidth(visibleNodes, borderNodes, previousDiagram);
const southBorderNodeFootprintWidth = getSouthBorderNodeFootprintWidth(visibleNodes, borderNodes, previousDiagram);
const labelOnlyWidth =
rectangularNodePadding + (labelElement?.getBoundingClientRect().width ?? 0) + rectangularNodePadding;
const nodeWidth =
Math.max(
directChildrenAwareNodeWidth,
labelOnlyWidth,
northBorderNodeFootprintWidth,
southBorderNodeFootprintWidth
) +
borderWidth * 2;
// WARN: the label is not used for the height because children are already position under the label
const directChildrenAwareNodeHeight = childrenContentBox.y + childrenContentBox.height + rectangularNodePadding;
const eastBorderNodeFootprintHeight = getEastBorderNodeFootprintHeight(visibleNodes, borderNodes, previousDiagram);
const westBorderNodeFootprintHeight = getWestBorderNodeFootprintHeight(visibleNodes, borderNodes, previousDiagram);
const nodeHeight =
Math.max(directChildrenAwareNodeHeight, eastBorderNodeFootprintHeight, westBorderNodeFootprintHeight) +
borderWidth * 2;
const minNodeWith = forceWidth ?? getNodeOrMinWidth(nodeWidth, node); // WARN: not sure yet for the forceWidth to be here.
const minNodeheight = getNodeOrMinHeight(nodeHeight, node);
const previousNode = (previousDiagram?.nodes ?? []).find((previouseNode) => previouseNode.id === node.id);
const previousDimensions = computePreviousSize(previousNode, node);
if (node.data.nodeDescription?.userResizable) {
if (minNodeWith > previousDimensions.width) {
node.width = minNodeWith;
} else {
node.width = previousDimensions.width;
}
if (minNodeheight > previousDimensions.height) {
node.height = minNodeheight;
} else {
node.height = previousDimensions.height;
}
} else {
node.width = minNodeWith;
node.height = minNodeheight;
}
if (node.data.nodeDescription?.keepAspectRatio) {
applyRatioOnNewNodeSizeValue(node);
}
// Update border nodes positions
borderNodes.forEach((borderNode) => {
borderNode.extent = getBorderNodeExtent(node, borderNode);
});
setBorderNodesPosition(borderNodes, node, previousDiagram);
}
private handleLeafNode(
previousDiagram: RawDiagram | null,
node: Node<RectangularNodeData | ImageNodeData, 'rectangularNode' | 'imageNode'>,
visibleNodes: Node<NodeData, DiagramNodeType>[],
borderWidth: number,
forceWidth?: number
) {
const nodeIndex = findNodeIndex(visibleNodes, node.id);
const labelElement = document.getElementById(`${node.id}-label-${nodeIndex}`);
const labelWidth =
rectangularNodePadding +
(labelElement?.getBoundingClientRect().width ?? 0) +
rectangularNodePadding +
borderWidth * 2;
const labelHeight =
rectangularNodePadding + (labelElement?.getBoundingClientRect().height ?? 0) + rectangularNodePadding;
const minNodeWith = forceWidth ?? getNodeOrMinWidth(labelWidth, node);
const minNodeheight = getNodeOrMinHeight(labelHeight, node);
const previousNode = (previousDiagram?.nodes ?? []).find((previouseNode) => previouseNode.id === node.id);
const previousDimensions = computePreviousSize(previousNode, node);
if (node.data.nodeDescription?.userResizable) {
if (minNodeWith > previousDimensions.width) {
node.width = minNodeWith;
} else {
node.width = previousDimensions.width;
}
if (minNodeheight > previousDimensions.height) {
node.height = minNodeheight;
} else {
node.height = previousDimensions.height;
}
} else {
node.width = minNodeWith;
node.height = minNodeheight;
}
if (node.data.nodeDescription?.keepAspectRatio) {
applyRatioOnNewNodeSizeValue(node);
}
}
}