Skip to content
Merged
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
54 changes: 26 additions & 28 deletions core/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Blockly.Connection.prototype.connect_ = function(childConnection) {
// Attempt to reattach the orphan at the end of the newly inserted
// block. Since this block may be a row, walk down to the end
// or to the first (and only) shadow block.
var connection = Blockly.Connection.getConnectionForOrphanedOutput(
var connection = Blockly.Connection.lastConnectionInRow(
childBlock, orphanBlock);
if (connection) {
orphanBlock.outputConnection.connect(connection);
Expand Down Expand Up @@ -372,31 +372,30 @@ Blockly.Connection.connectReciprocally_ = function(first, second) {
};

/**
* Returns the single connection on the block that will accept the orphaned
* block, if one can be found. If the block has multiple compatible connections
* (even if they are filled) this returns null. If the block has no compatible
* connections, this returns null.
* Does the given block have one and only one connection point that will accept
* an orphaned block?
* @param {!Blockly.Block} block The superior block.
* @param {!Blockly.Block} orphanBlock The inferior block.
* @return {Blockly.Connection} The suitable connection point on 'block',
* or null.
* @private
*/
Blockly.Connection.getSingleConnection_ = function(block, orphanBlock) {
var foundConnection = null;
Blockly.Connection.singleConnection_ = function(block, orphanBlock) {
var connection = null;
var output = orphanBlock.outputConnection;
var typeChecker = output.getConnectionChecker();

for (var i = 0, input; (input = block.inputList[i]); i++) {
var connection = input.connection;
if (connection && typeChecker.canConnect(output, connection, false)) {
if (foundConnection) {
for (var i = 0; i < block.inputList.length; i++) {
var thisConnection = block.inputList[i].connection;
var typeChecker = output.getConnectionChecker();
if (thisConnection &&
thisConnection.type == Blockly.connectionTypes.INPUT_VALUE &&
typeChecker.canConnect(output, thisConnection, false)) {
if (connection) {
return null; // More than one connection.
}
foundConnection = connection;
connection = thisConnection;
}
}
return foundConnection;
return connection;
};

/**
Expand All @@ -411,19 +410,18 @@ Blockly.Connection.getSingleConnection_ = function(block, orphanBlock) {
* of blocks, or null.
* @package
*/
Blockly.Connection.getConnectionForOrphanedOutput =
function(startBlock, orphanBlock) {
var newBlock = startBlock;
var connection;
while ((connection = Blockly.Connection.getSingleConnection_(
/** @type {!Blockly.Block} */ (newBlock), orphanBlock))) {
newBlock = connection.targetBlock();
if (!newBlock || newBlock.isShadow()) {
return connection;
}
}
return null;
};
Blockly.Connection.lastConnectionInRow = function(startBlock, orphanBlock) {
var newBlock = startBlock;
var connection;
while ((connection = Blockly.Connection.singleConnection_(
/** @type {!Blockly.Block} */ (newBlock), orphanBlock))) {
newBlock = connection.targetBlock();
if (!newBlock || newBlock.isShadow()) {
return connection;
}
}
return null;
};

/**
* Disconnect this connection.
Expand Down
14 changes: 10 additions & 4 deletions core/renderers/common/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,19 @@ Blockly.blockRendering.Renderer.prototype.orphanCanConnectAtEnd =
function(topBlock, orphanBlock, localType) {
var orphanConnection = null;
var lastConnection = null;
if (localType == Blockly.connectionTypes.OUTPUT_VALUE) {
if (localType ==
Blockly.connectionTypes
.OUTPUT_VALUE) { // We are replacing an output.
orphanConnection = orphanBlock.outputConnection;
lastConnection =
Blockly.Connection.getConnectionForOrphanedOutput(
// TODO: I don't think this function necessarily has the correct logic,
// but for now it is being kept for behavioral backwards-compat.
lastConnection = Blockly.Connection
.lastConnectionInRow(
/** @type {!Blockly.Block} **/ (topBlock), orphanBlock);
} else {
} else { // We are replacing a previous.
orphanConnection = orphanBlock.previousConnection;
// TODO: This lives on the block while lastConnectionInRow lives on
// on the connection. Something is fishy.
lastConnection = topBlock.lastConnectionInStack();
}

Expand Down
Loading