update(examples): Add layouting example
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import dagre from 'dagre'
|
||||||
|
import initialElements from './initial-elements'
|
||||||
|
import './layouting.css'
|
||||||
|
|
||||||
|
import Flow, { Controls, addEdge, Connection, Edge, Elements, isNode, NodeExtent, Position, removeElements } from '~/index'
|
||||||
|
|
||||||
|
const dagreGraph = new dagre.graphlib.Graph()
|
||||||
|
dagreGraph.setDefaultEdgeLabel(() => ({}))
|
||||||
|
|
||||||
|
const nodeExtent: NodeExtent = [
|
||||||
|
[0, 0],
|
||||||
|
[1000, 1000],
|
||||||
|
]
|
||||||
|
|
||||||
|
const elements = ref<Elements>(initialElements)
|
||||||
|
const onConnect = (params: Connection | Edge) => (elements.value = addEdge(params, elements.value))
|
||||||
|
const onElementsRemove = (elementsToRemove: Elements) => (elements.value = removeElements(elementsToRemove, elements.value))
|
||||||
|
|
||||||
|
const onLayout = (direction: string) => {
|
||||||
|
const isHorizontal = direction === 'LR'
|
||||||
|
dagreGraph.setGraph({ rankdir: direction })
|
||||||
|
|
||||||
|
elements.value.forEach((el) => {
|
||||||
|
if (isNode(el)) {
|
||||||
|
dagreGraph.setNode(el.id, { width: 150, height: 50 })
|
||||||
|
} else {
|
||||||
|
dagreGraph.setEdge(el.source, el.target)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
dagre.layout(dagreGraph)
|
||||||
|
|
||||||
|
elements.value = elements.value.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
|
||||||
|
// we need to pass a slighltiy different position in order to notify react flow about the change
|
||||||
|
// @TODO how can we change the position handling so that we dont need this hack?
|
||||||
|
el.position = { x: nodeWithPosition.x + Math.random() / 1000, y: nodeWithPosition.y }
|
||||||
|
}
|
||||||
|
|
||||||
|
return el
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<div class="layoutflow">
|
||||||
|
<Flow
|
||||||
|
:elements="elements"
|
||||||
|
:node-extent="nodeExtent"
|
||||||
|
@connect="onConnect"
|
||||||
|
@clements-remove="onElementsRemove"
|
||||||
|
@load="() => onLayout('TB')"
|
||||||
|
>
|
||||||
|
<Controls />
|
||||||
|
</Flow>
|
||||||
|
<div class="controls">
|
||||||
|
<button :style="{ marginRight: 10 }" @click="() => onLayout('TB')">vertical layout</button>
|
||||||
|
<button @click="() => onLayout('LR')">horizontal layout</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import { Elements, XYPosition } from '~/index'
|
||||||
|
|
||||||
|
const position: XYPosition = { x: 0, y: 0 }
|
||||||
|
|
||||||
|
const elements: Elements = [
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
type: 'input',
|
||||||
|
data: { label: 'input' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2',
|
||||||
|
data: { label: 'node 2' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2a',
|
||||||
|
data: { label: 'node 2a' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2b',
|
||||||
|
data: { label: 'node 2b' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2c',
|
||||||
|
data: { label: 'node 2c' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2d',
|
||||||
|
data: { label: 'node 2d' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
data: { label: 'node 3' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '4',
|
||||||
|
data: { label: 'node 4' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '5',
|
||||||
|
data: { label: 'node 5' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '6',
|
||||||
|
type: 'output',
|
||||||
|
data: { label: 'output' },
|
||||||
|
position,
|
||||||
|
},
|
||||||
|
{ id: '7', type: 'output', data: { label: 'output' }, position: { x: 400, y: 450 } },
|
||||||
|
{ id: 'e12', source: '1', target: '2', type: 'smoothstep' },
|
||||||
|
{ id: 'e13', source: '1', target: '3', type: 'smoothstep' },
|
||||||
|
{ id: 'e22a', source: '2', target: '2a', type: 'smoothstep' },
|
||||||
|
{ id: 'e22b', source: '2', target: '2b', type: 'smoothstep' },
|
||||||
|
{ id: 'e22c', source: '2', target: '2c', type: 'smoothstep' },
|
||||||
|
{ id: 'e2c2d', source: '2c', target: '2d', type: 'smoothstep' },
|
||||||
|
|
||||||
|
{ id: 'e45', source: '4', target: '5', type: 'smoothstep' },
|
||||||
|
{ id: 'e56', source: '5', target: '6', type: 'smoothstep' },
|
||||||
|
{ id: 'e57', source: '5', target: '7', type: 'smoothstep' },
|
||||||
|
] as Elements
|
||||||
|
|
||||||
|
export default elements
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.layoutflow {
|
||||||
|
flex-grow: 1;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.layoutflow .controls {
|
||||||
|
position: absolute;
|
||||||
|
right: 10px;
|
||||||
|
top: 10px;
|
||||||
|
z-index: 10;
|
||||||
|
}
|
||||||
@@ -45,6 +45,10 @@ export const routes: RouterOptions['routes'] = [
|
|||||||
path: '/interaction',
|
path: '/interaction',
|
||||||
component: () => import('./Interaction/InteractionExample.vue'),
|
component: () => import('./Interaction/InteractionExample.vue'),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '/layouting',
|
||||||
|
component: () => import('./Layouting/LayoutingExample.vue'),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
export const router = createRouter({
|
export const router = createRouter({
|
||||||
|
|||||||
@@ -51,8 +51,10 @@
|
|||||||
"@rollup/plugin-commonjs": "^19.0.2",
|
"@rollup/plugin-commonjs": "^19.0.2",
|
||||||
"@rollup/plugin-node-resolve": "^13.0.6",
|
"@rollup/plugin-node-resolve": "^13.0.6",
|
||||||
"@rollup/plugin-replace": "^2.4.2",
|
"@rollup/plugin-replace": "^2.4.2",
|
||||||
|
"@types/dagre": "^0.7.46",
|
||||||
"@vitejs/plugin-vue": "^1.9.3",
|
"@vitejs/plugin-vue": "^1.9.3",
|
||||||
"autoprefixer": "^10.3.7",
|
"autoprefixer": "^10.3.7",
|
||||||
|
"dagre": "^0.8.5",
|
||||||
"esbuild-register": "^2.6.0",
|
"esbuild-register": "^2.6.0",
|
||||||
"eslint": "^7.32.0",
|
"eslint": "^7.32.0",
|
||||||
"eslint-config-prettier": "^8.3.0",
|
"eslint-config-prettier": "^8.3.0",
|
||||||
|
|||||||
Generated
+21
@@ -9,12 +9,14 @@ specifiers:
|
|||||||
'@rollup/plugin-node-resolve': ^13.0.6
|
'@rollup/plugin-node-resolve': ^13.0.6
|
||||||
'@rollup/plugin-replace': ^2.4.2
|
'@rollup/plugin-replace': ^2.4.2
|
||||||
'@types/d3': ^7.1.0
|
'@types/d3': ^7.1.0
|
||||||
|
'@types/dagre': ^0.7.46
|
||||||
'@vitejs/plugin-vue': ^1.9.3
|
'@vitejs/plugin-vue': ^1.9.3
|
||||||
'@vueuse/core': ^6.6.2
|
'@vueuse/core': ^6.6.2
|
||||||
autoprefixer: ^10.3.7
|
autoprefixer: ^10.3.7
|
||||||
d3: ^7.1.1
|
d3: ^7.1.1
|
||||||
d3-selection: ^3.0.0
|
d3-selection: ^3.0.0
|
||||||
d3-zoom: ^3.0.0
|
d3-zoom: ^3.0.0
|
||||||
|
dagre: ^0.8.5
|
||||||
esbuild-register: ^2.6.0
|
esbuild-register: ^2.6.0
|
||||||
eslint: ^7.32.0
|
eslint: ^7.32.0
|
||||||
eslint-config-prettier: ^8.3.0
|
eslint-config-prettier: ^8.3.0
|
||||||
@@ -58,8 +60,10 @@ devDependencies:
|
|||||||
'@rollup/plugin-commonjs': 19.0.2_rollup@2.58.0
|
'@rollup/plugin-commonjs': 19.0.2_rollup@2.58.0
|
||||||
'@rollup/plugin-node-resolve': 13.0.6_rollup@2.58.0
|
'@rollup/plugin-node-resolve': 13.0.6_rollup@2.58.0
|
||||||
'@rollup/plugin-replace': 2.4.2_rollup@2.58.0
|
'@rollup/plugin-replace': 2.4.2_rollup@2.58.0
|
||||||
|
'@types/dagre': 0.7.46
|
||||||
'@vitejs/plugin-vue': 1.9.3_vite@2.6.10
|
'@vitejs/plugin-vue': 1.9.3_vite@2.6.10
|
||||||
autoprefixer: 10.3.7_postcss@8.3.9
|
autoprefixer: 10.3.7_postcss@8.3.9
|
||||||
|
dagre: 0.8.5
|
||||||
esbuild-register: 2.6.0
|
esbuild-register: 2.6.0
|
||||||
eslint: 7.32.0
|
eslint: 7.32.0
|
||||||
eslint-config-prettier: 8.3.0_eslint@7.32.0
|
eslint-config-prettier: 8.3.0_eslint@7.32.0
|
||||||
@@ -813,6 +817,10 @@ packages:
|
|||||||
'@types/d3-zoom': 3.0.1
|
'@types/d3-zoom': 3.0.1
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
/@types/dagre/0.7.46:
|
||||||
|
resolution: {integrity: sha512-ku3y+F8sPqmiB5Ugl22NpukI2dLAViJiWwdtueXLeuF4fxZozl+bytPSFVlLu/gDgaKiwobu3LBXXRWktIMiIA==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
/@types/estree/0.0.39:
|
/@types/estree/0.0.39:
|
||||||
resolution: {integrity: sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==}
|
resolution: {integrity: sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==}
|
||||||
dev: true
|
dev: true
|
||||||
@@ -2099,6 +2107,13 @@ packages:
|
|||||||
d3-zoom: 3.0.0
|
d3-zoom: 3.0.0
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
/dagre/0.8.5:
|
||||||
|
resolution: {integrity: sha512-/aTqmnRta7x7MCCpExk7HQL2O4owCT2h8NT//9I1OQ9vt29Pa0BzSAkR5lwFUcQ7491yVi/3CXU9jQ5o0Mn2Sw==}
|
||||||
|
dependencies:
|
||||||
|
graphlib: 2.1.8
|
||||||
|
lodash: 4.17.21
|
||||||
|
dev: true
|
||||||
|
|
||||||
/date-fns/1.30.1:
|
/date-fns/1.30.1:
|
||||||
resolution: {integrity: sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==}
|
resolution: {integrity: sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==}
|
||||||
dev: true
|
dev: true
|
||||||
@@ -3266,6 +3281,12 @@ packages:
|
|||||||
resolution: {integrity: sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==}
|
resolution: {integrity: sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/graphlib/2.1.8:
|
||||||
|
resolution: {integrity: sha512-jcLLfkpoVGmH7/InMC/1hIvOPSUh38oJtGhvrOFGzioE1DZ+0YW16RgmOJhHiuWTvGiJQ9Z1Ik43JvkRPRvE+A==}
|
||||||
|
dependencies:
|
||||||
|
lodash: 4.17.21
|
||||||
|
dev: true
|
||||||
|
|
||||||
/hard-rejection/2.1.0:
|
/hard-rejection/2.1.0:
|
||||||
resolution: {integrity: sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==}
|
resolution: {integrity: sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==}
|
||||||
engines: {node: '>=6'}
|
engines: {node: '>=6'}
|
||||||
|
|||||||
Reference in New Issue
Block a user