feat(helper): export project function closes #237

This commit is contained in:
moklick
2020-05-19 18:33:29 +02:00
parent cebab6731b
commit c0390eec79
5 changed files with 28 additions and 5 deletions
+16 -2
View File
@@ -7,7 +7,7 @@ React Flow is a library for building node-based graphs. You can easily implement
- [Key Features](#key-features) - [Key Features](#key-features)
- [Installation](#installation) - [Installation](#installation)
- [Usage](#usage) - [Usage](#usage)
- [ReactFlow Component Props](#reactflow-component-props) - [ReactFlow Component Prop Types](#reactflow-component-prop-types)
- [Styling](#styling) - [Styling](#styling)
- [Nodes](#nodes) - [Nodes](#nodes)
- [Options](#options-1) - [Options](#options-1)
@@ -62,7 +62,7 @@ const BasicFlow = () => (
); );
``` ```
## ReactFlow Component Props ## ReactFlow Component Prop Types
- `elements`: array of [nodes](#nodes) and [edges](#edges) *(required)* - `elements`: array of [nodes](#nodes) and [edges](#edges) *(required)*
- `onElementClick`: element click handler - `onElementClick`: element click handler
@@ -249,20 +249,34 @@ import ReactFlow, { isNode, isEdge, removeElements, addEdge } from 'react-flow-r
#### isEdge #### isEdge
Returns true if element is an edge
`isEdge = (element: Node | Edge): element is Edge` `isEdge = (element: Node | Edge): element is Edge`
#### isNode #### isNode
Returns true if element is a node
`isNode = (element: Node | Edge): element is Node` `isNode = (element: Node | Edge): element is Node`
#### removeElements #### removeElements
Returns elements without the elements from `elementsToRemove`
`removeElements = (elementsToRemove: Elements, elements: Elements): Elements` `removeElements = (elementsToRemove: Elements, elements: Elements): Elements`
#### addEdge #### addEdge
Returns elements array with added edge
`addEdge = (edgeParams: Edge, elements: Elements): Elements` `addEdge = (edgeParams: Edge, elements: Elements): Elements`
#### project
Transforms pixel coordinates to the internal React Flow coordinate system
`project = (position: XYPosition): XYPosition`
You can use these function as seen in [this example](/example/src/Overview/index.js#L40-L41) or use your own ones. You can use these function as seen in [this example](/example/src/Overview/index.js#L40-L41) or use your own ones.
## Plugins ## Plugins
+2 -2
View File
@@ -10701,13 +10701,13 @@
"react-flow-renderer": { "react-flow-renderer": {
"version": "file:..", "version": "file:..",
"requires": { "requires": {
"@welldone-software/why-did-you-render": "^4.2.0", "@welldone-software/why-did-you-render": "^4.2.1",
"classnames": "^2.2.6", "classnames": "^2.2.6",
"d3-selection": "^1.4.1", "d3-selection": "^1.4.1",
"d3-zoom": "^1.8.3", "d3-zoom": "^1.8.3",
"easy-peasy": "^3.3.0", "easy-peasy": "^3.3.0",
"fast-deep-equal": "^3.1.1", "fast-deep-equal": "^3.1.1",
"react-draggable": "^4.3.1", "react-draggable": "^4.4.2",
"resize-observer": "^1.0.0" "resize-observer": "^1.0.0"
}, },
"dependencies": { "dependencies": {
+2 -1
View File
@@ -12,7 +12,7 @@ import useD3Zoom from '../../hooks/useD3Zoom';
import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler'; import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler';
import useElementUpdater from '../../hooks/useElementUpdater'; import useElementUpdater from '../../hooks/useElementUpdater';
import { getDimensions } from '../../utils'; import { getDimensions } from '../../utils';
import { fitView, zoomIn, zoomOut } from '../../utils/graph'; import { fitView, zoomIn, zoomOut, project } from '../../utils/graph';
import { Elements, NodeTypesType, EdgeTypesType, GridType, OnLoadFunc, Node, Edge, Connection } from '../../types'; import { Elements, NodeTypesType, EdgeTypesType, GridType, OnLoadFunc, Node, Edge, Connection } from '../../types';
export interface GraphViewProps { export interface GraphViewProps {
@@ -115,6 +115,7 @@ const GraphView = memo(
fitView, fitView,
zoomIn, zoomIn,
zoomOut, zoomOut,
project,
}); });
} }
}, [state.d3Initialised]); }, [state.d3Initialised]);
+2
View File
@@ -133,11 +133,13 @@ export type FitViewParams = {
padding: number; padding: number;
}; };
export type FitViewFunc = (fitViewOptions: FitViewParams) => void; export type FitViewFunc = (fitViewOptions: FitViewParams) => void;
export type ProjectFunc = (position: XYPosition) => XYPosition;
type OnLoadParams = { type OnLoadParams = {
zoomIn: () => void; zoomIn: () => void;
zoomOut: () => void; zoomOut: () => void;
fitView: FitViewFunc; fitView: FitViewFunc;
project: ProjectFunc;
}; };
export type OnLoadFunc = (params: OnLoadParams) => void; export type OnLoadFunc = (params: OnLoadParams) => void;
+6
View File
@@ -74,6 +74,12 @@ export const pointToRendererPoint = (
return position; return position;
}; };
export const project = (position: XYPosition): XYPosition => {
const { transform, snapToGrid, snapGrid } = store.getState();
return pointToRendererPoint(position, transform, snapToGrid, snapGrid);
};
export const parseElement = (element: Node | Edge): Node | Edge => { export const parseElement = (element: Node | Edge): Node | Edge => {
if (!element.id) { if (!element.id) {
throw new Error('All elements (nodes and edges) need to have an id.'); throw new Error('All elements (nodes and edges) need to have an id.');