feat(core): add useNodeId composable
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import { computed, inject, ref } from 'vue'
|
import { computed, inject, ref } from 'vue'
|
||||||
import type { CustomEvent, ElementData } from '../types'
|
import type { CustomEvent, ElementData } from '../types'
|
||||||
import { ErrorCode, VueFlowError, getConnectedEdges } from '../utils'
|
import { ErrorCode, VueFlowError, getConnectedEdges } from '../utils'
|
||||||
import { NodeId, NodeRef } from '../context'
|
import { NodeRef } from '../context'
|
||||||
import { useVueFlow } from './useVueFlow'
|
import { useVueFlow } from './useVueFlow'
|
||||||
|
import { useNodeId } from './useNodeId'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Access a node, it's parent (if one exists) and connected edges
|
* Access a node, it's parent (if one exists) and connected edges
|
||||||
@@ -12,7 +13,7 @@ import { useVueFlow } from './useVueFlow'
|
|||||||
* Meaning if you do not provide an id, this composable has to be called in a child of your custom node component, or it will throw
|
* Meaning if you do not provide an id, this composable has to be called in a child of your custom node component, or it will throw
|
||||||
*/
|
*/
|
||||||
export function useNode<Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(id?: string) {
|
export function useNode<Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(id?: string) {
|
||||||
const nodeId = id ?? inject(NodeId, '')
|
const nodeId = id ?? useNodeId()
|
||||||
const nodeEl = inject(NodeRef, ref(null))
|
const nodeEl = inject(NodeRef, ref(null))
|
||||||
|
|
||||||
const { findNode, edges, emits } = useVueFlow()
|
const { findNode, edges, emits } = useVueFlow()
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { inject } from 'vue'
|
||||||
|
import { NodeId } from '../context'
|
||||||
|
|
||||||
|
export function useNodeId() {
|
||||||
|
const nodeId = inject(NodeId, null)
|
||||||
|
|
||||||
|
if (!nodeId) {
|
||||||
|
// todo: emit error through hook
|
||||||
|
throw new Error('useNodeId must be called inside a Node component')
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodeId
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user