From c620d0f40c17847643292f487c7ace7f8b9068af Mon Sep 17 00:00:00 2001 From: moklick Date: Fri, 29 May 2020 18:53:23 +0200 Subject: [PATCH] refactor(connectionLine): create connection line type enum --- src/components/ConnectionLine/index.tsx | 10 +++++----- src/container/EdgeRenderer/index.tsx | 4 ++-- src/container/GraphView/index.tsx | 15 ++++++++++++--- src/container/ReactFlow/index.tsx | 18 +++++++++++++----- src/types/index.ts | 5 +++++ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/components/ConnectionLine/index.tsx b/src/components/ConnectionLine/index.tsx index 9eec2b54..b2d81d37 100644 --- a/src/components/ConnectionLine/index.tsx +++ b/src/components/ConnectionLine/index.tsx @@ -1,13 +1,13 @@ import React, { useEffect, useState, CSSProperties } from 'react'; import cx from 'classnames'; -import { ElementId, Node, Transform, HandleElement, Position } from '../../types'; +import { ElementId, Node, Transform, HandleElement, Position, ConnectionLineType } from '../../types'; interface ConnectionLineProps { connectionSourceId: ElementId; connectionPositionX: number; connectionPositionY: number; - connectionLineType?: string | null; + connectionLineType: ConnectionLineType; nodes: Node[]; transform: Transform; isInteractive: boolean; @@ -17,10 +17,10 @@ interface ConnectionLineProps { export default ({ connectionSourceId, - connectionLineStyle = {}, + connectionLineStyle, connectionPositionX, connectionPositionY, - connectionLineType, + connectionLineType = ConnectionLineType.Bezier, nodes = [], className, transform, @@ -60,7 +60,7 @@ export default ({ let dAttr: string = ''; - if (connectionLineType === 'bezier') { + if (connectionLineType === ConnectionLineType.Bezier) { if (sourceHandle?.position === Position.Left || sourceHandle?.position === Position.Right) { const xOffset = Math.abs(targetX - sourceX) / 2; const centerX = targetX < sourceX ? targetX + xOffset : targetX - xOffset; diff --git a/src/container/EdgeRenderer/index.tsx b/src/container/EdgeRenderer/index.tsx index a25b0167..762e4175 100644 --- a/src/container/EdgeRenderer/index.tsx +++ b/src/container/EdgeRenderer/index.tsx @@ -3,14 +3,14 @@ import React, { memo, CSSProperties } from 'react'; import { useStoreState } from '../../store/hooks'; import ConnectionLine from '../../components/ConnectionLine/index'; import { isEdge } from '../../utils/graph'; -import { XYPosition, Position, Edge, Node, ElementId, HandleElement, Elements } from '../../types'; +import { XYPosition, Position, Edge, Node, ElementId, HandleElement, Elements, ConnectionLineType } from '../../types'; interface EdgeRendererProps { width: number; height: number; edgeTypes: any; + connectionLineType: ConnectionLineType; connectionLineStyle?: CSSProperties; - connectionLineType?: string; onElementClick?: (element: Node | Edge) => void; } diff --git a/src/container/GraphView/index.tsx b/src/container/GraphView/index.tsx index 54e8450b..1dca125c 100644 --- a/src/container/GraphView/index.tsx +++ b/src/container/GraphView/index.tsx @@ -12,7 +12,16 @@ import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler'; import useElementUpdater from '../../hooks/useElementUpdater'; import { getDimensions } from '../../utils'; import { fitView, zoomIn, zoomOut, project } from '../../utils/graph'; -import { Elements, NodeTypesType, EdgeTypesType, OnLoadFunc, Node, Edge, Connection } from '../../types'; +import { + Elements, + NodeTypesType, + EdgeTypesType, + OnLoadFunc, + Node, + Edge, + Connection, + ConnectionLineType, +} from '../../types'; export interface GraphViewProps { elements: Elements; @@ -26,8 +35,8 @@ export interface GraphViewProps { selectionKeyCode: number; nodeTypes: NodeTypesType; edgeTypes: EdgeTypesType; - connectionLineType: string; - connectionLineStyle: CSSProperties; + connectionLineType: ConnectionLineType; + connectionLineStyle?: CSSProperties; deleteKeyCode: number; snapToGrid: boolean; snapGrid: [number, number]; diff --git a/src/container/ReactFlow/index.tsx b/src/container/ReactFlow/index.tsx index c3c1d99a..1339529e 100644 --- a/src/container/ReactFlow/index.tsx +++ b/src/container/ReactFlow/index.tsx @@ -20,7 +20,16 @@ import StraightEdge from '../../components/Edges/StraightEdge'; import StepEdge from '../../components/Edges/StepEdge'; import { createEdgeTypes } from '../EdgeRenderer/utils'; import store from '../../store'; -import { Elements, NodeTypesType, EdgeTypesType, OnLoadFunc, Node, Edge, Connection } from '../../types'; +import { + Elements, + NodeTypesType, + EdgeTypesType, + OnLoadFunc, + Node, + Edge, + Connection, + ConnectionLineType, +} from '../../types'; import '../../style.css'; @@ -36,8 +45,8 @@ export interface ReactFlowProps extends Omit, 'on onSelectionChange?: (elements: Elements | null) => void; nodeTypes: NodeTypesType; edgeTypes: EdgeTypesType; - connectionLineType: string; - connectionLineStyle: CSSProperties; + connectionLineType: ConnectionLineType; + connectionLineStyle?: CSSProperties; deleteKeyCode: number; selectionKeyCode: number; snapToGrid: boolean; @@ -119,8 +128,7 @@ ReactFlow.defaultProps = { straight: StraightEdge, step: StepEdge, }, - connectionLineType: 'bezier', - connectionLineStyle: {}, + connectionLineType: ConnectionLineType.Bezier, deleteKeyCode: 8, selectionKeyCode: 16, snapToGrid: false, diff --git a/src/types/index.ts b/src/types/index.ts index ce324c63..8a29f12b 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -153,6 +153,11 @@ export interface Connection { target: ElementId | null; } +export enum ConnectionLineType { + Bezier = 'bezier', + Straight = 'straight', +} + export type OnConnectFunc = (connection: Connection) => void; export interface HandleElement extends XYPosition, Dimensions {