Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): update source/target node of updated edge #1705

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-ducks-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---

Update sourceNode and targetNode properties of edge when using `updateEdge`.
25 changes: 24 additions & 1 deletion packages/core/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,30 @@ export function useActions(state: State, nodeLookup: ComputedRef<NodeLookup>, ed
}

const updateEdge: Actions['updateEdge'] = (oldEdge, newConnection, shouldReplaceId = true) => {
return updateEdgeAction(oldEdge, newConnection, state.edges, findEdge, shouldReplaceId, state.hooks.error.trigger)
const prevEdge = findEdge(oldEdge.id)!

const newEdge = updateEdgeAction(oldEdge, newConnection, prevEdge, shouldReplaceId, state.hooks.error.trigger)

if (newEdge) {
const [validEdge] = createGraphEdges(
[newEdge],
state.isValidConnection,
findNode,
findEdge,
state.hooks.error.trigger,
state.defaultEdgeOptions,
state.nodes,
state.edges,
)

state.edges.splice(state.edges.indexOf(prevEdge), 1, validEdge)

updateConnectionLookup(state.connectionLookup, [validEdge])

return validEdge
}

return false
}

const updateEdgeData: Actions['updateEdgeData'] = (id, dataUpdate, options = { replace: false }) => {
Expand Down
13 changes: 3 additions & 10 deletions packages/core/src/utils/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export function addEdgeToStore(
export function updateEdgeAction(
edge: GraphEdge,
newConnection: Connection,
edges: GraphEdge[],
findEdge: Actions['findEdge'],
prevEdge: GraphEdge | undefined,
shouldReplaceId: boolean,
triggerError: State['hooks']['error']['trigger'],
) {
Expand All @@ -66,27 +65,21 @@ export function updateEdgeAction(
return false
}

const foundEdge = findEdge(edge.id)

if (!foundEdge) {
if (!prevEdge) {
triggerError(new VueFlowError(ErrorCode.EDGE_NOT_FOUND, edge.id))
return false
}

const { id, ...rest } = edge

const newEdge = {
return {
...rest,
id: shouldReplaceId ? getEdgeId(newConnection) : id,
source: newConnection.source,
target: newConnection.target,
sourceHandle: newConnection.sourceHandle,
targetHandle: newConnection.targetHandle,
}

edges.splice(edges.indexOf(foundEdge), 1, newEdge)

return newEdge
}

export function createGraphNodes(nodes: Node[], findNode: Actions['findNode'], triggerError: State['hooks']['error']['trigger']) {
Expand Down
Loading