Skip to content

Commit

Permalink
chore: 解决添加节点跟手性能问题
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjuning committed Dec 16, 2024
1 parent 13e3334 commit 6a2537b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
5 changes: 5 additions & 0 deletions packages/x-flow/src/XFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const XFlow: FC<FlowProps> = memo(() => {
onEdgesChange,
onConnect,
setCandidateNode,
isAddingNode,
setMousePosition,
} = useStore(
s => ({
Expand All @@ -62,6 +63,7 @@ const XFlow: FC<FlowProps> = memo(() => {
layout: s.layout,
panOnDrag: s.panOnDrag,
setMousePosition: s.setMousePosition,
isAddingNode: s.isAddingNode,
setCandidateNode: s.setCandidateNode,
onNodesChange: s.onNodesChange,
onEdgesChange: s.onEdgesChange,
Expand Down Expand Up @@ -101,6 +103,9 @@ const XFlow: FC<FlowProps> = memo(() => {
elementY: e.clientY - containerClientRect.top,
});
}
}, {
target: workflowContainerRef.current,
enable: isAddingNode
});

const { eventEmitter } = useEventEmitterContextContext();
Expand Down
22 changes: 12 additions & 10 deletions packages/x-flow/src/components/CandidateNode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import CustomNode from '../CustomNode';
const CandidateNode = () => {
const { zoom } = useViewport();
const reactflow = useReactFlow();
const { candidateNode, mousePosition, setCandidateNode, addNodes } = useStore(
(state: any) => ({
nodes: state.nodes,
edges: state.edges,
addNodes: state.addNodes,
candidateNode: state.candidateNode,
mousePosition: state.mousePosition,
setCandidateNode: state.setCandidateNode,
onNodesChange: state.onNodesChange,
onEdgesChange: state.onEdgesChange,
const { candidateNode, mousePosition, setIsAddingNode, setCandidateNode, addNodes } = useStore(
(s: any) => ({
nodes: s.nodes,
edges: s.edges,
addNodes: s.addNodes,
candidateNode: s.candidateNode,
setIsAddingNode: s.setIsAddingNode,
mousePosition: s.mousePosition,
setCandidateNode: s.setCandidateNode,
onNodesChange: s.onNodesChange,
onEdgesChange: s.onEdgesChange,
}),
shallow
);
Expand All @@ -42,6 +43,7 @@ const CandidateNode = () => {
position: { x, y },
};
addNodes(newNodes);
setIsAddingNode(false)
setCandidateNode(null);
});

Expand Down
6 changes: 5 additions & 1 deletion packages/x-flow/src/components/NodesPopover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import React, {
useRef,
useState,
} from 'react';
import { useStore } from '../../hooks/useStore';
import { ConfigContext } from '../../models/context';
import NodesMenu from '../NodesMenu';

export default forwardRef((props: any, popoverRef) => {
const { addNode, children, onNodeSelectPopoverChange } = props;

const { setIsAddingNode } = useStore(s => ({
setIsAddingNode: s.setIsAddingNode,
}));
const ref = useRef<any>(null);
const closeRef: any = useRef<HTMLButtonElement>(null);
const [open, setOpen] = useState(false);
Expand Down Expand Up @@ -53,6 +56,7 @@ export default forwardRef((props: any, popoverRef) => {
open={open}
onOpenChange={() => {
setTimeout(() => {
setIsAddingNode(true);
closeRef.current = true;
setOpen(true);
}, 50);
Expand Down
6 changes: 6 additions & 0 deletions packages/x-flow/src/models/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type FlowState = {
nodes?: FlowNode[];
edges?: Edge[];
panOnDrag?: boolean;
isAddingNode?: boolean;
candidateNode: any;
mousePosition: any;
onNodesChange: OnNodesChange<FlowNode>;
Expand All @@ -38,6 +39,7 @@ export type FlowState = {
addNodes: (nodes: FlowNode[]) => void;
addEdges: (edges: Edge[]) => void;
setLayout: (layout: 'LR' | 'TB') => void;
setIsAddingNode: (payload: boolean) => void;
setCandidateNode: (candidateNode: any) => void;
setMousePosition: (mousePosition: any) => void;
};
Expand All @@ -55,6 +57,7 @@ const createStore = (initProps?: Partial<FlowProps>) => {
(set, get) => ({
...DEFAULT_PROPS,
...initProps,
isAddingNode: false,
candidateNode: null,
// nodeMenus: [],
mousePosition: { pageX: 0, pageY: 0, elementX: 0, elementY: 0 },
Expand Down Expand Up @@ -95,6 +98,9 @@ const createStore = (initProps?: Partial<FlowProps>) => {
// setNodeMenus: (nodeMenus: any) => {
// set({ nodeMenus });
// },
setIsAddingNode: payload => {
set({ isAddingNode: payload });
},
setCandidateNode: candidateNode => {
set({ candidateNode });
},
Expand Down

0 comments on commit 6a2537b

Please sign in to comment.