Skip to content

Commit 04658b2

Browse files
committed
fix(core): avoid options passed to useVueFlow overwriting default state
1 parent 3c288e6 commit 04658b2

File tree

4 files changed

+27
-43
lines changed

4 files changed

+27
-43
lines changed

packages/core/src/composables/useVueFlow.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { toRefs, tryOnScopeDispose } from '@vueuse/core'
22
import type { EffectScope } from 'vue'
33
import { computed, effectScope, getCurrentScope, inject, provide, reactive, watch } from 'vue'
4-
import type { EdgeChange, FlowOptions, FlowProps, NodeChange, State, VueFlowStore } from '../types'
4+
import type { EdgeChange, FlowOptions, FlowProps, NodeChange, VueFlowStore } from '../types'
55
import { warn } from '../utils'
66
import { useActions, useGetters, useState } from '../store'
77
import { VueFlow } from '../context'
@@ -35,7 +35,7 @@ export class Storage {
3535
}
3636

3737
public create(id: string, preloadedState?: FlowOptions): VueFlowStore {
38-
const state: State = useState(preloadedState)
38+
const state = useState()
3939

4040
const reactiveState = reactive(state)
4141

@@ -58,7 +58,7 @@ export class Storage {
5858

5959
const actions = useActions(id, reactiveState, getters, nodeIds, edgeIds)
6060

61-
actions.setState(reactiveState)
61+
actions.setState({ ...reactiveState, ...preloadedState })
6262

6363
const flow: VueFlowStore = {
6464
...hooksOn,

packages/core/src/store/actions.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
updateConnectionLookup,
5252
updateEdgeAction,
5353
} from '../utils'
54-
import { useState } from './state'
54+
import { storeOptionsToSkip, useState } from './state'
5555

5656
export function useActions(
5757
id: string,
@@ -345,7 +345,6 @@ export function useActions(
345345

346346
const setNodeExtent: Actions['setNodeExtent'] = (nodeExtent) => {
347347
state.nodeExtent = nodeExtent
348-
349348
updateNodeInternals(nodeIds.value)
350349
}
351350

@@ -742,19 +741,6 @@ export function useActions(
742741
const setState: Actions['setState'] = (options) => {
743742
const opts = options instanceof Function ? options(state) : options
744743

745-
// these options will be set using the appropriate methods
746-
const skip: (keyof typeof opts)[] = [
747-
'modelValue',
748-
'nodes',
749-
'edges',
750-
'maxZoom',
751-
'minZoom',
752-
'translateExtent',
753-
'nodeExtent',
754-
'hooks',
755-
'defaultEdgeOptions',
756-
]
757-
758744
// these options cannot be set after initialization
759745
const exclude: (keyof typeof opts)[] = [
760746
'd3Zoom',
@@ -799,16 +785,13 @@ export function useActions(
799785
if (isDef(opts.translateExtent)) {
800786
setTranslateExtent(opts.translateExtent)
801787
}
802-
if (isDef(opts.nodeExtent)) {
803-
setNodeExtent(opts.nodeExtent)
804-
}
805788
}
806789

807790
for (const o of Object.keys(opts)) {
808791
const key = o as keyof State
809792
const option = opts[key]
810793

811-
if (![...skip, ...exclude].includes(key) && isDef(option)) {
794+
if (![...storeOptionsToSkip, ...exclude].includes(key) && isDef(option)) {
812795
;(<any>state)[key] = option
813796
}
814797
}

packages/core/src/store/state.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
StepEdge,
1111
StraightEdge,
1212
} from '../components'
13-
import { isDef, isMacOs } from '../utils'
13+
import { isMacOs } from '../utils'
1414
import { createHooks } from './hooks'
1515

1616
export const defaultNodeTypes: DefaultNodeTypes = {
@@ -27,7 +27,7 @@ export const defaultEdgeTypes: DefaultEdgeTypes = {
2727
simplebezier: SimpleBezierEdge,
2828
}
2929

30-
function defaultState(): State {
30+
export function useState(): State {
3131
return {
3232
vueFlowRef: null,
3333
viewportRef: null,
@@ -144,17 +144,18 @@ function defaultState(): State {
144144
}
145145
}
146146

147-
export function useState(opts?: FlowOptions): State {
148-
const state = defaultState()
149-
150-
if (opts) {
151-
for (const key of Object.keys(opts)) {
152-
const option = opts[key as keyof typeof opts]
153-
if (isDef(option)) {
154-
;(state as any)[key] = option
155-
}
156-
}
157-
}
158-
159-
return state
160-
}
147+
// these options will be set using the appropriate methods
148+
export const storeOptionsToSkip: (keyof Partial<FlowOptions & Omit<State, 'nodes' | 'edges' | 'modelValue'>>)[] = [
149+
'id',
150+
'vueFlowRef',
151+
'viewportRef',
152+
'initialized',
153+
'modelValue',
154+
'nodes',
155+
'edges',
156+
'maxZoom',
157+
'minZoom',
158+
'translateExtent',
159+
'hooks',
160+
'defaultEdgeOptions',
161+
]

packages/core/src/utils/store.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function createGraphNodes(
8484
) {
8585
const parentNodes: Record<string, true> = {}
8686

87-
const graphNodes = nodes.reduce((nextNodes, node, currentIndex) => {
87+
const nextNodes = nodes.reduce((nextNodes, node, currentIndex) => {
8888
// make sure we don't try to add invalid nodes
8989
if (!isNode(node)) {
9090
triggerError(
@@ -103,10 +103,10 @@ export function createGraphNodes(
103103
return nextNodes.concat(parsed)
104104
}, [] as GraphNode[])
105105

106-
const nextNodes = [...graphNodes, ...currGraphNodes]
106+
const allNodes = [...nextNodes, ...currGraphNodes]
107107

108-
for (const node of graphNodes) {
109-
const parentNode = nextNodes.find((n) => n.id === node.parentNode)
108+
for (const node of nextNodes) {
109+
const parentNode = allNodes.find((n) => n.id === node.parentNode)
110110

111111
if (node.parentNode && !parentNode) {
112112
triggerError(new VueFlowError(ErrorCode.NODE_MISSING_PARENT, node.id, node.parentNode))
@@ -123,7 +123,7 @@ export function createGraphNodes(
123123
}
124124
}
125125

126-
return graphNodes
126+
return nextNodes
127127
}
128128

129129
export function updateConnectionLookup(connectionLookup: ConnectionLookup, edges: Edge[]) {

0 commit comments

Comments
 (0)