Feature: Draggable edge

This commit is contained in:
Long Nguyen
2020-10-12 21:34:35 +09:00
parent f27634ebe2
commit bd63a0131a
13 changed files with 6583 additions and 9 deletions
+52
View File
@@ -32,6 +32,7 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
elementsSelectable,
markerEndId,
isHidden,
onEitherEndOfEdgePress,
}: WrapEdgeProps) => {
const setSelectedElements = useStoreActions((actions) => actions.setSelectedElements);
@@ -63,12 +64,53 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
[elementsSelectable, id, source, target, type, data, onClick]
);
const handleEitherEndOfEdgePress = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>, isEdgeHeader?: boolean): void => {
if (elementsSelectable) {
setSelectedElements({ id, source, target });
}
const edgeElement: Edge = { id, source, target, type };
if (typeof data !== 'undefined') {
edgeElement.data = data;
}
onEitherEndOfEdgePress(event, edgeElement, isEdgeHeader);
},
[elementsSelectable, id, source, target, type, data, onEitherEndOfEdgePress]
);
const handleEdgeHeaderPress = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>): void => {
handleEitherEndOfEdgePress(event, true);
},
[handleEitherEndOfEdgePress],
);
const handleEdgeFooterPress = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>): void => {
handleEitherEndOfEdgePress(event);
},
[handleEitherEndOfEdgePress],
);
if (isHidden) {
return null;
}
return (
<g className={edgeClasses} onClick={onEdgeClick} style={edgeGroupStyle}>
<g onMouseDown={handleEdgeFooterPress}>
<circle
className="move-handler"
cx={sourceX}
cy={sourceY}
r="12"
stroke="transparent"
fill="transparent"
/>
</g>
<EdgeComponent
id={id}
source={source}
@@ -92,6 +134,16 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
targetPosition={targetPosition}
markerEndId={markerEndId}
/>
<g onMouseDown={handleEdgeHeaderPress}>
<circle
className="move-handler"
cx={targetX}
cy={targetY}
r="12"
stroke="transparent"
fill="transparent"
/>
</g>
</g>
);
};
+4 -4
View File
@@ -15,7 +15,7 @@ import {
} from '../../types';
type ValidConnectionFunc = (connection: Connection) => boolean;
type SetSourceIdFunc = (params: SetConnectionId) => void;
export type SetSourceIdFunc = (params: SetConnectionId) => void;
interface BaseHandleProps {
type: HandleType;
@@ -40,7 +40,7 @@ type Result = {
isHoveringHandle: boolean;
};
function onMouseDown(
export function onMouseDown(
event: ReactMouseEvent,
nodeId: ElementId,
setConnectionNodeId: SetSourceIdFunc,
@@ -67,7 +67,7 @@ function onMouseDown(
y: event.clientY - containerBounds.top,
});
setConnectionNodeId({ connectionNodeId: nodeId, connectionHandleType: handleType });
if (onConnectStart) {
onConnectStart(event, { nodeId, handleType });
}
@@ -136,7 +136,7 @@ function onMouseDown(
function onMouseUp(event: MouseEvent) {
const { connection, isValid } = checkElementBelowIsValid(event);
if (onConnectStop) {
onConnectStop(event);
}