feat(core): add updateEdgeData action (#1452)
* feat(core): add `updateEdgeData` action * chore(changeset): add
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@vue-flow/core": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add `updateEdgeData` action
|
||||||
@@ -558,8 +558,21 @@ export function useActions(
|
|||||||
state.hooks.edgesChange.trigger(changes)
|
state.hooks.edgesChange.trigger(changes)
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateEdge: Actions['updateEdge'] = (oldEdge, newConnection, shouldReplaceId = true) =>
|
const updateEdge: Actions['updateEdge'] = (oldEdge, newConnection, shouldReplaceId = true) => {
|
||||||
updateEdgeAction(oldEdge, newConnection, state.edges, findEdge, shouldReplaceId, state.hooks.error.trigger)
|
return updateEdgeAction(oldEdge, newConnection, state.edges, findEdge, shouldReplaceId, state.hooks.error.trigger)
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateEdgeData: Actions['updateEdgeData'] = (id, dataUpdate, options = { replace: false }) => {
|
||||||
|
const edge = findEdge(id)
|
||||||
|
|
||||||
|
if (!edge) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextData = typeof dataUpdate === 'function' ? dataUpdate(edge) : dataUpdate
|
||||||
|
|
||||||
|
edge.data = options.replace ? nextData : { ...edge.data, ...nextData }
|
||||||
|
}
|
||||||
|
|
||||||
const applyNodeChanges: Actions['applyNodeChanges'] = (changes) => {
|
const applyNodeChanges: Actions['applyNodeChanges'] = (changes) => {
|
||||||
return applyChanges(changes, state.nodes)
|
return applyChanges(changes, state.nodes)
|
||||||
@@ -591,14 +604,15 @@ export function useActions(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updateNodeData: Actions['updateNodeData'] = (id, dataUpdate, options = { replace: false }) => {
|
const updateNodeData: Actions['updateNodeData'] = (id, dataUpdate, options = { replace: false }) => {
|
||||||
updateNode(
|
const node = findNode(id)
|
||||||
id,
|
|
||||||
(node) => {
|
if (!node) {
|
||||||
const nextData = typeof dataUpdate === 'function' ? dataUpdate(node) : dataUpdate
|
return
|
||||||
return options.replace ? { ...node, data: nextData } : { ...node, data: { ...node.data, ...nextData } }
|
}
|
||||||
},
|
|
||||||
options,
|
const nextData = typeof dataUpdate === 'function' ? dataUpdate(node) : dataUpdate
|
||||||
)
|
|
||||||
|
node.data = options.replace ? nextData : { ...node.data, ...nextData }
|
||||||
}
|
}
|
||||||
|
|
||||||
const startConnection: Actions['startConnection'] = (startHandle, position, event, isClick = false) => {
|
const startConnection: Actions['startConnection'] = (startHandle, position, event, isClick = false) => {
|
||||||
@@ -897,6 +911,7 @@ export function useActions(
|
|||||||
findNode,
|
findNode,
|
||||||
findEdge,
|
findEdge,
|
||||||
updateEdge,
|
updateEdge,
|
||||||
|
updateEdgeData,
|
||||||
updateNode,
|
updateNode,
|
||||||
updateNodeData,
|
updateNodeData,
|
||||||
applyEdgeChanges,
|
applyEdgeChanges,
|
||||||
|
|||||||
@@ -181,6 +181,12 @@ export type AddEdges = (
|
|||||||
|
|
||||||
export type UpdateEdge = (oldEdge: GraphEdge, newConnection: Connection, shouldReplaceId?: boolean) => GraphEdge | false
|
export type UpdateEdge = (oldEdge: GraphEdge, newConnection: Connection, shouldReplaceId?: boolean) => GraphEdge | false
|
||||||
|
|
||||||
|
export type UpdateEdgeData = <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
|
||||||
|
id: string,
|
||||||
|
dataUpdate: Partial<Data> | ((edge: GraphEdge<Data, CustomEvents>) => Partial<Data>),
|
||||||
|
options?: { replace: boolean },
|
||||||
|
) => void
|
||||||
|
|
||||||
export type SetState = (
|
export type SetState = (
|
||||||
state:
|
state:
|
||||||
| Partial<FlowOptions & Omit<State, 'nodes' | 'edges' | 'modelValue'>>
|
| Partial<FlowOptions & Omit<State, 'nodes' | 'edges' | 'modelValue'>>
|
||||||
@@ -242,6 +248,8 @@ export interface Actions extends Omit<ViewportHelper, 'viewportInitialized'> {
|
|||||||
findEdge: FindEdge
|
findEdge: FindEdge
|
||||||
/** updates an edge */
|
/** updates an edge */
|
||||||
updateEdge: UpdateEdge
|
updateEdge: UpdateEdge
|
||||||
|
/** updates the data of an edge */
|
||||||
|
updateEdgeData: UpdateEdgeData
|
||||||
/** updates a node */
|
/** updates a node */
|
||||||
updateNode: UpdateNode
|
updateNode: UpdateNode
|
||||||
/** updates the data of a node */
|
/** updates the data of a node */
|
||||||
|
|||||||
Reference in New Issue
Block a user