refactor(ui): clean code

This commit is contained in:
Fu Diwei
2025-03-06 20:59:40 +08:00
parent 5b2bc6bff9
commit 574ad0445e
5 changed files with 59 additions and 31 deletions

View File

@@ -173,8 +173,13 @@ export type WorkflowNodeIOValueSelector = {
id: string;
name: string;
};
// #endregion
const isBranchLike = (node: WorkflowNode) => {
return node.type === WorkflowNodeType.Branch || node.type === WorkflowNodeType.ExecuteResultBranch;
};
type InitWorkflowOptions = {
template?: "standard";
};
@@ -268,11 +273,15 @@ export const updateNode = (node: WorkflowNode, targetNode: WorkflowNode) => {
Object.assign(current, targetNode);
break;
}
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
current.branches = current.branches!.map((branch) => updateNode(branch, targetNode));
if (isBranchLike(current)) {
current.branches ??= [];
current.branches = current.branches.map((branch) => updateNode(branch, targetNode));
}
current = current.next as WorkflowNode;
}
return draft;
});
};
@@ -281,20 +290,24 @@ export const addNode = (node: WorkflowNode, previousNodeId: string, targetNode:
return produce(node, (draft) => {
let current = draft;
while (current) {
if (current.id === previousNodeId && targetNode.type !== WorkflowNodeType.Branch && targetNode.type !== WorkflowNodeType.ExecuteResultBranch) {
if (current.id === previousNodeId && !isBranchLike(targetNode)) {
targetNode.next = current.next;
current.next = targetNode;
break;
} else if (current.id === previousNodeId && (targetNode.type === WorkflowNodeType.Branch || targetNode.type === WorkflowNodeType.ExecuteResultBranch)) {
} else if (current.id === previousNodeId && isBranchLike(targetNode)) {
targetNode.branches![0].next = current.next;
current.next = targetNode;
break;
}
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
current.branches = current.branches!.map((branch) => addNode(branch, previousNodeId, targetNode));
if (isBranchLike(current)) {
current.branches ??= [];
current.branches = current.branches.map((branch) => addNode(branch, previousNodeId, targetNode));
}
current = current.next as WorkflowNode;
}
return draft;
});
};
@@ -307,18 +320,24 @@ export const addBranch = (node: WorkflowNode, branchNodeId: string) => {
if (current.type !== WorkflowNodeType.Branch) {
return draft;
}
current.branches!.push(
current.branches ??= [];
current.branches.push(
newNode(WorkflowNodeType.Condition, {
branchIndex: current.branches!.length,
branchIndex: current.branches.length,
})
);
break;
}
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
current.branches = current.branches!.map((branch) => addBranch(branch, branchNodeId));
if (isBranchLike(current)) {
current.branches ??= [];
current.branches = current.branches.map((branch) => addBranch(branch, branchNodeId));
}
current = current.next as WorkflowNode;
}
return draft;
});
};
@@ -331,11 +350,15 @@ export const removeNode = (node: WorkflowNode, targetNodeId: string) => {
current.next = current.next.next;
break;
}
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
current.branches = current.branches!.map((branch) => removeNode(branch, targetNodeId));
if (isBranchLike(current)) {
current.branches ??= [];
current.branches = current.branches.map((branch) => removeNode(branch, targetNodeId));
}
current = current.next as WorkflowNode;
}
return draft;
});
};
@@ -351,14 +374,16 @@ export const removeBranch = (node: WorkflowNode, branchNodeId: string, branchInd
};
while (current && last) {
if (current.id === branchNodeId) {
if (current.type !== WorkflowNodeType.Branch && current.type !== WorkflowNodeType.ExecuteResultBranch) {
if (!isBranchLike(current)) {
return draft;
}
current.branches!.splice(branchIndex, 1);
current.branches ??= [];
current.branches.splice(branchIndex, 1);
// 如果仅剩一个分支,删除分支节点,将分支节点的下一个节点挂载到当前节点
if (current.branches!.length === 1) {
const branch = current.branches![0];
if (current.branches.length === 1) {
const branch = current.branches[0];
if (branch.next) {
last.next = branch.next;
let lastNode: WorkflowNode | undefined = branch.next;
@@ -373,19 +398,23 @@ export const removeBranch = (node: WorkflowNode, branchNodeId: string, branchInd
break;
}
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
current.branches = current.branches!.map((branch) => removeBranch(branch, branchNodeId, branchIndex));
if (isBranchLike(current)) {
current.branches ??= [];
current.branches = current.branches.map((branch) => removeBranch(branch, branchNodeId, branchIndex));
}
current = current.next as WorkflowNode;
last = last.next;
}
return draft;
});
};
export const getWorkflowOutputBeforeId = (root: WorkflowNode, nodeId: string, type: string): WorkflowNode[] => {
// 1 个分支的节点,不应该能获取到相邻分支上节点的输出
const output: WorkflowNode[] = [];
export const getOutputBeforeNodeId = (root: WorkflowNode, nodeId: string, type: string): WorkflowNode[] => {
// 个分支的节点,不应该能获取到相邻分支上节点的输出
const outputs: WorkflowNode[] = [];
const traverse = (current: WorkflowNode, output: WorkflowNode[]) => {
if (!current) {
@@ -395,7 +424,7 @@ export const getWorkflowOutputBeforeId = (root: WorkflowNode, nodeId: string, ty
return true;
}
// 如果当前节点是execute_failure,清除execute_result_branch节点前一个节点的输出
// 如果当前节点是 ExecuteFailure清除 ExecuteResultBranch 节点前一个节点的输出
if (current.type === WorkflowNodeType.ExecuteFailure) {
output.splice(output.length - 1);
}
@@ -407,7 +436,7 @@ export const getWorkflowOutputBeforeId = (root: WorkflowNode, nodeId: string, ty
});
}
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
if (isBranchLike(current)) {
const currentLength = output.length;
for (const branch of current.branches!) {
if (traverse(branch, output)) {
@@ -423,14 +452,14 @@ export const getWorkflowOutputBeforeId = (root: WorkflowNode, nodeId: string, ty
return traverse(current.next as WorkflowNode, output);
};
traverse(root, output);
return output;
traverse(root, outputs);
return outputs;
};
export const isAllNodesValidated = (node: WorkflowNode): boolean => {
let current = node as typeof node | undefined;
while (current) {
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
if (isBranchLike(current)) {
for (const branch of current.branches!) {
if (!isAllNodesValidated(branch)) {
return false;