chore(examples): update nuxt example
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
</template>
|
||||
|
||||
<style>
|
||||
@import './style.css';
|
||||
@import '@vue-flow/core/dist/style.css';
|
||||
@import '@vue-flow/core/dist/theme-default.css';
|
||||
@import '@vue-flow/controls/dist/style.css';
|
||||
|
||||
html,
|
||||
body,
|
||||
@@ -18,7 +20,5 @@ body,
|
||||
font-family: 'JetBrains Mono', monospace;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
text-align: center;
|
||||
color: #2c3e50;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
<script setup>
|
||||
import { VueFlow, isNode, useVueFlow } from '@vue-flow/core'
|
||||
import { ref } from 'vue'
|
||||
import { initialElements } from './initial-elements.js'
|
||||
|
||||
/**
|
||||
* useVueFlow provides all event handlers and store properties
|
||||
* You can pass the composable an object that has the same properties as the VueFlow component props
|
||||
*/
|
||||
const { onPaneReady, onNodeDragStop, onConnect, instance, addEdges } = useVueFlow()
|
||||
|
||||
/**
|
||||
* Our elements
|
||||
*/
|
||||
const elements = ref(initialElements)
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* onPaneReady is called when viewpane & nodes have visible dimensions
|
||||
*/
|
||||
onPaneReady(({ fitView }) => {
|
||||
fitView()
|
||||
})
|
||||
|
||||
onNodeDragStop((e) => console.log('drag stop', e))
|
||||
|
||||
/**
|
||||
* onConnect is called when a new connection is created.
|
||||
* You can add additional properties to your new edge (like a type or label) or block the creation altogether
|
||||
*/
|
||||
onConnect((params) => addEdges([params]))
|
||||
|
||||
const dark = ref(false)
|
||||
|
||||
/**
|
||||
* To update node properties you can simply use your elements v-model and mutate the elements directly
|
||||
* Changes should always be reflected on the graph reactively, without the need to overwrite the elements
|
||||
*/
|
||||
function updatePos() {
|
||||
return elements.value.forEach((el) => {
|
||||
console.log(el, elements.value)
|
||||
if (isNode(el)) {
|
||||
el.position = {
|
||||
x: Math.random() * 400,
|
||||
y: Math.random() * 400,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* toObject transforms your current graph data to an easily persist-able object
|
||||
*/
|
||||
function logToObject() {
|
||||
return console.log(instance.value?.toObject())
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the current viewpane transformation (zoom & pan)
|
||||
*/
|
||||
function resetTransform() {
|
||||
return instance.value?.setTransform({ x: 0, y: 0, zoom: 1 })
|
||||
}
|
||||
|
||||
function toggleClass() {
|
||||
dark.value = !dark.value
|
||||
elements.value.forEach((el) => (el.class = dark.value ? 'dark' : 'light'))
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<VueFlow v-model="elements" class="basicflow" :default-zoom="1.5" :min-zoom="0.2" :max-zoom="4">
|
||||
<div class="controls">
|
||||
<button style="background-color: #113285; color: white" @click="resetTransform">reset transform</button>
|
||||
<button style="background-color: #6f3381; color: white" @click="updatePos">update positions</button>
|
||||
<button
|
||||
:style="{
|
||||
backgroundColor: dark ? '#FFFFFB' : '#1C1C1C',
|
||||
color: dark ? '#1C1C1C' : '#FFFFFB',
|
||||
}"
|
||||
@click="toggleClass"
|
||||
>
|
||||
toggle {{ dark ? 'light' : 'dark' }}
|
||||
</button>
|
||||
<button @click="logToObject">log toObject</button>
|
||||
</div>
|
||||
</VueFlow>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
.basicflow .vue-flow__node.dark {
|
||||
background: #1c1c1c;
|
||||
color: #fffffb;
|
||||
}
|
||||
|
||||
.basicflow .controls {
|
||||
position: absolute;
|
||||
left: 10px;
|
||||
top: 10px;
|
||||
z-index: 4;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.basicflow .controls button {
|
||||
padding: 5px;
|
||||
border-radius: 5px;
|
||||
font-weight: 500;
|
||||
box-shadow: 0 5px 10px #0000004d;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.basicflow .controls button:hover {
|
||||
opacity: 0.8;
|
||||
transform: scale(105%);
|
||||
transition: 0.25s all ease;
|
||||
}
|
||||
</style>
|
||||
125
examples/nuxt3/components/Flow.vue
Normal file
125
examples/nuxt3/components/Flow.vue
Normal file
@@ -0,0 +1,125 @@
|
||||
<script setup>
|
||||
import { ref } from 'vue'
|
||||
import { VueFlow, useVueFlow } from '@vue-flow/core'
|
||||
import { Background } from '@vue-flow/background'
|
||||
import { ControlButton, Controls } from '@vue-flow/controls'
|
||||
import { MiniMap } from '@vue-flow/minimap'
|
||||
import { initialEdges, initialNodes } from './initial-elements.js'
|
||||
import Icon from './Icon.vue'
|
||||
|
||||
/**
|
||||
* useVueFlow provides all event handlers and store properties
|
||||
* You can pass the composable an object that has the same properties as the VueFlow component props
|
||||
*/
|
||||
const { onPaneReady, onNodeDragStop, onConnect, addEdges, setViewport, toObject } = useVueFlow()
|
||||
|
||||
const nodes = ref(initialNodes)
|
||||
|
||||
const edges = ref(initialEdges)
|
||||
|
||||
// our dark mode toggle flag
|
||||
const dark = ref(false)
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Any event that is available as `@event-name` on the VueFlow component is also available as `onEventName` on the composable and vice versa
|
||||
*
|
||||
* onPaneReady is called when viewpane & nodes have visible dimensions
|
||||
*/
|
||||
onPaneReady(({ fitView }) => {
|
||||
fitView()
|
||||
})
|
||||
|
||||
/**
|
||||
* onNodeDragStop is called when a node is done being dragged
|
||||
*
|
||||
* Node drag events provide you with:
|
||||
* 1. the event object
|
||||
* 2. the nodes array (if multiple nodes are dragged)
|
||||
* 3. the node that initiated the drag
|
||||
* 4. any intersections with other nodes
|
||||
*/
|
||||
onNodeDragStop(({ event, nodes, node, intersections }) => {
|
||||
console.log('Node Drag Stop', { event, nodes, node, intersections })
|
||||
})
|
||||
|
||||
/**
|
||||
* onConnect is called when a new connection is created.
|
||||
*
|
||||
* You can add additional properties to your new edge (like a type or label) or block the creation altogether by not calling `addEdges`
|
||||
*/
|
||||
onConnect((connection) => {
|
||||
addEdges(connection)
|
||||
})
|
||||
|
||||
/**
|
||||
* To update a node or multiple nodes, you can
|
||||
* 1. Mutate the node objects *if* you're using `v-model`
|
||||
* 2. Use the `updateNode` method (from `useVueFlow`) to update the node(s)
|
||||
* 3. Create a new array of nodes and pass it to the `nodes` ref
|
||||
*/
|
||||
function updatePos() {
|
||||
nodes.value = nodes.value.map((node) => {
|
||||
return {
|
||||
...node,
|
||||
position: {
|
||||
x: Math.random() * 400,
|
||||
y: Math.random() * 400,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* toObject transforms your current graph data to an easily persist-able object
|
||||
*/
|
||||
function logToObject() {
|
||||
console.log(toObject())
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the current viewport transformation (zoom & pan)
|
||||
*/
|
||||
function resetTransform() {
|
||||
setViewport({ x: 0, y: 0, zoom: 1 })
|
||||
}
|
||||
|
||||
function toggleDarkMode() {
|
||||
dark.value = !dark.value
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<VueFlow
|
||||
:nodes="nodes"
|
||||
:edges="edges"
|
||||
:class="{ dark }"
|
||||
class="basicflow"
|
||||
:default-viewport="{ zoom: 1.5 }"
|
||||
:min-zoom="0.2"
|
||||
:max-zoom="4"
|
||||
>
|
||||
<Background pattern-color="#aaa" :gap="16" />
|
||||
|
||||
<MiniMap />
|
||||
|
||||
<Controls position="top-right">
|
||||
<ControlButton title="Reset Transform" @click="resetTransform">
|
||||
<Icon name="reset" />
|
||||
</ControlButton>
|
||||
|
||||
<ControlButton title="Shuffle Node Positions" @click="updatePos">
|
||||
<Icon name="update" />
|
||||
</ControlButton>
|
||||
|
||||
<ControlButton title="Toggle Dark Mode" @click="toggleDarkMode">
|
||||
<Icon v-if="dark" name="sun" />
|
||||
<Icon v-else name="moon" />
|
||||
</ControlButton>
|
||||
|
||||
<ControlButton title="Log `toObject`" @click="logToObject">
|
||||
<Icon name="log" />
|
||||
</ControlButton>
|
||||
</Controls>
|
||||
</VueFlow>
|
||||
</template>
|
||||
38
examples/nuxt3/components/Icon.vue
Normal file
38
examples/nuxt3/components/Icon.vue
Normal file
@@ -0,0 +1,38 @@
|
||||
<script setup>
|
||||
defineProps({
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<svg v-if="name === 'reset'" width="16" height="16" viewBox="0 0 32 32">
|
||||
<path d="M18 28A12 12 0 1 0 6 16v6.2l-3.6-3.6L1 20l6 6l6-6l-1.4-1.4L8 22.2V16a10 10 0 1 1 10 10Z" />
|
||||
</svg>
|
||||
|
||||
<svg v-if="name === 'update'" width="16" height="16" viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M14 20v-2h2.6l-3.2-3.2l1.425-1.425L18 16.55V14h2v6Zm-8.6 0L4 18.6L16.6 6H14V4h6v6h-2V7.4Zm3.775-9.425L4 5.4L5.4 4l5.175 5.175Z"
|
||||
/>
|
||||
</svg>
|
||||
|
||||
<svg v-if="name === 'sun'" width="16" height="16" viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M12 17q-2.075 0-3.537-1.463Q7 14.075 7 12t1.463-3.538Q9.925 7 12 7t3.538 1.462Q17 9.925 17 12q0 2.075-1.462 3.537Q14.075 17 12 17ZM2 13q-.425 0-.712-.288Q1 12.425 1 12t.288-.713Q1.575 11 2 11h2q.425 0 .713.287Q5 11.575 5 12t-.287.712Q4.425 13 4 13Zm18 0q-.425 0-.712-.288Q19 12.425 19 12t.288-.713Q19.575 11 20 11h2q.425 0 .712.287q.288.288.288.713t-.288.712Q22.425 13 22 13Zm-8-8q-.425 0-.712-.288Q11 4.425 11 4V2q0-.425.288-.713Q11.575 1 12 1t.713.287Q13 1.575 13 2v2q0 .425-.287.712Q12.425 5 12 5Zm0 18q-.425 0-.712-.288Q11 22.425 11 22v-2q0-.425.288-.712Q11.575 19 12 19t.713.288Q13 19.575 13 20v2q0 .425-.287.712Q12.425 23 12 23ZM5.65 7.05L4.575 6q-.3-.275-.288-.7q.013-.425.288-.725q.3-.3.725-.3t.7.3L7.05 5.65q.275.3.275.7q0 .4-.275.7q-.275.3-.687.287q-.413-.012-.713-.287ZM18 19.425l-1.05-1.075q-.275-.3-.275-.712q0-.413.275-.688q.275-.3.688-.287q.412.012.712.287L19.425 18q.3.275.288.7q-.013.425-.288.725q-.3.3-.725.3t-.7-.3ZM16.95 7.05q-.3-.275-.287-.688q.012-.412.287-.712L18 4.575q.275-.3.7-.288q.425.013.725.288q.3.3.3.725t-.3.7L18.35 7.05q-.3.275-.7.275q-.4 0-.7-.275ZM4.575 19.425q-.3-.3-.3-.725t.3-.7l1.075-1.05q.3-.275.713-.275q.412 0 .687.275q.3.275.288.688q-.013.412-.288.712L6 19.425q-.275.3-.7.287q-.425-.012-.725-.287Z"
|
||||
/>
|
||||
</svg>
|
||||
|
||||
<svg v-if="name === 'moon'" width="16" height="16" viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M12 21q-3.75 0-6.375-2.625T3 12q0-3.75 2.625-6.375T12 3q.35 0 .688.025q.337.025.662.075q-1.025.725-1.637 1.887Q11.1 6.15 11.1 7.5q0 2.25 1.575 3.825Q14.25 12.9 16.5 12.9q1.375 0 2.525-.613q1.15-.612 1.875-1.637q.05.325.075.662Q21 11.65 21 12q0 3.75-2.625 6.375T12 21Z"
|
||||
/>
|
||||
</svg>
|
||||
|
||||
<svg v-if="name === 'log'" width="16" height="16" viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M20 19V7H4v12h16m0-16a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h16m-7 14v-2h5v2h-5m-3.42-4L5.57 9H8.4l3.3 3.3c.39.39.39 1.03 0 1.42L8.42 17H5.59l3.99-4Z"
|
||||
/>
|
||||
</svg>
|
||||
</template>
|
||||
@@ -1,41 +1,16 @@
|
||||
import { MarkerType } from '@vue-flow/core'
|
||||
|
||||
/**
|
||||
* You can pass elements together as a v-model value
|
||||
* or split them up into nodes and edges and pass them to the `nodes` and `edges` props of Vue Flow (or useVueFlow composable)
|
||||
*/
|
||||
export const initialElements = [
|
||||
{
|
||||
id: '1',
|
||||
type: 'input',
|
||||
label: 'Node 1',
|
||||
position: { x: 250, y: 5 },
|
||||
class: 'light',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
type: 'output',
|
||||
label: 'Node 2',
|
||||
position: { x: 100, y: 100 },
|
||||
class: 'light',
|
||||
},
|
||||
export const initialNodes = [
|
||||
{ id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 0 }, class: 'light' },
|
||||
{ id: '2', type: 'output', label: 'Node 2', position: { x: 100, y: 100 }, class: 'light' },
|
||||
{ id: '3', label: 'Node 3', position: { x: 400, y: 100 }, class: 'light' },
|
||||
{ id: '4', label: 'Node 4', position: { x: 150, y: 200 }, class: 'light' },
|
||||
{
|
||||
id: '5',
|
||||
type: 'output',
|
||||
label: 'Node 5',
|
||||
position: { x: 300, y: 300 },
|
||||
class: 'light',
|
||||
},
|
||||
{ id: '5', type: 'output', label: 'Node 5', position: { x: 300, y: 300 }, class: 'light' },
|
||||
]
|
||||
|
||||
export const initialEdges = [
|
||||
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||
{
|
||||
id: 'e1-3',
|
||||
label: 'edge with arrowhead',
|
||||
source: '1',
|
||||
target: '3',
|
||||
markerEnd: MarkerType.Arrow,
|
||||
},
|
||||
{ id: 'e1-3', label: 'edge with arrowhead', source: '1', target: '3', markerEnd: MarkerType.ArrowClosed },
|
||||
{
|
||||
id: 'e4-5',
|
||||
type: 'step',
|
||||
@@ -45,11 +20,5 @@ export const initialElements = [
|
||||
style: { stroke: 'orange' },
|
||||
labelBgStyle: { fill: 'orange' },
|
||||
},
|
||||
{
|
||||
id: 'e3-4',
|
||||
type: 'smoothstep',
|
||||
label: 'smoothstep-edge',
|
||||
source: '3',
|
||||
target: '4',
|
||||
},
|
||||
{ id: 'e3-4', type: 'smoothstep', label: 'smoothstep-edge', source: '3', target: '4' },
|
||||
]
|
||||
|
||||
@@ -14,6 +14,6 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tooling/eslint-config": "workspace:*",
|
||||
"nuxt": "^3.7.4"
|
||||
"nuxt": "^3.10.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div style="height: 100%">
|
||||
<BasicFlow />
|
||||
<Flow />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
48
examples/nuxt3/style.css
Normal file
48
examples/nuxt3/style.css
Normal file
@@ -0,0 +1,48 @@
|
||||
.basicflow.dark {
|
||||
background: #000000;
|
||||
color: #FFFFFB;
|
||||
}
|
||||
|
||||
.basicflow.dark .vue-flow__node {
|
||||
background: hsl(0, 0%, 10%);
|
||||
color: #FFFFFB;
|
||||
}
|
||||
|
||||
.basicflow.dark .vue-flow__node.selected {
|
||||
background: hsl(0, 0%, 20%);
|
||||
border: 1px solid hotpink;
|
||||
}
|
||||
|
||||
.basicflow .vue-flow__controls {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.basicflow.dark .vue-flow__controls {
|
||||
border: 1px solid #FFFFFB;
|
||||
}
|
||||
|
||||
.basicflow .vue-flow__controls .vue-flow__controls-button {
|
||||
border: none;
|
||||
border-right: 1px solid #eee;
|
||||
}
|
||||
|
||||
|
||||
.basicflow.dark .vue-flow__controls .vue-flow__controls-button {
|
||||
background: hsl(0, 0%, 20%);
|
||||
fill: #FFFFFB;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.basicflow.dark .vue-flow__controls .vue-flow__controls-button:hover {
|
||||
background: hsl(0, 0%, 30%);
|
||||
}
|
||||
|
||||
.basicflow.dark .vue-flow__edge-textbg {
|
||||
fill: #292524;
|
||||
}
|
||||
|
||||
.basicflow.dark .vue-flow__edge-text {
|
||||
fill: #FFFFFB;
|
||||
}
|
||||
Reference in New Issue
Block a user