refactor(utils): cleanup
This commit is contained in:
@@ -1,47 +1,9 @@
|
||||
import { GetState } from 'zustand';
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
import { pointToRendererPoint } from '../utils/graph';
|
||||
import { useStoreApi } from '../store';
|
||||
import useZoomPanHelper from '../hooks/useZoomPanHelper';
|
||||
import { OnLoadFunc, ReactFlowState, XYPosition, Node, Edge, FlowExportObject } from '../types';
|
||||
|
||||
export const onLoadProject = (getState: GetState<ReactFlowState>) => {
|
||||
return (position: XYPosition): XYPosition => {
|
||||
const { transform, snapToGrid, snapGrid } = getState();
|
||||
|
||||
return pointToRendererPoint(position, transform, snapToGrid, snapGrid);
|
||||
};
|
||||
};
|
||||
|
||||
export const onLoadGetNodes = (getState: GetState<ReactFlowState>) => {
|
||||
return (): Node[] => {
|
||||
const { nodes = [] } = getState();
|
||||
|
||||
return nodes.map((n) => ({ ...n }));
|
||||
};
|
||||
};
|
||||
|
||||
export const onLoadGetEdges = (getState: GetState<ReactFlowState>) => {
|
||||
return (): Edge[] => {
|
||||
const { edges = [] } = getState();
|
||||
|
||||
return edges.map((e) => ({ ...e }));
|
||||
};
|
||||
};
|
||||
|
||||
export const onLoadToObject = (getState: GetState<ReactFlowState>) => {
|
||||
return (): FlowExportObject => {
|
||||
const { nodes = [], edges = [], transform } = getState();
|
||||
|
||||
return {
|
||||
nodes: nodes.map((n) => ({ ...n })),
|
||||
edges: edges.map((e) => ({ ...e })),
|
||||
position: [transform[0], transform[1]],
|
||||
zoom: transform[2],
|
||||
};
|
||||
};
|
||||
};
|
||||
import { OnLoadFunc, XYPosition, Node, Edge, FlowExportObject } from '../types';
|
||||
|
||||
function useOnLoadHandler(onLoad: OnLoadFunc<any> | undefined) {
|
||||
const isInitialized = useRef<boolean>(false);
|
||||
@@ -51,16 +13,42 @@ function useOnLoadHandler(onLoad: OnLoadFunc<any> | undefined) {
|
||||
useEffect(() => {
|
||||
if (!isInitialized.current && initialized) {
|
||||
if (onLoad) {
|
||||
const project = (position: XYPosition): XYPosition => {
|
||||
const { transform, snapToGrid, snapGrid } = store.getState();
|
||||
return pointToRendererPoint(position, transform, snapToGrid, snapGrid);
|
||||
};
|
||||
|
||||
const getNodes = (): Node[] => {
|
||||
const { nodes = [] } = store.getState();
|
||||
return nodes.map((n) => ({ ...n }));
|
||||
};
|
||||
|
||||
const getEdges = (): Edge[] => {
|
||||
const { edges = [] } = store.getState();
|
||||
return edges.map((e) => ({ ...e }));
|
||||
};
|
||||
|
||||
const toObject = (): FlowExportObject => {
|
||||
const { nodes = [], edges = [], transform } = store.getState();
|
||||
|
||||
return {
|
||||
nodes: nodes.map((n) => ({ ...n })),
|
||||
edges: edges.map((e) => ({ ...e })),
|
||||
position: [transform[0], transform[1]],
|
||||
zoom: transform[2],
|
||||
};
|
||||
};
|
||||
|
||||
onLoad({
|
||||
fitView: (params = { padding: 0.1 }) => fitView(params),
|
||||
zoomIn,
|
||||
zoomOut,
|
||||
zoomTo,
|
||||
setTransform,
|
||||
project: onLoadProject(store.getState),
|
||||
getNodes: onLoadGetNodes(store.getState),
|
||||
getEdges: onLoadGetEdges(store.getState),
|
||||
toObject: onLoadToObject(store.getState),
|
||||
project,
|
||||
getNodes,
|
||||
getEdges,
|
||||
toObject,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user