File tree Expand file tree Collapse file tree 3 files changed +10
-3
lines changed Expand file tree Collapse file tree 3 files changed +10
-3
lines changed Original file line number Diff line number Diff line change 3535- fitBounds:适应边界
3636- screenToFlowPosition:将屏幕坐标转换为画布坐标
3737- flowToScreenPosition:将画布坐标转换为屏幕坐标
38+ - runAutoLayout:自动布局节点
3839
3940## useNodes
4041
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ const edgeTypes = { buttonedge: memo(CustomEdge) };
4242 */
4343const 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
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { useMemoizedFn } from 'ahooks';
33import { useMemo } from 'react' ;
44import { FlowNode } from '../models/store' ;
55import { 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 ) ;
You can’t perform that action at this time.
0 commit comments