import React, { useState, useEffect } from 'react'; import { Modal, Button, Space, message, Card, Form, Input, Select, InputNumber, } from 'antd'; import { PlusOutlined, SaveOutlined, DeleteOutlined, CheckOutlined, } from '@ant-design/icons'; import intl from 'react-intl-universal'; import { WorkflowGraph, WorkflowNode, NodeType } from './type'; import './workflowEditor.less'; interface WorkflowEditorModalProps { visible: boolean; workflowGraph?: WorkflowGraph; onOk: (graph: WorkflowGraph) => void; onCancel: () => void; } const { TextArea } = Input; const { Option } = Select; const WorkflowEditorModal: React.FC = ({ visible, workflowGraph, onOk, onCancel, }) => { const [localGraph, setLocalGraph] = useState({ nodes: [], startNode: undefined, }); const [selectedNodeId, setSelectedNodeId] = useState(null); const [form] = Form.useForm(); useEffect(() => { if (visible && workflowGraph) { setLocalGraph(workflowGraph); setSelectedNodeId(null); } else if (visible) { setLocalGraph({ nodes: [], startNode: undefined }); setSelectedNodeId(null); } }, [visible, workflowGraph]); useEffect(() => { if (selectedNodeId) { const node = localGraph.nodes.find((n) => n.id === selectedNodeId); if (node) { form.setFieldsValue({ label: node.label, type: node.type, ...node.config, }); } } else { form.resetFields(); } }, [selectedNodeId, localGraph, form]); const addNode = (type: NodeType) => { const newNode: WorkflowNode = { id: `node_${Date.now()}`, type, label: `${intl.get(getNodeTypeName(type))} ${localGraph.nodes.length + 1}`, config: {}, x: 100 + (localGraph.nodes.length % 5) * 150, y: 100 + Math.floor(localGraph.nodes.length / 5) * 100, }; setLocalGraph({ ...localGraph, nodes: [...localGraph.nodes, newNode], }); setSelectedNodeId(newNode.id); message.success(`${intl.get('添加节点')}成功`); }; const getNodeTypeName = (type: NodeType): string => { const typeMap: Record = { http: 'HTTP请求', script: '脚本执行', condition: '条件判断', delay: '延迟', loop: '循环', }; return typeMap[type]; }; const deleteNode = () => { if (!selectedNodeId) { message.warning(intl.get('请选择节点')); return; } Modal.confirm({ title: intl.get('确认删除节点'), content: `${intl.get('确认')}${intl.get('删除节点')}${intl.get('吗')}?`, onOk: () => { setLocalGraph({ ...localGraph, nodes: localGraph.nodes.filter((n) => n.id !== selectedNodeId), }); setSelectedNodeId(null); message.success(`${intl.get('删除')}成功`); }, }); }; const updateNode = () => { if (!selectedNodeId) { return; } form.validateFields().then((values) => { const updatedNodes = localGraph.nodes.map((node) => { if (node.id === selectedNodeId) { return { ...node, label: values.label, type: values.type, config: { ...values, }, }; } return node; }); setLocalGraph({ ...localGraph, nodes: updatedNodes, }); message.success(`${intl.get('保存')}成功`); }); }; const validateWorkflow = () => { if (localGraph.nodes.length === 0) { message.warning(intl.get('工作流至少需要一个节点')); return false; } message.success(intl.get('工作流验证通过')); return true; }; const handleOk = () => { if (validateWorkflow()) { onOk(localGraph); } }; const renderNodeConfig = () => { if (!selectedNodeId) { return (
{intl.get('请选择节点')}
); } const selectedNode = localGraph.nodes.find((n) => n.id === selectedNodeId); if (!selectedNode) return null; return (
{selectedNode.type === 'http' && ( <>