From fc985d3c9effe2157d5105c1222d5af1e4cb9689 Mon Sep 17 00:00:00 2001 From: "duzc2dtw@gmail.com" Date: Mon, 27 Nov 2017 22:33:30 +0800 Subject: [PATCH] Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_MUTATION to make sure setCheck before event trigger --- blocks/variables_dynamic.js | 28 ++++++++-- core/variables_dynamic.js | 100 ++++++++++++++++++------------------ 2 files changed, 75 insertions(+), 53 deletions(-) diff --git a/blocks/variables_dynamic.js b/blocks/variables_dynamic.js index f06ae972c..cd5f66e05 100644 --- a/blocks/variables_dynamic.js +++ b/blocks/variables_dynamic.js @@ -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); diff --git a/core/variables_dynamic.js b/core/variables_dynamic.js index 980818758..5134b5a5d 100644 --- a/core/variables_dynamic.js +++ b/core/variables_dynamic.js @@ -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.} 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.} 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 = '' + + 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 = '' + '' + Blockly.Variables.generateVariableFieldXml_(firstVariable) + '' + ''; - 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 = '' + + 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 = '' + '' + Blockly.Variables.generateVariableFieldXml_(variable) + '' + ''; - var block = Blockly.Xml.textToDom(blockText).firstChild; - xmlList.push(block); - } - } + var block = Blockly.Xml.textToDom(blockText).firstChild; + xmlList.push(block); + } } - return xmlList; -}; \ No newline at end of file + } + return xmlList; +};