Commit 837b3c83 authored by Evan W. Patton's avatar Evan W. Patton Committed by Susan Rati Lane

Wrap typeblock updates in an event group (#1958)

Change-Id: Ife5cf5abdbaa2a0498ed018c4ab7a6398236a2da
parent efc47714
...@@ -484,7 +484,7 @@ Blockly.TypeBlock.prototype.createAutoComplete_ = function(inputText){ ...@@ -484,7 +484,7 @@ Blockly.TypeBlock.prototype.createAutoComplete_ = function(inputText){
var numberMatch = numberReg.exec(blockName); var numberMatch = numberReg.exec(blockName);
var textReg = new RegExp('^[\"|\']+', 'g'); var textReg = new RegExp('^[\"|\']+', 'g');
var textMatch = textReg.exec(blockName); var textMatch = textReg.exec(blockName);
if (numberMatch && numberMatch.length > 0){ if (numberMatch && numberMatch.length > 0) {
blockToCreate = { blockToCreate = {
canonicName: 'math_number', canonicName: 'math_number',
dropDown: { dropDown: {
...@@ -492,8 +492,7 @@ Blockly.TypeBlock.prototype.createAutoComplete_ = function(inputText){ ...@@ -492,8 +492,7 @@ Blockly.TypeBlock.prototype.createAutoComplete_ = function(inputText){
value: blockName value: blockName
} }
}; };
} } else if (textMatch && textMatch.length === 1) {
else if (textMatch && textMatch.length === 1){
blockToCreate = { blockToCreate = {
canonicName: 'text', canonicName: 'text',
dropDown: { dropDown: {
...@@ -501,74 +500,78 @@ Blockly.TypeBlock.prototype.createAutoComplete_ = function(inputText){ ...@@ -501,74 +500,78 @@ Blockly.TypeBlock.prototype.createAutoComplete_ = function(inputText){
value: blockName.substring(1) value: blockName.substring(1)
} }
}; };
} } else {
else
return; // block does not exist: return return; // block does not exist: return
}
} }
var blockToCreateName = ''; var blockToCreateName = '';
var block; var block;
if (blockToCreate.dropDown){ //All blocks should have a dropDown property, even if empty Blockly.Events.setGroup(true);
blockToCreateName = blockToCreate.canonicName; try {
// components have mutator attributes we need to deal with. We can also add these for special blocks if (blockToCreate.dropDown) { //All blocks should have a dropDown property, even if empty
// e.g., this is done for create empty list blockToCreateName = blockToCreate.canonicName;
if(!goog.object.isEmpty(blockToCreate.mutatorAttributes)) { // components have mutator attributes we need to deal with. We can also add these for special blocks
//construct xml // e.g., this is done for create empty list
var xmlString = '<xml><block type="' + blockToCreateName + '"><mutation '; if (!goog.object.isEmpty(blockToCreate.mutatorAttributes)) {
for(var attributeName in blockToCreate.mutatorAttributes) { //construct xml
xmlString += attributeName + '="' + blockToCreate.mutatorAttributes[attributeName] + '" '; var xmlString = '<xml><block type="' + blockToCreateName + '"><mutation ';
} for (var attributeName in blockToCreate.mutatorAttributes) {
xmlString += attributeName + '="' + blockToCreate.mutatorAttributes[attributeName] + '" ';
}
xmlString += '>'; xmlString += '>';
xmlString += '</mutation></block></xml>'; xmlString += '</mutation></block></xml>';
var xml = Blockly.Xml.textToDom(xmlString); var xml = Blockly.Xml.textToDom(xmlString);
block = Blockly.Xml.domToBlock(xml.firstChild, self.workspace_); block = Blockly.Xml.domToBlock(xml.firstChild, self.workspace_);
} else { } else {
block = self.workspace_.newBlock(blockToCreateName); block = self.workspace_.newBlock(blockToCreateName);
block.initSvg(); //Need to init the block before doing anything else block.initSvg(); //Need to init the block before doing anything else
if (block.type && (block.type == "procedures_callnoreturn" || block.type == "procedures_callreturn")) { if (block.type && (block.type == "procedures_callnoreturn" || block.type == "procedures_callreturn")) {
//Need to make sure Procedure Block inputs are updated //Need to make sure Procedure Block inputs are updated
Blockly.FieldProcedure.onChange.call(block.getField("PROCNAME"), blockToCreate.dropDown.value); Blockly.FieldProcedure.onChange.call(block.getField("PROCNAME"), blockToCreate.dropDown.value);
}
} }
}
if (blockToCreate.dropDown.titleName && blockToCreate.dropDown.value){ if (blockToCreate.dropDown.titleName && blockToCreate.dropDown.value) {
block.setFieldValue(blockToCreate.dropDown.value, blockToCreate.dropDown.titleName); block.setFieldValue(blockToCreate.dropDown.value, blockToCreate.dropDown.titleName);
// change type checking for split blocks // change type checking for split blocks
if(blockToCreate.dropDown.value == 'SPLITATFIRST' || blockToCreate.dropDown.value == 'SPLIT') { if (blockToCreate.dropDown.value == 'SPLITATFIRST' || blockToCreate.dropDown.value == 'SPLIT') {
block.getInput("AT").setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("text",Blockly.Blocks.Utilities.INPUT)); block.getInput("AT").setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("text", Blockly.Blocks.Utilities.INPUT));
} else if(blockToCreate.dropDown.value == 'SPLITATFIRSTOFANY' || blockToCreate.dropDown.value == 'SPLITATANY') { } else if (blockToCreate.dropDown.value == 'SPLITATFIRSTOFANY' || blockToCreate.dropDown.value == 'SPLITATANY') {
block.getInput("AT").setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("list",Blockly.Blocks.Utilities.INPUT)); block.getInput("AT").setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("list", Blockly.Blocks.Utilities.INPUT));
}
} }
} else {
throw new Error('Type Block not correctly set up for: ' + blockToCreateName);
} }
} else { block.render();
throw new Error('Type Block not correctly set up for: ' + blockToCreateName); var blockSelected = Blockly.selected;
} var selectedX, selectedY, selectedXY;
block.render(); if (blockSelected) {
var blockSelected = Blockly.selected; selectedXY = blockSelected.getRelativeToSurfaceXY();
var selectedX, selectedY, selectedXY; selectedX = selectedXY.x;
if (blockSelected) { selectedY = selectedXY.y;
selectedXY = blockSelected.getRelativeToSurfaceXY(); self.connectIfPossible(blockSelected, block);
selectedX = selectedXY.x; if (!block.parentBlock_) {
selectedY = selectedXY.y; //Place it close but a bit out of the way from the one we created.
self.connectIfPossible(blockSelected, block); block.moveBy(Blockly.selected.getRelativeToSurfaceXY().x + 110,
if(!block.parentBlock_){ Blockly.selected.getRelativeToSurfaceXY().y + 50);
//Place it close but a bit out of the way from the one we created. }
block.moveBy(Blockly.selected.getRelativeToSurfaceXY().x + 110, block.select();
Blockly.selected.getRelativeToSurfaceXY().y + 50); } else {
//calculate positions relative to the view and the latest click
var left = self.workspace_.latestClick.x;
var top = self.workspace_.latestClick.y;
block.moveBy(left, top);
block.select();
} }
block.select(); self.workspace_.requestErrorChecking(block);
} self.hide();
else { self.workspace_.getParentSvg().parentNode.focus(); // refocus workspace div
//calculate positions relative to the view and the latest click } finally {
var left = self.workspace_.latestClick.x; Blockly.Events.setGroup(false);
var top = self.workspace_.latestClick.y;
block.moveBy(left, top);
block.select();
} }
self.workspace_.requestErrorChecking(block);
self.hide();
self.workspace_.getParentSvg().parentNode.focus(); // refocus workspace div
} }
); );
}; };
...@@ -603,6 +606,7 @@ Blockly.TypeBlock.prototype.connectIfPossible = function(blockSelected, createdB ...@@ -603,6 +606,7 @@ Blockly.TypeBlock.prototype.connectIfPossible = function(blockSelected, createdB
// Only attempt a connection if the input is empty // Only attempt a connection if the input is empty
else if (!inputList[i].connection.isConnected()) { else if (!inputList[i].connection.isConnected()) {
createdBlock.previousConnection.connect(inputList[i].connection); createdBlock.previousConnection.connect(inputList[i].connection);
break;
} }
} catch(e) { } catch(e) {
//We can ignore these exceptions; they happen when connecting two blocks //We can ignore these exceptions; they happen when connecting two blocks
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment