fix(core): avoid options passed to useVueFlow overwriting default state

This commit is contained in:
braks
2024-02-05 07:51:12 +01:00
committed by Braks
parent 3c288e643b
commit 04658b2c0d
4 changed files with 27 additions and 43 deletions
+3 -3
View File
@@ -1,7 +1,7 @@
import { toRefs, tryOnScopeDispose } from '@vueuse/core' import { toRefs, tryOnScopeDispose } from '@vueuse/core'
import type { EffectScope } from 'vue' import type { EffectScope } from 'vue'
import { computed, effectScope, getCurrentScope, inject, provide, reactive, watch } from 'vue' import { computed, effectScope, getCurrentScope, inject, provide, reactive, watch } from 'vue'
import type { EdgeChange, FlowOptions, FlowProps, NodeChange, State, VueFlowStore } from '../types' import type { EdgeChange, FlowOptions, FlowProps, NodeChange, VueFlowStore } from '../types'
import { warn } from '../utils' import { warn } from '../utils'
import { useActions, useGetters, useState } from '../store' import { useActions, useGetters, useState } from '../store'
import { VueFlow } from '../context' import { VueFlow } from '../context'
@@ -35,7 +35,7 @@ export class Storage {
} }
public create(id: string, preloadedState?: FlowOptions): VueFlowStore { public create(id: string, preloadedState?: FlowOptions): VueFlowStore {
const state: State = useState(preloadedState) const state = useState()
const reactiveState = reactive(state) const reactiveState = reactive(state)
@@ -58,7 +58,7 @@ export class Storage {
const actions = useActions(id, reactiveState, getters, nodeIds, edgeIds) const actions = useActions(id, reactiveState, getters, nodeIds, edgeIds)
actions.setState(reactiveState) actions.setState({ ...reactiveState, ...preloadedState })
const flow: VueFlowStore = { const flow: VueFlowStore = {
...hooksOn, ...hooksOn,
+2 -19
View File
@@ -51,7 +51,7 @@ import {
updateConnectionLookup, updateConnectionLookup,
updateEdgeAction, updateEdgeAction,
} from '../utils' } from '../utils'
import { useState } from './state' import { storeOptionsToSkip, useState } from './state'
export function useActions( export function useActions(
id: string, id: string,
@@ -345,7 +345,6 @@ export function useActions(
const setNodeExtent: Actions['setNodeExtent'] = (nodeExtent) => { const setNodeExtent: Actions['setNodeExtent'] = (nodeExtent) => {
state.nodeExtent = nodeExtent state.nodeExtent = nodeExtent
updateNodeInternals(nodeIds.value) updateNodeInternals(nodeIds.value)
} }
@@ -742,19 +741,6 @@ export function useActions(
const setState: Actions['setState'] = (options) => { const setState: Actions['setState'] = (options) => {
const opts = options instanceof Function ? options(state) : options const opts = options instanceof Function ? options(state) : options
// these options will be set using the appropriate methods
const skip: (keyof typeof opts)[] = [
'modelValue',
'nodes',
'edges',
'maxZoom',
'minZoom',
'translateExtent',
'nodeExtent',
'hooks',
'defaultEdgeOptions',
]
// these options cannot be set after initialization // these options cannot be set after initialization
const exclude: (keyof typeof opts)[] = [ const exclude: (keyof typeof opts)[] = [
'd3Zoom', 'd3Zoom',
@@ -799,16 +785,13 @@ export function useActions(
if (isDef(opts.translateExtent)) { if (isDef(opts.translateExtent)) {
setTranslateExtent(opts.translateExtent) setTranslateExtent(opts.translateExtent)
} }
if (isDef(opts.nodeExtent)) {
setNodeExtent(opts.nodeExtent)
}
} }
for (const o of Object.keys(opts)) { for (const o of Object.keys(opts)) {
const key = o as keyof State const key = o as keyof State
const option = opts[key] const option = opts[key]
if (![...skip, ...exclude].includes(key) && isDef(option)) { if (![...storeOptionsToSkip, ...exclude].includes(key) && isDef(option)) {
;(<any>state)[key] = option ;(<any>state)[key] = option
} }
} }
+17 -16
View File
@@ -10,7 +10,7 @@ import {
StepEdge, StepEdge,
StraightEdge, StraightEdge,
} from '../components' } from '../components'
import { isDef, isMacOs } from '../utils' import { isMacOs } from '../utils'
import { createHooks } from './hooks' import { createHooks } from './hooks'
export const defaultNodeTypes: DefaultNodeTypes = { export const defaultNodeTypes: DefaultNodeTypes = {
@@ -27,7 +27,7 @@ export const defaultEdgeTypes: DefaultEdgeTypes = {
simplebezier: SimpleBezierEdge, simplebezier: SimpleBezierEdge,
} }
function defaultState(): State { export function useState(): State {
return { return {
vueFlowRef: null, vueFlowRef: null,
viewportRef: null, viewportRef: null,
@@ -144,17 +144,18 @@ function defaultState(): State {
} }
} }
export function useState(opts?: FlowOptions): State { // these options will be set using the appropriate methods
const state = defaultState() export const storeOptionsToSkip: (keyof Partial<FlowOptions & Omit<State, 'nodes' | 'edges' | 'modelValue'>>)[] = [
'id',
if (opts) { 'vueFlowRef',
for (const key of Object.keys(opts)) { 'viewportRef',
const option = opts[key as keyof typeof opts] 'initialized',
if (isDef(option)) { 'modelValue',
;(state as any)[key] = option 'nodes',
} 'edges',
} 'maxZoom',
} 'minZoom',
'translateExtent',
return state 'hooks',
} 'defaultEdgeOptions',
]
+5 -5
View File
@@ -84,7 +84,7 @@ export function createGraphNodes(
) { ) {
const parentNodes: Record<string, true> = {} const parentNodes: Record<string, true> = {}
const graphNodes = nodes.reduce((nextNodes, node, currentIndex) => { const nextNodes = nodes.reduce((nextNodes, node, currentIndex) => {
// make sure we don't try to add invalid nodes // make sure we don't try to add invalid nodes
if (!isNode(node)) { if (!isNode(node)) {
triggerError( triggerError(
@@ -103,10 +103,10 @@ export function createGraphNodes(
return nextNodes.concat(parsed) return nextNodes.concat(parsed)
}, [] as GraphNode[]) }, [] as GraphNode[])
const nextNodes = [...graphNodes, ...currGraphNodes] const allNodes = [...nextNodes, ...currGraphNodes]
for (const node of graphNodes) { for (const node of nextNodes) {
const parentNode = nextNodes.find((n) => n.id === node.parentNode) const parentNode = allNodes.find((n) => n.id === node.parentNode)
if (node.parentNode && !parentNode) { if (node.parentNode && !parentNode) {
triggerError(new VueFlowError(ErrorCode.NODE_MISSING_PARENT, node.id, node.parentNode)) triggerError(new VueFlowError(ErrorCode.NODE_MISSING_PARENT, node.id, node.parentNode))
@@ -123,7 +123,7 @@ export function createGraphNodes(
} }
} }
return graphNodes return nextNodes
} }
export function updateConnectionLookup(connectionLookup: ConnectionLookup, edges: Edge[]) { export function updateConnectionLookup(connectionLookup: ConnectionLookup, edges: Edge[]) {