Skip to content

Commit 3c8182e

Browse files
committed
chore(docs): cleanup basic example and expand comments
1 parent 2637733 commit 3c8182e

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

docs/examples/basic/App.vue

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,104 @@
11
<script setup>
2-
import { VueFlow, isNode, useVueFlow } from '@vue-flow/core'
2+
import { ref } from 'vue'
3+
import { VueFlow, useVueFlow } from '@vue-flow/core'
34
import { Background } from '@vue-flow/background'
45
import { ControlButton, Controls } from '@vue-flow/controls'
56
import { MiniMap } from '@vue-flow/minimap'
6-
import { ref } from 'vue'
7+
import { initialEdges, initialNodes } from './initial-elements.js'
78
import Icon from './Icon.vue'
8-
import { initialElements } from './initial-elements.js'
99
10-
// useVueFlow returns all the event-hooks and methods you need to interact with the graph
10+
/**
11+
* useVueFlow provides all event handlers and store properties
12+
* You can pass the composable an object that has the same properties as the VueFlow component props
13+
*/
1114
const { onPaneReady, onNodeDragStop, onConnect, addEdges, setViewport, toObject } = useVueFlow()
1215
13-
// Elements v-model
14-
const elements = ref(initialElements)
16+
const nodes = ref(initialNodes)
1517
16-
// Dark mode toggle
18+
const edges = ref(initialEdges)
19+
20+
// our dark mode toggle flag
1721
const dark = ref(false)
1822
1923
/**
20-
* Event Hooks
24+
* This is a Vue Flow event-hook which can be listened to from anywhere you call the composable, instead of only on the main component
25+
* Any event that is available as `@event-name` on the VueFlow component is also available as `onEventName` on the composable and vice versa
2126
*
22-
* These are Vue Flow event-hooks which can be used to listen to events happening on the graph.
27+
* onPaneReady is called when viewpane & nodes have visible dimensions
2328
*/
24-
25-
// onPaneReady is called when viewpane is ready (dimensions exist and are not 0)
2629
onPaneReady(({ fitView }) => {
2730
fitView()
2831
})
2932
30-
// onNodeDragStop is called when a node is dragged and then released
31-
onNodeDragStop((e) => console.log('drag stop', e))
33+
/**
34+
* onNodeDragStop is called when a node is done being dragged
35+
*
36+
* Node drag events provide you with:
37+
* 1. the event object
38+
* 2. the nodes array (if multiple nodes are dragged)
39+
* 3. the node that initiated the drag
40+
* 4. any intersections with other nodes
41+
*/
42+
onNodeDragStop(({ event, nodes, node, intersections }) => {
43+
console.log('Node Drag Stop', { event, nodes, node, intersections })
44+
})
3245
3346
/**
3447
* onConnect is called when a new connection is created.
35-
* You can add additional properties to your new edge (like a type or label) or block the creation altogether
48+
*
49+
* You can add additional properties to your new edge (like a type or label) or block the creation altogether by not calling `addEdges`
3650
*/
37-
onConnect((params) => addEdges(params))
51+
onConnect((connection) => {
52+
addEdges(connection)
53+
})
3854
3955
/**
40-
* To update node properties you can simply use your elements v-model and mutate the elements directly
41-
* Changes should always be reflected on the graph reactively, without the need to overwrite the original elements
42-
* Be aware that VueFlow will change your initial `Node[]` and `Edge[]` elements to `GraphNode[]` and `GraphEdge[]`
56+
* To update a node or multiple nodes, you can
57+
* 1. Mutate the node objects *if* you're using `v-model`
58+
* 2. Use the `updateNode` method (from `useVueFlow`) to update the node(s)
59+
* 3. Create a new array of nodes and pass it to the `nodes` ref
4360
*/
4461
function updatePos() {
45-
return elements.value.forEach((el) => {
46-
if (isNode(el)) {
47-
el.position = {
62+
nodes.value = nodes.value.map((node) => {
63+
return {
64+
...node,
65+
position: {
4866
x: Math.random() * 400,
4967
y: Math.random() * 400,
50-
}
68+
},
5169
}
5270
})
5371
}
5472
73+
/**
74+
* toObject transforms your current graph data to an easily persist-able object
75+
*/
5576
function logToObject() {
56-
// `toObject` transforms your current graph data to an easily persist-able object
57-
return console.log(toObject())
77+
console.log(toObject())
5878
}
5979
80+
/**
81+
* Resets the current viewport transformation (zoom & pan)
82+
*/
6083
function resetTransform() {
61-
return setViewport({ x: 0, y: 0, zoom: 1 })
84+
setViewport({ x: 0, y: 0, zoom: 1 })
6285
}
6386
64-
function toggleClass() {
65-
return (dark.value = !dark.value)
87+
function toggleDarkMode() {
88+
dark.value = !dark.value
6689
}
6790
</script>
6891

6992
<template>
70-
<VueFlow v-model="elements" :class="{ dark }" class="basicflow" :default-viewport="{ zoom: 1.5 }" :min-zoom="0.2" :max-zoom="4">
93+
<VueFlow
94+
:nodes="nodes"
95+
:edges="edges"
96+
:class="{ dark }"
97+
class="basicflow"
98+
:default-viewport="{ zoom: 1.5 }"
99+
:min-zoom="0.2"
100+
:max-zoom="4"
101+
>
71102
<Background pattern-color="#aaa" :gap="16" />
72103

73104
<MiniMap />
@@ -81,7 +112,7 @@ function toggleClass() {
81112
<Icon name="update" />
82113
</ControlButton>
83114

84-
<ControlButton title="Toggle Mode" @click="toggleClass">
115+
<ControlButton title="Toggle Dark Mode" @click="toggleDarkMode">
85116
<Icon v-if="dark" name="sun" />
86117
<Icon v-else name="moon" />
87118
</ControlButton>

docs/examples/basic/initial-elements.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { MarkerType } from '@vue-flow/core'
22

3-
/**
4-
* You can pass elements together as a v-model value
5-
* or split them up into nodes and edges and pass them to the `nodes` and `edges` props of Vue Flow (or useVueFlow composable)
6-
*/
7-
export const initialElements = [
3+
export const initialNodes = [
84
{ id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 0 }, class: 'light' },
95
{ id: '2', type: 'output', label: 'Node 2', position: { x: 100, y: 100 }, class: 'light' },
106
{ id: '3', label: 'Node 3', position: { x: 400, y: 100 }, class: 'light' },
117
{ id: '4', label: 'Node 4', position: { x: 150, y: 200 }, class: 'light' },
128
{ id: '5', type: 'output', label: 'Node 5', position: { x: 300, y: 300 }, class: 'light' },
9+
]
10+
11+
export const initialEdges = [
1312
{ id: 'e1-2', source: '1', target: '2', animated: true },
1413
{ id: 'e1-3', label: 'edge with arrowhead', source: '1', target: '3', markerEnd: MarkerType.ArrowClosed },
1514
{

0 commit comments

Comments
 (0)