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:
Braks
2021-11-19 13:42:41 +01:00
parent 48da1eccba
commit b91d65fd3a
2 changed files with 10 additions and 9 deletions

View File

@@ -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)

View File

@@ -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]
},