From b91d65fd3ac16a6e09bf2cf99e0ef6af1d67fa54 Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Fri, 19 Nov 2021 13:42:41 +0100 Subject: [PATCH] fix: storage not properly preloading * Add elements to store state when using addElements action Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com> --- src/composables/useStore.ts | 18 +++++++++--------- src/store/flowStore.ts | 1 + 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/composables/useStore.ts b/src/composables/useStore.ts index da659a57..b318c42f 100644 --- a/src/composables/useStore.ts +++ b/src/composables/useStore.ts @@ -6,25 +6,25 @@ import { onLoadToObject } from '~/utils' let id = 0 export default (options?: Partial) => { 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() 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) diff --git a/src/store/flowStore.ts b/src/store/flowStore.ts index eddf29c9..11865c2e 100644 --- a/src/store/flowStore.ts +++ b/src/store/flowStore.ts @@ -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] },