Skip to content

Commit

Permalink
feat: when Categorize establishes a connection with other operators, …
Browse files Browse the repository at this point in the history
…it adds the target node to the to field. infiniflow#918 (infiniflow#1418)

### What problem does this PR solve?
feat: when Categorize establishes a connection with other operators, it
adds the target node to the to field. infiniflow#918

feat: modify the Chinese text of loop infiniflow#918

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Jul 8, 2024
1 parent 697e3ef commit 0c9d880
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
3 changes: 1 addition & 2 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"human-id": "^4.1.1",
"i18next": "^23.7.16",
"i18next-browser-languagedetector": "^8.0.0",
"immer": "^10.1.1",
"js-base64": "^3.7.5",
"jsencrypt": "^3.3.2",
"lodash": "^4.17.21",
Expand Down
2 changes: 1 addition & 1 deletion web/src/locales/zh-traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ export default {
messagePlaceholder: '訊息',
messageMsg: '請輸入訊息或刪除此欄位。',
addField: '新增字段',
loop: '',
loop: '循環上限',
createFlow: '创建工作流',
yes: '是',
no: '否',
Expand Down
2 changes: 1 addition & 1 deletion web/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export default {
messagePlaceholder: '消息',
messageMsg: '请输入消息或删除此字段。',
addField: '新增字段',
loop: '',
loop: '循环上限',
createFlow: '创建工作流',
yes: '是',
no: '否',
Expand Down
21 changes: 12 additions & 9 deletions web/src/pages/flow/canvas/node/categorize-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ export function CategorizeNode({ id, data, selected }: NodeProps<NodeData>) {
className={styles.handle}
id={'c'}
></Handle>
{Object.keys(categoryData).map((x, idx) => (
<CategorizeHandle
top={CategorizeAnchorPointPositions[idx].top}
right={CategorizeAnchorPointPositions[idx].right}
key={idx}
text={x}
idx={idx}
></CategorizeHandle>
))}
{Object.keys(categoryData).map((x, idx) => {
console.info(categoryData, id, data);
return (
<CategorizeHandle
top={CategorizeAnchorPointPositions[idx].top}
right={CategorizeAnchorPointPositions[idx].right}
key={idx}
text={x}
idx={idx}
></CategorizeHandle>
);
})}
<Flex vertical align="center" justify="center" gap={6}>
<OperatorIcon
name={data.label as Operator}
Expand Down
63 changes: 52 additions & 11 deletions web/src/pages/flow/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from 'reactflow';
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { Operator } from './constant';
import { NodeData } from './interface';

Expand All @@ -32,7 +33,7 @@ export type RFState = {
onConnect: OnConnect;
setNodes: (nodes: Node[]) => void;
setEdges: (edges: Edge[]) => void;
updateNodeForm: (nodeId: string, values: any) => void;
updateNodeForm: (nodeId: string, values: any, path?: string[]) => void;
onSelectionChange: OnSelectionChangeFunc;
addNode: (nodes: Node) => void;
getNode: (id?: string | null) => Node<NodeData> | undefined;
Expand All @@ -55,7 +56,7 @@ export type RFState = {
// this is our useStore hook that we can use in our components to get parts of the store and call actions
const useGraphStore = create<RFState>()(
devtools(
(set, get) => ({
immer((set, get) => ({
nodes: [] as Node[],
edges: [] as Edge[],
selectedNodeIds: [] as string[],
Expand Down Expand Up @@ -108,15 +109,32 @@ const useGraphStore = create<RFState>()(
edges: addEdge(connection, get().edges),
});
get().deletePreviousEdgeOfClassificationNode(connection);
// TODO: This may not be reasonable. You need to choose between listening to changes in the form.
get().updateFormDataOnConnect(connection);
},
getEdge: (id: string) => {
return get().edges.find((x) => x.id === id);
},
updateFormDataOnConnect: (connection: Connection) => {
const { getOperatorTypeFromId, updateNodeForm } = get();
const { source, target, sourceHandle } = connection;
if (source && getOperatorTypeFromId(source) === Operator.Relevant) {
updateNodeForm(source, { [sourceHandle as string]: target });
const operatorType = getOperatorTypeFromId(source);
if (source) {
switch (operatorType) {
case Operator.Relevant:
updateNodeForm(source, { [sourceHandle as string]: target });
break;
case Operator.Categorize:
if (sourceHandle)
updateNodeForm(source, target, [
'category_description',
sourceHandle,
'to',
]);
break;
default:
break;
}
}
},
deletePreviousEdgeOfClassificationNode: (connection: Connection) => {
Expand Down Expand Up @@ -166,13 +184,30 @@ const useGraphStore = create<RFState>()(
});
},
deleteEdgeById: (id: string) => {
const { edges, updateNodeForm } = get();
const { edges, updateNodeForm, getOperatorTypeFromId } = get();
const currentEdge = edges.find((x) => x.id === id);

if (currentEdge) {
const { source, sourceHandle } = currentEdge;
const operatorType = getOperatorTypeFromId(source);
// After deleting the edge, set the corresponding field in the node's form field to undefined
updateNodeForm(currentEdge.source, {
[currentEdge.sourceHandle as string]: undefined,
});
switch (operatorType) {
case Operator.Relevant:
updateNodeForm(source, {
[sourceHandle as string]: undefined,
});
break;
case Operator.Categorize:
if (sourceHandle)
updateNodeForm(source, undefined, [
'category_description',
sourceHandle,
'to',
]);
break;
default:
break;
}
}
set({
edges: edges.filter((edge) => edge.id !== id),
Expand Down Expand Up @@ -203,19 +238,25 @@ const useGraphStore = create<RFState>()(
findNodeByName: (name: Operator) => {
return get().nodes.find((x) => x.data.label === name);
},
updateNodeForm: (nodeId: string, values: any) => {
updateNodeForm: (nodeId: string, values: any, path: string[] = []) => {
set({
nodes: get().nodes.map((node) => {
if (node.id === nodeId) {
// node.data = {
// ...node.data,
// form: { ...node.data.form, ...values },
// };
let nextForm: Record<string, unknown> = { ...node.data.form };
if (path.length === 0) {
nextForm = Object.assign(nextForm, values);
} else {
lodashSet(nextForm, path, values);
}
return {
...node,
data: {
...node.data,
form: { ...node.data.form, ...values },
form: nextForm,
},
} as any;
}
Expand Down Expand Up @@ -247,7 +288,7 @@ const useGraphStore = create<RFState>()(
setClickedNodeId: (id?: string) => {
set({ clickedNodeId: id });
},
}),
})),
{ name: 'graph' },
),
);
Expand Down

0 comments on commit 0c9d880

Please sign in to comment.