docs: add dagre layout example
This commit is contained in:
@@ -5,7 +5,7 @@ import CodeMirror from '@vue/repl/codemirror-editor'
|
|||||||
import { useVueFlow } from '@vue-flow/core'
|
import { useVueFlow } from '@vue-flow/core'
|
||||||
import { exampleImports } from '../examples'
|
import { exampleImports } from '../examples'
|
||||||
|
|
||||||
const props = defineProps<{ example: keyof typeof exampleImports; mainFile?: string; dependencies?: Record<string, string> }>()
|
const props = defineProps<{ example: keyof typeof exampleImports; mainFile?: string }>()
|
||||||
|
|
||||||
const { vueFlowVersion } = useVueFlow()
|
const { vueFlowVersion } = useVueFlow()
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Suspense } from 'vue'
|
|||||||
const DocsRepl = defineAsyncComponent(() => import('./DocsRepl.vue'))
|
const DocsRepl = defineAsyncComponent(() => import('./DocsRepl.vue'))
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
props: ['example', 'examplesImports', 'dependencies'],
|
props: ['example', 'mainFile'],
|
||||||
setup(props) {
|
setup(props) {
|
||||||
return () => {
|
return () => {
|
||||||
if (typeof navigator === 'undefined') {
|
if (typeof navigator === 'undefined') {
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue'
|
import { ref, toRef } from 'vue'
|
||||||
import { MiniMap } from '@vue-flow/minimap'
|
import { MiniMap } from '@vue-flow/minimap'
|
||||||
import { Position, VueFlow, useNodesData, useVueFlow } from '@vue-flow/core'
|
import { Position, VueFlow } from '@vue-flow/core'
|
||||||
import ColorSelectorNode from './ColorSelectorNode.vue'
|
import ColorSelectorNode from './ColorSelectorNode.vue'
|
||||||
import OutputNode from './OutputNode.vue'
|
import OutputNode from './OutputNode.vue'
|
||||||
import { presets } from './presets.js'
|
import { presets } from './presets.js'
|
||||||
|
|
||||||
useVueFlow()
|
|
||||||
|
|
||||||
const nodeData = useNodesData('1')
|
|
||||||
|
|
||||||
const nodes = ref([
|
const nodes = ref([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
@@ -25,6 +21,8 @@ const nodes = ref([
|
|||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
|
const colorSelectorData = toRef(() => nodes.value[0].data)
|
||||||
|
|
||||||
const edges = ref([
|
const edges = ref([
|
||||||
{
|
{
|
||||||
id: 'e1a-2',
|
id: 'e1a-2',
|
||||||
@@ -33,7 +31,7 @@ const edges = ref([
|
|||||||
target: '2',
|
target: '2',
|
||||||
animated: true,
|
animated: true,
|
||||||
style: () => ({
|
style: () => ({
|
||||||
stroke: nodeData.value?.color,
|
stroke: colorSelectorData.value?.color,
|
||||||
filter: 'invert(100%)',
|
filter: 'invert(100%)',
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@@ -55,7 +53,7 @@ function nodeStroke(n) {
|
|||||||
|
|
||||||
function nodeColor(n) {
|
function nodeColor(n) {
|
||||||
if (n.type === 'color-selector') {
|
if (n.type === 'color-selector') {
|
||||||
return nodeData.value?.color
|
return colorSelectorData.value?.color
|
||||||
}
|
}
|
||||||
|
|
||||||
return '#fff'
|
return '#fff'
|
||||||
@@ -64,20 +62,20 @@ function nodeColor(n) {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<VueFlow
|
<VueFlow
|
||||||
:nodes="nodes"
|
v-model:nodes="nodes"
|
||||||
:edges="edges"
|
:edges="edges"
|
||||||
class="custom-node-flow"
|
class="custom-node-flow"
|
||||||
:class="[nodeData?.isGradient ? 'animated-bg-gradient' : '']"
|
:class="[colorSelectorData?.isGradient ? 'animated-bg-gradient' : '']"
|
||||||
:style="{ backgroundColor: nodeData?.color }"
|
:style="{ backgroundColor: colorSelectorData?.color }"
|
||||||
:default-viewport="{ zoom: 1.5 }"
|
:default-viewport="{ zoom: 1.5 }"
|
||||||
fit-view-on-init
|
fit-view-on-init
|
||||||
>
|
>
|
||||||
<template #node-color-selector="{ data }">
|
<template #node-color-selector="{ id }">
|
||||||
<ColorSelectorNode :data="data" />
|
<ColorSelectorNode :id="id" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<template #node-output="{ data }">
|
<template #node-output>
|
||||||
<OutputNode :data="data" />
|
<OutputNode />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<MiniMap :node-stroke-color="nodeStroke" :node-color="nodeColor" />
|
<MiniMap :node-stroke-color="nodeStroke" :node-color="nodeColor" />
|
||||||
|
|||||||
@@ -1,24 +1,22 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { Handle, Position, useNodeId, useVueFlow } from '@vue-flow/core'
|
import { Handle, Position, useVueFlow } from '@vue-flow/core'
|
||||||
import { colors } from './presets.js'
|
import { colors } from './presets.js'
|
||||||
|
|
||||||
defineProps({
|
const props = defineProps({
|
||||||
data: {
|
id: {
|
||||||
type: Object,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const nodeId = useNodeId()
|
|
||||||
|
|
||||||
const { updateNodeData } = useVueFlow()
|
const { updateNodeData } = useVueFlow()
|
||||||
|
|
||||||
function onSelect(color) {
|
function onSelect(color) {
|
||||||
updateNodeData(nodeId, { color }, { replace: true })
|
updateNodeData(props.id, { color, isGradient: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
function onGradient() {
|
function onGradient() {
|
||||||
updateNodeData(nodeId, { isGradient: true }, { replace: true })
|
updateNodeData(props.id, { isGradient: true })
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { Handle, Position, useHandleConnections, useNodesData } from '@vue-flow/core'
|
import { Handle, Position, useHandleConnections, useNodesData } from '@vue-flow/core'
|
||||||
|
|
||||||
defineProps({
|
|
||||||
data: {
|
|
||||||
type: Object,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const connections = useHandleConnections({
|
const connections = useHandleConnections({
|
||||||
type: 'target',
|
type: 'target',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import { IntersectionApp, IntersectionCSS } from './intersection'
|
|||||||
import { SnapToHandleApp, SnappableConnectionLine } from './connection-radius'
|
import { SnapToHandleApp, SnappableConnectionLine } from './connection-radius'
|
||||||
import { NodeResizerApp, ResizableNode } from './node-resizer'
|
import { NodeResizerApp, ResizableNode } from './node-resizer'
|
||||||
import { ToolbarApp, ToolbarNode } from './node-toolbar'
|
import { ToolbarApp, ToolbarNode } from './node-toolbar'
|
||||||
|
import { LayoutApp, LayoutElements } from './layout'
|
||||||
|
|
||||||
export const exampleImports = {
|
export const exampleImports = {
|
||||||
basic: {
|
basic: {
|
||||||
@@ -123,4 +124,11 @@ export const exampleImports = {
|
|||||||
'App.vue': ToolbarApp,
|
'App.vue': ToolbarApp,
|
||||||
'ToolbarNode.vue': ToolbarNode,
|
'ToolbarNode.vue': ToolbarNode,
|
||||||
},
|
},
|
||||||
|
layout: {
|
||||||
|
'App.vue': LayoutApp,
|
||||||
|
'initial-elements.js': LayoutElements,
|
||||||
|
'additionalImports': {
|
||||||
|
dagre: 'https://cdn.skypack.dev/pin/dagre@v0.8.5-NOlknF82nBdUHQKLJWRC/mode=imports,min/optimized/dagre.js',
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
<script setup>
|
||||||
|
import dagre from 'dagre'
|
||||||
|
import { nextTick, ref } from 'vue'
|
||||||
|
import { Panel, Position, VueFlow, useVueFlow } from '@vue-flow/core'
|
||||||
|
|
||||||
|
import { initialEdges, initialNodes } from './initial-elements.js'
|
||||||
|
|
||||||
|
const nodes = ref(initialNodes)
|
||||||
|
|
||||||
|
const edges = ref(initialEdges)
|
||||||
|
|
||||||
|
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.setDefaultEdgeLabel(() => ({}))
|
||||||
|
|
||||||
|
const isHorizontal = direction === 'LR'
|
||||||
|
dagreGraph.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 })
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const edge of edges.value) {
|
||||||
|
dagreGraph.setEdge(edge.source, edge.target)
|
||||||
|
}
|
||||||
|
|
||||||
|
dagre.layout(dagreGraph)
|
||||||
|
|
||||||
|
// set nodes with updated positions
|
||||||
|
nodes.value = nodes.value.map((node) => {
|
||||||
|
const nodeWithPosition = dagreGraph.node(node.id)
|
||||||
|
|
||||||
|
return {
|
||||||
|
...node,
|
||||||
|
targetPosition: isHorizontal ? Position.Left : Position.Top,
|
||||||
|
sourcePosition: isHorizontal ? Position.Right : Position.Bottom,
|
||||||
|
position: { x: nodeWithPosition.x, y: nodeWithPosition.y },
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
nextTick(() => {
|
||||||
|
fitView()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="layoutflow">
|
||||||
|
<VueFlow :nodes="nodes" :edges="edges" @nodes-initialized="handleLayout('TB')">
|
||||||
|
<Panel style="display: flex; gap: 1rem" position="top-right">
|
||||||
|
<button @click="handleLayout('TB')">vertical layout</button>
|
||||||
|
<button @click="handleLayout('LR')">horizontal layout</button>
|
||||||
|
</Panel>
|
||||||
|
</VueFlow>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.layoutflow {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export { default as LayoutApp } from './App.vue?raw'
|
||||||
|
export { default as LayoutElements } from './initial-elements.js?raw'
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
const position = { x: 0, y: 0 }
|
||||||
|
|
||||||
|
export const initialNodes = [
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
type: 'input',
|
||||||
|
label: 'input',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2',
|
||||||
|
label: 'node 2',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2a',
|
||||||
|
label: 'node 2a',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2b',
|
||||||
|
label: 'node 2b',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2c',
|
||||||
|
label: 'node 2c',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2d',
|
||||||
|
label: 'node 2d',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
label: 'node 3',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '4',
|
||||||
|
label: 'node 4',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '5',
|
||||||
|
label: 'node 5',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '6',
|
||||||
|
type: 'output',
|
||||||
|
label: 'output',
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
export const initialEdges = [
|
||||||
|
{ id: '7', type: 'output', label: 'output', position: { x: 400, y: 450 } },
|
||||||
|
{ id: 'e12', source: '1', target: '2', type: 'smoothstep', animated: true },
|
||||||
|
{ id: 'e13', source: '1', target: '3', type: 'smoothstep', animated: true },
|
||||||
|
{ id: 'e22a', source: '2', target: '2a', type: 'smoothstep', animated: true },
|
||||||
|
{ id: 'e22b', source: '2', target: '2b', type: 'smoothstep', animated: true },
|
||||||
|
{ id: 'e22c', source: '2', target: '2c', type: 'smoothstep', animated: true },
|
||||||
|
{ id: 'e2c2d', source: '2c', target: '2d', type: 'smoothstep', animated: true },
|
||||||
|
|
||||||
|
{ id: 'e45', source: '4', target: '5', type: 'smoothstep', animated: true },
|
||||||
|
{ id: 'e56', source: '5', target: '6', type: 'smoothstep', animated: true },
|
||||||
|
{ id: 'e57', source: '5', target: '7', type: 'smoothstep', animated: true },
|
||||||
|
]
|
||||||
@@ -13,23 +13,19 @@ const edges = ref(initialEdges)
|
|||||||
|
|
||||||
const { dimensions, fitView } = useVueFlow()
|
const { dimensions, fitView } = useVueFlow()
|
||||||
|
|
||||||
function toggleClass() {
|
|
||||||
nodes.value = nodes.value.map((node) => ({ ...node, class: node.class === 'light' ? 'dark' : 'light' }))
|
|
||||||
}
|
|
||||||
|
|
||||||
function updatePos() {
|
function updatePos() {
|
||||||
nodes.value = nodes.value.map((node) => {
|
nodes.value = nodes.value.map((node) => {
|
||||||
return {
|
return {
|
||||||
...node,
|
...node,
|
||||||
position: {
|
position: {
|
||||||
x: Math.random() * 10 * dimensions.value.width,
|
x: Math.random() * dimensions.value.width,
|
||||||
y: Math.random() * 10 * dimensions.value.height,
|
y: Math.random() * dimensions.value.height,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
fitView({ duration: 1000, padding: 0.5 })
|
fitView({ padding: 0.5 })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -42,7 +38,6 @@ function updatePos() {
|
|||||||
|
|
||||||
<Panel position="top-right">
|
<Panel position="top-right">
|
||||||
<button style="margin-right: 5px" @click="updatePos">update positions</button>
|
<button style="margin-right: 5px" @click="updatePos">update positions</button>
|
||||||
<button @click="toggleClass">toggle class</button>
|
|
||||||
</Panel>
|
</Panel>
|
||||||
</VueFlow>
|
</VueFlow>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -205,6 +205,7 @@ export default defineConfigWithTheme<DefaultTheme.Config>({
|
|||||||
{ text: 'Save & Restore', link: '/examples/save' },
|
{ text: 'Save & Restore', link: '/examples/save' },
|
||||||
{ text: 'Drag & Drop', link: '/examples/dnd' },
|
{ text: 'Drag & Drop', link: '/examples/dnd' },
|
||||||
{ text: 'Hide/Show', link: '/examples/hidden' },
|
{ text: 'Hide/Show', link: '/examples/hidden' },
|
||||||
|
{ text: 'Layouting', link: '/examples/layout' },
|
||||||
{ text: 'Interactions', link: '/examples/interaction' },
|
{ text: 'Interactions', link: '/examples/interaction' },
|
||||||
{ text: 'Intersection', link: '/examples/intersection' },
|
{ text: 'Intersection', link: '/examples/intersection' },
|
||||||
{ text: 'Teleport', link: '/examples/teleport' },
|
{ text: 'Teleport', link: '/examples/teleport' },
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Layouting
|
||||||
|
|
||||||
|
A lot of the time, you'll want to layout your nodes automatically instead of assigning their positions manually.
|
||||||
|
Vue Flow does *not* have a built-in layouting system, but it's easy to use a third-party library to achieve this.
|
||||||
|
|
||||||
|
In this example we are going to use [dagre](https://github.com/dagrejs/dagre) to layout our nodes.
|
||||||
|
|
||||||
|
<div class="mt-6">
|
||||||
|
<Repl example="layout"></Repl>
|
||||||
|
</div>
|
||||||
@@ -56,3 +56,10 @@ function onLayout(direction: string) {
|
|||||||
</VueFlow>
|
</VueFlow>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.layoutflow {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user