Skip to content

Commit

Permalink
chore: useFlow 暴露 runAutoLayout 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjuning committed Dec 31, 2024
1 parent 082bf59 commit 72c60e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/xflow/FlowProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ group:
- fitBounds:适应边界
- screenToFlowPosition:将屏幕坐标转换为画布坐标
- flowToScreenPosition:将画布坐标转换为屏幕坐标
- runAutoLayout:自动布局节点

## useNodes

Expand Down
4 changes: 2 additions & 2 deletions packages/x-flow/src/XFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const edgeTypes = { buttonedge: memo(CustomEdge) };
*/
const XFlow: FC<FlowProps> = memo(props => {
const workflowContainerRef = useRef<HTMLDivElement>(null);
const store = useStoreApi();
const storeApi = useStoreApi();
const { zoomTo } = useReactFlow();
const {
layout,
Expand Down Expand Up @@ -124,7 +124,7 @@ const XFlow: FC<FlowProps> = memo(props => {
eventEmitter?.useSubscription((v: any) => {
// 整理画布
if (v.type === 'auto-layout-nodes') {
const newNodes: any = autoLayoutNodes(store.getState().nodes, edges);
const newNodes: any = autoLayoutNodes(storeApi.getState().nodes, edges);
setNodes(newNodes, false);
}

Expand Down
8 changes: 7 additions & 1 deletion packages/x-flow/src/hooks/useFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMemoizedFn } from 'ahooks';
import { useMemo } from 'react';
import { FlowNode } from '../models/store';
import { useStoreApi } from './useStore';
import autoLayoutNodes from '../utils/autoLayoutNodes';

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

return useMemo(
() => ({
Expand All @@ -58,7 +63,8 @@ export const useFlow = () => {
setCenter,
fitBounds,
screenToFlowPosition,
flowToScreenPosition
flowToScreenPosition,
runAutoLayout
}),
[instance]
);
Expand Down

0 comments on commit 72c60e8

Please sign in to comment.