|
@@ -1,5 +1,4 @@
|
|
|
-import dagre from "dagre";
|
|
|
-import { Elements, isNode, Position } from "react-flow-renderer";
|
|
|
+import { isNode } from "react-flow-renderer";
|
|
|
import { CanvasDirection } from "reaflow";
|
|
|
import { parser } from "src/utils/json-editor-parser";
|
|
|
|
|
@@ -50,47 +49,6 @@ export function getEdgeNodes(graph: any): any {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-const dagreGraph = new dagre.graphlib.Graph();
|
|
|
-dagreGraph.setDefaultEdgeLabel(() => ({}));
|
|
|
-
|
|
|
-export const getLayoutPosition = (
|
|
|
- direction: string,
|
|
|
- elements: Elements,
|
|
|
- dynamic = false
|
|
|
-) => {
|
|
|
- const isHorizontal = direction === "LR";
|
|
|
- dagreGraph.setGraph({ rankdir: direction });
|
|
|
-
|
|
|
- elements.forEach((el) => {
|
|
|
- if (isNode(el)) {
|
|
|
- dagreGraph.setNode(el.id, {
|
|
|
- width: dynamic ? el.__rf.width : 400,
|
|
|
- height: dynamic ? el.__rf.height : 100,
|
|
|
- });
|
|
|
- } else {
|
|
|
- dagreGraph.setEdge(el.source, el.target);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- dagre.layout(dagreGraph);
|
|
|
-
|
|
|
- const layoutedElements = elements.map((el) => {
|
|
|
- if (isNode(el)) {
|
|
|
- const nodeWithPosition = dagreGraph.node(el.id);
|
|
|
- el.targetPosition = isHorizontal ? Position.Left : Position.Top;
|
|
|
- el.sourcePosition = isHorizontal ? Position.Right : Position.Bottom;
|
|
|
- el.position = {
|
|
|
- x: nodeWithPosition.x + Math.random() / 1000,
|
|
|
- y: nodeWithPosition.y,
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- return el;
|
|
|
- });
|
|
|
-
|
|
|
- return layoutedElements;
|
|
|
-};
|
|
|
-
|
|
|
export function getNextLayout(layout: CanvasDirection) {
|
|
|
switch (layout) {
|
|
|
case "LEFT":
|
|
@@ -106,10 +64,3 @@ export function getNextLayout(layout: CanvasDirection) {
|
|
|
return "LEFT";
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-export function getLayout(layout: CanvasDirection, json: string, dynamic = false) {
|
|
|
- const jsonToGraph = parser(json);
|
|
|
- const layoutedElements = getLayoutPosition(layout, jsonToGraph, dynamic);
|
|
|
-
|
|
|
- return layoutedElements;
|
|
|
-}
|