fix: Flow not properly pre-loading state
* pass key to initFlow to possibly find a pre-existing state Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -55,7 +55,6 @@ const toggleclasss = () => {
|
|||||||
return el
|
return el
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
useVueFlow()
|
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VueFlow
|
<VueFlow
|
||||||
@@ -64,6 +63,7 @@ useVueFlow()
|
|||||||
:default-zoom="1.5"
|
:default-zoom="1.5"
|
||||||
:min-zoom="0.2"
|
:min-zoom="0.2"
|
||||||
:max-zoom="4"
|
:max-zoom="4"
|
||||||
|
:zoom-on-scroll="false"
|
||||||
@elements-remove="onElementsRemove"
|
@elements-remove="onElementsRemove"
|
||||||
@connect="onConnect"
|
@connect="onConnect"
|
||||||
@node-drag-stop="onNodeDragStop"
|
@node-drag-stop="onNodeDragStop"
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ import { Store } from '~/context'
|
|||||||
import { onLoadToObject } from '~/utils'
|
import { onLoadToObject } from '~/utils'
|
||||||
|
|
||||||
let id = 0
|
let id = 0
|
||||||
export default (options?: Partial<FlowOptions>) => {
|
export default (options?: Partial<FlowOptions>, key?: string) => {
|
||||||
let store = inject(Store, null)
|
let store = inject(Store, null)
|
||||||
|
|
||||||
if (!store) {
|
if (!store) {
|
||||||
const withStorage = options?.storageKey ?? false
|
const withStorage = options?.storageKey ?? key ?? false
|
||||||
if (import.meta.env.DEV) console.warn('store context not found; creating default store')
|
if (import.meta.env.DEV) console.warn('store context not found; creating default store')
|
||||||
let storedState = ref<FlowExportObject>()
|
let storedState = ref<FlowExportObject>()
|
||||||
const initial = initialState()
|
const initial = initialState()
|
||||||
const storageKey = typeof options?.storageKey === 'string' ? options.storageKey : options?.id ?? `vue-flow-${id++}`
|
const storageKey = key ?? `vue-flow-${id++}`
|
||||||
const preloadedState = {
|
const preloadedState = {
|
||||||
...initial,
|
...initial,
|
||||||
...options,
|
...options,
|
||||||
@@ -24,6 +24,7 @@ export default (options?: Partial<FlowOptions>) => {
|
|||||||
if (storedState.value.position && storedState.value.zoom)
|
if (storedState.value.position && storedState.value.zoom)
|
||||||
preloadedState.transform = [storedState.value.position[0], storedState.value.position[1], storedState.value.zoom]
|
preloadedState.transform = [storedState.value.position[0], storedState.value.position[1], storedState.value.zoom]
|
||||||
}
|
}
|
||||||
|
console.log(storedState.value)
|
||||||
}
|
}
|
||||||
store = useStateStore(storageKey, preloadedState)()
|
store = useStateStore(storageKey, preloadedState)()
|
||||||
if (withStorage && storageKey === store.$id) {
|
if (withStorage && storageKey === store.$id) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import useStore from './useStore'
|
|||||||
import useHooks from './useHooks'
|
import useHooks from './useHooks'
|
||||||
import { EmitFunc, FlowOptions, FlowStore } from '~/types'
|
import { EmitFunc, FlowOptions, FlowStore } from '~/types'
|
||||||
|
|
||||||
export const initFlow = (emit: EmitFunc, store = useStore()) => {
|
export const initFlow = (emit: EmitFunc, key?: string, store = useStore(undefined, key)) => {
|
||||||
useHooks(store, emit)
|
useHooks(store, emit)
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
NodeTypes,
|
NodeTypes,
|
||||||
EdgeTypes,
|
EdgeTypes,
|
||||||
FlowStore,
|
FlowStore,
|
||||||
|
FlowState,
|
||||||
} from '../../types'
|
} from '../../types'
|
||||||
import ZoomPane from '../../container/ZoomPane/ZoomPane.vue'
|
import ZoomPane from '../../container/ZoomPane/ZoomPane.vue'
|
||||||
import SelectionPane from '../../container/SelectionPane/SelectionPane.vue'
|
import SelectionPane from '../../container/SelectionPane/SelectionPane.vue'
|
||||||
@@ -100,28 +101,28 @@ const props = withDefaults(defineProps<FlowProps>(), {
|
|||||||
paneMoveable: true,
|
paneMoveable: true,
|
||||||
edgeUpdaterRadius: 10,
|
edgeUpdaterRadius: 10,
|
||||||
})
|
})
|
||||||
const store = initFlow(emit, props.store)
|
const store = initFlow(emit, typeof props.storageKey === 'string' ? props.storageKey : props.id, props.store)
|
||||||
const elements = useVModel(props, props.elements.length ? 'elements' : 'modelValue', emit)
|
const elements = useVModel(props, props.elements.length ? 'elements' : 'modelValue', emit)
|
||||||
|
|
||||||
const options = Object.assign({}, props, store.$state)
|
const options = Object.assign({}, props, store.$state, props)
|
||||||
|
|
||||||
// if there are preloaded elements we overwrite the current elements with the stored ones
|
// if there are preloaded elements we overwrite the current elements with the stored ones
|
||||||
if (store.elements.length) elements.value = store.elements
|
if (store.elements.length) elements.value = store.elements
|
||||||
|
|
||||||
const init = (opts: typeof props) => {
|
const init = (state: FlowState) => {
|
||||||
for (const opt of Object.keys(opts)) {
|
for (const opt of Object.keys(state)) {
|
||||||
const val = opts[opt as keyof FlowProps]
|
const val = state[opt as keyof FlowState]
|
||||||
if (val && typeof val !== 'undefined') {
|
if (typeof val !== 'undefined') {
|
||||||
if (typeof val === 'object' && !Array.isArray(val)) {
|
if (typeof val === 'object' && !Array.isArray(val)) {
|
||||||
;(store as any)[opt] = { ...(store.$state as any)[opt], ...val }
|
;(store as any)[opt] = { ...(store as any)[opt], ...val }
|
||||||
} else (store as any)[opt] = val
|
} else (store as any)[opt] = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
store.setElements(elements.value)
|
store.setElements(elements.value)
|
||||||
store.setMinZoom(opts.minZoom)
|
store.setMinZoom(state.minZoom)
|
||||||
store.setMaxZoom(opts.maxZoom)
|
store.setMaxZoom(state.maxZoom)
|
||||||
store.setTranslateExtent(opts.translateExtent)
|
store.setTranslateExtent(state.translateExtent)
|
||||||
store.setNodeExtent(opts.nodeExtent)
|
store.setNodeExtent(state.nodeExtent)
|
||||||
}
|
}
|
||||||
onBeforeUnmount(() => store?.$dispose())
|
onBeforeUnmount(() => store?.$dispose())
|
||||||
|
|
||||||
@@ -142,7 +143,7 @@ onMounted(() => {
|
|||||||
() => props,
|
() => props,
|
||||||
(val, oldVal) => {
|
(val, oldVal) => {
|
||||||
const hasDiff = diff(val, oldVal)
|
const hasDiff = diff(val, oldVal)
|
||||||
if (hasDiff.length > 0) init({ ...options, ...val })
|
if (hasDiff.length > 0) init({ ...store.$state, ...val } as FlowState)
|
||||||
},
|
},
|
||||||
{ flush: 'pre', deep: true },
|
{ flush: 'pre', deep: true },
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user