Skip to content

Commit

Permalink
Merge pull request #1778 from AnmAtAnm/shadow-pop
Browse files Browse the repository at this point in the history
Rewrote LOGIC_COMPARE_ONCHANGE_MIXIN to fix #1408.
  • Loading branch information
rachel-fenichel authored Apr 14, 2018
2 parents eaafc23 + ef23b0e commit f52b75a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
34 changes: 23 additions & 11 deletions blocks/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,34 +520,46 @@ 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.
* @param {!Blockly.Events.Abstract} e Change event.
* @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 && !prevA.isShadow()) {
// The shadow block is automatically replaced during unplug().
this.getInput('A').connection.connect(prevA.outputConnection);
}
}
var prevB = this.prevBlocks_[1];
if (prevB !== blockB) {
blockB.unplug();
if (prevB && !prevB.isShadow()) {
// The shadow block is automatically replaced during unplug().
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');
}
};

Expand Down
27 changes: 27 additions & 0 deletions tests/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,33 @@ <h1>Blockly Playground</h1>
<block type="example_angle"></block>
<block type="example_date"></block>
</category>
<category name="Mutators">
<label text="logic_compare"></label>
<block type="logic_compare">
<value name="A">
<shadow type="math_number">
<field name="NUM">10</field>
</shadow>
</value>
<value name="B">
<shadow type="math_number">
<field name="NUM">10</field>
</shadow>
</value>
</block>
<block type="logic_compare">
<value name="A">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</category>
<category name="Style">
<label text="Hats"></label>
<block type="styled_event_cap"></block>
Expand Down

0 comments on commit f52b75a

Please sign in to comment.