-
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
Showing
6 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
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
120 changes: 120 additions & 0 deletions
120
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,120 @@ | ||
/******************************************************************************* | ||
* 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'; | ||
|
||
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: { | ||
// TODO: compute label placement from style (flex, ect...) | ||
'nodeLabels.placement': '[H_CENTER, V_TOP, INSIDE]', | ||
}, | ||
}; | ||
if (node.data?.label) { | ||
const label = document.querySelector<HTMLDivElement>(`[data-id="${node.id}"] > div > div`); | ||
const elkLabel: ElkLabel = { | ||
width: label.getBoundingClientRect().width, | ||
height: label.getBoundingClientRect().height, | ||
text: node.data.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); | ||
} | ||
}); | ||
}; |