diff --git a/blocks/procedures.js b/blocks/procedures.js index 82b383b51..19a5f6e68 100644 --- a/blocks/procedures.js +++ b/blocks/procedures.js @@ -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); + } } }; diff --git a/core/field_textinput.js b/core/field_textinput.js index ba9375e4c..859244bba 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -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);