From d2eb10123b0dccd69b43243d1b1e77e46b6d09d7 Mon Sep 17 00:00:00 2001 From: zhaoying Date: Wed, 14 Jan 2026 20:03:15 +0800 Subject: [PATCH] fix(web): chat variable bugfix --- .../views/Workflow/components/Chat/Chat.tsx | 2 +- .../components/Properties/CaseList/index.tsx | 47 +++++++++---------- .../Properties/VariableEditModal.tsx | 1 + .../Workflow/components/Properties/index.tsx | 4 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/web/src/views/Workflow/components/Chat/Chat.tsx b/web/src/views/Workflow/components/Chat/Chat.tsx index c1047251..246c2e4c 100644 --- a/web/src/views/Workflow/components/Chat/Chat.tsx +++ b/web/src/views/Workflow/components/Chat/Chat.tsx @@ -40,7 +40,7 @@ const Chat = forwardRef(({ appId const curVariables = startNodes[0].config.variables?.defaultValue curVariables.forEach((vo: StartVariableItem) => { - if (vo.default) { + if (typeof vo.default !== 'undefined') { vo.value = vo.default } const lastVo = variables.find(item => item.name === vo.name) diff --git a/web/src/views/Workflow/components/Properties/CaseList/index.tsx b/web/src/views/Workflow/components/Properties/CaseList/index.tsx index 0126b76d..c475034e 100644 --- a/web/src/views/Workflow/components/Properties/CaseList/index.tsx +++ b/web/src/views/Workflow/components/Properties/CaseList/index.tsx @@ -55,6 +55,11 @@ const CaseList: FC = ({ const updateNodePorts = (caseCount: number, removedCaseIndex?: number) => { if (!selectedNode || !graphRef?.current) return; + // 获取当前端口数量来判断是添加还是删除操作 + const currentPorts = selectedNode.getPorts().filter((port: any) => port.group === 'right'); + const currentCaseCount = currentPorts.length - 1; // 减去ELSE端口 + const isAddingCase = removedCaseIndex === undefined && caseCount > currentCaseCount; + // 保存现有连线信息(包括左侧端口连线) const existingEdges = graphRef.current.getEdges().filter((edge: any) => edge.getSourceCellId() === selectedNode.id || edge.getTargetCellId() === selectedNode.id @@ -83,14 +88,10 @@ const CaseList: FC = ({ selectedNode.prop('size', { width: 240, height: newHeight }) - // 计算端口间距 - const dy = totalPorts; - // 添加 IF 端口 selectedNode.addPort({ id: 'CASE1', group: 'right', - // args: { dy }, attrs: { text: { text: 'IF', fontSize: 12, fill: '#5B6167' }} }); @@ -99,7 +100,6 @@ const CaseList: FC = ({ selectedNode.addPort({ id: `CASE${i + 1}`, group: 'right', - // args: { dy }, attrs: { text: { text: 'ELIF', fontSize: 12, fill: '#5B6167' }} }); } @@ -108,22 +108,11 @@ const CaseList: FC = ({ selectedNode.addPort({ id: `CASE${caseCount + 1}`, group: 'right', - // args: { dy }, attrs: { text: { text: 'ELSE', fontSize: 12, fill: '#5B6167' }} }); - // 恢复仍然存在的端口连线 + // 恢复连线 setTimeout(() => { - // 计算删除前的总端口数来确定原ELSE端口编号 - const originalCaseCount = removedCaseIndex !== undefined ? caseCount + 1 : caseCount; - const originalElsePortNumber = originalCaseCount + 1; - - // 检查ELSE端口是否有连线 - const elseHasConnection = edgeConnections.some(({ sourcePortId, isIncoming }: any) => { - const caseNumber = parseInt(sourcePortId.match(/CASE(\d+)/)?.[1] || '0'); - return !isIncoming && caseNumber === originalElsePortNumber; - }); - edgeConnections.forEach(({ edge, sourcePortId, targetCellId, targetPortId, sourceCellId, isIncoming }: any) => { // 如果是进入连线(左侧端口),直接恢复 if (isIncoming) { @@ -151,7 +140,7 @@ const CaseList: FC = ({ // 处理右侧端口连线 const originalCaseNumber = parseInt(sourcePortId.match(/CASE(\d+)/)?.[1] || '0'); - // 如果是被删除的端口,只删除该端口的连线 + // 如果是删除操作且是被删除的端口,删除连线 if (removedCaseIndex !== undefined && originalCaseNumber === removedCaseIndex + 1) { graphRef.current?.removeCell(edge); return; @@ -159,12 +148,22 @@ const CaseList: FC = ({ let newPortId = sourcePortId; - // 如果是原来的ELSE端口且有连线,重新映射到新的ELSE端口 - if (originalCaseNumber === originalElsePortNumber && elseHasConnection) { - newPortId = `CASE${caseCount + 1}`; // 新的ELSE端口 - } else if (removedCaseIndex !== undefined && originalCaseNumber > removedCaseIndex + 1) { - // 如果是被删除端口之后的端口,编号向前移动 - newPortId = `CASE${originalCaseNumber - 1}`; + // 如果是删除操作,需要重新映射端口ID + if (removedCaseIndex !== undefined) { + if (originalCaseNumber > removedCaseIndex + 1) { + // 被删除端口之后的端口,编号向前移动 + newPortId = `CASE${originalCaseNumber - 1}`; + } + // ELSE端口始终映射到新的ELSE端口位置 + else if (originalCaseNumber === currentCaseCount + 1) { + newPortId = `CASE${caseCount + 1}`; + } + } else if (isAddingCase) { + // 如果是添加操作,ELSE端口需要重新映射 + if (originalCaseNumber === currentCaseCount + 1) { + newPortId = `CASE${caseCount + 1}`; // 新的ELSE端口 + } + // 新添加的端口不恢复任何连线 } const newPorts = selectedNode.getPorts(); diff --git a/web/src/views/Workflow/components/Properties/VariableEditModal.tsx b/web/src/views/Workflow/components/Properties/VariableEditModal.tsx index db4a38a3..075cd695 100644 --- a/web/src/views/Workflow/components/Properties/VariableEditModal.tsx +++ b/web/src/views/Workflow/components/Properties/VariableEditModal.tsx @@ -110,6 +110,7 @@ const VariableEditModal = forwardRef form.setFieldValue('default', undefined)} labelRender={(props) =>
{props.label} {variableType[props.value as keyof typeof variableType]}
} optionRender={(props) =>
{props.label} {variableType[props.value as keyof typeof variableType]}
} /> diff --git a/web/src/views/Workflow/components/Properties/index.tsx b/web/src/views/Workflow/components/Properties/index.tsx index 8752121e..77c3414f 100644 --- a/web/src/views/Workflow/components/Properties/index.tsx +++ b/web/src/views/Workflow/components/Properties/index.tsx @@ -242,6 +242,7 @@ const Properties: FC = ({ }, [values, selectedNode, form]) const handleAddVariable = () => { + setEditIndex(null) variableModalRef.current?.handleOpen() } const handleEditVariable = (index: number, vo: StartVariableItem) => { @@ -250,6 +251,7 @@ const Properties: FC = ({ } const handleRefreshVariable = (value: StartVariableItem) => { if (!selectedNode) return + if (editIndex !== null) { const defaultValue = selectedNode.data.config.variables.defaultValue ?? [] defaultValue[editIndex] = value @@ -260,7 +262,7 @@ const Properties: FC = ({ } selectedNode?.setData({ ...selectedNode.data}) - setConfigs({ ...selectedNode.data.config}) + setConfigs({ ...selectedNode.data.config }) } const handleDeleteVariable = (index: number, vo: StartVariableItem) => { if (!selectedNode) return