From 91009e1dca90f8022e2cf9d3a9dbf67228bbbf4b Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Mon, 13 Dec 2021 00:40:59 +0100 Subject: [PATCH] feat(store): add store as refs to useVueFlow Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com> --- src/additional-components/MiniMap/MiniMap.vue | 2 + src/composables/useVueFlow.ts | 9 +++-- src/container/VueFlow/VueFlow.vue | 14 +++---- src/store/hooks.ts | 2 +- src/store/state.ts | 3 +- src/store/store.ts | 38 ++++++++++++------- src/types/flow.ts | 5 ++- src/types/store.ts | 2 +- 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/additional-components/MiniMap/MiniMap.vue b/src/additional-components/MiniMap/MiniMap.vue index 80131084..3275e68a 100644 --- a/src/additional-components/MiniMap/MiniMap.vue +++ b/src/additional-components/MiniMap/MiniMap.vue @@ -5,6 +5,8 @@ import { getBoundsofRects, getRectOfNodes } from '../../utils' import type { MiniMapProps } from '../../types/components' import MiniMapNode from './MiniMapNode.vue' +type StringFunc = (node: GraphNode) => string + const props = withDefaults(defineProps(), { nodeStrokeColor: '#555', nodeColor: '#fff', diff --git a/src/composables/useVueFlow.ts b/src/composables/useVueFlow.ts index bdaf7c30..d1aed5e1 100644 --- a/src/composables/useVueFlow.ts +++ b/src/composables/useVueFlow.ts @@ -8,16 +8,19 @@ import { useStore } from '~/store' const applyNodeChanges = (changes: NodeChange[], nodes: GraphNode[]) => applyChanges(changes, nodes) const applyEdgeChanges = (changes: EdgeChange[], edges: GraphEdge[]) => applyChanges(changes, edges) +let id = 0 export default (options?: FlowOptions): UseVueFlow => { const currentInstance = getCurrentInstance() let vueFlow: UseVueFlow | false | undefined = currentInstance ? inject(VueFlow, undefined) : false - if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.store.id)) { - const store = reactive(useStore(options)) + if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) { + const name = options?.id ?? `vue-flow-${id++}` + const store = reactive(useStore(name, options)) const applyNodes = (changes: NodeChange[]) => applyNodeChanges(changes, store.nodes) const applyEdges = (changes: EdgeChange[]) => applyEdgeChanges(changes, store.edges) vueFlow = { - id: store.id, + id: name, store, + ...toRefs(store), useNodesState: (nodes, applyDefault = true) => useNodesState(store, applyNodes)({ nodes, applyDefault }), useEdgesState: (edges, applyDefault = true) => useEdgesState(store, applyEdges)({ edges, applyDefault }), applyNodeChanges: applyNodes, diff --git a/src/container/VueFlow/VueFlow.vue b/src/container/VueFlow/VueFlow.vue index 1a184f0a..8c114497 100644 --- a/src/container/VueFlow/VueFlow.vue +++ b/src/container/VueFlow/VueFlow.vue @@ -23,7 +23,7 @@ const emit = defineEmits([...Object.keys(createHooks()), 'update:modelValue', 'u const { modelValue, nodes, edges } = useVModels(props, emit) const { store } = useVueFlow(props) -useHooks(store, emit) +useHooks(store.hooks, emit) nextTick(() => { modelValue && (modelValue.value = [...store.nodes, ...store.edges]) @@ -33,13 +33,11 @@ nextTick(() => { modelValue && (modelValue.value = [...store.nodes, ...store.edges]) edges && (edges.value = store.edges) }) -onMounted(() => { - watch( - () => props, - (v) => nextTick(() => store.setState(v)), - { flush: 'sync' }, - ) -}) +watch( + () => props, + (v) => nextTick(() => store.setState(v)), + { flush: 'sync', deep: true, immediate: true }, +)