feat(hooks): add useReactFlow hook, refactor types and properties
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
import React, { CSSProperties, HTMLAttributes, MouseEvent as ReactMouseEvent, WheelEvent } from 'react';
|
||||
|
||||
import {
|
||||
OnSelectionChangeFunc,
|
||||
NodeTypesType,
|
||||
EdgeTypesType,
|
||||
Node,
|
||||
Edge,
|
||||
ConnectionMode,
|
||||
ConnectionLineType,
|
||||
ConnectionLineComponent,
|
||||
Viewport,
|
||||
OnConnectStart,
|
||||
OnConnectStop,
|
||||
OnConnectEnd,
|
||||
OnConnect,
|
||||
CoordinateExtent,
|
||||
KeyCode,
|
||||
PanOnScrollMode,
|
||||
OnEdgeUpdateFunc,
|
||||
NodeChange,
|
||||
EdgeChange,
|
||||
OnPaneReady,
|
||||
ProOptions,
|
||||
AttributionPosition,
|
||||
DefaultEdgeOptions,
|
||||
FitViewParams,
|
||||
BackgroundVariant,
|
||||
} from '.';
|
||||
|
||||
export interface ReactFlowProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onPaneReady'> {
|
||||
nodes?: Node[];
|
||||
edges?: Edge[];
|
||||
defaultNodes?: Node[];
|
||||
defaultEdges?: Edge[];
|
||||
defaultEdgeOptions?: DefaultEdgeOptions;
|
||||
onNodesChange?: (nodeChanges: NodeChange[]) => void;
|
||||
onEdgesChange?: (edgeChanges: EdgeChange[]) => void;
|
||||
onNodeClick?: (event: React.MouseEvent, node: Node) => void;
|
||||
onEdgeClick?: (event: React.MouseEvent, node: Edge) => void;
|
||||
onNodeDoubleClick?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onNodeMouseEnter?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onNodeMouseMove?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onNodeMouseLeave?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onNodeContextMenu?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onNodeDragStart?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onNodeDrag?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onNodeDragStop?: (event: ReactMouseEvent, node: Node) => void;
|
||||
onConnect?: OnConnect;
|
||||
onConnectStart?: OnConnectStart;
|
||||
onConnectStop?: OnConnectStop;
|
||||
onConnectEnd?: OnConnectEnd;
|
||||
onPaneReady?: OnPaneReady;
|
||||
onMove?: (viewport?: Viewport) => void;
|
||||
onMoveStart?: (viewport?: Viewport) => void;
|
||||
onMoveEnd?: (viewport?: Viewport) => void;
|
||||
onSelectionChange?: OnSelectionChangeFunc;
|
||||
onSelectionDragStart?: (event: ReactMouseEvent, nodes: Node[]) => void;
|
||||
onSelectionDrag?: (event: ReactMouseEvent, nodes: Node[]) => void;
|
||||
onSelectionDragStop?: (event: ReactMouseEvent, nodes: Node[]) => void;
|
||||
onSelectionContextMenu?: (event: ReactMouseEvent, nodes: Node[]) => void;
|
||||
onPaneScroll?: (event?: WheelEvent) => void;
|
||||
onPaneClick?: (event: ReactMouseEvent) => void;
|
||||
onPaneContextMenu?: (event: ReactMouseEvent) => void;
|
||||
nodeTypes?: NodeTypesType;
|
||||
edgeTypes?: EdgeTypesType;
|
||||
connectionMode?: ConnectionMode;
|
||||
connectionLineType?: ConnectionLineType;
|
||||
connectionLineStyle?: CSSProperties;
|
||||
connectionLineComponent?: ConnectionLineComponent;
|
||||
deleteKeyCode?: KeyCode;
|
||||
selectionKeyCode?: KeyCode;
|
||||
multiSelectionKeyCode?: KeyCode;
|
||||
zoomActivationKeyCode?: KeyCode;
|
||||
snapToGrid?: boolean;
|
||||
snapGrid?: [number, number];
|
||||
onlyRenderVisibleElements?: boolean;
|
||||
nodesDraggable?: boolean;
|
||||
nodesConnectable?: boolean;
|
||||
elementsSelectable?: boolean;
|
||||
selectNodesOnDrag?: boolean;
|
||||
paneMoveable?: boolean;
|
||||
minZoom?: number;
|
||||
maxZoom?: number;
|
||||
defaultZoom?: number;
|
||||
defaultPosition?: [number, number];
|
||||
translateExtent?: CoordinateExtent;
|
||||
preventScrolling?: boolean;
|
||||
nodeExtent?: CoordinateExtent;
|
||||
defaultMarkerColor?: string;
|
||||
zoomOnScroll?: boolean;
|
||||
zoomOnPinch?: boolean;
|
||||
panOnScroll?: boolean;
|
||||
panOnScrollSpeed?: number;
|
||||
panOnScrollMode?: PanOnScrollMode;
|
||||
zoomOnDoubleClick?: boolean;
|
||||
onEdgeUpdate?: OnEdgeUpdateFunc;
|
||||
onEdgeContextMenu?: (event: ReactMouseEvent, edge: Edge) => void;
|
||||
onEdgeMouseEnter?: (event: ReactMouseEvent, edge: Edge) => void;
|
||||
onEdgeMouseMove?: (event: ReactMouseEvent, edge: Edge) => void;
|
||||
onEdgeMouseLeave?: (event: ReactMouseEvent, edge: Edge) => void;
|
||||
onEdgeDoubleClick?: (event: ReactMouseEvent, edge: Edge) => void;
|
||||
onEdgeUpdateStart?: (event: ReactMouseEvent, edge: Edge) => void;
|
||||
onEdgeUpdateEnd?: (event: MouseEvent, edge: Edge) => void;
|
||||
edgeUpdaterRadius?: number;
|
||||
noDragClassName?: string;
|
||||
noWheelClassName?: string;
|
||||
noPanClassName?: string;
|
||||
fitViewOnInit?: boolean;
|
||||
connectOnClick?: boolean;
|
||||
attributionPosition?: AttributionPosition;
|
||||
pro?: ProOptions;
|
||||
}
|
||||
|
||||
export type ReactFlowRefType = HTMLDivElement;
|
||||
|
||||
export type GetMiniMapNodeAttribute<NodeData = any> = (node: Node<NodeData>) => string;
|
||||
|
||||
export interface MiniMapProps<NodeData = any> extends HTMLAttributes<SVGSVGElement> {
|
||||
nodeColor?: string | GetMiniMapNodeAttribute<NodeData>;
|
||||
nodeStrokeColor?: string | GetMiniMapNodeAttribute<NodeData>;
|
||||
nodeClassName?: string | GetMiniMapNodeAttribute<NodeData>;
|
||||
nodeBorderRadius?: number;
|
||||
nodeStrokeWidth?: number;
|
||||
maskColor?: string;
|
||||
}
|
||||
|
||||
export interface ControlProps extends HTMLAttributes<HTMLDivElement> {
|
||||
showZoom?: boolean;
|
||||
showFitView?: boolean;
|
||||
showInteractive?: boolean;
|
||||
fitViewParams?: FitViewParams;
|
||||
onZoomIn?: () => void;
|
||||
onZoomOut?: () => void;
|
||||
onFitView?: () => void;
|
||||
onInteractiveChange?: (interactiveStatus: boolean) => void;
|
||||
}
|
||||
|
||||
export interface ControlButtonProps extends HTMLAttributes<HTMLButtonElement> {}
|
||||
|
||||
export interface BackgroundProps extends HTMLAttributes<SVGElement> {
|
||||
variant?: BackgroundVariant;
|
||||
gap?: number;
|
||||
color?: string;
|
||||
size?: number;
|
||||
}
|
||||
+15
-36
@@ -7,6 +7,7 @@ import { Node, NodeInternals, NodeDimensionUpdate, NodeDiffUpdate } from './node
|
||||
import { Edge } from './edges';
|
||||
import { HandleType, StartHandle } from './handles';
|
||||
import { DefaultEdgeOptions } from '.';
|
||||
import { ReactFlowInstance } from './instance';
|
||||
|
||||
export type NodeTypesType = { [key: string]: ReactNode };
|
||||
export type EdgeTypesType = NodeTypesType;
|
||||
@@ -15,36 +16,21 @@ export type FitView = (fitViewOptions?: FitViewParams) => void;
|
||||
|
||||
export type Project = (position: XYPosition) => XYPosition;
|
||||
|
||||
export type ToObject<T = any> = () => FlowExportObject<T>;
|
||||
|
||||
export type OnNodesChange = (nodes: NodeChange[]) => void;
|
||||
|
||||
export type OnEdgesChange = (nodes: EdgeChange[]) => void;
|
||||
|
||||
export type ZoomInOut = (options?: ZoomPanHelperFunctionOptions) => void;
|
||||
export type ZoomTo = (zoomLevel: number, options?: ZoomPanHelperFunctionOptions) => void;
|
||||
export type ZoomInOut = (options?: ViewportHelperFunctionOptions) => void;
|
||||
export type ZoomTo = (zoomLevel: number, options?: ViewportHelperFunctionOptions) => void;
|
||||
export type GetZoom = () => number;
|
||||
export type GetTransform = () => FlowTransform;
|
||||
export type SetTransform = (transform: FlowTransform, options?: ZoomPanHelperFunctionOptions) => void;
|
||||
export type GetViewport = () => Viewport;
|
||||
export type SetViewport = (viewport: Viewport, options?: ViewportHelperFunctionOptions) => void;
|
||||
export type SetCenter = (x: number, y: number, options?: SetCenterOptions) => void;
|
||||
export type FitBounds = (bounds: Rect, options?: FitBoundsOptions) => void;
|
||||
|
||||
export type ReactFlowInstance<T = any> = {
|
||||
zoomIn: ZoomInOut;
|
||||
zoomOut: ZoomInOut;
|
||||
zoomTo: ZoomTo;
|
||||
getZoom: () => number;
|
||||
setCenter: SetCenter;
|
||||
fitView: FitView;
|
||||
project: Project;
|
||||
getNodes: () => Node<T>[];
|
||||
getEdges: () => Edge<T>[];
|
||||
setTransform: SetTransform;
|
||||
getTransform: () => FlowTransform;
|
||||
toObject: ToObject<T>;
|
||||
};
|
||||
|
||||
export type OnPaneReady<T = any> = (reactFlowInstance: ReactFlowInstance<T>) => void;
|
||||
export type OnPaneReady<NodeData = any, EdgeData = any> = (
|
||||
reactFlowInstance: ReactFlowInstance<NodeData, EdgeData>
|
||||
) => void;
|
||||
|
||||
export interface Connection {
|
||||
source: string | null;
|
||||
@@ -58,13 +44,6 @@ export enum ConnectionMode {
|
||||
Loose = 'loose',
|
||||
}
|
||||
|
||||
export type FlowExportObject<T = any> = {
|
||||
nodes: Node<T>[];
|
||||
edges: Edge<T>[];
|
||||
position: [number, number];
|
||||
zoom: number;
|
||||
};
|
||||
|
||||
export type OnConnect = (connection: Connection) => void;
|
||||
|
||||
export type FitViewParams = {
|
||||
@@ -92,7 +71,7 @@ export enum BackgroundVariant {
|
||||
Dots = 'dots',
|
||||
}
|
||||
|
||||
export type FlowTransform = {
|
||||
export type Viewport = {
|
||||
x: number;
|
||||
y: number;
|
||||
zoom: number;
|
||||
@@ -108,25 +87,25 @@ export enum PanOnScrollMode {
|
||||
Horizontal = 'horizontal',
|
||||
}
|
||||
|
||||
export type ZoomPanHelperFunctionOptions = {
|
||||
export type ViewportHelperFunctionOptions = {
|
||||
duration?: number;
|
||||
};
|
||||
|
||||
export type SetCenterOptions = ZoomPanHelperFunctionOptions & {
|
||||
export type SetCenterOptions = ViewportHelperFunctionOptions & {
|
||||
zoom?: number;
|
||||
};
|
||||
|
||||
export type FitBoundsOptions = ZoomPanHelperFunctionOptions & {
|
||||
export type FitBoundsOptions = ViewportHelperFunctionOptions & {
|
||||
padding?: number;
|
||||
};
|
||||
|
||||
export interface ZoomPanHelperFunctions {
|
||||
export interface ViewportHelperFunctions {
|
||||
zoomIn: ZoomInOut;
|
||||
zoomOut: ZoomInOut;
|
||||
zoomTo: ZoomTo;
|
||||
getZoom: GetZoom;
|
||||
setTransform: SetTransform;
|
||||
getTransform: GetTransform;
|
||||
setViewport: SetViewport;
|
||||
getViewport: GetViewport;
|
||||
fitView: FitView;
|
||||
setCenter: SetCenter;
|
||||
fitBounds: FitBounds;
|
||||
|
||||
@@ -4,3 +4,5 @@ export * from './edges';
|
||||
export * from './handles';
|
||||
export * from './changes';
|
||||
export * from './utils';
|
||||
export * from './instance';
|
||||
export * from './component-props';
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { ViewportHelperFunctions, Viewport } from './general';
|
||||
import { Node } from './nodes';
|
||||
import { Edge } from './edges';
|
||||
|
||||
export type ReactFlowJsonObject<NodeData = any, EdgeData = any> = {
|
||||
nodes: Node<NodeData>[];
|
||||
edges: Edge<EdgeData>[];
|
||||
viewport: Viewport;
|
||||
};
|
||||
|
||||
export namespace Instance {
|
||||
export type GetNodes<NodeData> = () => Node<NodeData>[];
|
||||
export type SetNodes<NodeData> = (
|
||||
payload: Node<NodeData>[] | ((nodes: Node<NodeData>[]) => Node<NodeData>[])
|
||||
) => void;
|
||||
export type AddNodes<NodeData> = (payload: Node<NodeData>[] | Node<NodeData>) => void;
|
||||
export type GetNode<NodeData> = (id: string) => Node<NodeData> | undefined;
|
||||
export type GetEdges<EdgeData> = () => Edge<EdgeData>[];
|
||||
export type SetEdges<EdgeData> = (
|
||||
payload: Edge<EdgeData>[] | ((edges: Edge<EdgeData>[]) => Edge<EdgeData>[])
|
||||
) => void;
|
||||
export type GetEdge<EdgeData> = (id: string) => Edge<EdgeData> | undefined;
|
||||
export type AddEdges<EdgeData> = (payload: Edge<EdgeData>[] | Edge<EdgeData>) => void;
|
||||
export type ToObject<NodeData = any, EdgeData = any> = () => ReactFlowJsonObject<NodeData, EdgeData>;
|
||||
}
|
||||
|
||||
export type ReactFlowInstance<NodeData = any, EdgeData = any> = {
|
||||
getNodes: Instance.GetNodes<NodeData>;
|
||||
setNodes: Instance.SetNodes<NodeData>;
|
||||
addNodes: Instance.AddNodes<NodeData>;
|
||||
getNode: Instance.GetNode<NodeData>;
|
||||
getEdges: Instance.GetEdges<EdgeData>;
|
||||
setEdges: Instance.SetEdges<EdgeData>;
|
||||
addEdges: Instance.AddEdges<EdgeData>;
|
||||
getEdge: Instance.GetEdge<EdgeData>;
|
||||
toObject: Instance.ToObject<NodeData, EdgeData>;
|
||||
viewportInitialized: boolean;
|
||||
} & Omit<ViewportHelperFunctions, 'initialized'>;
|
||||
Reference in New Issue
Block a user