@@ -3,40 +3,48 @@ import dagre from 'dagre'
3
3
import { nextTick , ref } from ' vue'
4
4
import { Panel , Position , VueFlow , useVueFlow } from ' @vue-flow/core'
5
5
import { Background } from ' @vue-flow/background'
6
+ import ProcessNode from ' ./ProcessNode.vue'
6
7
7
8
import { initialEdges , initialNodes } from ' ./initial-elements.js'
9
+ import { useRunProcess } from ' ./useRunProcess'
8
10
9
11
const nodes = ref (initialNodes)
10
12
11
13
const edges = ref (initialEdges)
12
14
15
+ const dagreGraph = ref (new dagre.graphlib.Graph ())
16
+
17
+ dagreGraph .value .setDefaultEdgeLabel (() => ({}))
18
+
19
+ const { run } = useRunProcess ()
20
+
13
21
const { findNode , fitView } = useVueFlow ()
14
22
15
23
function handleLayout (direction ) {
16
24
// we create a new graph instance, in case some nodes/edges were removed, otherwise dagre would act as if they were still there
17
- const dagreGraph = new dagre.graphlib.Graph ()
25
+ dagreGraph . value = new dagre.graphlib.Graph ()
18
26
19
- dagreGraph .setDefaultEdgeLabel (() => ({}))
27
+ dagreGraph .value . setDefaultEdgeLabel (() => ({}))
20
28
21
29
const isHorizontal = direction === ' LR'
22
- dagreGraph .setGraph ({ rankdir: direction })
30
+ dagreGraph .value . setGraph ({ rankdir: direction })
23
31
24
32
for (const node of nodes .value ) {
25
33
// if you need width+height of nodes for your layout, you can use the dimensions property of the internal node (`GraphNode` type)
26
34
const graphNode = findNode (node .id )
27
35
28
- dagreGraph .setNode (node .id , { width: graphNode .dimensions .width || 150 , height: graphNode .dimensions .height || 50 })
36
+ dagreGraph .value . setNode (node .id , { width: graphNode .dimensions .width || 150 , height: graphNode .dimensions .height || 50 })
29
37
}
30
38
31
39
for (const edge of edges .value ) {
32
- dagreGraph .setEdge (edge .source , edge .target )
40
+ dagreGraph .value . setEdge (edge .source , edge .target )
33
41
}
34
42
35
- dagre .layout (dagreGraph)
43
+ dagre .layout (dagreGraph . value )
36
44
37
45
// set nodes with updated positions
38
46
nodes .value = nodes .value .map ((node ) => {
39
- const nodeWithPosition = dagreGraph .node (node .id )
47
+ const nodeWithPosition = dagreGraph .value . node (node .id )
40
48
41
49
return {
42
50
... node,
@@ -55,11 +63,17 @@ function handleLayout(direction) {
55
63
<template >
56
64
<div class =" layoutflow" >
57
65
<VueFlow :nodes =" nodes" :edges =" edges" @nodes-initialized =" handleLayout('TB')" >
66
+ <template #node-process =" props " >
67
+ <ProcessNode v-bind =" props" />
68
+ </template >
69
+
58
70
<Background />
59
71
60
72
<Panel style =" display : flex ; gap : 1rem " position =" top-right" >
61
- <button @click =" handleLayout('TB')" >vertical layout</button >
62
- <button @click =" handleLayout('LR')" >horizontal layout</button >
73
+ <button @click =" handleLayout('TB')" >vertical</button >
74
+ <button @click =" handleLayout('LR')" >horizontal</button >
75
+
76
+ <button @click =" run(nodes, dagreGraph)" >Run</button >
63
77
</Panel >
64
78
</VueFlow >
65
79
</div >
0 commit comments