Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loopback edges #299

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ All props are detailed below.
| `nodes` | `Array<INode>` | `true` | Array of graph nodes. |
| `edges` | `Array<IEdge>` | `true` | Array of graph edges. |
| `allowMultiselect` | `boolean` | `false` | Use Ctrl-Shift-LeftMouse to draw a multiple selection box |
| `allowLoopbackEdge` | `boolean` | `false` | When set to `true` edges that have the same start and end node are allowed to be set by the user.|
| `selected` | `object` | `true` | The currently selected graph entity. |
| `selectedNodes` | `Array<INode>` | `false` | If allowMultiselect is true, this should be the currently selected array of nodes. |
| `selectedEdges` | `Array<IEdge>` | `true` | If allowMultiselect is true, this should be the currently selected array of edges. |
Expand Down
9 changes: 7 additions & 2 deletions src/components/graph-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class GraphView extends React.Component<IGraphViewProps, IGraphViewState> {
zoomDur: 750,
rotateEdgeHandle: true,
centerNodeOnMove: true,
allowLoopbackEdge: false,
};

static getDerivedStateFromProps(
Expand Down Expand Up @@ -1023,7 +1024,6 @@ class GraphView extends React.Component<IGraphViewProps, IGraphViewState> {

if (
edgesMap &&
hoveredNodeData !== edgeEndNode &&
canCreateEdge &&
canCreateEdge(hoveredNodeData, edgeEndNode) &&
!edgesMap[mapId1] &&
Expand Down Expand Up @@ -1089,9 +1089,14 @@ class GraphView extends React.Component<IGraphViewProps, IGraphViewState> {

handleNodeMouseEnter = (event: any, data: any) => {
const { draggingEdge, hoveredNodeData } = this.state;
const { allowLoopbackEdge } = this.props;

// hovered is false when creating edges
if (hoveredNodeData && data !== hoveredNodeData && draggingEdge) {
if (
hoveredNodeData &&
(data !== hoveredNodeData || allowLoopbackEdge) &&
draggingEdge
) {
this.setState({
edgeEndNode: data,
});
Expand Down
14 changes: 6 additions & 8 deletions src/examples/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,11 @@ class Graph extends React.Component<IGraphProps, IGraphState> {
type,
};

// Only add the edge when the source node is not the same as the target
if (viewEdge.source !== viewEdge.target) {
graph.edges = [...graph.edges, viewEdge];
this.setState({
graph,
selected: viewEdge,
});
}
graph.edges = [...graph.edges, viewEdge];
this.setState({
graph,
selected: viewEdge,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this code needs to be updated (see merge conflicts)

});
};

// Called when an edge is reattached to a different target.
Expand Down Expand Up @@ -664,6 +661,7 @@ class Graph extends React.Component<IGraphProps, IGraphState> {
onCopySelected={this.onCopySelected}
onPasteSelected={this.onPasteSelected}
layoutEngineType={this.state.layoutEngineType}
allowLoopbackEdge={true}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please expose this in example similar to allowMultiselect

/>
</div>
</>
Expand Down
29 changes: 29 additions & 0 deletions src/helpers/edge-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,35 @@ export function getPathDescription(
viewWrapperElem
);

if (sourceNode === targetNode) {
const arrowOffset = nodeSize * 0.8;

const returningLinePoints = [
{
x: srcX - srcOff.xOff + nodeSize / 2,
y: srcY - srcOff.yOff,
},
{
x: srcX - srcOff.xOff + arrowOffset,
y: srcY - srcOff.yOff,
},
{
x: srcX - srcOff.xOff + arrowOffset,
y: srcY - srcOff.yOff - arrowOffset,
},
{
x: srcX - srcOff.xOff,
y: srcY - srcOff.yOff - arrowOffset,
},
{
x: srcX - trgOff.xOff,
y: srcY - trgOff.yOff - nodeSize / 2,
},
];

return getLine(returningLinePoints);
}

const linePoints = [
{
x: srcX - srcOff.xOff,
Expand Down
1 change: 1 addition & 0 deletions src/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ $button-size: 31px;
}

.edge {
fill: transparent;
color: $light-color;
stroke: $primary-color;
stroke-width: 2px;
Expand Down