Skip to content

Commit

Permalink
fix: customize flow remove change (#2858)
Browse files Browse the repository at this point in the history
* fix: customize flow remove change

* fix when mouse not in canvas
  • Loading branch information
newfish-cmyk authored Oct 9, 2024
1 parent 3a4b4a8 commit f06aca9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { useTranslation } from 'next-i18next';
import styles from './index.module.scss';
import { maxZoom, minZoom } from '../index';
import { useKeyPress } from 'ahooks';
import { FlowNodeItemType } from '@fastgpt/global/core/workflow/type/node';

const buttonStyle = {
border: 'none',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ import { useContextSelector } from 'use-context-selector';
import { WorkflowContext } from '../../context';
import { THelperLine } from '@fastgpt/global/core/workflow/type';
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { useMemoizedFn } from 'ahooks';
import { useKeyPress, useMemoizedFn } from 'ahooks';
import {
Input_Template_Node_Height,
Input_Template_Node_Width
} from '@fastgpt/global/core/workflow/template/input';
import { FlowNodeItemType } from '@fastgpt/global/core/workflow/type/node';
import { getHandleId } from '@fastgpt/global/core/workflow/utils';
import { IfElseResultEnum } from '@fastgpt/global/core/workflow/template/system/ifElse/constant';

/*
Compute helper lines for snapping nodes to each other
Expand Down Expand Up @@ -284,7 +282,8 @@ export const useWorkflow = () => {
onChangeNode,
onEdgesChange,
setHoverEdgeId,
setMenu
setMenu,
mouseInCanvas
} = useContextSelector(WorkflowContext, (v) => v);

const { getIntersectingNodes } = useReactFlow();
Expand Down Expand Up @@ -414,7 +413,7 @@ export const useWorkflow = () => {
});

/* node */
const handleRemoveNode = useMemoizedFn((change: NodeRemoveChange, nodeId: string) => {
const handleRemoveNode = useMemoizedFn((nodeId: string) => {
// If the node has child nodes, remove the child nodes
const childNodes = nodes.filter((n) => n.data.parentNodeId === nodeId);
if (childNodes.length > 0) {
Expand All @@ -436,8 +435,7 @@ export const useWorkflow = () => {
}))
);
}

onNodesChange([change]);
onNodesChange([{ type: 'remove', id: nodeId }]);

return;
});
Expand Down Expand Up @@ -504,25 +502,10 @@ export const useWorkflow = () => {
}
}
);

const handleNodesChange = useMemoizedFn((changes: NodeChange[]) => {
for (const change of changes) {
if (change.type === 'remove') {
const node = nodes.find((n) => n.id === change.id);
if (!node) continue;

const parentNodeDeleted = changes.find(
(c) => c.type === 'remove' && c.id === node?.data.parentNodeId
);
// Forbidden delete && Parents are not deleted together
if (node.data.forbidDelete && !parentNodeDeleted) {
toast({
status: 'warning',
title: t('common:core.workflow.Can not delete node')
});
continue;
}
handleRemoveNode(change, node.id);
} else if (change.type === 'select') {
if (change.type === 'select') {
handleSelectNode(change);
} else if (change.type === 'position') {
const node = nodes.find((n) => n.id === change.id);
Expand All @@ -532,8 +515,7 @@ export const useWorkflow = () => {
}
}

// Remove separately
onNodesChange(changes.filter((c) => c.type !== 'remove'));
onNodesChange(changes);
});

const handleEdgeChange = useCallback(
Expand Down Expand Up @@ -634,6 +616,45 @@ export const useWorkflow = () => {
setMenu(null);
}, [setMenu]);

useKeyPress(['Delete', 'Backspace'], (e) => {
if (!mouseInCanvas) return;
const selectedNodes = nodes.filter((node) => node.selected);
const selectedEdges = edges.filter((edge) => edge.selected);

if (selectedNodes.length > 0) {
for (const node of selectedNodes) {
if (node.data.forbidDelete) {
toast({
status: 'warning',
title: t('common:core.workflow.Can not delete node')
});
continue;
}

const removedNodeId = node.id;
handleRemoveNode(removedNodeId);
const edgesToRemove = edges.filter(
(edge) => edge.source === removedNodeId || edge.target === removedNodeId
);
onEdgesChange(
edgesToRemove.map((edge) => ({
type: 'remove',
id: edge.id
}))
);
}
}

if (selectedEdges.length > 0) {
onEdgesChange(
selectedEdges.map((edge) => ({
type: 'remove',
id: edge.id
}))
);
}
});

return {
handleNodesChange,
handleEdgeChange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const Workflow = () => {
}
: {})}
onNodeDragStop={onNodeDragStop}
deleteKeyCode={[]}
>
<FlowController />
<HelperLines horizontal={helperLineHorizontal} vertical={helperLineVertical} />
Expand Down

0 comments on commit f06aca9

Please sign in to comment.