fix(connectionLine): sourceHandle with connectionMode = loose
This commit is contained in:
@@ -11,7 +11,7 @@ const onConnect = (params: Connection | Edge) => console.log('handle onConnect',
|
|||||||
const ColorSelectorNode: FC<NodeProps> = ({ data, isConnectable }) => {
|
const ColorSelectorNode: FC<NodeProps> = ({ data, isConnectable }) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Handle type="target" position={Position.Left} style={targetHandleStyle} onConnect={onConnect} />
|
<Handle type="source" position={Position.Left} style={targetHandleStyle} onConnect={onConnect} />
|
||||||
<div>
|
<div>
|
||||||
Custom Color Picker Node: <strong>{data.color}</strong>
|
Custom Color Picker Node: <strong>{data.color}</strong>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,11 +7,13 @@ import ReactFlow, {
|
|||||||
addEdge,
|
addEdge,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
Controls,
|
Controls,
|
||||||
|
updateEdge,
|
||||||
Node,
|
Node,
|
||||||
FlowElement,
|
FlowElement,
|
||||||
OnLoadParams,
|
OnLoadParams,
|
||||||
Elements,
|
Elements,
|
||||||
Position,
|
Position,
|
||||||
|
ConnectionMode,
|
||||||
SnapGrid,
|
SnapGrid,
|
||||||
Connection,
|
Connection,
|
||||||
Edge,
|
Edge,
|
||||||
@@ -79,6 +81,7 @@ const CustomNodeFlow = () => {
|
|||||||
data: { label: 'Output A' },
|
data: { label: 'Output A' },
|
||||||
position: { x: 550, y: 25 },
|
position: { x: 550, y: 25 },
|
||||||
targetPosition: Position.Left,
|
targetPosition: Position.Left,
|
||||||
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '4',
|
id: '4',
|
||||||
@@ -86,6 +89,7 @@ const CustomNodeFlow = () => {
|
|||||||
data: { label: 'Output B' },
|
data: { label: 'Output B' },
|
||||||
position: { x: 550, y: 100 },
|
position: { x: 550, y: 100 },
|
||||||
targetPosition: Position.Left,
|
targetPosition: Position.Left,
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } },
|
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } },
|
||||||
@@ -97,7 +101,8 @@ const CustomNodeFlow = () => {
|
|||||||
const onElementsRemove = (elementsToRemove: Elements) => setElements((els) => removeElements(elementsToRemove, els));
|
const onElementsRemove = (elementsToRemove: Elements) => setElements((els) => removeElements(elementsToRemove, els));
|
||||||
const onConnect = (params: Connection | Edge) =>
|
const onConnect = (params: Connection | Edge) =>
|
||||||
setElements((els) => addEdge({ ...params, animated: true, style: { stroke: '#fff' } }, els));
|
setElements((els) => addEdge({ ...params, animated: true, style: { stroke: '#fff' } }, els));
|
||||||
|
const onEdgeUpdate = (oldEdge: Edge, newConnection: Connection) =>
|
||||||
|
setElements((els) => updateEdge(oldEdge, newConnection, els));
|
||||||
return (
|
return (
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
elements={elements}
|
elements={elements}
|
||||||
@@ -112,6 +117,9 @@ const CustomNodeFlow = () => {
|
|||||||
snapToGrid={true}
|
snapToGrid={true}
|
||||||
snapGrid={snapGrid}
|
snapGrid={snapGrid}
|
||||||
defaultZoom={1.5}
|
defaultZoom={1.5}
|
||||||
|
onEdgeUpdate={onEdgeUpdate}
|
||||||
|
connectionMode={ConnectionMode.Loose}
|
||||||
|
|
||||||
>
|
>
|
||||||
<MiniMap
|
<MiniMap
|
||||||
nodeStrokeColor={(n: Node): string => {
|
nodeStrokeColor={(n: Node): string => {
|
||||||
|
|||||||
@@ -53,9 +53,22 @@ export default ({
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const sourceHandle = handleId
|
const getSourceHandle = (handleId: ElementId | null, sourceNode: Node) => {
|
||||||
? sourceNode.__rf.handleBounds[connectionHandleType].find((d: HandleElement) => d.id === handleId)
|
const handleBound = sourceNode.__rf.handleBounds[connectionHandleType];
|
||||||
: sourceNode.__rf.handleBounds[connectionHandleType][0];
|
if (handleBound) {
|
||||||
|
if (handleId) {
|
||||||
|
return sourceNode.__rf.handleBounds[connectionHandleType].find((d: HandleElement) => d.id === handleId);
|
||||||
|
}
|
||||||
|
return sourceNode.__rf.handleBounds[connectionHandleType][0]
|
||||||
|
}
|
||||||
|
const handleType = connectionHandleType === 'source' ? 'target' : 'source';
|
||||||
|
if (handleId) {
|
||||||
|
return sourceNode.__rf.handleBounds[handleType].find((d: HandleElement) => d.id === handleId);
|
||||||
|
}
|
||||||
|
return sourceNode.__rf.handleBounds[handleType][0]
|
||||||
|
}
|
||||||
|
|
||||||
|
const sourceHandle = getSourceHandle(handleId, sourceNode);
|
||||||
const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : sourceNode.__rf.width / 2;
|
const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : sourceNode.__rf.width / 2;
|
||||||
const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.__rf.height;
|
const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.__rf.height;
|
||||||
const sourceX = sourceNode.__rf.position.x + sourceHandleX;
|
const sourceX = sourceNode.__rf.position.x + sourceHandleX;
|
||||||
@@ -68,6 +81,7 @@ export default ({
|
|||||||
const targetPosition = isRightOrLeft ? Position.Left : Position.Top;
|
const targetPosition = isRightOrLeft ? Position.Left : Position.Top;
|
||||||
|
|
||||||
if (CustomConnectionLineComponent) {
|
if (CustomConnectionLineComponent) {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<g className="react-flow__connection">
|
<g className="react-flow__connection">
|
||||||
<CustomConnectionLineComponent
|
<CustomConnectionLineComponent
|
||||||
|
|||||||
Reference in New Issue
Block a user