feat(reactflowInstance): add toObject function closes #603
This commit is contained in:
@@ -2,7 +2,6 @@ import React, { useState } from 'react';
|
|||||||
|
|
||||||
import ReactFlow, { removeElements, addEdge, isNode, Background } from 'react-flow-renderer';
|
import ReactFlow, { removeElements, addEdge, isNode, Background } from 'react-flow-renderer';
|
||||||
|
|
||||||
const onLoad = (reactFlowInstance) => console.log('flow loaded:', reactFlowInstance);
|
|
||||||
const onNodeDragStop = (event, node) => console.log('drag stop', node);
|
const onNodeDragStop = (event, node) => console.log('drag stop', node);
|
||||||
const onElementClick = (event, element) => console.log('click', element);
|
const onElementClick = (event, element) => console.log('click', element);
|
||||||
|
|
||||||
@@ -16,9 +15,11 @@ const initialElements = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const BasicFlow = () => {
|
const BasicFlow = () => {
|
||||||
|
const [rfInstance, setRfInstance] = useState(null);
|
||||||
const [elements, setElements] = useState(initialElements);
|
const [elements, setElements] = useState(initialElements);
|
||||||
const onElementsRemove = (elementsToRemove) => setElements((els) => removeElements(elementsToRemove, els));
|
const onElementsRemove = (elementsToRemove) => setElements((els) => removeElements(elementsToRemove, els));
|
||||||
const onConnect = (params) => setElements((els) => addEdge(params, els));
|
const onConnect = (params) => setElements((els) => addEdge(params, els));
|
||||||
|
const onLoad = (reactFlowInstance) => setRfInstance(reactFlowInstance);
|
||||||
|
|
||||||
const updatePos = () => {
|
const updatePos = () => {
|
||||||
setElements((elms) => {
|
setElements((elms) => {
|
||||||
@@ -38,6 +39,8 @@ const BasicFlow = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const logToObject = () => console.log(rfInstance.toObject());
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
elements={elements}
|
elements={elements}
|
||||||
@@ -53,9 +56,12 @@ const BasicFlow = () => {
|
|||||||
>
|
>
|
||||||
<Background variant="lines" />
|
<Background variant="lines" />
|
||||||
|
|
||||||
<button onClick={updatePos} style={{ position: 'absolute', right: 10, top: 30, zIndex: 4 }}>
|
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}>
|
||||||
change pos
|
<button onClick={updatePos} style={{ marginRight: 5 }}>
|
||||||
</button>
|
change pos
|
||||||
|
</button>
|
||||||
|
<button onClick={logToObject}>toObject</button>
|
||||||
|
</div>
|
||||||
</ReactFlow>
|
</ReactFlow>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import FlowRenderer from '../FlowRenderer';
|
|||||||
import NodeRenderer from '../NodeRenderer';
|
import NodeRenderer from '../NodeRenderer';
|
||||||
import EdgeRenderer from '../EdgeRenderer';
|
import EdgeRenderer from '../EdgeRenderer';
|
||||||
import useElementUpdater from '../../hooks/useElementUpdater';
|
import useElementUpdater from '../../hooks/useElementUpdater';
|
||||||
import { onLoadProject, onLoadGetElements } from '../../utils/graph';
|
import { onLoadProject, onLoadGetElements, onLoadToObject } from '../../utils/graph';
|
||||||
import {
|
import {
|
||||||
Elements,
|
Elements,
|
||||||
NodeTypesType,
|
NodeTypesType,
|
||||||
@@ -163,6 +163,7 @@ const GraphView = ({
|
|||||||
getElements: onLoadGetElements(currentStore),
|
getElements: onLoadGetElements(currentStore),
|
||||||
setTransform: (transform: FlowTransform) =>
|
setTransform: (transform: FlowTransform) =>
|
||||||
setInitTransform({ x: transform.x, y: transform.y, k: transform.zoom }),
|
setInitTransform({ x: transform.x, y: transform.y, k: transform.zoom }),
|
||||||
|
toObject: onLoadToObject(currentStore),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -229,8 +229,16 @@ export interface WrapNodeProps {
|
|||||||
export type FitViewParams = {
|
export type FitViewParams = {
|
||||||
padding: number;
|
padding: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type FlowExportObject = {
|
||||||
|
elements: Elements;
|
||||||
|
position: [number, number];
|
||||||
|
zoom: number;
|
||||||
|
};
|
||||||
|
|
||||||
export type FitViewFunc = (fitViewOptions?: FitViewParams) => void;
|
export type FitViewFunc = (fitViewOptions?: FitViewParams) => void;
|
||||||
export type ProjectFunc = (position: XYPosition) => XYPosition;
|
export type ProjectFunc = (position: XYPosition) => XYPosition;
|
||||||
|
export type ToObjectFunc = () => FlowExportObject;
|
||||||
|
|
||||||
export type OnLoadParams = {
|
export type OnLoadParams = {
|
||||||
zoomIn: () => void;
|
zoomIn: () => void;
|
||||||
@@ -240,6 +248,7 @@ export type OnLoadParams = {
|
|||||||
project: ProjectFunc;
|
project: ProjectFunc;
|
||||||
getElements: () => Elements;
|
getElements: () => Elements;
|
||||||
setTransform: (transform: FlowTransform) => void;
|
setTransform: (transform: FlowTransform) => void;
|
||||||
|
toObject: ToObjectFunc;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type OnLoadFunc = (params: OnLoadParams) => void;
|
export type OnLoadFunc = (params: OnLoadParams) => void;
|
||||||
|
|||||||
+24
-1
@@ -1,6 +1,17 @@
|
|||||||
import { Store } from 'easy-peasy';
|
import { Store } from 'easy-peasy';
|
||||||
import { StoreModel } from '../store';
|
import { StoreModel } from '../store';
|
||||||
import { ElementId, Node, Edge, Elements, Transform, XYPosition, Rect, Box, Connection } from '../types';
|
import {
|
||||||
|
ElementId,
|
||||||
|
Node,
|
||||||
|
Edge,
|
||||||
|
Elements,
|
||||||
|
Transform,
|
||||||
|
XYPosition,
|
||||||
|
Rect,
|
||||||
|
Box,
|
||||||
|
Connection,
|
||||||
|
FlowExportObject,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
export const isEdge = (element: Node | Connection | Edge): element is Edge =>
|
export const isEdge = (element: Node | Connection | Edge): element is Edge =>
|
||||||
'id' in element && 'source' in element && 'target' in element;
|
'id' in element && 'source' in element && 'target' in element;
|
||||||
@@ -236,3 +247,15 @@ export const onLoadGetElements = (currentStore: Store<StoreModel>) => {
|
|||||||
return parseElements(nodes, edges);
|
return parseElements(nodes, edges);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const onLoadToObject = (currentStore: Store<StoreModel>) => {
|
||||||
|
return (): FlowExportObject => {
|
||||||
|
const { nodes = [], edges = [], transform } = currentStore.getState();
|
||||||
|
|
||||||
|
return {
|
||||||
|
elements: parseElements(nodes, edges),
|
||||||
|
position: [transform[0], transform[1]],
|
||||||
|
zoom: transform[2],
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user