-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2090] Add the support for arrange all with reactflow
Bug: #2090 Signed-off-by: Guillaume Coutable <guillaume.coutable@obeo.fr>
- Loading branch information
1 parent
4dd85bd
commit f7fa39e
Showing
11 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/renderer/layout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/******************************************************************************* | ||
* 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 ELK, { ElkExtendedEdge, ElkLabel, ElkNode, LayoutOptions } from 'elkjs/lib/elk.bundled.js'; | ||
import { Edge, Node } from 'reactflow'; | ||
import { RectangularNodeData } from './RectangularNode.types'; | ||
|
||
const elk = new ELK(); | ||
|
||
export const performLayout = ( | ||
nodes: Node[], | ||
edges: Edge[], | ||
layoutOptions: LayoutOptions | ||
): Promise<{ nodes: Node[] }> => { | ||
const graph: ElkNode = { | ||
id: 'root', | ||
layoutOptions, | ||
children: [], | ||
edges: [], | ||
}; | ||
|
||
const nodeId2Node = new Map<string, Node>(); | ||
nodes.forEach((node) => nodeId2Node.set(node.id, node)); | ||
|
||
const nodeId2ElkNode = new Map<String, ElkNode>(); | ||
nodes.forEach((node) => { | ||
const elkNode: ElkNode = { | ||
id: node.id, | ||
children: [], | ||
labels: [], | ||
layoutOptions: { | ||
'nodeLabels.placement': '[H_CENTER, V_TOP, INSIDE]', | ||
}, | ||
}; | ||
if (node.type === 'rectangularNode') { | ||
const rectangularNodeData: RectangularNodeData = node.data as RectangularNodeData; | ||
|
||
const label = document.querySelector<HTMLDivElement>(`[data-id="${rectangularNodeData.label.id}"]`); | ||
const elkLabel: ElkLabel = { | ||
width: label.getBoundingClientRect().width, | ||
height: label.getBoundingClientRect().height, | ||
text: rectangularNodeData.label.text, | ||
}; | ||
|
||
elkNode.labels.push(elkLabel); | ||
} | ||
|
||
const element = document.querySelector(`[data-id="${node.id}"]`); | ||
if (element) { | ||
elkNode.width = element.clientWidth; | ||
elkNode.height = element.clientHeight; | ||
} | ||
|
||
nodeId2ElkNode.set(elkNode.id, elkNode); | ||
}); | ||
|
||
nodes.forEach((node) => { | ||
if (graph.children) { | ||
if (!!node.parentNode) { | ||
const elknodeChild = nodeId2ElkNode.get(node.id); | ||
const elkNodeParent = nodeId2ElkNode.get(node.parentNode); | ||
elkNodeParent.children.push(elknodeChild); | ||
} else { | ||
const elkNodeRoot = nodeId2ElkNode.get(node.id); | ||
graph.children.push(elkNodeRoot); | ||
} | ||
} | ||
}); | ||
|
||
edges.forEach((edge) => { | ||
if (graph.edges) { | ||
const elkEdge: ElkExtendedEdge = { | ||
id: edge.id, | ||
sources: [edge.source], | ||
targets: [edge.target], | ||
}; | ||
graph.edges.push(elkEdge); | ||
} | ||
}); | ||
|
||
return elk.layout(graph).then((layoutedGraph) => { | ||
const nodes: Node[] = []; | ||
elkToReactFlow(layoutedGraph.children ?? [], nodes, nodeId2Node); | ||
return { | ||
nodes, | ||
}; | ||
}); | ||
}; | ||
|
||
const elkToReactFlow = (elkNodes: ElkNode[], nodes: Node[], nodeId2Node: Map<string, Node>) => { | ||
elkNodes.map((elkNode) => { | ||
const node = nodeId2Node.get(elkNode.id); | ||
if (node) { | ||
node.position.x = elkNode.x ?? 0; | ||
node.position.y = elkNode.y ?? 0; | ||
node.width = elkNode.width ?? 150; | ||
node.height = elkNode.height ?? 70; | ||
if (node.style) { | ||
if (node.style.width) { | ||
node.style.width = `${node.width}px`; | ||
} | ||
if (node.style.height) { | ||
node.style.height = `${node.height}px`; | ||
} | ||
} | ||
nodes.push(node); | ||
} | ||
if (elkNode.children.length > 0) { | ||
elkToReactFlow(elkNode.children, nodes, nodeId2Node); | ||
} | ||
}); | ||
}; |