|
| 1 | +<script setup> |
| 2 | +import { ref } from 'vue' |
| 3 | +import { VueFlow, useVueFlow } from '@vue-flow/core' |
| 4 | +import { Background } from '@vue-flow/background' |
| 5 | +import { ControlButton, Controls } from '@vue-flow/controls' |
| 6 | +import { MiniMap } from '@vue-flow/minimap' |
| 7 | +import { initialEdges, initialNodes } from './initial-elements.js' |
| 8 | +import Icon from './Icon.vue' |
| 9 | +
|
| 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 | + */ |
| 14 | +const { onPaneReady, onNodeDragStop, onConnect, addEdges, setViewport, toObject } = useVueFlow() |
| 15 | +
|
| 16 | +const nodes = ref(initialNodes) |
| 17 | +
|
| 18 | +const edges = ref(initialEdges) |
| 19 | +
|
| 20 | +// our dark mode toggle flag |
| 21 | +const dark = ref(false) |
| 22 | +
|
| 23 | +/** |
| 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 |
| 26 | + * |
| 27 | + * onPaneReady is called when viewpane & nodes have visible dimensions |
| 28 | + */ |
| 29 | +onPaneReady(({ fitView }) => { |
| 30 | + fitView() |
| 31 | +}) |
| 32 | +
|
| 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 | +}) |
| 45 | +
|
| 46 | +/** |
| 47 | + * onConnect is called when a new connection is created. |
| 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` |
| 50 | + */ |
| 51 | +onConnect((connection) => { |
| 52 | + addEdges(connection) |
| 53 | +}) |
| 54 | +
|
| 55 | +/** |
| 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 |
| 60 | + */ |
| 61 | +function updatePos() { |
| 62 | + nodes.value = nodes.value.map((node) => { |
| 63 | + return { |
| 64 | + ...node, |
| 65 | + position: { |
| 66 | + x: Math.random() * 400, |
| 67 | + y: Math.random() * 400, |
| 68 | + }, |
| 69 | + } |
| 70 | + }) |
| 71 | +} |
| 72 | +
|
| 73 | +/** |
| 74 | + * toObject transforms your current graph data to an easily persist-able object |
| 75 | + */ |
| 76 | +function logToObject() { |
| 77 | + console.log(toObject()) |
| 78 | +} |
| 79 | +
|
| 80 | +/** |
| 81 | + * Resets the current viewport transformation (zoom & pan) |
| 82 | + */ |
| 83 | +function resetTransform() { |
| 84 | + setViewport({ x: 0, y: 0, zoom: 1 }) |
| 85 | +} |
| 86 | +
|
| 87 | +function toggleDarkMode() { |
| 88 | + dark.value = !dark.value |
| 89 | +} |
| 90 | +</script> |
| 91 | + |
| 92 | +<template> |
| 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 | + > |
| 102 | + <Background pattern-color="#aaa" :gap="16" /> |
| 103 | + |
| 104 | + <MiniMap /> |
| 105 | + |
| 106 | + <Controls position="top-right"> |
| 107 | + <ControlButton title="Reset Transform" @click="resetTransform"> |
| 108 | + <Icon name="reset" /> |
| 109 | + </ControlButton> |
| 110 | + |
| 111 | + <ControlButton title="Shuffle Node Positions" @click="updatePos"> |
| 112 | + <Icon name="update" /> |
| 113 | + </ControlButton> |
| 114 | + |
| 115 | + <ControlButton title="Toggle Dark Mode" @click="toggleDarkMode"> |
| 116 | + <Icon v-if="dark" name="sun" /> |
| 117 | + <Icon v-else name="moon" /> |
| 118 | + </ControlButton> |
| 119 | + |
| 120 | + <ControlButton title="Log `toObject`" @click="logToObject"> |
| 121 | + <Icon name="log" /> |
| 122 | + </ControlButton> |
| 123 | + </Controls> |
| 124 | + </VueFlow> |
| 125 | +</template> |
0 commit comments