docs: add run process to layouting example
This commit is contained in:
@@ -3,40 +3,48 @@ import dagre from 'dagre'
|
||||
import { nextTick, ref } from 'vue'
|
||||
import { Panel, Position, VueFlow, useVueFlow } from '@vue-flow/core'
|
||||
import { Background } from '@vue-flow/background'
|
||||
import ProcessNode from './ProcessNode.vue'
|
||||
|
||||
import { initialEdges, initialNodes } from './initial-elements.js'
|
||||
import { useRunProcess } from './useRunProcess'
|
||||
|
||||
const nodes = ref(initialNodes)
|
||||
|
||||
const edges = ref(initialEdges)
|
||||
|
||||
const dagreGraph = ref(new dagre.graphlib.Graph())
|
||||
|
||||
dagreGraph.value.setDefaultEdgeLabel(() => ({}))
|
||||
|
||||
const { run } = useRunProcess()
|
||||
|
||||
const { findNode, fitView } = useVueFlow()
|
||||
|
||||
function handleLayout(direction) {
|
||||
// we create a new graph instance, in case some nodes/edges were removed, otherwise dagre would act as if they were still there
|
||||
const dagreGraph = new dagre.graphlib.Graph()
|
||||
dagreGraph.value = new dagre.graphlib.Graph()
|
||||
|
||||
dagreGraph.setDefaultEdgeLabel(() => ({}))
|
||||
dagreGraph.value.setDefaultEdgeLabel(() => ({}))
|
||||
|
||||
const isHorizontal = direction === 'LR'
|
||||
dagreGraph.setGraph({ rankdir: direction })
|
||||
dagreGraph.value.setGraph({ rankdir: direction })
|
||||
|
||||
for (const node of nodes.value) {
|
||||
// if you need width+height of nodes for your layout, you can use the dimensions property of the internal node (`GraphNode` type)
|
||||
const graphNode = findNode(node.id)
|
||||
|
||||
dagreGraph.setNode(node.id, { width: graphNode.dimensions.width || 150, height: graphNode.dimensions.height || 50 })
|
||||
dagreGraph.value.setNode(node.id, { width: graphNode.dimensions.width || 150, height: graphNode.dimensions.height || 50 })
|
||||
}
|
||||
|
||||
for (const edge of edges.value) {
|
||||
dagreGraph.setEdge(edge.source, edge.target)
|
||||
dagreGraph.value.setEdge(edge.source, edge.target)
|
||||
}
|
||||
|
||||
dagre.layout(dagreGraph)
|
||||
dagre.layout(dagreGraph.value)
|
||||
|
||||
// set nodes with updated positions
|
||||
nodes.value = nodes.value.map((node) => {
|
||||
const nodeWithPosition = dagreGraph.node(node.id)
|
||||
const nodeWithPosition = dagreGraph.value.node(node.id)
|
||||
|
||||
return {
|
||||
...node,
|
||||
@@ -55,11 +63,17 @@ function handleLayout(direction) {
|
||||
<template>
|
||||
<div class="layoutflow">
|
||||
<VueFlow :nodes="nodes" :edges="edges" @nodes-initialized="handleLayout('TB')">
|
||||
<template #node-process="props">
|
||||
<ProcessNode v-bind="props" />
|
||||
</template>
|
||||
|
||||
<Background />
|
||||
|
||||
<Panel style="display: flex; gap: 1rem" position="top-right">
|
||||
<button @click="handleLayout('TB')">vertical layout</button>
|
||||
<button @click="handleLayout('LR')">horizontal layout</button>
|
||||
<button @click="handleLayout('TB')">vertical</button>
|
||||
<button @click="handleLayout('LR')">horizontal</button>
|
||||
|
||||
<button @click="run(nodes, dagreGraph)">Run</button>
|
||||
</Panel>
|
||||
</VueFlow>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user