mirror of
https://github.com/google/blockly.git
synced 2026-01-10 18:37:09 +01:00
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_MUTATION
to make sure setCheck before event trigger
This commit is contained in:
@@ -56,7 +56,8 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
|
||||
"colour": "%{BKY_VARIABLES_DYNAMIC_HUE}",
|
||||
"helpUrl": "%{BKY_VARIABLES_GET_HELPURL}",
|
||||
"tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}",
|
||||
"extensions": ["contextMenu_variableDynamicSetterGetter"]
|
||||
"extensions": ["contextMenu_variableDynamicSetterGetter"],
|
||||
"mutator":'contextMenu_variableDynamicMutation'
|
||||
},
|
||||
// Block for variable setter.
|
||||
{
|
||||
@@ -77,7 +78,8 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
|
||||
"colour": "%{BKY_VARIABLES_DYNAMIC_HUE}",
|
||||
"tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}",
|
||||
"helpUrl": "%{BKY_VARIABLES_SET_HELPURL}",
|
||||
"extensions": ["contextMenu_variableDynamicSetterGetter"]
|
||||
"extensions": ["contextMenu_variableDynamicSetterGetter"],
|
||||
"mutator":'contextMenu_variableDynamicMutation'
|
||||
}
|
||||
]); // END JSON EXTRACT (Do not delete this comment.)
|
||||
|
||||
@@ -132,9 +134,29 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI
|
||||
} else {
|
||||
this.getInput("VALUE").connection.setCheck(variableModel.type);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This mutator make sure the type check work before any event trigger.
|
||||
* Some event handler check the type , and disconnect the connection which not match.
|
||||
* @readonly
|
||||
*/
|
||||
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_MUTATION = {
|
||||
mutationToDom: function() {
|
||||
var container = document.createElement('mutation');
|
||||
var name = this.getFieldValue('VAR');
|
||||
var variableModel = this.workspace.getVariable(name);
|
||||
if (this.type == 'variables_get_dynamic') {
|
||||
this.outputConnection.setCheck(variableModel.type);
|
||||
} else {
|
||||
this.getInput("VALUE").connection.setCheck(variableModel.type);
|
||||
}
|
||||
return container;
|
||||
},
|
||||
domToMutation: function() {}
|
||||
};
|
||||
Blockly.Extensions.registerMixin('contextMenu_variableDynamicSetterGetter',
|
||||
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);
|
||||
Blockly.Extensions.registerMutator('contextMenu_variableDynamicMutation',
|
||||
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_MUTATION);
|
||||
|
||||
@@ -43,24 +43,24 @@ goog.require('goog.string');
|
||||
* variable name, or null if the user picked something illegal.
|
||||
*/
|
||||
Blockly.VariablesDynamic.promptType = function(promptText, defaultText, callback) {
|
||||
Blockly.prompt(promptText, defaultText, function(newVarType) {
|
||||
// Merge runs of whitespace. Strip leading and trailing whitespace.
|
||||
// Beyond this, all types are legal.
|
||||
if (newVarType) {
|
||||
newVarType = newVarType.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
|
||||
}
|
||||
callback(newVarType);
|
||||
});
|
||||
Blockly.prompt(promptText, defaultText, function(newVarType) {
|
||||
// Merge runs of whitespace. Strip leading and trailing whitespace.
|
||||
// Beyond this, all types are legal.
|
||||
if (newVarType) {
|
||||
newVarType = newVarType.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
|
||||
}
|
||||
callback(newVarType);
|
||||
});
|
||||
};
|
||||
Blockly.VariablesDynamic.onCreateVariableButtonClick = function(button) {
|
||||
Blockly.VariablesDynamic.promptType(Blockly.Msg.NEW_VARIABLE_TYPE_TITLE, '', function(type) {
|
||||
if (type) {
|
||||
Blockly.Variables.createVariable(button.getTargetWorkspace(), null, type);
|
||||
}
|
||||
});
|
||||
// workspace.createVariable("abc", "string");
|
||||
// workspace.createVariable("123", "number");
|
||||
// workspace.createVariable("abcd", "string");
|
||||
Blockly.VariablesDynamic.promptType(Blockly.Msg.NEW_VARIABLE_TYPE_TITLE, '', function(type) {
|
||||
if (type) {
|
||||
Blockly.Variables.createVariable(button.getTargetWorkspace(), null, type);
|
||||
}
|
||||
});
|
||||
// workspace.createVariable("abc", "string");
|
||||
// workspace.createVariable("123", "number");
|
||||
// workspace.createVariable("abcd", "string");
|
||||
};
|
||||
/**
|
||||
* Construct the elements (blocks and button) required by the flyout for the
|
||||
@@ -69,18 +69,18 @@ Blockly.VariablesDynamic.onCreateVariableButtonClick = function(button) {
|
||||
* @return {!Array.<!Element>} Array of XML elements.
|
||||
*/
|
||||
Blockly.VariablesDynamic.flyoutCategory = function(workspace) {
|
||||
var xmlList = [];
|
||||
var button = goog.dom.createDom('button');
|
||||
button.setAttribute('text', Blockly.Msg.NEW_VARIABLE);
|
||||
button.setAttribute('callbackKey', 'CREATE_VARIABLE');
|
||||
var xmlList = [];
|
||||
var button = goog.dom.createDom('button');
|
||||
button.setAttribute('text', Blockly.Msg.NEW_VARIABLE);
|
||||
button.setAttribute('callbackKey', 'CREATE_VARIABLE');
|
||||
|
||||
workspace.registerButtonCallback('CREATE_VARIABLE', Blockly.VariablesDynamic.onCreateVariableButtonClick);
|
||||
workspace.registerButtonCallback('CREATE_VARIABLE', Blockly.VariablesDynamic.onCreateVariableButtonClick);
|
||||
|
||||
xmlList.push(button);
|
||||
xmlList.push(button);
|
||||
|
||||
var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace);
|
||||
xmlList = xmlList.concat(blockList);
|
||||
return xmlList;
|
||||
var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace);
|
||||
xmlList = xmlList.concat(blockList);
|
||||
return xmlList;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -89,39 +89,39 @@ Blockly.VariablesDynamic.flyoutCategory = function(workspace) {
|
||||
* @return {!Array.<!Element>} Array of XML block elements.
|
||||
*/
|
||||
Blockly.VariablesDynamic.flyoutCategoryBlocks = function(workspace) {
|
||||
var variableModelList = workspace.getAllVariables();
|
||||
variableModelList.sort(Blockly.VariableModel.compareByName);
|
||||
var variableModelList = workspace.getAllVariables();
|
||||
variableModelList.sort(Blockly.VariableModel.compareByName);
|
||||
|
||||
var xmlList = [];
|
||||
if (variableModelList.length > 0) {
|
||||
var xmlList = [];
|
||||
if (variableModelList.length > 0) {
|
||||
|
||||
var varTypes = workspace.getVariableTypes();
|
||||
for (var i in varTypes) {
|
||||
var varType = varTypes[i];
|
||||
var variableModelListOfType = workspace.getVariablesOfType(varType);
|
||||
var firstVariable = variableModelListOfType[0];
|
||||
if (Blockly.Blocks['variables_set_dynamic']) {
|
||||
var gap = i == varTypes.length - 1 ? 24 : 8;
|
||||
var blockText = '<xml>' +
|
||||
var varTypes = workspace.getVariableTypes();
|
||||
for (var i in varTypes) {
|
||||
var varType = varTypes[i];
|
||||
var variableModelListOfType = workspace.getVariablesOfType(varType);
|
||||
var firstVariable = variableModelListOfType[0];
|
||||
if (Blockly.Blocks['variables_set_dynamic']) {
|
||||
var gap = i == varTypes.length - 1 ? 24 : 8;
|
||||
var blockText = '<xml>' +
|
||||
'<block type="variables_set_dynamic" gap="' + gap + '">' +
|
||||
Blockly.Variables.generateVariableFieldXml_(firstVariable) +
|
||||
'</block>' +
|
||||
'</xml>';
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
for (var i = 0, variable; variable = variableModelList[i]; i++) {
|
||||
if (Blockly.Blocks['variables_get_dynamic']) {
|
||||
var blockText = '<xml>' +
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
for (var i = 0, variable; variable = variableModelList[i]; i++) {
|
||||
if (Blockly.Blocks['variables_get_dynamic']) {
|
||||
var blockText = '<xml>' +
|
||||
'<block type="variables_get_dynamic" gap="8">' +
|
||||
Blockly.Variables.generateVariableFieldXml_(variable) +
|
||||
'</block>' +
|
||||
'</xml>';
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
return xmlList;
|
||||
};
|
||||
}
|
||||
return xmlList;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user