chore(examples): update custom node example
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -1,21 +1,21 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { CSSProperties } from 'vue'
|
import type { CSSProperties } from 'vue'
|
||||||
import type { Connection, Edge, NodeProps } from '@vue-flow/core'
|
import type { NodeProps } from '@vue-flow/core'
|
||||||
import { Handle, Position } from '@vue-flow/core'
|
import { Handle, Position } from '@vue-flow/core'
|
||||||
|
|
||||||
interface ColorSelectorNodeProps extends NodeProps {
|
interface Data {
|
||||||
data: {
|
color: string
|
||||||
color: string
|
onChange: (event: InputEvent) => void
|
||||||
onChange: (event: any) => void
|
}
|
||||||
}
|
|
||||||
|
interface ColorSelectorNodeProps extends NodeProps<Data, {}, 'selectorNode'> {
|
||||||
|
data: Data
|
||||||
}
|
}
|
||||||
const props = defineProps<ColorSelectorNodeProps>()
|
const props = defineProps<ColorSelectorNodeProps>()
|
||||||
|
|
||||||
const targetHandleStyle: CSSProperties = { background: '#555' }
|
const targetHandleStyle: CSSProperties = { background: '#555' }
|
||||||
const sourceHandleStyleA: CSSProperties = { ...targetHandleStyle, top: '10px' }
|
const sourceHandleStyleA: CSSProperties = { ...targetHandleStyle, top: '10px' }
|
||||||
const sourceHandleStyleB: CSSProperties = { ...targetHandleStyle, bottom: '10px', top: 'auto' }
|
const sourceHandleStyleB: CSSProperties = { ...targetHandleStyle, bottom: '10px', top: 'auto' }
|
||||||
|
|
||||||
const onConnect = (params: Connection | Edge) => console.log('handle onConnect', params)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
@@ -25,7 +25,7 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Handle type="target" :position="Position.Left" :style="targetHandleStyle" :on-connect="onConnect" />
|
<Handle type="target" :position="Position.Left" :style="targetHandleStyle" />
|
||||||
<div>
|
<div>
|
||||||
Custom Color Picker Node: <strong>{{ data.color }}</strong>
|
Custom Color Picker Node: <strong>{{ data.color }}</strong>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,65 +7,45 @@ import { MiniMap } from '@vue-flow/minimap'
|
|||||||
|
|
||||||
import ColorSelectorNode from './ColorSelectorNode.vue'
|
import ColorSelectorNode from './ColorSelectorNode.vue'
|
||||||
|
|
||||||
const elements = ref<Elements>([])
|
|
||||||
const bgColor = ref('#1A192B')
|
const bgColor = ref('#1A192B')
|
||||||
const connectionLineStyle = { stroke: '#fff' }
|
const connectionLineStyle = { stroke: '#fff' }
|
||||||
const snapGrid: SnapGrid = [16, 16]
|
const snapGrid: SnapGrid = [16, 16]
|
||||||
|
|
||||||
const nodeStroke = (n: Node): string => {
|
const elements = ref<Elements>([
|
||||||
if (n.type === 'input') return '#0041d0'
|
{
|
||||||
if (n.type === 'selectorNode') return bgColor.value
|
id: '1',
|
||||||
if (n.type === 'output') return '#ff0072'
|
type: 'input',
|
||||||
return '#eee'
|
label: 'An input node',
|
||||||
}
|
position: { x: 0, y: 50 },
|
||||||
const nodeColor = (n: Node): string => {
|
sourcePosition: Position.Right,
|
||||||
if (n.type === 'selectorNode') return bgColor.value
|
},
|
||||||
return '#fff'
|
{
|
||||||
}
|
id: '2',
|
||||||
|
type: 'selectorNode',
|
||||||
|
data: { onChange, color: bgColor },
|
||||||
|
style: { border: '1px solid #777', padding: '10px' },
|
||||||
|
parentNode: '1',
|
||||||
|
position: { x: 250, y: 50 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
type: 'output',
|
||||||
|
label: 'Output A',
|
||||||
|
position: { x: 650, y: 25 },
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '4',
|
||||||
|
type: 'output',
|
||||||
|
label: 'Output B',
|
||||||
|
position: { x: 650, y: 100 },
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
},
|
||||||
|
|
||||||
const onChange = (event: Event) => {
|
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } },
|
||||||
elements.value.forEach((e) => {
|
{ id: 'e2a-3', source: '2', sourceHandle: 'a', target: '3', animated: true, style: { stroke: '#fff' } },
|
||||||
if (isEdge(e) || e.id !== '2') return e
|
{ id: 'e2b-4', source: '2', sourceHandle: 'b', target: '4', animated: true, style: { stroke: '#fff' } },
|
||||||
bgColor.value = (event.target as HTMLInputElement).value
|
])
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
elements.value = [
|
|
||||||
{
|
|
||||||
id: '1',
|
|
||||||
type: 'input',
|
|
||||||
label: 'An input node',
|
|
||||||
position: { x: 0, y: 50 },
|
|
||||||
sourcePosition: Position.Right,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '2',
|
|
||||||
type: 'selectorNode',
|
|
||||||
data: { onChange, color: bgColor },
|
|
||||||
style: { border: '1px solid #777', padding: '10px' },
|
|
||||||
position: { x: 250, y: 50 },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '3',
|
|
||||||
type: 'output',
|
|
||||||
label: 'Output A',
|
|
||||||
position: { x: 650, y: 25 },
|
|
||||||
targetPosition: Position.Left,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '4',
|
|
||||||
type: 'output',
|
|
||||||
label: 'Output B',
|
|
||||||
position: { x: 650, y: 100 },
|
|
||||||
targetPosition: Position.Left,
|
|
||||||
},
|
|
||||||
|
|
||||||
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } },
|
|
||||||
{ id: 'e2a-3', source: '2', sourceHandle: 'a', target: '3', animated: true, style: { stroke: '#fff' } },
|
|
||||||
{ id: 'e2b-4', source: '2', sourceHandle: 'b', target: '4', animated: true, style: { stroke: '#fff' } },
|
|
||||||
]
|
|
||||||
})
|
|
||||||
|
|
||||||
useVueFlow({
|
useVueFlow({
|
||||||
connectionMode: ConnectionMode.Loose,
|
connectionMode: ConnectionMode.Loose,
|
||||||
@@ -75,12 +55,30 @@ useVueFlow({
|
|||||||
snapToGrid: true,
|
snapToGrid: true,
|
||||||
snapGrid,
|
snapGrid,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function nodeStroke(n: Node) {
|
||||||
|
if (n.type === 'input') return '#0041d0'
|
||||||
|
if (n.type === 'selectorNode') return bgColor.value
|
||||||
|
if (n.type === 'output') return '#ff0072'
|
||||||
|
return '#eee'
|
||||||
|
}
|
||||||
|
function nodeColor(n: Node) {
|
||||||
|
if (n.type === 'selectorNode') return bgColor.value
|
||||||
|
return '#fff'
|
||||||
|
}
|
||||||
|
|
||||||
|
function onChange(event: InputEvent) {
|
||||||
|
elements.value.forEach((e) => {
|
||||||
|
if (isEdge(e) || e.id !== '2') return e
|
||||||
|
bgColor.value = (event.target as HTMLInputElement).value
|
||||||
|
})
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<VueFlow v-model="elements" fit-view-on-init :style="{ backgroundColor: bgColor }">
|
<VueFlow v-model="elements" fit-view-on-init :style="{ backgroundColor: bgColor }">
|
||||||
<template #node-selectorNode="props">
|
<template #node-selectorNode="props">
|
||||||
<ColorSelectorNode v-bind="props" />
|
<ColorSelectorNode :data="props.data" :parent-node="props.parentNode" :position="props.position" />
|
||||||
</template>
|
</template>
|
||||||
<MiniMap :node-stroke-color="nodeStroke" :node-color="nodeColor" />
|
<MiniMap :node-stroke-color="nodeStroke" :node-color="nodeColor" />
|
||||||
<Controls />
|
<Controls />
|
||||||
|
|||||||
Reference in New Issue
Block a user