mirror of
https://github.com/google/blockly.git
synced 2026-01-09 01:50:11 +01:00
Create variables from procedure definitions
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user