mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? feat: Generate operator names in an auto-incremental manner #1739 ### Type of change - [ ] Bug Fix (non-breaking change which fixes an issue) - [x] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe):
This commit is contained in:
@ -75,6 +75,10 @@ export enum Operator {
|
|||||||
Note = 'Note',
|
Note = 'Note',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const CommonOperatorList = Object.values(Operator).filter(
|
||||||
|
(x) => x !== Operator.Note,
|
||||||
|
);
|
||||||
|
|
||||||
export const operatorIconMap = {
|
export const operatorIconMap = {
|
||||||
[Operator.Retrieval]: RocketOutlined,
|
[Operator.Retrieval]: RocketOutlined,
|
||||||
[Operator.Generate]: MergeCellsOutlined,
|
[Operator.Generate]: MergeCellsOutlined,
|
||||||
|
|||||||
@ -23,7 +23,9 @@ import api from '@/utils/api';
|
|||||||
import { useDebounceEffect } from 'ahooks';
|
import { useDebounceEffect } from 'ahooks';
|
||||||
import { FormInstance, message } from 'antd';
|
import { FormInstance, message } from 'antd';
|
||||||
import { humanId } from 'human-id';
|
import { humanId } from 'human-id';
|
||||||
|
import { lowerFirst } from 'lodash';
|
||||||
import trim from 'lodash/trim';
|
import trim from 'lodash/trim';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import {
|
import {
|
||||||
@ -152,17 +154,68 @@ export const useHandleDrag = () => {
|
|||||||
return { handleDragStart };
|
return { handleDragStart };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const splitName = (name: string) => {
|
||||||
|
const names = name.split('_');
|
||||||
|
const type = names.at(0);
|
||||||
|
const index = Number(names.at(-1));
|
||||||
|
|
||||||
|
return { type, index };
|
||||||
|
};
|
||||||
|
|
||||||
export const useHandleDrop = () => {
|
export const useHandleDrop = () => {
|
||||||
const addNode = useGraphStore((state) => state.addNode);
|
const addNode = useGraphStore((state) => state.addNode);
|
||||||
|
const nodes = useGraphStore((state) => state.nodes);
|
||||||
const [reactFlowInstance, setReactFlowInstance] =
|
const [reactFlowInstance, setReactFlowInstance] =
|
||||||
useState<ReactFlowInstance<any, any>>();
|
useState<ReactFlowInstance<any, any>>();
|
||||||
const initializeOperatorParams = useInitializeOperatorParams();
|
const initializeOperatorParams = useInitializeOperatorParams();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const onDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
|
const onDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.dataTransfer.dropEffect = 'move';
|
event.dataTransfer.dropEffect = 'move';
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const generateNodeName = useCallback(
|
||||||
|
(type: string) => {
|
||||||
|
const name = t(`flow.${lowerFirst(type)}`);
|
||||||
|
const templateNameList = nodes
|
||||||
|
.filter((x) => {
|
||||||
|
const temporaryName = x.data.name;
|
||||||
|
|
||||||
|
const { type, index } = splitName(temporaryName);
|
||||||
|
|
||||||
|
return (
|
||||||
|
temporaryName.match(/_/g)?.length === 1 &&
|
||||||
|
type === name &&
|
||||||
|
!isNaN(index)
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.map((x) => {
|
||||||
|
const temporaryName = x.data.name;
|
||||||
|
const { index } = splitName(temporaryName);
|
||||||
|
|
||||||
|
return {
|
||||||
|
idx: index,
|
||||||
|
name: temporaryName,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.sort((a, b) => a.idx - b.idx);
|
||||||
|
|
||||||
|
let index: number = 0;
|
||||||
|
for (let i = 0; i < templateNameList.length; i++) {
|
||||||
|
const idx = templateNameList[i]?.idx;
|
||||||
|
const nextIdx = templateNameList[i + 1]?.idx;
|
||||||
|
if (idx + 1 !== nextIdx) {
|
||||||
|
index = idx + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${name}_${index}`;
|
||||||
|
},
|
||||||
|
[t, nodes],
|
||||||
|
);
|
||||||
|
|
||||||
const onDrop = useCallback(
|
const onDrop = useCallback(
|
||||||
(event: React.DragEvent<HTMLDivElement>) => {
|
(event: React.DragEvent<HTMLDivElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@ -190,7 +243,7 @@ export const useHandleDrop = () => {
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
label: `${type}`,
|
label: `${type}`,
|
||||||
name: humanId(),
|
name: generateNodeName(type),
|
||||||
form: initializeOperatorParams(type as Operator),
|
form: initializeOperatorParams(type as Operator),
|
||||||
},
|
},
|
||||||
sourcePosition: Position.Right,
|
sourcePosition: Position.Right,
|
||||||
@ -199,7 +252,7 @@ export const useHandleDrop = () => {
|
|||||||
|
|
||||||
addNode(newNode);
|
addNode(newNode);
|
||||||
},
|
},
|
||||||
[reactFlowInstance, addNode, initializeOperatorParams],
|
[reactFlowInstance, addNode, initializeOperatorParams, generateNodeName],
|
||||||
);
|
);
|
||||||
|
|
||||||
return { onDrop, onDragOver, setReactFlowInstance };
|
return { onDrop, onDragOver, setReactFlowInstance };
|
||||||
|
|||||||
Reference in New Issue
Block a user