fix: storage not properly preloading
* Add elements to store state when using addElements action Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -6,25 +6,25 @@ import { onLoadToObject } from '~/utils'
|
||||
let id = 0
|
||||
export default (options?: Partial<FlowOptions>) => {
|
||||
let store = inject(Store)!
|
||||
const withStorage = options?.storageKey ?? false
|
||||
|
||||
if (!store) {
|
||||
const withStorage = options?.storageKey ?? false
|
||||
if (import.meta.env.DEV) console.warn('store context not found; creating default store')
|
||||
let storedState = ref<FlowExportObject>()
|
||||
const initial = initialState()
|
||||
const storageKey = typeof options?.storageKey === 'string' ? options.storageKey : options?.id ?? `vue-flow-${id++}`
|
||||
if (withStorage) {
|
||||
storedState = useStorage(storageKey, {} as FlowExportObject)
|
||||
if (storedState.value) {
|
||||
initial.elements = storedState.value.elements ?? options?.elements ?? []
|
||||
if (storedState.value.position && storedState.value.zoom)
|
||||
initial.transform = [storedState.value.position[0], storedState.value.position[1], storedState.value.zoom]
|
||||
}
|
||||
}
|
||||
const preloadedState = {
|
||||
...initial,
|
||||
...options,
|
||||
}
|
||||
if (withStorage) {
|
||||
storedState = useStorage(storageKey, {} as FlowExportObject)
|
||||
if (storedState.value) {
|
||||
preloadedState.elements = storedState.value.elements ?? options?.elements ?? []
|
||||
if (storedState.value.position && storedState.value.zoom)
|
||||
preloadedState.transform = [storedState.value.position[0], storedState.value.position[1], storedState.value.zoom]
|
||||
}
|
||||
}
|
||||
store = useFlowStore(storageKey, preloadedState)()
|
||||
if (withStorage && storageKey === store.$id) {
|
||||
const toObject = onLoadToObject(store)
|
||||
|
||||
@@ -236,6 +236,7 @@ export default function flowStore(
|
||||
},
|
||||
addElements(elements: Elements) {
|
||||
const { nextNodes, nextEdges } = parseElements(elements, this.nodes, this.edges, this.nodeExtent)
|
||||
this.elements = [...this.elements, ...elements]
|
||||
this.nodes = [...this.nodes, ...nextNodes]
|
||||
this.edges = [...this.edges, ...nextEdges]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user