feat(edges): Use slot injection for custom connection lines
This commit is contained in:
@@ -2,12 +2,20 @@
|
|||||||
import { getBezierPath, getSmoothStepPath } from '../Edges/utils'
|
import { getBezierPath, getSmoothStepPath } from '../Edges/utils'
|
||||||
import { ConnectionLineType, GraphNode, HandleElement, Position } from '../../types'
|
import { ConnectionLineType, GraphNode, HandleElement, Position } from '../../types'
|
||||||
import { useVueFlow } from '../../composables'
|
import { useVueFlow } from '../../composables'
|
||||||
|
import { Slots } from '../../context'
|
||||||
|
|
||||||
interface ConnectionLineProps {
|
interface ConnectionLineProps {
|
||||||
sourceNode: GraphNode
|
sourceNode: GraphNode
|
||||||
}
|
}
|
||||||
const props = defineProps<ConnectionLineProps>()
|
const props = defineProps<ConnectionLineProps>()
|
||||||
const { store } = useVueFlow()
|
const { store } = useVueFlow()
|
||||||
|
|
||||||
|
const slots = inject(Slots)?.['connection-line']?.({})
|
||||||
|
const slot = slots?.[0]
|
||||||
|
if (slots && slots.length > 1) {
|
||||||
|
console.warn('[vue-flow]: More than one element in connection-line-slot detected. Using fallback to first slot item.')
|
||||||
|
}
|
||||||
|
|
||||||
const sourceHandle =
|
const sourceHandle =
|
||||||
store.connectionHandleId && store.connectionHandleType
|
store.connectionHandleId && store.connectionHandleType
|
||||||
? props.sourceNode.handleBounds[store.connectionHandleType]?.find((d: HandleElement) => d.id === store.connectionHandleId)
|
? props.sourceNode.handleBounds[store.connectionHandleType]?.find((d: HandleElement) => d.id === store.connectionHandleId)
|
||||||
@@ -68,7 +76,9 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<g class="vue-flow__connection">
|
<g class="vue-flow__connection">
|
||||||
<slot
|
<component
|
||||||
|
:is="slot"
|
||||||
|
v-if="slot"
|
||||||
v-bind="{
|
v-bind="{
|
||||||
sourceX,
|
sourceX,
|
||||||
sourceY,
|
sourceY,
|
||||||
@@ -82,8 +92,7 @@ export default {
|
|||||||
sourceNode: props.sourceNode,
|
sourceNode: props.sourceNode,
|
||||||
sourceHandle,
|
sourceHandle,
|
||||||
}"
|
}"
|
||||||
>
|
/>
|
||||||
<path :d="dAttr" class="vue-flow__connection-path" :style="store.connectionLineStyle || {}" />
|
<path v-else :d="dAttr" class="vue-flow__connection-path" :style="store.connectionLineStyle || {}" />
|
||||||
</slot>
|
|
||||||
</g>
|
</g>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -52,11 +52,7 @@ export default {
|
|||||||
<slot v-if="edge.type" :name="`edge-${edge.type}`" v-bind="edgeProps"></slot>
|
<slot v-if="edge.type" :name="`edge-${edge.type}`" v-bind="edgeProps"></slot>
|
||||||
</template>
|
</template>
|
||||||
</EdgeWrapper>
|
</EdgeWrapper>
|
||||||
<ConnectionLine v-if="connectionLineVisible && sourceNode" :source-node="sourceNode">
|
<ConnectionLine v-if="connectionLineVisible && sourceNode" :source-node="sourceNode" />
|
||||||
<template #default="customConnectionLineProps">
|
|
||||||
<slot name="connection-line" v-bind="customConnectionLineProps"></slot>
|
|
||||||
</template>
|
|
||||||
</ConnectionLine>
|
|
||||||
</g>
|
</g>
|
||||||
</svg>
|
</svg>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -35,8 +35,7 @@ const transform = computed(() => `translate(${store.transform[0]}px,${store.tran
|
|||||||
<template>
|
<template>
|
||||||
<div :key="`transformation-pane-${id}`" class="vue-flow__transformation-pane vue-flow__container" :style="{ transform }">
|
<div :key="`transformation-pane-${id}`" class="vue-flow__transformation-pane vue-flow__container" :style="{ transform }">
|
||||||
<NodeRenderer v-if="store.getNodes.length" :key="`node-renderer-${id}`" />
|
<NodeRenderer v-if="store.getNodes.length" :key="`node-renderer-${id}`" />
|
||||||
<EdgeRenderer :key="`edge-renderer-${id}`">
|
<EdgeRenderer :key="`edge-renderer-${id}`" />
|
||||||
</EdgeRenderer>
|
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user