Skip to content

Commit 72c60e8

Browse files
committed
chore: useFlow 暴露 runAutoLayout 方法
1 parent 082bf59 commit 72c60e8

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

docs/xflow/FlowProvider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ group:
3535
- fitBounds:适应边界
3636
- screenToFlowPosition:将屏幕坐标转换为画布坐标
3737
- flowToScreenPosition:将画布坐标转换为屏幕坐标
38+
- runAutoLayout:自动布局节点
3839

3940
## useNodes
4041

packages/x-flow/src/XFlow.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const edgeTypes = { buttonedge: memo(CustomEdge) };
4242
*/
4343
const XFlow: FC<FlowProps> = memo(props => {
4444
const workflowContainerRef = useRef<HTMLDivElement>(null);
45-
const store = useStoreApi();
45+
const storeApi = useStoreApi();
4646
const { zoomTo } = useReactFlow();
4747
const {
4848
layout,
@@ -124,7 +124,7 @@ const XFlow: FC<FlowProps> = memo(props => {
124124
eventEmitter?.useSubscription((v: any) => {
125125
// 整理画布
126126
if (v.type === 'auto-layout-nodes') {
127-
const newNodes: any = autoLayoutNodes(store.getState().nodes, edges);
127+
const newNodes: any = autoLayoutNodes(storeApi.getState().nodes, edges);
128128
setNodes(newNodes, false);
129129
}
130130

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useMemoizedFn } from 'ahooks';
33
import { useMemo } from 'react';
44
import { FlowNode } from '../models/store';
55
import { useStoreApi } from './useStore';
6+
import autoLayoutNodes from '../utils/autoLayoutNodes';
67

78
// useFlow 维护原则
89
// 1. 尽量复用 reactflow 已有的方法,不要重复造轮子
@@ -38,6 +39,10 @@ export const useFlow = () => {
3839
const addEdges = useMemoizedFn((edges: Edge[]) => {
3940
storeApi.getState().addEdges(edges);
4041
});
42+
const runAutoLayout = useMemoizedFn(() => {
43+
const newNodes: any = autoLayoutNodes(storeApi.getState().nodes, storeApi.getState().edges);
44+
setNodes(newNodes, false);
45+
});
4146

4247
return useMemo(
4348
() => ({
@@ -58,7 +63,8 @@ export const useFlow = () => {
5863
setCenter,
5964
fitBounds,
6065
screenToFlowPosition,
61-
flowToScreenPosition
66+
flowToScreenPosition,
67+
runAutoLayout
6268
}),
6369
[instance]
6470
);

0 commit comments

Comments
 (0)