Skip to content

Commit dacf5c4

Browse files
committed
feat: 迭代开发
1 parent bbc90de commit dacf5c4

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

packages/x-flow/src/hooks/useFlow.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Edge, useReactFlow } from '@xyflow/react';
2-
import { useMemoizedFn } from 'ahooks';
31
import { useMemo } from 'react';
2+
import { useMemoizedFn } from 'ahooks';
3+
import { Edge, useReactFlow } from '@xyflow/react';
4+
45
import { FlowNode } from '../models/store';
56
import { useStoreApi } from './useStore';
67
import { useTemporalStore } from './useTemporalStore';
@@ -11,9 +12,11 @@ import { message } from 'antd';
1112
// useFlow 维护原则
1213
// 1. 尽量复用 reactflow 已有的方法,不要重复造轮子
1314
// 2. 非必要不暴露新的方法和状态
15+
1416
export const useFlow = () => {
1517
const storeApi = useStoreApi();
1618
const instance = storeApi.getState();
19+
1720
const {
1821
zoomIn,
1922
zoomOut,
@@ -33,6 +36,32 @@ export const useFlow = () => {
3336

3437
const { record } = useTemporalStore();
3538

39+
const toObject = useMemoizedFn(() => {
40+
const { nodes, ...rest } = _toObject();
41+
return {
42+
...rest,
43+
nodes: getNodes(nodes)
44+
};
45+
});
46+
47+
const getFlowData = () => {
48+
const { nodes, edges } = _toObject();
49+
return {
50+
edges,
51+
nodes: getNodes(nodes)
52+
};
53+
};
54+
55+
const setFlowData = ({ nodes, edges }) => {
56+
if (!!nodes) {
57+
setNodes(nodes);
58+
}
59+
60+
if (!!edges) {
61+
setEdges(edges);
62+
}
63+
};
64+
3665
const getNodes = useMemoizedFn((_nodes: any) => {
3766
const nodes = _nodes || _getNodes();
3867
const result = nodes.map((item: any) => {
@@ -47,14 +76,6 @@ export const useFlow = () => {
4776
return result;
4877
});
4978

50-
const toObject = () => {
51-
const { nodes, ...rest } = _toObject();
52-
return {
53-
...rest,
54-
nodes: getNodes(nodes)
55-
};
56-
};
57-
5879
const setNodes = useMemoizedFn((nodes: FlowNode[], isTransform = false) => {
5980
storeApi.getState().setNodes(nodes, isTransform);
6081
});
@@ -113,7 +134,7 @@ export const useFlow = () => {
113134
nodes: storeApi.getState().nodes.filter((node) => node.id !== nodeId),
114135
});
115136
})
116-
})
137+
});
117138

118139
const runAutoLayout = useMemoizedFn(() => {
119140
const newNodes: any = autoLayoutNodes(storeApi.getState().nodes, storeApi.getState().edges, storeApi.getState().layout);
@@ -126,6 +147,8 @@ export const useFlow = () => {
126147
addNodes,
127148
setEdges,
128149
addEdges,
150+
getFlowData,
151+
setFlowData,
129152
getNodes,
130153
getEdges,
131154
toObject,
@@ -147,4 +170,4 @@ export const useFlow = () => {
147170
}),
148171
[instance]
149172
);
150-
};
173+
}

packages/x-flow/src/models/store.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { message } from 'antd';
21
import { generateCopyNodes, transformNodes, uuid } from '../utils';
32
import {
43
addEdge,

0 commit comments

Comments
 (0)