update(edges): support fragments as edge slots

This commit is contained in:
Braks
2022-04-04 21:42:48 +02:00
parent 1347304667
commit 70646c5f95
2 changed files with 45 additions and 40 deletions
+5 -39
View File
@@ -1,12 +1,12 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useHandle, useVueFlow } from '../../composables' import { useHandle, useVueFlow } from '../../composables'
import { ConnectionMode, Position, EdgeComponent } from '../../types' import { ConnectionMode, Position } from '../../types'
import { getEdgePositions, getHandle, getMarkerId } from '../../utils' import { getEdgePositions, getHandle, getMarkerId } from '../../utils'
import { Slots } from '../../context'
import EdgeAnchor from './EdgeAnchor.vue' import EdgeAnchor from './EdgeAnchor.vue'
interface EdgeWrapper { interface EdgeWrapper {
id: string id: string
name: string
selectable?: boolean selectable?: boolean
updatable?: boolean updatable?: boolean
} }
@@ -98,44 +98,11 @@ onMounted(() => {
{ immediate: true, deep: true }, { immediate: true, deep: true },
) )
}) })
const slots = inject(Slots)
const name = ref(edge.value.type ?? 'default')
watch(
() => edge.value.type,
(v) => v && (name.value = v),
)
const type = computed(() => {
const instance = getCurrentInstance()
let edgeType = store.getEdgeTypes[name.value]
if (typeof edgeType === 'string') {
if (instance) {
const components = Object.keys(instance.appContext.components)
if (components && components.includes(name.value)) {
edgeType = resolveComponent(name.value, false) as EdgeComponent
}
}
}
if (typeof edgeType !== 'string') return edgeType
const slot = slots?.[`edge-${name.value}`]?.({})
if (!slot) {
console.warn(`[vueflow]: Edge type "${edge.value.type}" not found and no edge-slot detected. Using fallback type "default".`)
name.value = 'default'
return store.getEdgeTypes.default
}
if (slot.length > 1) {
console.warn('[vue-flow]: More than one element in edge-slot detected. Using fallback to first slot item.')
}
return slot[0]
})
</script> </script>
<script lang="ts"> <script lang="ts">
export default { export default {
name: 'Edge', name: 'Edge',
inheritAttrs: false,
} }
</script> </script>
<template> <template>
@@ -143,7 +110,7 @@ export default {
:key="`edge-${edge.id}`" :key="`edge-${edge.id}`"
:class="[ :class="[
'vue-flow__edge', 'vue-flow__edge',
`vue-flow__edge-${name}`, `vue-flow__edge-${props.name}`,
store.noPanClassName, store.noPanClassName,
{ {
selected: edge.selected, selected: edge.selected,
@@ -160,8 +127,7 @@ export default {
@mousemove="onEdgeMouseMove" @mousemove="onEdgeMouseMove"
@mouseleave="onEdgeMouseLeave" @mouseleave="onEdgeMouseLeave"
> >
<component <slot
:is="type"
v-bind="{ v-bind="{
id: edge.id, id: edge.id,
sourceNode: edge.sourceNode, sourceNode: edge.sourceNode,
@@ -3,10 +3,44 @@ import EdgeWrapper from '../../components/Edges/EdgeWrapper.vue'
import ConnectionLine from '../../components/ConnectionLine/ConnectionLine.vue' import ConnectionLine from '../../components/ConnectionLine/ConnectionLine.vue'
import { useVueFlow } from '../../composables' import { useVueFlow } from '../../composables'
import { groupEdgesByZLevel } from '../../utils' import { groupEdgesByZLevel } from '../../utils'
import { Slots } from '../../context'
import { EdgeComponent } from '../../types'
import MarkerDefinitions from './MarkerDefinitions.vue' import MarkerDefinitions from './MarkerDefinitions.vue'
const { store } = useVueFlow() const { store } = useVueFlow()
const slots = inject(Slots)
const names: Record<string, string> = reactive({})
const getType = (name = 'default') => {
const instance = getCurrentInstance()
let edgeType = store.getEdgeTypes[name]
if (typeof edgeType === 'string') {
if (instance) {
const components = Object.keys(instance.appContext.components)
if (components && components.includes(name)) {
edgeType = resolveComponent(name, false) as EdgeComponent
}
}
}
if (typeof edgeType !== 'string') {
names[name] = name
return edgeType
}
const slot = slots?.[`node-${name}`]
if (!slot?.({})) {
console.warn(`[vueflow]: Edge type "${name}" not found and no edge-slot detected. Using fallback type "default".`)
names[name] = 'default'
return store.getEdgeTypes.default
}
names[name] = name
return slot
}
const sourceNode = controlledComputed( const sourceNode = controlledComputed(
() => store.connectionNodeId, () => store.connectionNodeId,
() => { () => {
@@ -46,9 +80,14 @@ export default {
v-for="edge of group.edges" v-for="edge of group.edges"
:id="edge.id" :id="edge.id"
:key="edge.id" :key="edge.id"
:name="names[edge.type] || 'default'"
:selectable="typeof edge.selectable === 'undefined' ? store.elementsSelectable : edge.selectable" :selectable="typeof edge.selectable === 'undefined' ? store.elementsSelectable : edge.selectable"
:updatable="typeof edge.updatable === 'undefined' ? store.edgesUpdatable : edge.updatable" :updatable="typeof edge.updatable === 'undefined' ? store.edgesUpdatable : edge.updatable"
/> >
<template #default="props">
<component :is="getType(edge.type)" v-bind="props" />
</template>
</EdgeWrapper>
</template> </template>
<ConnectionLine v-if="connectionLineVisible && sourceNode" :source-node="sourceNode"> <ConnectionLine v-if="connectionLineVisible && sourceNode" :source-node="sourceNode">
<slot name="connection-line" /> <slot name="connection-line" />