fix: typings for UseVueFlow

Signed-off-by: bcakmakoglu <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
bcakmakoglu
2022-02-21 20:25:17 +01:00
committed by Braks
parent f44784b1d0
commit 2731d0bf5e
6 changed files with 18 additions and 12 deletions
+6 -4
View File
@@ -4,9 +4,11 @@ import { VueFlow } from '~/context'
import { useStore } from '~/store' import { useStore } from '~/store'
let id = 0 let id = 0
export default (options?: Partial<FlowOptions>): UseVueFlow => { export default <N = any, E = N>(options?: Partial<FlowOptions<N, E>>): UseVueFlow<N, E> => {
const currentInstance: any = getCurrentInstance() const currentInstance: any = getCurrentInstance()
let vueFlow = currentInstance ? inject(VueFlow) ?? (currentInstance.vueFlow as UseVueFlow) : false let vueFlow: false | UseVueFlow = currentInstance
? inject(VueFlow, undefined) ?? (currentInstance.vueFlow as UseVueFlow)
: false
if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) { if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) {
const name = options?.id ?? `vue-flow-${id++}` const name = options?.id ?? `vue-flow-${id++}`
const store = useStore(options) const store = useStore(options)
@@ -17,12 +19,12 @@ export default (options?: Partial<FlowOptions>): UseVueFlow => {
...store.getters, ...store.getters,
...store.actions, ...store.actions,
...store.hooksOn, ...store.hooksOn,
} } as unknown as UseVueFlow<N, E>
} }
if (currentInstance) { if (currentInstance) {
provide(VueFlow, vueFlow) provide(VueFlow, vueFlow)
currentInstance.vueFlow = vueFlow currentInstance.vueFlow = vueFlow
} }
return vueFlow as UseVueFlow return <UseVueFlow<N, E>>vueFlow
} }
-2
View File
@@ -40,12 +40,10 @@ const {
applyEdgeChanges, applyEdgeChanges,
nodes: storedNodes, nodes: storedNodes,
edges: storedEdges, edges: storedEdges,
state,
} = useVueFlow(props) } = useVueFlow(props)
useHooks(hooks.value, emit) useHooks(hooks.value, emit)
const { modelValue, nodes, edges } = useVModels(props, emit) const { modelValue, nodes, edges } = useVModels(props, emit)
const foo = store.nodes
if (applyDefault.value) { if (applyDefault.value) {
onNodesChange(applyNodeChanges) onNodesChange(applyNodeChanges)
onEdgesChange(applyEdgeChanges) onEdgesChange(applyEdgeChanges)
+5 -1
View File
@@ -49,7 +49,11 @@ export default (
}) })
} }
watch([() => props.maxZoom], (v) => isDef(v) && store.setMaxZoom(v), { immediate: isDef(props.maxZoom) }) watch(
() => props.maxZoom,
(v) => isDef(v) && store.setMaxZoom(v),
{ immediate: isDef(props.maxZoom) },
)
watch( watch(
() => props.minZoom, () => props.minZoom,
(v) => isDef(v) && store.setMinZoom(v), (v) => isDef(v) && store.setMinZoom(v),
+2 -2
View File
@@ -1,5 +1,5 @@
import { InjectionKey } from 'vue' import { InjectionKey } from 'vue'
import { UseVueFlow } from '~/types' import { UseVueFlow } from '~/types'
export const VueFlow: InjectionKey<UseVueFlow> = Symbol('vueFlow') export const VueFlow: InjectionKey<UseVueFlow> = Symbol('vueFlow') as InjectionKey<UseVueFlow>
export const NodeId: InjectionKey<string> = Symbol('nodeId') export const NodeId: InjectionKey<string> = Symbol('nodeId') as InjectionKey<string>
+2 -1
View File
@@ -1,4 +1,5 @@
import { CSSProperties, ToRefs } from 'vue' import { CSSProperties } from 'vue'
import { ToRefs } from '@vue/reactivity'
import { GraphEdge, Edge } from './edge' import { GraphEdge, Edge } from './edge'
import { GraphNode, CoordinateExtent, Node } from './node' import { GraphNode, CoordinateExtent, Node } from './node'
import { ConnectionLineType, ConnectionMode } from './connection' import { ConnectionLineType, ConnectionMode } from './connection'
+3 -2
View File
@@ -1,5 +1,5 @@
import { ComputedRef, ToRefs } from 'vue' import { ComputedRef } from 'vue'
import { UnwrapNestedRefs } from '@vue/reactivity' import { UnwrapNestedRefs, ToRefs } from '@vue/reactivity'
import { Dimensions, Elements, FlowElements, FlowInstance, FlowOptions, Rect, SnapGrid, Transform, XYPosition } from './flow' import { Dimensions, Elements, FlowElements, FlowInstance, FlowOptions, Rect, SnapGrid, Transform, XYPosition } from './flow'
import { HandleType, EdgeComponent, NodeComponent } from './components' import { HandleType, EdgeComponent, NodeComponent } from './components'
import { Connection, ConnectionLineType, ConnectionMode, SetConnectionId } from './connection' import { Connection, ConnectionLineType, ConnectionMode, SetConnectionId } from './connection'
@@ -103,6 +103,7 @@ export interface Getters<N = any, E = N> {
getSelectedNodes: ComputedRef<GraphNode<N>[]> getSelectedNodes: ComputedRef<GraphNode<N>[]>
getSelectedEdges: ComputedRef<GraphEdge<E>[]> getSelectedEdges: ComputedRef<GraphEdge<E>[]>
} }
interface StoreBase<N = any, E = N> { interface StoreBase<N = any, E = N> {
state: State<N, E> state: State<N, E>
actions: Actions<N, E> actions: Actions<N, E>