feat(flow): export applychanges utilities separately

Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
Braks
2021-12-20 19:29:52 +01:00
parent 39501d0bb0
commit 994dd9929c
2 changed files with 12 additions and 8 deletions
+11 -7
View File
@@ -58,14 +58,19 @@ const updateEdge = (edge: GraphEdge, newConnection: Connection, edges: GraphEdge
return edge return edge
} }
const applyNodeChanges = (changes: NodeChange[], nodes: GraphNode[]) => applyChanges(changes, nodes) export const applyEdgeChanges = (changes: EdgeChange[]) => {
const applyEdgeChanges = (changes: EdgeChange[], edges: GraphEdge[]) => applyChanges(changes, edges) const { store } = useVueFlow()
applyChanges(changes, store.edges)
}
export const applyNodeChanges = (changes: NodeChange[]) => {
const { store } = useVueFlow()
applyChanges(changes, store.nodes)
}
export const useEdgesState = ({ edges, applyDefault }: UseEdgesStateOptions = { applyDefault: true }): UseEdgesState => { export const useEdgesState = ({ edges, applyDefault }: UseEdgesStateOptions = { applyDefault: true }): UseEdgesState => {
const { store } = useVueFlow() const { store } = useVueFlow()
const applyEdges = (changes: EdgeChange[]) => applyEdgeChanges(changes, store.edges)
if (edges && edges.length) store.setEdges(edges) if (edges && edges.length) store.setEdges(edges)
if (applyDefault) store.hooksOn.OnEdgesChange((e) => applyEdges(e)) if (applyDefault) store.hooksOn.OnEdgesChange(applyEdgeChanges)
return { return {
edges: store.edges, edges: store.edges,
setEdges: store.setEdges, setEdges: store.setEdges,
@@ -95,10 +100,9 @@ export const useEdgesState = ({ edges, applyDefault }: UseEdgesStateOptions = {
} }
export const useNodesState = ({ nodes, applyDefault }: UseNodesStateOptions = { applyDefault: true }): UseNodesState => { export const useNodesState = ({ nodes, applyDefault }: UseNodesStateOptions = { applyDefault: true }): UseNodesState => {
const { id, store } = useVueFlow() const { store } = useVueFlow()
const applyNodes = (changes: NodeChange[]) => applyNodeChanges(changes, store.nodes)
if (nodes && nodes.length) store.setNodes(nodes) if (nodes && nodes.length) store.setNodes(nodes)
if (applyDefault) store.hooksOn.OnNodesChange((e) => applyNodes(e)) if (applyDefault) store.hooksOn.OnNodesChange(applyNodeChanges)
return { return {
nodes: store.nodes, nodes: store.nodes,
+1 -1
View File
@@ -4,7 +4,7 @@ import { GraphNode, CoordinateExtent, Node } from './node'
import { Connection, ConnectionLineType, ConnectionMode } from './connection' import { Connection, ConnectionLineType, ConnectionMode } from './connection'
import { KeyCode, PanOnScrollMode, UseZoomPanHelper } from './zoom' import { KeyCode, PanOnScrollMode, UseZoomPanHelper } from './zoom'
import { FlowActions, FlowStore } from './store' import { FlowActions, FlowStore } from './store'
import { EdgeChange, FlowHooksOn, NodeChange } from './hooks' import { FlowHooksOn } from './hooks'
export type FlowElement<N = any, E = any> = GraphNode<N> | GraphEdge<E> export type FlowElement<N = any, E = any> = GraphNode<N> | GraphEdge<E>
export type FlowElements<N = any, E = any> = FlowElement<N, E>[] export type FlowElements<N = any, E = any> = FlowElement<N, E>[]