fix: delete end node (#4372)

This commit is contained in:
zxhlyh
2024-05-14 15:51:08 +08:00
committed by GitHub
parent 08e4103fa1
commit 6f1633fa75
3 changed files with 64 additions and 32 deletions

View File

@@ -488,12 +488,22 @@ export const useNodesInteractions = () => {
_connectedNodeIsSelected: true,
},
}
const nodesConnectedSourceOrTargetHandleIdsMap = getNodesConnectedSourceOrTargetHandleIdsMap(
[
{ type: 'add', edge: newEdge },
],
nodes,
)
const newNodes = produce(nodes, (draft: Node[]) => {
draft.forEach((node) => {
node.data.selected = false
if (node.id === prevNode.id)
node.data._connectedSourceHandleIds?.push(prevNodeSourceHandle!)
if (nodesConnectedSourceOrTargetHandleIdsMap[node.id]) {
node.data = {
...node.data,
...nodesConnectedSourceOrTargetHandleIdsMap[node.id],
}
}
})
draft.push(newNode)
})
@@ -512,26 +522,42 @@ export const useNodesInteractions = () => {
if (!prevNodeId && nextNodeId) {
const nextNodeIndex = nodes.findIndex(node => node.id === nextNodeId)
const nextNode = nodes[nextNodeIndex]!
newNode.data._connectedSourceHandleIds = [sourceHandle]
if ((nodeType !== BlockEnum.IfElse) && (nodeType !== BlockEnum.QuestionClassifier))
newNode.data._connectedSourceHandleIds = [sourceHandle]
newNode.data._connectedTargetHandleIds = []
newNode.position = {
x: nextNode.position.x,
y: nextNode.position.y,
}
const newEdge = {
id: `${newNode.id}-${nextNodeId}`,
type: 'custom',
source: newNode.id,
sourceHandle,
target: nextNodeId,
targetHandle: nextNodeTargetHandle,
data: {
sourceType: newNode.data.type,
targetType: nextNode.data.type,
_connectedNodeIsSelected: true,
},
let newEdge
if ((nodeType !== BlockEnum.IfElse) && (nodeType !== BlockEnum.QuestionClassifier)) {
newEdge = {
id: `${newNode.id}-${nextNodeId}`,
type: 'custom',
source: newNode.id,
sourceHandle,
target: nextNodeId,
targetHandle: nextNodeTargetHandle,
data: {
sourceType: newNode.data.type,
targetType: nextNode.data.type,
_connectedNodeIsSelected: true,
},
}
}
let nodesConnectedSourceOrTargetHandleIdsMap: Record<string, any>
if (newEdge) {
nodesConnectedSourceOrTargetHandleIdsMap = getNodesConnectedSourceOrTargetHandleIdsMap(
[
{ type: 'add', edge: newEdge },
],
nodes,
)
}
const afterNodesInSameBranch = getAfterNodesInSameBranch(nextNodeId!)
const afterNodesInSameBranchIds = afterNodesInSameBranch.map(node => node.id)
const newNodes = produce(nodes, (draft) => {
@@ -541,22 +567,28 @@ export const useNodesInteractions = () => {
if (afterNodesInSameBranchIds.includes(node.id))
node.position.x += NODE_WIDTH_X_OFFSET
if (node.id === nextNodeId)
node.data._connectedTargetHandleIds?.push(nextNodeTargetHandle!)
if (nodesConnectedSourceOrTargetHandleIdsMap?.[node.id]) {
node.data = {
...node.data,
...nodesConnectedSourceOrTargetHandleIdsMap[node.id],
}
}
})
draft.push(newNode)
})
setNodes(newNodes)
const newEdges = produce(edges, (draft) => {
draft.forEach((item) => {
item.data = {
...item.data,
_connectedNodeIsSelected: false,
}
if (newEdge) {
const newEdges = produce(edges, (draft) => {
draft.forEach((item) => {
item.data = {
...item.data,
_connectedNodeIsSelected: false,
}
})
draft.push(newEdge)
})
draft.push(newEdge)
})
setEdges(newEdges)
setEdges(newEdges)
}
}
if (prevNodeId && nextNodeId) {
const prevNode = nodes.find(node => node.id === prevNodeId)!
@@ -771,14 +803,14 @@ export const useNodesInteractions = () => {
} = store.getState()
const nodes = getNodes()
const bundledNodes = nodes.filter(node => node.data._isBundled && node.data.type !== BlockEnum.Start && node.data.type !== BlockEnum.End)
const bundledNodes = nodes.filter(node => node.data._isBundled && node.data.type !== BlockEnum.Start)
if (bundledNodes.length) {
setClipboardElements(bundledNodes)
return
}
const selectedNode = nodes.find(node => node.data.selected && node.data.type !== BlockEnum.Start && node.data.type !== BlockEnum.End)
const selectedNode = nodes.find(node => node.data.selected && node.data.type !== BlockEnum.Start)
if (selectedNode)
setClipboardElements([selectedNode])
@@ -850,7 +882,7 @@ export const useNodesInteractions = () => {
} = store.getState()
const nodes = getNodes()
const selectedNode = nodes.find(node => node.data.selected && node.data.type !== BlockEnum.Start && node.data.type !== BlockEnum.End)
const selectedNode = nodes.find(node => node.data.selected && node.data.type !== BlockEnum.Start)
if (selectedNode) {
const nodeType = selectedNode.data.type