update(nodes): support fragments as node slots
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { DraggableEventListener, DraggableCore } from '@braks/revue-draggable'
|
import { DraggableEventListener, DraggableCore } from '@braks/revue-draggable/src/index'
|
||||||
import { useVueFlow } from '../../composables'
|
import { useVueFlow } from '../../composables'
|
||||||
import { NodeComponent, SnapGrid } from '../../types'
|
import { SnapGrid } from '../../types'
|
||||||
import { NodeId, Slots } from '../../context'
|
import { NodeId } from '../../context'
|
||||||
import { getXYZPos } from '../../utils'
|
import { getXYZPos } from '../../utils'
|
||||||
|
|
||||||
interface NodeWrapperProps {
|
interface NodeWrapperProps {
|
||||||
id: string
|
id: string
|
||||||
|
name: string
|
||||||
draggable: boolean
|
draggable: boolean
|
||||||
selectable: boolean
|
selectable: boolean
|
||||||
connectable: boolean
|
connectable: boolean
|
||||||
@@ -107,44 +108,11 @@ 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
|
|
||||||
}
|
|
||||||
if (slot.length > 1) {
|
|
||||||
console.warn('[vue-flow]: More than one element in node-slot detected. Using fallback to first slot item.')
|
|
||||||
}
|
|
||||||
|
|
||||||
return slot[0]
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export default {
|
export default {
|
||||||
name: 'Node',
|
name: 'Node',
|
||||||
|
inheritAttrs: false,
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
@@ -164,7 +132,7 @@ export default {
|
|||||||
:key="`node-${node.id}`"
|
:key="`node-${node.id}`"
|
||||||
:class="[
|
:class="[
|
||||||
'vue-flow__node',
|
'vue-flow__node',
|
||||||
`vue-flow__node-${name}`,
|
`vue-flow__node-${props.name}`,
|
||||||
store.noPanClassName,
|
store.noPanClassName,
|
||||||
{
|
{
|
||||||
dragging: node.dragging,
|
dragging: node.dragging,
|
||||||
@@ -187,8 +155,7 @@ export default {
|
|||||||
@click="onSelectNodeHandler"
|
@click="onSelectNodeHandler"
|
||||||
@dblclick="onDoubleClick"
|
@dblclick="onDoubleClick"
|
||||||
>
|
>
|
||||||
<component
|
<slot
|
||||||
:is="type"
|
|
||||||
v-bind="{
|
v-bind="{
|
||||||
nodeElement,
|
nodeElement,
|
||||||
id: node.id,
|
id: node.id,
|
||||||
|
|||||||
@@ -1,8 +1,41 @@
|
|||||||
<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 {
|
||||||
@@ -15,10 +48,15 @@ export default {
|
|||||||
v-for="node of store.getNodes"
|
v-for="node of store.getNodes"
|
||||||
:id="node.id"
|
:id="node.id"
|
||||||
:key="`vue-flow__node-${node.id}`"
|
:key="`vue-flow__node-${node.id}`"
|
||||||
|
:name="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>
|
||||||
|
|||||||
Reference in New Issue
Block a user