Create variables from procedure definitions

This commit is contained in:
Rachel Fenichel
2016-08-11 14:59:11 -07:00
parent 3019269f93
commit c7a5944ea3
2 changed files with 24 additions and 1 deletions

View File

@@ -400,14 +400,20 @@ Blockly.Blocks['procedures_mutatorarg'] = {
* @this Blockly.Block
*/
init: function() {
var field = new Blockly.FieldTextInput('x', this.validator_);
this.appendDummyInput()
.appendField(Blockly.Msg.PROCEDURES_MUTATORARG_TITLE)
.appendField(new Blockly.FieldTextInput('x', this.validator_), 'NAME');
.appendField(field, 'NAME');
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(Blockly.Blocks.procedures.HUE);
this.setTooltip(Blockly.Msg.PROCEDURES_MUTATORARG_TOOLTIP);
this.contextMenu = false;
// Create the default variable when we drag the block in from the flyout.
// Have to do this after installing the field on the block.
field.onFinishEditing_ = this.createNewVar_;
field.onFinishEditing_('x');
},
/**
* Obtain a valid name for the procedure.
@@ -421,6 +427,20 @@ Blockly.Blocks['procedures_mutatorarg'] = {
validator_: function(newVar) {
newVar = newVar.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
return newVar || null;
},
/**
* Called when focusing away from the text field.
* Creates a new variable with this name.
* @param {string} newText The new variable name.
* @private
* @this Blockly.FieldTextInput
*/
createNewVar_: function(newText) {
var source = this.sourceBlock_;
if (source && source.workspace && source.workspace.options
&& source.workspace.options.parentWorkspace) {
source.workspace.options.parentWorkspace.createVariable(newText);
}
}
};

View File

@@ -269,6 +269,9 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
} else {
// Validation function has changed the text.
text = text1;
if (thisField.onFinishEditing_) {
thisField.onFinishEditing_(text);
}
}
}
thisField.setValue(text);