feat(flow): 添加代码节点编辑功能
- 新增 CodeEditor 组件支持代码编辑与参数配置 - 实现 CodeMirror 组件用于代码高亮与语言切换 - 添加代码节点默认模板与初始化逻辑 - 扩展节点类型支持 CODE 类型并注册对应组件 - 更新本地节点数据配置以支持代码节点参数定义 - 增加节点内容展示组件 nodeContentCode 用于显示代码节点信息 - 优化事件节点内容解析逻辑,增强 JSON 数据判断master
parent
19f7d0cc0d
commit
d8f80e62ea
@ -0,0 +1,30 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { useStore } from '@xyflow/react';
|
||||||
|
import styles from '@/components/FlowEditor/node/style/baseOther.module.less';
|
||||||
|
import DynamicIcon from '@/components/DynamicIcon';
|
||||||
|
import NodeContentCode from '@/pages/flowEditor/components/nodeContentCode';
|
||||||
|
|
||||||
|
const setIcon = () => {
|
||||||
|
return <DynamicIcon type="IconCode" style={{ fontSize: '16px', marginRight: '5px' }} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
const CodeNode = ({ data, id }: { data: any; id: string }) => {
|
||||||
|
const title = data.title || '代码编辑器';
|
||||||
|
|
||||||
|
// 获取节点选中状态 - 适配React Flow v12 API
|
||||||
|
const isSelected = useStore((state) =>
|
||||||
|
state.nodeLookup.get(id)?.selected || false
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={`${styles['node-container']} ${isSelected ? styles.selected : ''}`}>
|
||||||
|
<div className={styles['node-header']} style={{ backgroundColor: '#1890ff' }}>
|
||||||
|
{setIcon()}
|
||||||
|
{title}
|
||||||
|
</div>
|
||||||
|
<NodeContentCode data={data} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CodeNode;
|
||||||
@ -0,0 +1,185 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import styles from '@/components/FlowEditor/node/style/baseOther.module.less';
|
||||||
|
import { Handle, Position, useStore } from '@xyflow/react';
|
||||||
|
import { deserializeValue, isJSON } from '@/utils/common';
|
||||||
|
import cronstrue from 'cronstrue/i18n';
|
||||||
|
|
||||||
|
interface NodeContentData {
|
||||||
|
parameters?: {
|
||||||
|
dataIns?: any[];
|
||||||
|
dataOuts?: any[];
|
||||||
|
apiIns?: any[];
|
||||||
|
apiOuts?: any[];
|
||||||
|
};
|
||||||
|
showFooter?: boolean;
|
||||||
|
type?: string;
|
||||||
|
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义通用的句柄样式
|
||||||
|
const handleStyles = {
|
||||||
|
mainSource: {
|
||||||
|
background: '#2290f6',
|
||||||
|
width: '8px',
|
||||||
|
height: '8px',
|
||||||
|
border: '2px solid #fff',
|
||||||
|
boxShadow: '0 0 4px rgba(0,0,0,0.2)'
|
||||||
|
},
|
||||||
|
mainTarget: {
|
||||||
|
background: '#2290f6',
|
||||||
|
width: '8px',
|
||||||
|
height: '8px',
|
||||||
|
border: '2px solid #fff',
|
||||||
|
boxShadow: '0 0 4px rgba(0,0,0,0.2)'
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
background: '#555',
|
||||||
|
width: '6px',
|
||||||
|
height: '6px',
|
||||||
|
border: '1px solid #fff',
|
||||||
|
boxShadow: '0 0 2px rgba(0,0,0,0.2)'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 渲染普通节点的句柄
|
||||||
|
const renderRegularNodeHandles = (dataIns: any[], dataOuts: any[], apiIns: any[], apiOuts: any[]) => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{apiOuts.map((_, index) => (
|
||||||
|
<Handle
|
||||||
|
key={`api-output-handle-${index}`}
|
||||||
|
type="source"
|
||||||
|
position={Position.Right}
|
||||||
|
id={apiOuts[index].name || apiOuts[index].id || `output-${index}`}
|
||||||
|
style={{
|
||||||
|
...handleStyles.mainSource,
|
||||||
|
top: `${35 + index * 20}px`
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
{apiIns.map((_, index) => (
|
||||||
|
<Handle
|
||||||
|
key={`api-input-handle-${index}`}
|
||||||
|
type="target"
|
||||||
|
position={Position.Left}
|
||||||
|
id={apiIns[index].name || apiIns[index].id || `input-${index}`}
|
||||||
|
style={{
|
||||||
|
...handleStyles.mainTarget,
|
||||||
|
top: `${35 + index * 20}px`
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
|
||||||
|
{/* 输入参数连接端点 */}
|
||||||
|
{dataIns.map((_, index) => (
|
||||||
|
<Handle
|
||||||
|
key={`data-input-handle-${index}`}
|
||||||
|
type="target"
|
||||||
|
position={Position.Left}
|
||||||
|
id={dataIns[index].name || dataIns[index].id || `input-${index}`}
|
||||||
|
style={{
|
||||||
|
...handleStyles.data,
|
||||||
|
top: `${70 + (apiIns.length + index) * 20}px`
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
|
||||||
|
{/* 输出参数连接端点 */}
|
||||||
|
{dataOuts.map((_, index) => (
|
||||||
|
<Handle
|
||||||
|
key={`data-output-handle-${index}`}
|
||||||
|
type="source"
|
||||||
|
position={Position.Right}
|
||||||
|
id={dataOuts[index].name || dataOuts[index].id || `output-${index}`}
|
||||||
|
style={{
|
||||||
|
...handleStyles.data,
|
||||||
|
top: `${70 + (apiOuts.length + index) * 20}px`
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const NodeContent = ({ data }: { data: NodeContentData }) => {
|
||||||
|
const apiIns = data.parameters?.apiIns || [];
|
||||||
|
const apiOuts = data.parameters?.apiOuts || [];
|
||||||
|
const dataIns = data.parameters?.dataIns || [];
|
||||||
|
const dataOuts = data.parameters?.dataOuts || [];
|
||||||
|
const showFooter = data?.component?.customDef || false;
|
||||||
|
|
||||||
|
// 判断节点类型
|
||||||
|
const isStartNode = data.type === 'start';
|
||||||
|
const isEndNode = data.type === 'end';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/*content栏-api部分*/}
|
||||||
|
<div className={styles['node-api-box']}>
|
||||||
|
<div className={styles['node-content-api']}>
|
||||||
|
{apiIns.length > 0 && (
|
||||||
|
<div className={styles['node-inputs']}>
|
||||||
|
{apiIns.map((input, index) => (
|
||||||
|
<div key={input.id || `input-${index}`} className={styles['node-input-label']}>
|
||||||
|
{input.desc}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{apiOuts.length > 0 && (
|
||||||
|
<div className={styles['node-outputs-api']}>
|
||||||
|
{apiOuts.map((output, index) => (
|
||||||
|
<div key={output.id || `output-${index}`} className={styles['node-input-label']}>
|
||||||
|
{output.desc}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{(dataIns.length > 0 || dataOuts.length > 0) && (
|
||||||
|
<>
|
||||||
|
{/*分割*/}
|
||||||
|
<div
|
||||||
|
className={styles['node-split-line']}
|
||||||
|
>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/*content栏-data部分*/}
|
||||||
|
<div className={styles['node-data-box']}>
|
||||||
|
<div className={styles['node-content']}>
|
||||||
|
{dataIns.length > 0 && !isStartNode && (
|
||||||
|
<div className={styles['node-inputs']}>
|
||||||
|
{dataIns.map((input, index) => (
|
||||||
|
<div key={input.id || `input-${index}`} className={styles['node-input-label']}>
|
||||||
|
{input.id || `输入${index + 1}`}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{dataOuts.length > 0 && !isEndNode && (
|
||||||
|
<div className={styles['node-outputs']}>
|
||||||
|
{dataOuts.map((output, index) => (
|
||||||
|
<div key={output.id || `output-${index}`} className={styles['node-input-label']}>
|
||||||
|
{`${output.id} ${output.dataType}` || `输出${index + 1}`}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
|
||||||
|
{/* 根据节点类型渲染不同的句柄 */}
|
||||||
|
{renderRegularNodeHandles(dataIns, dataOuts, apiIns, apiOuts)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default NodeContent;
|
||||||
Loading…
Reference in New Issue