fix(nodes,edges): compute type in wrapper

This commit is contained in:
Braks
2022-04-04 21:42:48 +02:00
parent 8e6e85a007
commit 546dd4baaf
4 changed files with 75 additions and 91 deletions
+36 -4
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, GraphEdge, Position } from '../../types' import { ConnectionMode, EdgeComponent, GraphEdge, 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
edge: GraphEdge edge: GraphEdge
selectable?: boolean selectable?: boolean
updatable?: boolean updatable?: boolean
@@ -104,6 +104,37 @@ 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
}
return slot
})
</script> </script>
<script lang="ts"> <script lang="ts">
export default { export default {
@@ -116,7 +147,7 @@ export default {
:key="`edge-${edge.id}`" :key="`edge-${edge.id}`"
:class="[ :class="[
'vue-flow__edge', 'vue-flow__edge',
`vue-flow__edge-${props.name}`, `vue-flow__edge-${name}`,
store.noPanClassName, store.noPanClassName,
{ {
selected: edge.selected, selected: edge.selected,
@@ -133,7 +164,8 @@ export default {
@mousemove="onEdgeMouseMove" @mousemove="onEdgeMouseMove"
@mouseleave="onEdgeMouseLeave" @mouseleave="onEdgeMouseLeave"
> >
<slot <component
:is="type"
v-bind="{ v-bind="{
id: edge.id, id: edge.id,
sourceNode: edge.sourceNode, sourceNode: edge.sourceNode,
+36 -5
View File
@@ -1,13 +1,12 @@
<script lang="ts" setup> <script lang="ts" setup>
import { DraggableEventListener, DraggableCore } from '@braks/revue-draggable' import { DraggableEventListener, DraggableCore } from '@braks/revue-draggable'
import { useVueFlow } from '../../composables' import { useVueFlow } from '../../composables'
import { GraphNode, SnapGrid } from '../../types' import { GraphNode, NodeComponent, SnapGrid } from '../../types'
import { NodeId } from '../../context' import { NodeId, Slots } from '../../context'
import { getXYZPos } from '../../utils' import { getXYZPos } from '../../utils'
interface NodeWrapperProps { interface NodeWrapperProps {
id: string id: string
name: string
node: GraphNode node: GraphNode
draggable: boolean draggable: boolean
selectable: boolean selectable: boolean
@@ -112,6 +111,37 @@ watch(
) )
store.updateNodePosition({ id: node.value.id, diff: { x: 0, y: 0 } }) store.updateNodePosition({ id: node.value.id, diff: { x: 0, y: 0 } })
const slots = inject(Slots)
const name = ref(node.value.type ?? 'default')
watch(
() => node.value.type,
(v) => v && (name.value = v),
)
const type = computed(() => {
const instance = getCurrentInstance()
let nodeType = store.getNodeTypes[name.value]
if (typeof nodeType === 'string') {
if (instance) {
const components = Object.keys(instance.appContext.components)
if (components && components.includes(name.value)) {
nodeType = resolveComponent(name.value, false) as NodeComponent
}
}
}
if (typeof nodeType !== 'string') return nodeType
const slot = slots?.[`node-${name.value}`]
if (!slot?.({})) {
console.warn(`[vueflow]: Node type "${node.value.type}" not found and no node-slot detected. Using fallback type "default".`)
name.value = 'default'
return store.getNodeTypes.default
}
return slot
})
</script> </script>
<script lang="ts"> <script lang="ts">
export default { export default {
@@ -136,7 +166,7 @@ export default {
:key="`node-${node.id}`" :key="`node-${node.id}`"
:class="[ :class="[
'vue-flow__node', 'vue-flow__node',
`vue-flow__node-${props.name}`, `vue-flow__node-${name}`,
store.noPanClassName, store.noPanClassName,
{ {
dragging: node.dragging, dragging: node.dragging,
@@ -159,7 +189,8 @@ export default {
@click="onSelectNodeHandler" @click="onSelectNodeHandler"
@dblclick="onDoubleClick" @dblclick="onDoubleClick"
> >
<slot <component
:is="type"
v-bind="{ v-bind="{
nodeElement, nodeElement,
id: node.id, id: node.id,
@@ -3,44 +3,10 @@ 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,
() => { () => {
@@ -80,17 +46,10 @@ export default {
:id="edge.id" :id="edge.id"
:key="edge.id" :key="edge.id"
:edge="edge" :edge="edge"
:name="typeof edge.type !== 'undefined' && typeof names[edge.type] !== 'undefined' ? 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"> <ConnectionLine v-if="connectionLineVisible && sourceNode" :source-node="sourceNode" />
<component :is="getType(edge.type)" v-bind="props" />
</template>
</EdgeWrapper>
<ConnectionLine v-if="connectionLineVisible && sourceNode" :source-node="sourceNode">
<slot name="connection-line" />
</ConnectionLine>
</g> </g>
</svg> </svg>
</template> </template>
@@ -1,41 +1,8 @@
<script lang="ts" setup> <script lang="ts" setup>
import NodeWrapper from '../../components/Nodes/NodeWrapper.vue' import NodeWrapper from '../../components/Nodes/NodeWrapper.vue'
import { useVueFlow } from '../../composables' import { useVueFlow } from '../../composables'
import { NodeComponent } from '../../types'
import { Slots } from '../../context'
const { store } = useVueFlow() const { store } = useVueFlow()
const slots = inject(Slots)
const names: Record<string, string> = reactive({})
const getType = (name = 'default') => {
const instance = getCurrentInstance()
let nodeType = store.getNodeTypes[name]
if (typeof nodeType === 'string') {
if (instance) {
const components = Object.keys(instance.appContext.components)
if (components && components.includes(name)) {
nodeType = resolveComponent(name, false) as NodeComponent
}
}
}
if (typeof nodeType !== 'string') {
names[name] = name
return nodeType
}
const slot = slots?.[`node-${name}`]
if (!slot?.({})) {
console.warn(`[vueflow]: Node type "${name}" not found and no node-slot detected. Using fallback type "default".`)
names[name] = 'default'
return store.getNodeTypes.default
}
names[name] = name
return slot
}
</script> </script>
<script lang="ts"> <script lang="ts">
export default { export default {
@@ -49,15 +16,10 @@ export default {
:id="node.id" :id="node.id"
:key="`vue-flow__node-${node.id}`" :key="`vue-flow__node-${node.id}`"
:node="node" :node="node"
:name="typeof node.type !== 'undefined' && typeof names[node.type] !== 'undefined' ? names[node.type] : 'default'"
:draggable="typeof node.draggable === 'undefined' ? store.nodesDraggable : !!node.draggable" :draggable="typeof node.draggable === 'undefined' ? store.nodesDraggable : !!node.draggable"
:selectable="typeof node.selectable === 'undefined' ? store.elementsSelectable : !!node.selectable" :selectable="typeof node.selectable === 'undefined' ? store.elementsSelectable : !!node.selectable"
:connectable="typeof node.connectable === 'undefined' ? store.nodesConnectable : !!node.connectable" :connectable="typeof node.connectable === 'undefined' ? store.nodesConnectable : !!node.connectable"
:snap-grid="node.snapGrid ?? (store.snapToGrid ? store.snapGrid : undefined)" :snap-grid="node.snapGrid ?? (store.snapToGrid ? store.snapGrid : undefined)"
> />
<template #default="props">
<component :is="getType(node.type)" v-bind="props" />
</template>
</NodeWrapper>
</div> </div>
</template> </template>