From c0e220214cc8004e0ca69ce40f6ccc214df3aac9 Mon Sep 17 00:00:00 2001 From: Andrew n marshall Date: Thu, 12 Apr 2018 17:27:20 -0700 Subject: [PATCH] Rewrote LOGIC_COMPARE_ONCHANGE_MIXIN to fix #1408. * Create prevBlock_ upon first call to onchange. * Revert state upon an incompatible combination, bumping the new incompatible block, instead of the old block. Thus, the shadow is never the bumped block. Bug: * The undo stack get caught in a loop, and will never undo back to a state equivalent to the previous action. --- blocks/logic.js | 32 +++++++++++++++++++++----------- tests/playground.html | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/blocks/logic.js b/blocks/logic.js index 26e4e3f47..6b789db0a 100644 --- a/blocks/logic.js +++ b/blocks/logic.js @@ -520,8 +520,6 @@ Blockly.Constants.Logic.fixLogicCompareRtlOpLabels = * @readonly */ Blockly.Constants.Logic.LOGIC_COMPARE_ONCHANGE_MIXIN = { - prevBlocks_: [null, null], - /** * Called whenever anything on the workspace changes. * Prevent mismatched types from being compared. @@ -529,25 +527,37 @@ Blockly.Constants.Logic.LOGIC_COMPARE_ONCHANGE_MIXIN = { * @this Blockly.Block */ onchange: function(e) { + if (!this.prevBlocks_) { + this.prevBlocks_ = [null, null]; + } + var blockA = this.getInputTargetBlock('A'); var blockB = this.getInputTargetBlock('B'); // Disconnect blocks that existed prior to this change if they don't match. if (blockA && blockB && !blockA.outputConnection.checkType_(blockB.outputConnection)) { - // Mismatch between two inputs. Disconnect previous and bump it away. - // Ensure that any disconnections are grouped with the causing event. + // Mismatch between two inputs. Revert the block connections, + // bumping away the newly connected block(s). Blockly.Events.setGroup(e.group); - for (var i = 0; i < this.prevBlocks_.length; i++) { - var block = this.prevBlocks_[i]; - if (block === blockA || block === blockB) { - block.unplug(); - block.bumpNeighbours_(); + var prevA = this.prevBlocks_[0]; + if (prevA !== blockA) { + blockA.unplug(); + if (prevA) { + this.getInput('A').connection.connect(prevA.outputConnection); } } + var prevB = this.prevBlocks_[1]; + if (prevB !== blockB) { + blockB.unplug(); + if (prevB) { + this.getInput('B').connection.connect(prevB.outputConnection); + } + } + this.bumpNeighbours_(); Blockly.Events.setGroup(false); } - this.prevBlocks_[0] = blockA; - this.prevBlocks_[1] = blockB; + this.prevBlocks_[0] = this.getInputTargetBlock('A'); + this.prevBlocks_[1] = this.getInputTargetBlock('B'); } }; diff --git a/tests/playground.html b/tests/playground.html index a00de41e8..c91703281 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -1143,6 +1143,33 @@ h1 { + + + + + + 10 + + + + + 10 + + + + + + + 10 + + + + + 10 + + + +