Merge pull request #3478 from wbkd/react-flow-use-react-flow
useReactFlow improvements
This commit is contained in:
@@ -49,9 +49,9 @@ const DnDFlow = () => {
|
|||||||
|
|
||||||
if (reactFlowInstance) {
|
if (reactFlowInstance) {
|
||||||
const type = event.dataTransfer.getData('application/reactflow');
|
const type = event.dataTransfer.getData('application/reactflow');
|
||||||
const position = reactFlowInstance.project({
|
const position = reactFlowInstance.screenToFlowCoordinate({
|
||||||
x: event.clientX,
|
x: event.clientX,
|
||||||
y: event.clientY - 40,
|
y: event.clientY,
|
||||||
});
|
});
|
||||||
const newNode: Node = {
|
const newNode: Node = {
|
||||||
id: getId(),
|
id: getId(),
|
||||||
|
|||||||
@@ -164,6 +164,8 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { deletedNodes: matchingNodes, deletedEdges: matchingEdges };
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const getNodeRect = useCallback(
|
const getNodeRect = useCallback(
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { pointToRendererPoint, getTransformForBounds, fitView, type XYPosition } from '@xyflow/system';
|
import {
|
||||||
|
pointToRendererPoint,
|
||||||
|
getTransformForBounds,
|
||||||
|
fitView,
|
||||||
|
type XYPosition,
|
||||||
|
rendererPointToPoint,
|
||||||
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import { useStoreApi, useStore } from '../hooks/useStore';
|
import { useStoreApi, useStore } from '../hooks/useStore';
|
||||||
import type { ViewportHelperFunctions, ReactFlowState } from '../types';
|
import type { ViewportHelperFunctions, ReactFlowState } from '../types';
|
||||||
@@ -85,6 +91,36 @@ const useViewportHelper = (): ViewportHelperFunctions => {
|
|||||||
const { transform, snapToGrid, snapGrid } = store.getState();
|
const { transform, snapToGrid, snapGrid } = store.getState();
|
||||||
return pointToRendererPoint(position, transform, snapToGrid, snapGrid);
|
return pointToRendererPoint(position, transform, snapToGrid, snapGrid);
|
||||||
},
|
},
|
||||||
|
screenToFlowCoordinate: (position: XYPosition) => {
|
||||||
|
const { transform, snapToGrid, snapGrid, domNode } = store.getState();
|
||||||
|
if (domNode) {
|
||||||
|
const { x: domX, y: domY } = domNode.getBoundingClientRect();
|
||||||
|
|
||||||
|
const correctedPosition = {
|
||||||
|
x: position.x - domX,
|
||||||
|
y: position.y - domY,
|
||||||
|
};
|
||||||
|
|
||||||
|
return pointToRendererPoint(correctedPosition, transform, snapToGrid, snapGrid || [1, 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { x: 0, y: 0 };
|
||||||
|
},
|
||||||
|
flowToScreenCoordinate: (position: XYPosition) => {
|
||||||
|
const { transform, domNode } = store.getState();
|
||||||
|
if (domNode) {
|
||||||
|
const { x: domX, y: domY } = domNode.getBoundingClientRect();
|
||||||
|
|
||||||
|
const rendererPosition = rendererPointToPoint(position, transform);
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: rendererPosition.x + domX,
|
||||||
|
y: rendererPosition.y + domY,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { x: 0, y: 0 };
|
||||||
|
},
|
||||||
viewportInitialized: panZoomInitialized,
|
viewportInitialized: panZoomInitialized,
|
||||||
};
|
};
|
||||||
}, [panZoomInitialized]);
|
}, [panZoomInitialized]);
|
||||||
|
|||||||
@@ -55,5 +55,7 @@ export type ViewportHelperFunctions = {
|
|||||||
setCenter: SetCenter;
|
setCenter: SetCenter;
|
||||||
fitBounds: FitBounds;
|
fitBounds: FitBounds;
|
||||||
project: Project;
|
project: Project;
|
||||||
|
screenToFlowCoordinate: Project;
|
||||||
|
flowToScreenCoordinate: Project;
|
||||||
viewportInitialized: boolean;
|
viewportInitialized: boolean;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,7 +28,10 @@ export namespace Instance {
|
|||||||
export type GetEdge<EdgeData> = (id: string) => Edge<EdgeData> | undefined;
|
export type GetEdge<EdgeData> = (id: string) => Edge<EdgeData> | undefined;
|
||||||
export type AddEdges<EdgeData> = (payload: Edge<EdgeData>[] | Edge<EdgeData>) => void;
|
export type AddEdges<EdgeData> = (payload: Edge<EdgeData>[] | Edge<EdgeData>) => void;
|
||||||
export type ToObject<NodeData = any, EdgeData = any> = () => ReactFlowJsonObject<NodeData, EdgeData>;
|
export type ToObject<NodeData = any, EdgeData = any> = () => ReactFlowJsonObject<NodeData, EdgeData>;
|
||||||
export type DeleteElements = ({ nodes, edges }: DeleteElementsOptions) => void;
|
export type DeleteElements = ({ nodes, edges }: DeleteElementsOptions) => {
|
||||||
|
deletedNodes: Node[];
|
||||||
|
deletedEdges: Edge[];
|
||||||
|
};
|
||||||
export type GetIntersectingNodes<NodeData> = (
|
export type GetIntersectingNodes<NodeData> = (
|
||||||
node: (Partial<Node<NodeData>> & { id: Node['id'] }) | Rect,
|
node: (Partial<Node<NodeData>> & { id: Node['id'] }) | Rect,
|
||||||
partially?: boolean,
|
partially?: boolean,
|
||||||
|
|||||||
Reference in New Issue
Block a user