refactor(edges): pass original source and target ids, cleanup #476

This commit is contained in:
moklick
2020-09-15 11:17:06 +02:00
parent 63d1e4b218
commit 0b15211e18
5 changed files with 116 additions and 101 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ import React, { memo } from 'react';
import EdgeText from './EdgeText'; import EdgeText from './EdgeText';
import { getMarkerEnd, getCenter } from './utils'; import { getMarkerEnd, getCenter } from './utils';
import { EdgeBezierProps, Position } from '../../types'; import { EdgeProps, Position } from '../../types';
interface GetBezierPathParams { interface GetBezierPathParams {
sourceX: number; sourceX: number;
@@ -55,7 +55,7 @@ export default memo(
style, style,
arrowHeadType, arrowHeadType,
markerEndId, markerEndId,
}: EdgeBezierProps) => { }: EdgeProps) => {
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY }); const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY });
const path = getBezierPath({ const path = getBezierPath({
sourceX, sourceX,
+58 -56
View File
@@ -1,86 +1,80 @@
import React, { memo, ComponentType, CSSProperties } from 'react'; import React, { memo, useMemo, ComponentType, CSSProperties, useCallback } from 'react';
import cc from 'classcat'; import cc from 'classcat';
import { useStoreActions } from '../../store/hooks'; import { useStoreActions } from '../../store/hooks';
import { ElementId, Edge, EdgeCompProps } from '../../types'; import { Edge, EdgeProps, WrapEdgeProps } from '../../types';
interface EdgeWrapperProps { export default (EdgeComponent: ComponentType<EdgeProps>) => {
id: ElementId;
source: ElementId;
target: ElementId;
type: any;
label?: string;
labelStyle?: CSSProperties;
labelShowBg?: boolean;
labelBgStyle?: CSSProperties;
labelBgPadding?: [number, number];
labelBgBorderRadius?: number;
className?: string;
onClick?: (event: React.MouseEvent, edge: Edge) => void;
animated?: boolean;
selected: boolean;
elementsSelectable: boolean;
isHidden?: boolean;
data?: any;
}
export default (EdgeComponent: ComponentType<EdgeCompProps>) => {
const EdgeWrapper = ({ const EdgeWrapper = ({
id, id,
source, className,
target,
type, type,
animated, data,
selected,
onClick, onClick,
elementsSelectable, selected,
animated,
label, label,
labelStyle, labelStyle,
labelShowBg, labelShowBg,
labelBgStyle, labelBgStyle,
labelBgPadding, labelBgPadding,
labelBgBorderRadius, labelBgBorderRadius,
className, style,
arrowHeadType,
source,
target,
sourceX,
sourceY,
targetX,
targetY,
sourcePosition,
targetPosition,
elementsSelectable,
markerEndId,
isHidden, isHidden,
data, }: WrapEdgeProps) => {
...rest
}: EdgeWrapperProps) => {
const setSelectedElements = useStoreActions((actions) => actions.setSelectedElements); const setSelectedElements = useStoreActions((actions) => actions.setSelectedElements);
const edgeClasses = cc(['react-flow__edge', `react-flow__edge-${type}`, className, { selected, animated }]);
const edgeGroupStyle: CSSProperties = useMemo(
() => ({
pointerEvents: elementsSelectable || onClick ? 'all' : 'none',
}),
[elementsSelectable, onClick]
);
const onEdgeClick = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>): void => {
if (elementsSelectable) {
setSelectedElements({ id, source, target });
}
if (onClick) {
const edgeElement: Edge = { id, source, target, type };
if (typeof data !== 'undefined') {
edgeElement.data = data;
}
onClick(event, edgeElement);
}
},
[elementsSelectable, id, source, target, type, data, onClick]
);
if (isHidden) { if (isHidden) {
return null; return null;
} }
const edgeClasses = cc(['react-flow__edge', `react-flow__edge-${type}`, className, { selected, animated }]);
const edgeGroupStyle: CSSProperties = {
pointerEvents: elementsSelectable || onClick ? 'all' : 'none',
};
const onEdgeClick = (event: React.MouseEvent<SVGGElement, MouseEvent>): void => {
if (elementsSelectable) {
setSelectedElements({ id, source, target });
}
if (onClick) {
const edgeElement: Edge = { id, source, target, type };
if (typeof data !== 'undefined') {
edgeElement.data = data;
}
onClick(event, edgeElement);
}
};
return ( return (
<g className={edgeClasses} onClick={onEdgeClick} style={edgeGroupStyle}> <g className={edgeClasses} onClick={onEdgeClick} style={edgeGroupStyle}>
<EdgeComponent <EdgeComponent
id={id} id={id}
source={source} source={source}
target={target} target={target}
type={type}
animated={animated}
selected={selected} selected={selected}
onClick={onClick} animated={animated}
label={label} label={label}
labelStyle={labelStyle} labelStyle={labelStyle}
labelShowBg={labelShowBg} labelShowBg={labelShowBg}
@@ -88,7 +82,15 @@ export default (EdgeComponent: ComponentType<EdgeCompProps>) => {
labelBgPadding={labelBgPadding} labelBgPadding={labelBgPadding}
labelBgBorderRadius={labelBgBorderRadius} labelBgBorderRadius={labelBgBorderRadius}
data={data} data={data}
{...rest} style={style}
arrowHeadType={arrowHeadType}
sourceX={sourceX}
sourceY={sourceY}
targetX={targetX}
targetY={targetY}
sourcePosition={sourcePosition}
targetPosition={targetPosition}
markerEndId={markerEndId}
/> />
</g> </g>
); );
+2 -2
View File
@@ -175,8 +175,8 @@ function renderEdge(
labelBgBorderRadius={edge.labelBgBorderRadius} labelBgBorderRadius={edge.labelBgBorderRadius}
style={edge.style} style={edge.style}
arrowHeadType={edge.arrowHeadType} arrowHeadType={edge.arrowHeadType}
source={sourceId} source={edge.source}
target={targetId} target={edge.target}
sourceHandleId={sourceHandleId} sourceHandleId={sourceHandleId}
targetHandleId={targetHandleId} targetHandleId={targetHandleId}
sourceX={sourceX} sourceX={sourceX}
+6 -6
View File
@@ -3,21 +3,21 @@ import { ComponentType } from 'react';
import { BezierEdge, StepEdge, SmoothStepEdge, StraightEdge } from '../../components/Edges'; import { BezierEdge, StepEdge, SmoothStepEdge, StraightEdge } from '../../components/Edges';
import wrapEdge from '../../components/Edges/wrapEdge'; import wrapEdge from '../../components/Edges/wrapEdge';
import { EdgeTypesType, EdgeCompProps } from '../../types'; import { EdgeTypesType, EdgeProps } from '../../types';
export function createEdgeTypes(edgeTypes: EdgeTypesType): EdgeTypesType { export function createEdgeTypes(edgeTypes: EdgeTypesType): EdgeTypesType {
const standardTypes: EdgeTypesType = { const standardTypes: EdgeTypesType = {
default: wrapEdge((edgeTypes.default || BezierEdge) as ComponentType<EdgeCompProps>), default: wrapEdge((edgeTypes.default || BezierEdge) as ComponentType<EdgeProps>),
straight: wrapEdge((edgeTypes.bezier || StraightEdge) as ComponentType<EdgeCompProps>), straight: wrapEdge((edgeTypes.bezier || StraightEdge) as ComponentType<EdgeProps>),
step: wrapEdge((edgeTypes.step || StepEdge) as ComponentType<EdgeCompProps>), step: wrapEdge((edgeTypes.step || StepEdge) as ComponentType<EdgeProps>),
smoothstep: wrapEdge((edgeTypes.step || SmoothStepEdge) as ComponentType<EdgeCompProps>), smoothstep: wrapEdge((edgeTypes.step || SmoothStepEdge) as ComponentType<EdgeProps>),
}; };
const wrappedTypes = {} as EdgeTypesType; const wrappedTypes = {} as EdgeTypesType;
const specialTypes: EdgeTypesType = Object.keys(edgeTypes) const specialTypes: EdgeTypesType = Object.keys(edgeTypes)
.filter((k) => !['default', 'bezier'].includes(k)) .filter((k) => !['default', 'bezier'].includes(k))
.reduce((res, key) => { .reduce((res, key) => {
res[key] = wrapEdge((edgeTypes[key] || BezierEdge) as ComponentType<EdgeCompProps>); res[key] = wrapEdge((edgeTypes[key] || BezierEdge) as ComponentType<EdgeProps>);
return res; return res;
}, wrappedTypes); }, wrappedTypes);
+48 -35
View File
@@ -89,12 +89,48 @@ export interface SelectionRect extends Rect {
draw: boolean; draw: boolean;
} }
export interface EdgeProps { export interface WrapEdgeProps {
id: ElementId; id: ElementId;
className?: string;
type: string;
data?: any;
onClick?: (event: React.MouseEvent, edge: Edge) => void;
selected: boolean;
animated?: boolean;
label?: string;
labelStyle?: CSSProperties;
labelShowBg?: boolean;
labelBgStyle?: CSSProperties;
labelBgPadding?: [number, number];
labelBgBorderRadius?: number;
style?: CSSProperties;
arrowHeadType?: ArrowHeadType;
source: ElementId;
target: ElementId;
sourceHandleId?: string;
sourceX: number; sourceX: number;
sourceY: number; sourceY: number;
targetX: number; targetX: number;
targetY: number; targetY: number;
sourcePosition: Position;
targetPosition: Position;
elementsSelectable?: boolean;
markerEndId?: string;
isHidden?: boolean;
}
export interface EdgeProps {
id: ElementId;
source: ElementId;
target: ElementId;
sourceX: number;
sourceY: number;
targetX: number;
targetY: number;
selected?: boolean;
animated?: boolean;
sourcePosition: Position;
targetPosition: Position;
label?: string; label?: string;
labelStyle?: CSSProperties; labelStyle?: CSSProperties;
labelShowBg?: boolean; labelShowBg?: boolean;
@@ -106,14 +142,19 @@ export interface EdgeProps {
markerEndId?: string; markerEndId?: string;
data?: any; data?: any;
} }
export interface EdgeSmoothStepProps extends EdgeProps {
export interface EdgeBezierProps extends EdgeProps { borderRadius?: number;
sourcePosition: Position;
targetPosition: Position;
} }
export interface EdgeSmoothStepProps extends EdgeBezierProps { export interface EdgeTextProps {
borderRadius?: number; x: number;
y: number;
label?: string;
labelStyle?: CSSProperties;
labelShowBg?: boolean;
labelBgStyle?: CSSProperties;
labelBgPadding?: [number, number];
labelBgBorderRadius?: number;
} }
export interface NodeProps { export interface NodeProps {
@@ -237,34 +278,6 @@ export interface HandleProps {
className?: string; className?: string;
} }
export interface EdgeCompProps {
id: ElementId;
source: ElementId;
target: ElementId;
type: any;
label?: string;
labelStyle?: CSSProperties;
labelShowBg?: boolean;
labelBgStyle?: CSSProperties;
labelBgPadding?: [number, number];
labelBgBorderRadius?: number;
onClick?: (event: ReactMouseEvent, edge: Edge) => void;
animated?: boolean;
selected?: boolean;
data?: any;
}
export interface EdgeTextProps {
x: number;
y: number;
label?: string;
labelStyle?: CSSProperties;
labelShowBg?: boolean;
labelBgStyle?: CSSProperties;
labelBgPadding?: [number, number];
labelBgBorderRadius?: number;
}
export type NodePosUpdate = { export type NodePosUpdate = {
id: ElementId; id: ElementId;
pos: XYPosition; pos: XYPosition;