Skip to content

Commit

Permalink
fix-code
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk committed Sep 12, 2024
1 parent 2da6357 commit 93d744a
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 128 deletions.
1 change: 0 additions & 1 deletion packages/global/core/workflow/type/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export type FlowNodeItemType = FlowNodeTemplateType & {
response?: ChatHistoryItemResType;
isExpired?: boolean;
};
isIntersecting?: boolean;
};

// store node type
Expand Down
2 changes: 1 addition & 1 deletion packages/service/core/workflow/dispatch/tools/runLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.loopFlow]: { childNodes: Array<string> };
}>;
type Response = DispatchNodeResultType<{
[NodeOutputKeyEnum.loopArray]: any[];
[NodeOutputKeyEnum.loopArray]: Array<any>;
}>;

export const dispatchLoop = async (props: Props): Promise<Response> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import {
Box,
Flex,
Expand All @@ -14,7 +14,7 @@ import type {
NodeTemplateListItemType,
NodeTemplateListType
} from '@fastgpt/global/core/workflow/type/node.d';
import { useReactFlow, useViewport, XYPosition } from 'reactflow';
import { useReactFlow, XYPosition } from 'reactflow';
import { useSystemStore } from '@/web/common/system/useSystemStore';
import Avatar from '@fastgpt/web/components/common/Avatar';
import { nodeTemplate2FlowNode } from '@/web/core/workflow/utils';
Expand All @@ -36,9 +36,7 @@ import { useRouter } from 'next/router';
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
import { useContextSelector } from 'use-context-selector';
import { WorkflowContext } from '../context';
import { useI18n } from '@/web/context/I18n';
import { getTeamPlugTemplates } from '@/web/core/app/api/plugin';
import { AppTypeEnum } from '@fastgpt/global/core/app/constants';
import { ParentIdType } from '@fastgpt/global/common/parentFolder/type';
import MyBox from '@fastgpt/web/components/common/MyBox';
import FolderPath from '@/components/common/folder/Path';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
XYPosition,
useReactFlow,
getNodesBounds,
Rect
Rect,
NodeRemoveChange,
NodeSelectionChange
} from 'reactflow';
import { EDGE_TYPE, FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import 'reactflow/dist/style.css';
Expand All @@ -21,7 +23,7 @@ import { useKeyboard } from './useKeyboard';
import { useContextSelector } from 'use-context-selector';
import { WorkflowContext } from '../../context';
import { THelperLine } from '@fastgpt/global/core/workflow/type';
import { NodeInputKeyEnum, WorkflowIOValueTypeEnum } from '@fastgpt/global/core/workflow/constants';
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { FlowNodeInputItemType } from '@fastgpt/global/core/workflow/type/io';

/*
Expand Down Expand Up @@ -274,6 +276,7 @@ export const useWorkflow = () => {
onEdgesChange,
setHoverEdgeId
} = useContextSelector(WorkflowContext, (v) => v);

const { getIntersectingNodes } = useReactFlow();

const resetNodeSizeAndPosition = (rect: Rect, parentId: string) => {
Expand All @@ -283,33 +286,35 @@ export const useWorkflow = () => {
);

if (!loopFlow) return;
const newLoopFlowValue = {
...loopFlow.value,
width: rect.width + 110 > 900 ? rect.width + 110 : 900,
height: rect.height + 380 > 900 ? rect.height + 380 : 900
};

onChangeNode({
nodeId: parentId || '',
type: 'updateInput',
key: NodeInputKeyEnum.loopFlow,
value: {
...loopFlow,
value: {
...loopFlow?.value,
width: rect.width + 110 > 900 ? rect.width + 110 : 900,
height: rect.height + 380 > 900 ? rect.height + 380 : 900
}
value: newLoopFlowValue
}
});
setNodes((nodes) => {
return nodes.map((node) => {
if (node.id === parentId) {
return {
...node,
position: {
x: rect.x - 50,
y: rect.y - 280

setNodes((nodes) =>
nodes.map((node) =>
node.id === parentId
? {
...node,
position: {
x: rect.x - 50,
y: rect.y - 280
}
}
};
}
return node;
});
});
: node
)
);
};

/* helper line */
Expand Down Expand Up @@ -360,115 +365,136 @@ export const useWorkflow = () => {
for (const change of changes) {
if (change.type === 'remove') {
const node = nodes.find((n) => n.id === change.id);
if (node && node.data.forbidDelete) {
return toast({
status: 'warning',
title: t('common:core.workflow.Can not delete node')
});
} else if (node && nodes.find((item) => item.data.parentNodeId === node.id)) {
const childNodes = nodes.filter((n) => n.data.parentNodeId === node.id);
const childNodesChange = childNodes.map((node) => {
return {
...change,
id: node.id
};
});
return (() => {
onNodesChange(changes.concat(childNodesChange));
setEdges((state) =>
state.filter((edge) => edge.source !== change.id && edge.target !== change.id)
);
})();
} else {
return (() => {
onNodesChange(changes);
setEdges((state) =>
state.filter((edge) => edge.source !== change.id && edge.target !== change.id)
);
if (node?.data.parentNodeId) {
const parentId = node.data.parentNodeId;
const childNodes = nodes.filter(
(n) => n.data.parentNodeId === parentId && n.id !== node.id
);
const parentNode = nodes.find((n) => n.id === parentId);
const rect = getNodesBounds(childNodes);
const updatedLoopFlow = parentNode?.data.inputs.find(
(input: FlowNodeInputItemType) => input.key === NodeInputKeyEnum.loopFlow
);
if (updatedLoopFlow) {
updatedLoopFlow.value.childNodes = updatedLoopFlow.value.childNodes.filter(
(nodeId: string) => nodeId !== node.id
);
}
resetNodeSizeAndPosition(rect, parentId);
updatedLoopFlow &&
onChangeNode({
nodeId: parentId,
type: 'updateInput',
key: NodeInputKeyEnum.loopFlow,
value: updatedLoopFlow
});
}
})();
if (node) {
return handleRemoveNode(change, changes, node);
}
} else if (change.type === 'select' && change.selected === false && isDowningCtrl) {
change.selected = true;
} else if (change.type === 'position' && change.position) {
} else if (change.type === 'select') {
return handleSelectNode(change);
} else if (change.type === 'position') {
const node = nodes.find((n) => n.id === change.id);
if (node && node.data.parentNodeId) {
if (node) {
return handlePositionNode(change, changes, node);
}
}
}

customApplyNodeChanges(changes, nodes);

onNodesChange(changes);
};

const handleRemoveNode = (change: NodeRemoveChange, changes: NodeChange[], node: Node) => {
if (node.data.forbidDelete) {
return toast({
status: 'warning',
title: t('common:core.workflow.Can not delete node')
});
} else if (nodes.some((n) => n.data.parentNodeId === node.id)) {
const childNodes = nodes.filter((n) => n.data.parentNodeId === node.id);
const childNodesChange = childNodes.map((node) => ({
...change,
id: node.id
}));
return (() => {
onNodesChange([...changes, ...childNodesChange]);
setEdges((state) =>
state.filter((edge) => edge.source !== change.id && edge.target !== change.id)
);
})();
} else {
return (() => {
onNodesChange(changes);
setEdges((state) =>
state.filter((edge) => edge.source !== change.id && edge.target !== change.id)
);
if (node?.data.parentNodeId) {
const parentId = node.data.parentNodeId;
const childNodes = nodes.filter(
(n) => n.data.parentNodeId === parentId && n.id !== node.id
);
const parentNode = nodes.find((n) => n.id === parentId);
const childNodes = nodes.filter((n) => n.data.parentNodeId === parentId);

if (!parentNode) return;
const rect = getNodesBounds(childNodes);
return (() => {
customApplyNodeChanges(changes, childNodes);
onNodesChange(changes);
const updatedLoopFlow = parentNode?.data.inputs.find(
(input: FlowNodeInputItemType) => input.key === NodeInputKeyEnum.loopFlow
);
if (updatedLoopFlow) {
updatedLoopFlow.value.childNodes = updatedLoopFlow.value.childNodes.filter(
(nodeId: string) => nodeId !== node.id
);
resetNodeSizeAndPosition(rect, parentId);
})();
} else if (node && nodes.find((item) => item.data.parentNodeId === node.id)) {
const parentId = node.id;
const childNodes = nodes.filter((n) => n.data.parentNodeId === parentId);
const initPosition = node.position;
const deltaX = change.position.x - initPosition.x;
const deltaY = change.position.y - initPosition.y;
const childNodesChange = childNodes.map((node) => {
if (change.dragging) {
return {
...change,
id: node.id,
position: {
x: node.position.x + deltaX,
y: node.position.y + deltaY
},
positionAbsolute: {
x: node.position.x + deltaX,
y: node.position.y + deltaY
}
};
} else {
return {
...change,
id: node.id
};
}
});
return (() => {
// customApplyNodeChanges(
// changes,
// nodes.filter((node) => !node.data.parentNodeId)
// );
onNodesChange(changes.concat(childNodesChange));
})();
onChangeNode({
nodeId: parentId,
type: 'updateInput',
key: NodeInputKeyEnum.loopFlow,
value: updatedLoopFlow
});
}
}
}
})();
}
};

customApplyNodeChanges(changes, nodes);
const handleSelectNode = (change: NodeSelectionChange) => {
if (change.selected === false && isDowningCtrl) {
change.selected = true;
}
};

onNodesChange(changes);
const handlePositionNode = (change: NodePositionChange, changes: NodeChange[], node: Node) => {
if (node.data.parentNodeId) {
const parentId = node.data.parentNodeId;
const parentNode = nodes.find((n) => n.id === parentId);
const childNodes = nodes.filter((n) => n.data.parentNodeId === parentId);

if (!parentNode) return;
const rect = getNodesBounds(childNodes);
return (() => {
customApplyNodeChanges(changes, childNodes);
onNodesChange(changes);
resetNodeSizeAndPosition(rect, parentId);
})();
} else if (nodes.find((item) => item.data.parentNodeId === node.id)) {
const parentId = node.id;
const childNodes = nodes.filter((n) => n.data.parentNodeId === parentId);
const initPosition = node.position;
const deltaX = change.position?.x ? change.position.x - initPosition.x : 0;
const deltaY = change.position?.y ? change.position.y - initPosition.y : 0;
const childNodesChange = childNodes.map((node) => {
if (change.dragging) {
return {
...change,
id: node.id,
position: {
x: node.position.x + deltaX,
y: node.position.y + deltaY
},
positionAbsolute: {
x: node.position.x + deltaX,
y: node.position.y + deltaY
}
};
} else {
return {
...change,
id: node.id
};
}
});
return (() => {
// customApplyNodeChanges(
// changes,
// nodes.filter((node) => !node.data.parentNodeId)
// );
onNodesChange(changes.concat(childNodesChange));
})();
} else {
return (() => {
customApplyNodeChanges(changes, nodes);
onNodesChange(changes);
})();
}
};

const handleEdgeChange = useCallback(
(changes: EdgeChange[]) => {
onEdgesChange(changes.filter((change) => change.type !== 'remove'));
Expand All @@ -479,7 +505,7 @@ export const useWorkflow = () => {
const onNodeDragStop = useCallback(
(_: any, node: Node) => {
const intersections = getIntersectingNodes(node);
const parentNode = intersections.find((item) => item.type === 'loop');
const parentNode = intersections.find((item) => item.type === FlowNodeTypeEnum.loop);

const unSupportedTypes = [
FlowNodeTypeEnum.workflowStart,
Expand All @@ -500,7 +526,7 @@ export const useWorkflow = () => {
(input: FlowNodeInputItemType) => input.key === NodeInputKeyEnum.loopFlow
);
if (updatedLoopFlow) {
updatedLoopFlow.value.childNodes = updatedLoopFlow.value.childNodes.concat(node.id);
updatedLoopFlow.value.childNodes = [...updatedLoopFlow.value.childNodes, node.id];
}
onChangeNode({
nodeId: node.id,
Expand All @@ -518,7 +544,7 @@ export const useWorkflow = () => {
state.filter((edge) => edge.source !== node.id && edge.target !== node.id)
);

const childNodes = nodes.filter((n) => n.data.parentNodeId === parentNode.id).concat(node);
const childNodes = [...nodes.filter((n) => n.data.parentNodeId === parentNode.id), node];
const rect = getNodesBounds(childNodes);
resetNodeSizeAndPosition(rect, parentNode.id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ const NodeLoopStart = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
<VariableTable
variables={[
{
// label: t('workflow:Array_element'),
icon: 'core/workflow/inputType/array',
label: '数组元素',
type: typeMap[outputValueType as keyof typeof typeMap],
Expand Down

0 comments on commit 93d744a

Please sign in to comment.