-
Notifications
You must be signed in to change notification settings - Fork 45
/
layoutNode.ts
390 lines (356 loc) · 16 KB
/
layoutNode.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*******************************************************************************
* 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, XYPosition, boxToRect, rectToBox } from 'reactflow';
import { Diagram, NodeData } from '../DiagramRenderer.types';
import {
getBorderNodeExtent,
isEastBorderNode,
isNorthBorderNode,
isSouthBorderNode,
isWestBorderNode,
} from './layoutBorderNodes';
import { defaultHeight, defaultNodeMargin, defaultWidth, gap, rectangularNodePadding } from './layoutParams';
/**
* It requires that nodes are already positioned
*/
export const computeNodesBox = (allVisibleNodes: Node<NodeData>[], nodes: Node[]): Rect => {
if (nodes.length <= 0) {
return boxToRect({ x: 0, y: 0, x2: 0, y2: 0 });
}
const contentBox: Box = nodes.reduce<Box>(
(currentBox, node) => {
const bounds = getNodeFootprint(allVisibleNodes, node);
const nodeBox = rectToBox({
x: bounds.x,
y: bounds.y,
width: bounds.width ?? 0,
height: bounds.height ?? 0,
});
return getBoundsOfBoxes(currentBox, nodeBox);
},
{ x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity }
);
return boxToRect(contentBox);
};
const 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),
};
};
export const findNodeIndex = (nodes: Node<NodeData>[], nodeId: string): number => {
return nodes.findIndex((node) => node.id === nodeId);
};
export const getNodeOrMinWidth = (nodeWidth: number | undefined): number => {
return Math.max(nodeWidth ?? -Infinity, defaultWidth);
};
export const getNodeOrMinHeight = (nodeHeight: number | undefined): number => {
return Math.max(nodeHeight ?? -Infinity, defaultHeight);
};
export const getChildNodePosition = (
allVisibleNodes: Node<NodeData>[],
child: Node<NodeData>,
labelElement: HTMLElement | null,
withHeader: boolean,
displayHeaderSeparator: boolean,
borderWidth: number,
previousSibling?: Node<NodeData>
): XYPosition => {
const maxWestBorderNodeWidth = getChildren(child, allVisibleNodes)
.filter(isWestBorderNode)
.map((borderNode) => getNodeFootprint(allVisibleNodes, borderNode).width || 0)
.reduce((a, b) => Math.max(a, b), 0);
const maxNorthBorderNodeHeight = getChildren(child, allVisibleNodes)
.filter(isNorthBorderNode)
.map((borderNode) => getNodeFootprint(allVisibleNodes, borderNode).height || 0)
.reduce((a, b) => Math.max(a, b), 0);
if (!previousSibling) {
const headerFootprint = labelElement ? getHeaderFootprint(labelElement, withHeader, displayHeaderSeparator) : 0;
return {
x: rectangularNodePadding + borderWidth + maxWestBorderNodeWidth,
y: borderWidth + headerFootprint + maxNorthBorderNodeHeight,
};
} else {
const previousSiblingsMaxEastBorderNodeWidth = getChildren(previousSibling, allVisibleNodes)
.filter(isEastBorderNode)
.map((borderNode) => getNodeFootprint(allVisibleNodes, borderNode).width || 0)
.reduce((a, b) => Math.max(a, b), 0);
return {
...child.position,
x:
previousSibling.position.x +
previousSiblingsMaxEastBorderNodeWidth +
maxWestBorderNodeWidth +
(previousSibling.width ?? 0) +
gap,
};
}
};
const getHeaderFootprint = (
labelElement: HTMLElement,
withHeader: boolean,
displayHeaderSeparator: boolean
): number => {
let headerFootprint = 0;
if (withHeader) {
headerFootprint = labelElement.getBoundingClientRect().height;
if (displayHeaderSeparator) {
headerFootprint += rectangularNodePadding;
}
}
return headerFootprint;
};
/**
* Returns the node footprint.
* It requires node border nodes to be positioned.
*
* WARN: It takes only node border nodes into account, it will be updated to support outside label and the margin
*
* @param allVisibleNodes all diagram visible nodes
* @param node the node used to compute the footprint
* @returns the rect that represents the node footprint
*/
const getNodeFootprint = (allVisibleNodes: Node<NodeData>[], node: Node<NodeData>): Rect => {
const borderNodes = allVisibleNodes
.filter((visibleNode) => visibleNode.parentNode === node.id)
.filter((visibleNode) => visibleNode.data.isBorderNode);
const footPrint: Box = [node, ...borderNodes].reduce<Box>(
(currentFootPrint, child) => {
const nodeBox = rectToBox({
x: node !== child && child.data.isBorderNode ? node.position.x + child.position.x : child.position.x,
y: node !== child && child.data.isBorderNode ? node.position.y + child.position.y : child.position.y,
width: child.width ?? 0,
height: child.height ?? 0,
});
return getBoundsOfBoxes(currentFootPrint, nodeBox);
},
{ x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity }
);
return boxToRect(footPrint);
};
const spreadPositionedNodesFromNonPositionedNodes = (
nodes: Node<NodeData>[],
previousDiagram: Diagram | null
): { positionedNodes: Node<NodeData>[]; nonPositionedNodes: Node<NodeData>[] } => {
const positionedNodes: Node<NodeData>[] = [];
const nonPositionedNodes: Node<NodeData>[] = [];
nodes.forEach((borderNode) => {
const previousNode = (previousDiagram?.nodes ?? []).find((previousNode) => previousNode.id === borderNode.id);
if (previousNode) {
positionedNodes.push(previousNode);
} else {
nonPositionedNodes.push(borderNode);
}
});
return {
positionedNodes,
nonPositionedNodes,
};
};
export const setBorderNodesPosition = (
borderNodes: Node<NodeData>[],
nodeToLayout: Node<NodeData>,
previousDiagram: Diagram | null
): void => {
const borderNodesEast = borderNodes.filter(isEastBorderNode);
borderNodesEast.forEach((child, index) => {
const previousBorderNode = (previousDiagram?.nodes ?? []).find((previousNode) => previousNode.id === child.id);
if (previousBorderNode) {
child.position = {
x: nodeToLayout.width ?? 0,
y: previousBorderNode.position.y,
};
} else {
child.position = { x: nodeToLayout.width ?? 0, y: defaultNodeMargin };
const previousSibling = borderNodesEast[index - 1];
if (previousSibling) {
child.position = { ...child.position, y: previousSibling.position.y + (previousSibling.height ?? 0) + gap };
child.extent = getBorderNodeExtent(nodeToLayout, child);
}
}
});
const borderNodesWest = borderNodes.filter(isWestBorderNode);
borderNodesWest.forEach((child, index) => {
const previousBorderNode = (previousDiagram?.nodes ?? []).find((previousNode) => previousNode.id === child.id);
if (previousBorderNode) {
child.position = {
x: 0 - (child.width ?? 0),
y: previousBorderNode.position.y,
};
} else {
child.position = { x: 0 - (child.width ?? 0), y: defaultNodeMargin };
const previousSibling = borderNodesWest[index - 1];
if (previousSibling) {
child.position = { ...child.position, y: previousSibling.position.y + (previousSibling.height ?? 0) + gap };
}
}
});
const borderNodesSouth = borderNodes.filter(isSouthBorderNode);
borderNodesSouth.forEach((child, index) => {
const previousBorderNode = (previousDiagram?.nodes ?? []).find((previousNode) => previousNode.id === child.id);
if (previousBorderNode) {
child.position = {
x: previousBorderNode.position.x,
y: nodeToLayout.height ?? 0,
};
} else {
child.position = { x: defaultNodeMargin, y: nodeToLayout.height ?? 0 };
const previousSibling = borderNodesSouth[index - 1];
if (previousSibling) {
child.position = { ...child.position, x: previousSibling.position.x + (previousSibling.width ?? 0) + gap };
child.extent = getBorderNodeExtent(nodeToLayout, child);
}
}
});
const borderNodesNorth = borderNodes.filter(isNorthBorderNode);
borderNodesNorth.forEach((child, index) => {
const previousBorderNode = (previousDiagram?.nodes ?? []).find((previousNode) => previousNode.id === child.id);
if (previousBorderNode) {
child.position = {
x: previousBorderNode.position.x,
y: 0 - (child.height ?? 0),
};
} else {
child.position = { x: defaultNodeMargin, y: 0 - (child.height ?? 0) };
const previousSibling = borderNodesNorth[index - 1];
if (previousSibling) {
child.position = { ...child.position, x: previousSibling.position.x + (previousSibling.width ?? 0) + gap };
}
}
});
};
/**
* Returns the footprint width of north border nodes from given border nodes.
*
* It relies on _computeNodesBox_ to compute the width of already positioned border nodes.
* Then it considers new border nodes by adding their width and a gap if necessary.
*
* @param allVisibleNodes all diagram visible nodes
* @param borderNodes The border nodes used to compute the footprint width
* @param previousDiagram The previous diagram
* @returns the footprint width of north border nodes from given border nodes
*/
export const getNorthBorderNodeFootprintWidth = (
allVisibleNodes: Node<NodeData>[],
borderNodes: Node<NodeData>[],
previousDiagram: Diagram | null
): number => {
const northBorderNodes = borderNodes.filter(isNorthBorderNode);
const { positionedNodes: previousBorderNodes, nonPositionedNodes: nonPositionedBorderNode } =
spreadPositionedNodesFromNonPositionedNodes(northBorderNodes, previousDiagram);
const previousBorderNodeFootprint = computeNodesBox(allVisibleNodes, previousBorderNodes);
return nonPositionedBorderNode.reduce<number>(
(accumulatedSideWidth, currentBorderNode, currentBordernodeIndex) =>
accumulatedSideWidth +
(getNodeFootprint(allVisibleNodes, currentBorderNode).width ?? 0) +
(currentBordernodeIndex < nonPositionedBorderNode.length - 1 ? gap : 0), // WARN: Once getNodeFootprint will take the margin into account, we will have to merge the footprint margin of two siblings next to each other and remove the next line under.
// WARN: Because computeNodeBox rely on getNodeFootprint which do not take the margin into account we must add the margin twice.
// This will be updated once footprint will take the margin into account
previousBorderNodeFootprint.width + defaultNodeMargin * 2
);
};
/**
* Returns the footprint width of south border nodes from given border nodes.
*
* It relies on _computeNodesBox_ to compute the width of already positioned border nodes.
* Then it considers new border nodes by adding their width and a gap if necessary.
*
* @param allVisibleNodes all diagram visible nodes
* @param borderNodes The border nodes used to compute the footprint width
* @param previousDiagram The previous diagram
* @returns the footprint width of south border nodes from given border nodes
*/
export const getSouthBorderNodeFootprintWidth = (
allVisibleNodes: Node<NodeData>[],
borderNodes: Node<NodeData>[],
previousDiagram: Diagram | null
): number => {
const southBorderNodes = borderNodes.filter(isSouthBorderNode);
const { positionedNodes: previousBorderNodes, nonPositionedNodes: nonPositionedBorderNode } =
spreadPositionedNodesFromNonPositionedNodes(southBorderNodes, previousDiagram);
const previousBorderNodeFootprint = computeNodesBox(allVisibleNodes, previousBorderNodes);
return nonPositionedBorderNode.reduce<number>(
(accumulatedSideWidth, currentBorderNode, currentBordernodeIndex) =>
accumulatedSideWidth +
(getNodeFootprint(allVisibleNodes, currentBorderNode).width ?? 0) + // WARN: Once getNodeFootprint will take the margin into account, we will have to merge the footprint margin of two siblings next to each other and remove the next line under.
(currentBordernodeIndex < nonPositionedBorderNode.length - 1 ? gap : 0),
// WARN: Because computeNodeBox rely on getNodeFootprint which do not take the margin into account we must add the margin twice.
// This will be updated once footprint will take the margin into account
previousBorderNodeFootprint.width + defaultNodeMargin * 2
);
};
/**
* Returns the footprint height of east border nodes from given border nodes.
*
* It relies on _computeNodesBox_ to compute the height of already positioned border nodes.
* Then it considers new border nodes by adding their height and a gap if necessary.
*
* @param allVisibleNodes all diagram visible nodes
* @param borderNodes The border nodes used to compute the footprint height
* @param previousDiagram The previous diagram
* @returns the footprint height of east border nodes from given border nodes
*/
export const getEastBorderNodeFootprintHeight = (
allVisibleNodes: Node<NodeData>[],
borderNodes: Node<NodeData>[],
previousDiagram: Diagram | null
): number => {
const eastBorderNodes = borderNodes.filter(isEastBorderNode);
const { positionedNodes: previousBorderNodes, nonPositionedNodes: nonPositionedBorderNode } =
spreadPositionedNodesFromNonPositionedNodes(eastBorderNodes, previousDiagram);
const previousBorderNodeFootprint = computeNodesBox(allVisibleNodes, previousBorderNodes);
return nonPositionedBorderNode.reduce<number>(
(accumulatedSideHeight, currentBorderNode, currentBordernodeIndex) =>
accumulatedSideHeight +
(getNodeFootprint(allVisibleNodes, currentBorderNode).height ?? 0) +
(currentBordernodeIndex < nonPositionedBorderNode.length - 1 ? gap : 0), // WARN: Once getNodeFootprint will take the margin into account, we will have to merge the footprint margin of two siblings next to each other and remove the next line under.
// WARN: Because computeNodeBox rely on getNodeFootprint which do not take the margin into account we must add the margin twice.
// This will be updated once footprint will take the margin into account
previousBorderNodeFootprint.height + defaultNodeMargin * 2
);
};
/**
* Returns the footprint height of west border nodes from given border nodes.
*
* It relies on _computeNodesBox_ to compute the height of already positioned border nodes.
* Then it considers new border nodes by adding their height and a gap if necessary.
*
* @param allVisibleNodes all diagram visible nodes
* @param borderNodes The border nodes used to compute the footprint height
* @param previousDiagram The previous diagram
* @returns the footprint height of west border nodes from given border nodes
*/
export const getWestBorderNodeFootprintHeight = (
allVisibleNodes: Node<NodeData>[],
borderNodes: Node<NodeData>[],
previousDiagram: Diagram | null
): number => {
const westBorderNodes = borderNodes.filter(isWestBorderNode);
const { positionedNodes: previousBorderNodes, nonPositionedNodes: nonPositionedBorderNode } =
spreadPositionedNodesFromNonPositionedNodes(westBorderNodes, previousDiagram);
const previousBorderNodeFootprint = computeNodesBox(allVisibleNodes, previousBorderNodes);
return nonPositionedBorderNode.reduce<number>(
(accumulatedSideHeight, currentBorderNode, currentBordernodeIndex) =>
accumulatedSideHeight +
(getNodeFootprint(allVisibleNodes, currentBorderNode).height ?? 0) +
(currentBordernodeIndex < nonPositionedBorderNode.length - 1 ? gap : 0), // WARN: Once getNodeFootprint will take the margin into account, we will have to merge the footprint margin of two siblings next to each other and remove the next line under.
// WARN: Because computeNodeBox rely on getNodeFootprint which do not take the margin into account we must add the margin twice.
// This will be updated once footprint will take the margin into account
previousBorderNodeFootprint.height + defaultNodeMargin * 2
);
};
export const getChildren = (node: Node<NodeData>, allVisibleNodes: Node<NodeData>[]): Node<NodeData>[] => {
return allVisibleNodes.filter((child) => child.parentNode === node.id);
};