refactor(edge-updater): add separate class name for source and target, cleanup

This commit is contained in:
moklick
2022-09-20 16:00:22 +02:00
parent ef24df9c02
commit 809de82eb7
2 changed files with 44 additions and 28 deletions
@@ -1,4 +1,4 @@
import { FC, HTMLAttributes } from 'react'; import { FC, MouseEvent as ReactMouseEvent, SVGAttributes } from 'react';
import cc from 'classcat'; import cc from 'classcat';
import { Position } from '../../types'; import { Position } from '../../types';
@@ -15,22 +15,34 @@ const shiftY = (y: number, shift: number, position: Position): number => {
return y; return y;
}; };
export interface EdgeAnchorProps extends HTMLAttributes<HTMLDivElement> { export interface EdgeAnchorProps extends SVGAttributes<SVGGElement> {
position: Position; position: Position;
centerX: number; centerX: number;
centerY: number; centerY: number;
radius?: number; radius?: number;
onMouseDown: (event: ReactMouseEvent<SVGGElement, MouseEvent>) => void;
onMouseEnter: (event: ReactMouseEvent<SVGGElement, MouseEvent>) => void;
onMouseOut: (event: ReactMouseEvent<SVGGElement, MouseEvent>) => void;
type: string;
} }
const EdgeUpdaterClassName = 'react-flow__edgeupdater';
export const EdgeAnchor: FC<EdgeAnchorProps> = ({ export const EdgeAnchor: FC<EdgeAnchorProps> = ({
className,
position, position,
centerX, centerX,
centerY, centerY,
radius = 10, radius = 10,
onMouseDown,
onMouseEnter,
onMouseOut,
type,
}: EdgeAnchorProps) => ( }: EdgeAnchorProps) => (
<circle <circle
className={cc(['react-flow__edgeupdater', className])} onMouseDown={onMouseDown}
onMouseEnter={onMouseEnter}
onMouseOut={onMouseOut}
className={cc([EdgeUpdaterClassName, `${EdgeUpdaterClassName}-${type}`])}
cx={shiftX(centerX, radius, position)} cx={shiftX(centerX, radius, position)}
cy={shiftY(centerY, radius, position)} cy={shiftY(centerY, radius, position)}
r={radius} r={radius}
+28 -24
View File
@@ -129,12 +129,6 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
const inactive = !elementsSelectable && !onClick; const inactive = !elementsSelectable && !onClick;
const handleEdgeUpdate = typeof onEdgeUpdate !== 'undefined'; const handleEdgeUpdate = typeof onEdgeUpdate !== 'undefined';
const edgeClasses = cc([
'react-flow__edge',
`react-flow__edge-${type}`,
className,
{ selected, animated, inactive, updating: updateHover },
]);
const onKeyDown = (event: KeyboardEvent) => { const onKeyDown = (event: KeyboardEvent) => {
if (elementSelectionKeys.includes(event.key) && elementsSelectable) { if (elementSelectionKeys.includes(event.key) && elementsSelectable) {
@@ -152,7 +146,12 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
return ( return (
<g <g
className={edgeClasses} className={cc([
'react-flow__edge',
`react-flow__edge-${type}`,
className,
{ selected, animated, inactive, updating: updateHover },
])}
onClick={onEdgeClick} onClick={onEdgeClick}
onDoubleClick={onEdgeDoubleClickHandler} onDoubleClick={onEdgeDoubleClickHandler}
onContextMenu={onEdgeContextMenu} onContextMenu={onEdgeContextMenu}
@@ -196,24 +195,29 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
interactionWidth={interactionWidth} interactionWidth={interactionWidth}
/> />
)} )}
{handleEdgeUpdate && ( {handleEdgeUpdate && (
<g <>
onMouseDown={onEdgeUpdaterSourceMouseDown} <EdgeAnchor
onMouseEnter={onEdgeUpdaterMouseEnter} position={sourcePosition}
onMouseOut={onEdgeUpdaterMouseOut} centerX={sourceX}
> centerY={sourceY}
<EdgeAnchor position={sourcePosition} centerX={sourceX} centerY={sourceY} radius={edgeUpdaterRadius} /> radius={edgeUpdaterRadius}
</g> onMouseDown={onEdgeUpdaterSourceMouseDown}
)} onMouseEnter={onEdgeUpdaterMouseEnter}
{handleEdgeUpdate && ( onMouseOut={onEdgeUpdaterMouseOut}
<g type="source"
onMouseDown={onEdgeUpdaterTargetMouseDown} />
onMouseEnter={onEdgeUpdaterMouseEnter} <EdgeAnchor
onMouseOut={onEdgeUpdaterMouseOut} position={targetPosition}
> centerX={targetX}
<EdgeAnchor position={targetPosition} centerX={targetX} centerY={targetY} radius={edgeUpdaterRadius} /> centerY={targetY}
</g> radius={edgeUpdaterRadius}
onMouseDown={onEdgeUpdaterTargetMouseDown}
onMouseEnter={onEdgeUpdaterMouseEnter}
onMouseOut={onEdgeUpdaterMouseOut}
type="target"
/>
</>
)} )}
</g> </g>
); );