-
Notifications
You must be signed in to change notification settings - Fork 233
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
Feature/ondoubleclicknode #202
Feature/ondoubleclicknode #202
Conversation
@@ -64,6 +64,10 @@ export default class Sandbox extends React.Component { | |||
// }); | |||
}; | |||
|
|||
onDoubleClickNode = id => { | |||
!this.state.config.collapsible && window.alert(`Double clicked node ${id}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give it a try and write before the call add:
event.stopPropagation()
Note that first you need to grab the event in the parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done many attempts but it didn't work ... I've also tried preventDefault() but it doesnt do anything ...
I was thinking that maybe there would be a way to say to d3-zoom to zoom only if the double click happens on the graph ? As the onClickGraph function isn't triggered when clicking the node, it consider both clicks differently and we could leverage that ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, the only way I found to solve this event was disabling double-click zoom on the whole graph, by adding .on("dblclick.zoom", null) to the _zoomConfig function. I am trying to implement it only on the nodes but that doesn't work yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, please consider the following comments.
README.md
Outdated
@@ -20,9 +20,9 @@ | |||
|
|||
You can also load different datasets and configurations via URL query parameter, here are the links: | |||
|
|||
* [small dataset](https://goodguydaniel.com/react-d3-graph/sandbox/index.html?data=small) - small example. | |||
* [custom node dataset](https://goodguydaniel.com/react-d3-graph/sandbox/index.html?data=custom-node) - sample config with custom views. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did prettier ran against README.md
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I found a way to correct that :)
docs/DOCUMENTATION.md
Outdated
@@ -2,94 +2,94 @@ | |||
|
|||
### Table of Contents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please discard the changes on this file 🙂
package-lock.json
Outdated
@@ -20,10 +20,10 @@ | |||
"dev": true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please discard changes to this file 🙂
@@ -58,6 +58,10 @@ import utils from "../../utils"; | |||
* window.alert('Clicked node ${nodeId}'); | |||
* }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also discard changes for src/components/graph/Graph.jsx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this one, you mean I should delete the onDoubleClickNode function ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ignore this one 😄
this.props.onClickNode && this.props.onClickNode(clickedNodeId); | ||
if (!this.timeoutID) { | ||
this.timeoutID = setTimeout(() => { | ||
this.props.onClickNode && this.props.onClickNode(clickedNodeId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific reason for this call to be wrapped inside a setTimeout
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it represents the time when the Node is waiting to see if another click is coming, and then decides if it should apply the onClickNode or onDoubleClickNode function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if you tried it out, but there's also a native dblclick
event in JavaScript. Maybe you could try to use that instead of the setTimeout
hack and see what happens.
If it doesn't work, this solution you came up with looks good to me. Still, since this is meant to be a handler for nodes only, I think it would be better to do this at node level instead of in the Graph
component. Consider moving this logic to the handleOnClickNode
method in Node
component instead. 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the dblclick event, it didn't work, JS and React as a whole have huge problems managing click and doubleclick on the same element and StackOverflow is full of questions regarding this issue.
I have run some tests to try to move the logic to the Node
component but it doesn't work anymore, nothing is triggered when I double click, I don't understand why ...
src/components/graph/Graph.jsx
Outdated
@@ -293,7 +298,16 @@ export default class Graph extends React.Component { | |||
() => this.props.onClickNode && this.props.onClickNode(clickedNodeId) | |||
); | |||
} else { | |||
this.props.onClickNode && this.props.onClickNode(clickedNodeId); | |||
if (!this.timeoutID) { | |||
this.timeoutID = setTimeout(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the naming this.nodeClickTimer
. Also, do you need to assign the timer to this
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, from what I've tried we can't initiate a variable (here, a nodeClickTimer not linked to this) in a component, or I would need to add a state but I found that too heavy for just one functionality
I have disabled the double click zoom on the whole graph, I think it's the better (and only possible from what I know) way ! |
this.props.onClickNode && this.props.onClickNode(clickedNodeId); | ||
if (!this.timeoutID) { | ||
this.timeoutID = setTimeout(() => { | ||
this.props.onClickNode && this.props.onClickNode(clickedNodeId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if you tried it out, but there's also a native dblclick
event in JavaScript. Maybe you could try to use that instead of the setTimeout
hack and see what happens.
If it doesn't work, this solution you came up with looks good to me. Still, since this is meant to be a handler for nodes only, I think it would be better to do this at node level instead of in the Graph
component. Consider moving this logic to the handleOnClickNode
method in Node
component instead. 😉
sandbox/Sandbox.jsx
Outdated
@@ -64,6 +64,11 @@ export default class Sandbox extends React.Component { | |||
// }); | |||
}; | |||
|
|||
onDoubleClickNode = (event, id) => { | |||
event.stopPropagation(); //??? Marche pas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried running the sandbox in your branch, but I'm getting the following error when I double click on a node:
Uncaught TypeError: event.stopPropagation is not a function
I debugged it a little and I found out that the first parameter of the method contains the node id instead of an event. The id
parameter is always undefined
, and no event is ever received by Sandbox
's onDoubleClickNode
function.
Although you added the event
parameter in this function, you didn't add it in the Node
and Graph
components. The handleOnClickNode
function in the Node
component is the one that actually catches the event, and it must pass it to the parent components so it reaches the Sandbox
callback. To begin with, we must pass the event to the Graph
component like this:
handleOnClickNode = event => this.props.onClickNode && this.props.onClickNode(event, this.props.id);
After doing this, you have to also update the onClickNode
function inside of the Graph
component to receive the new event
parameter and then update the call to this.props.onDoubleClickNode
by passing it the event
as the first parameter.
this.props.onDoubleClickNode(event, clickedNodeId)
Only when this is done you could actually try to call event methods from the Sandbox
component. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line was supposed to be deleted sorry, it is when I tried to cancel the zoom due to d3-zoom but it also led me to a dead-end, I couldn't cancel it
Hello, |
Hey @IgorMacGregor thanks for noticing I will proceed with this soon 🙂 |
Hi ! I have added the onDoubleClick feature as requested on issue #194. There is still a small bug that I couldn't fix: when double-clicking on a node, a zoom is triggered on the graph and I don't know how we could cancel it but it should be related to the d3-zoom library.
I've done my best to update the documentation, tell me if something is missing ! And it seems that some prettier script makes me change some of the README signs, I don't know how I could cancel it :'(
Have a good day !