Update connect to work with multiple connecting blocks#2905
Conversation
| /** | ||
| * Tries to connect the given connections. | ||
| * | ||
| * We use the cursor to represent both the current connection and the target |
There was a problem hiding this comment.
Having one of the parameters called targetConnection makes this sentence really confusing since you're referring to the targetConnection property on each of the two connections being passed. Not sure how to clean that up...
| if (!movingConnection || !targetConnection) { | ||
| return false; | ||
| } | ||
| var markerTarget = targetConnection.targetConnection; |
There was a problem hiding this comment.
This rename also makes the code confusing. Because you didn't want to call it targetTarget you've implicitly renamed target to marker, which means when you use them both down below it's confusing how they're related. Change movingConnection and targetConnection to use names that can be continued with their Targets. Maybe movingConnection and toConnection or moving/destination?
|
|
||
| // When connecting a next and previous connection try inserting the moving | ||
| // connection into the stack before trying to connect them. | ||
| if (markerTarget && |
There was a problem hiding this comment.
This is assuming the targetConnection is always a previous connection, which isn't the case at the end of a stack. It gets caught by later cases, but it's better practice to explicitly check things instead of depending on failures.
| * were connected, false otherwise. | ||
| * @package | ||
| */ | ||
| Blockly.navigation.connect = function(movingConnection, targetConnection) { |
There was a problem hiding this comment.
I think you could make this method a bit more readable by having it do the following:
- On the moving connection, get the best inferior connection or null.
- If 1 succeeded, on the destination connection, get the best superior connection or null.
- If 1 and 2 succeeded try connecting those two.
- Else, try connecting the moving and destination connections.
Picking the best inferior would be:
- If it's already an inferior connection use it.
- If it's the next connection on a block use its previous/output connection if it exists.
Picking the best superior would be:
- If already superior use it.
- Use its target if it exists.
There was a problem hiding this comment.
Does this take into account #2 on the list? With the inferior connection being on the destination block? Also, if we have two next connections then it will find a way to connect those blocks, I don't particularly mind this but just throwing it out there.
There was a problem hiding this comment.
You're right, it doesn't. Let's stick with your version, but maybe organize it to be more readable instead of more efficient?
The basics
The details
Resolves
There were three cases when connecting multiple blocks that were failing:
Proposed Changes
Reason for Changes
Test Coverage
Tested on:
Documentation
Additional Information
There are a few more bugs around connecting inline inputs that will be addressed in a different PR.