From 3220e20c0e5251136c7364848cde0abaa364ee04 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Sat, 3 Feb 2024 15:14:53 +0100 Subject: [PATCH] docs: add dagre layout example --- docs/components/DocsRepl.vue | 2 +- docs/components/Repl.vue | 2 +- docs/examples/custom-node/App.vue | 28 ++++---- .../custom-node/ColorSelectorNode.vue | 14 ++-- docs/examples/custom-node/OutputNode.vue | 7 -- docs/examples/index.ts | 8 +++ docs/examples/layout/App.vue | 70 +++++++++++++++++++ docs/examples/layout/index.ts | 2 + docs/examples/layout/initial-elements.js | 70 +++++++++++++++++++ docs/examples/stress/App.vue | 11 +-- docs/src/.vitepress/config.mts | 1 + docs/src/examples/layout.md | 10 +++ .../vite/src/Layouting/LayoutingExample.vue | 7 ++ 13 files changed, 192 insertions(+), 40 deletions(-) create mode 100644 docs/examples/layout/App.vue create mode 100644 docs/examples/layout/index.ts create mode 100644 docs/examples/layout/initial-elements.js create mode 100644 docs/src/examples/layout.md diff --git a/docs/components/DocsRepl.vue b/docs/components/DocsRepl.vue index d0167f20..0b0409c3 100644 --- a/docs/components/DocsRepl.vue +++ b/docs/components/DocsRepl.vue @@ -5,7 +5,7 @@ import CodeMirror from '@vue/repl/codemirror-editor' import { useVueFlow } from '@vue-flow/core' import { exampleImports } from '../examples' -const props = defineProps<{ example: keyof typeof exampleImports; mainFile?: string; dependencies?: Record }>() +const props = defineProps<{ example: keyof typeof exampleImports; mainFile?: string }>() const { vueFlowVersion } = useVueFlow() diff --git a/docs/components/Repl.vue b/docs/components/Repl.vue index 35c35889..f0c67c84 100644 --- a/docs/components/Repl.vue +++ b/docs/components/Repl.vue @@ -4,7 +4,7 @@ import { Suspense } from 'vue' const DocsRepl = defineAsyncComponent(() => import('./DocsRepl.vue')) export default defineComponent({ - props: ['example', 'examplesImports', 'dependencies'], + props: ['example', 'mainFile'], setup(props) { return () => { if (typeof navigator === 'undefined') { diff --git a/docs/examples/custom-node/App.vue b/docs/examples/custom-node/App.vue index 78a2034a..819e487e 100644 --- a/docs/examples/custom-node/App.vue +++ b/docs/examples/custom-node/App.vue @@ -1,15 +1,11 @@ diff --git a/docs/examples/custom-node/OutputNode.vue b/docs/examples/custom-node/OutputNode.vue index 74f25791..39ab8275 100644 --- a/docs/examples/custom-node/OutputNode.vue +++ b/docs/examples/custom-node/OutputNode.vue @@ -1,13 +1,6 @@ + + + + diff --git a/docs/examples/layout/index.ts b/docs/examples/layout/index.ts new file mode 100644 index 00000000..dfe861e3 --- /dev/null +++ b/docs/examples/layout/index.ts @@ -0,0 +1,2 @@ +export { default as LayoutApp } from './App.vue?raw' +export { default as LayoutElements } from './initial-elements.js?raw' diff --git a/docs/examples/layout/initial-elements.js b/docs/examples/layout/initial-elements.js new file mode 100644 index 00000000..f73e9dac --- /dev/null +++ b/docs/examples/layout/initial-elements.js @@ -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 }, +] diff --git a/docs/examples/stress/App.vue b/docs/examples/stress/App.vue index 02ccd0d5..727bb684 100644 --- a/docs/examples/stress/App.vue +++ b/docs/examples/stress/App.vue @@ -13,23 +13,19 @@ const edges = ref(initialEdges) const { dimensions, fitView } = useVueFlow() -function toggleClass() { - nodes.value = nodes.value.map((node) => ({ ...node, class: node.class === 'light' ? 'dark' : 'light' })) -} - function updatePos() { nodes.value = nodes.value.map((node) => { return { ...node, position: { - x: Math.random() * 10 * dimensions.value.width, - y: Math.random() * 10 * dimensions.value.height, + x: Math.random() * dimensions.value.width, + y: Math.random() * dimensions.value.height, }, } }) nextTick(() => { - fitView({ duration: 1000, padding: 0.5 }) + fitView({ padding: 0.5 }) }) } @@ -42,7 +38,6 @@ function updatePos() { - diff --git a/docs/src/.vitepress/config.mts b/docs/src/.vitepress/config.mts index 2b643b45..9d9523cc 100644 --- a/docs/src/.vitepress/config.mts +++ b/docs/src/.vitepress/config.mts @@ -205,6 +205,7 @@ export default defineConfigWithTheme({ { text: 'Save & Restore', link: '/examples/save' }, { text: 'Drag & Drop', link: '/examples/dnd' }, { text: 'Hide/Show', link: '/examples/hidden' }, + { text: 'Layouting', link: '/examples/layout' }, { text: 'Interactions', link: '/examples/interaction' }, { text: 'Intersection', link: '/examples/intersection' }, { text: 'Teleport', link: '/examples/teleport' }, diff --git a/docs/src/examples/layout.md b/docs/src/examples/layout.md new file mode 100644 index 00000000..f03e9519 --- /dev/null +++ b/docs/src/examples/layout.md @@ -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. + +
+ +
diff --git a/examples/vite/src/Layouting/LayoutingExample.vue b/examples/vite/src/Layouting/LayoutingExample.vue index b0308ad8..f847e40d 100644 --- a/examples/vite/src/Layouting/LayoutingExample.vue +++ b/examples/vite/src/Layouting/LayoutingExample.vue @@ -56,3 +56,10 @@ function onLayout(direction: string) { + +