From a42c1bca74489005ff377edf7d55d8b99393b6c6 Mon Sep 17 00:00:00 2001 From: "duzc2dtw@gmail.com" Date: Wed, 6 Dec 2017 16:24:34 +0800 Subject: [PATCH] =?UTF-8?q?quotes=EF=BC=8Cchange=20UI=20,=20change=20categ?= =?UTF-8?q?ory=20custom=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blocks/variables_dynamic.js | 54 ++++++++++++++-------------- core/variables_dynamic.js | 72 ++++++++++++++++--------------------- tests/playground.html | 2 +- 3 files changed, 58 insertions(+), 70 deletions(-) diff --git a/blocks/variables_dynamic.js b/blocks/variables_dynamic.js index f2dba8494..a16ed3a8b 100644 --- a/blocks/variables_dynamic.js +++ b/blocks/variables_dynamic.js @@ -45,41 +45,41 @@ Blockly.Constants.VariablesDynamic.HUE = 310; Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT // Block for variable getter. { - 'type': 'variables_get_dynamic', - 'message0': '%1', - 'args0': [{ - 'type': 'field_variable', - 'name': 'VAR', - 'variable': '%{BKY_VARIABLES_DEFAULT_NAME}' + "type": "variables_get_dynamic", + "message0": "%1", + "args0": [{ + "type": "field_variable", + "name": "VAR", + "variable": "%{BKY_VARIABLES_DEFAULT_NAME}" }], - 'output': null, - 'colour': '%{BKY_VARIABLES_DYNAMIC_HUE}', - 'helpUrl': '%{BKY_VARIABLES_GET_HELPURL}', - 'tooltip': '%{BKY_VARIABLES_GET_TOOLTIP}', - 'extensions': ['contextMenu_variableDynamicSetterGetter'], - 'mutator':'contextMenu_variableDynamicMutation' + "output": null, + "colour": "%{BKY_VARIABLES_DYNAMIC_HUE}", + "helpUrl": "%{BKY_VARIABLES_GET_HELPURL}", + "tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}", + "extensions": ["contextMenu_variableDynamicSetterGetter"], + "mutator":"contextMenu_variableDynamicMutation" }, // Block for variable setter. { - 'type': 'variables_set_dynamic', - 'message0': '%{BKY_VARIABLES_SET}', - 'args0': [{ - 'type': 'field_variable', - 'name': 'VAR', - 'variable': '%{BKY_VARIABLES_DEFAULT_NAME}' + "type": "variables_set_dynamic", + "message0": "%{BKY_VARIABLES_SET}", + "args0": [{ + "type": "field_variable", + "name": "VAR", + "variable": "%{BKY_VARIABLES_DEFAULT_NAME}" }, { - 'type': 'input_value', - 'name': 'VALUE' + "type": "input_value", + "name": "VALUE" } ], - 'previousStatement': null, - 'nextStatement': null, - 'colour': '%{BKY_VARIABLES_DYNAMIC_HUE}', - 'tooltip': '%{BKY_VARIABLES_SET_TOOLTIP}', - 'helpUrl': '%{BKY_VARIABLES_SET_HELPURL}', - 'extensions': ['contextMenu_variableDynamicSetterGetter'], - 'mutator':'contextMenu_variableDynamicMutation' + "previousStatement": null, + "nextStatement": null, + "colour": "%{BKY_VARIABLES_DYNAMIC_HUE}", + "tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}", + "helpUrl": "%{BKY_VARIABLES_SET_HELPURL}", + "extensions": ["contextMenu_variableDynamicSetterGetter"], + "mutator":"contextMenu_variableDynamicMutation" } ]); // END JSON EXTRACT (Do not delete this comment.) diff --git a/core/variables_dynamic.js b/core/variables_dynamic.js index 876aeb481..a899c04c6 100644 --- a/core/variables_dynamic.js +++ b/core/variables_dynamic.js @@ -35,32 +35,14 @@ goog.require('Blockly.Workspace'); goog.require('goog.string'); -/** - * Prompt the user for a new variable name. - * @param {string} promptText The string of the prompt. - * @param {string} defaultText The default value to show in the prompt's field. - * @param {function(?string)} callback A callback. It will return the new - * 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.VariablesDynamic.onCreateVariableButtonClick_String = function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'String'); }; -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.onCreateVariableButtonClick_Number = function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'Number'); +}; +Blockly.VariablesDynamic.onCreateVariableButtonClick_Color = function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'Color'); }; /** * Construct the elements (blocks and button) required by the flyout for the @@ -72,11 +54,23 @@ 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'); - - workspace.registerButtonCallback('CREATE_VARIABLE', Blockly.VariablesDynamic.onCreateVariableButtonClick); - + button.setAttribute('callbackKey', 'CREATE_VARIABLE_STRING'); xmlList.push(button); + button = goog.dom.createDom('button'); + button.setAttribute('text', Blockly.Msg.NEW_VARIABLE); + button.setAttribute('callbackKey', 'CREATE_VARIABLE_NUMBER'); + xmlList.push(button);button = goog.dom.createDom('button'); + button.setAttribute('text', Blockly.Msg.NEW_VARIABLE); + button.setAttribute('callbackKey', 'CREATE_VARIABLE_COLOR'); + xmlList.push(button); + + workspace.registerButtonCallback('CREATE_VARIABLE_STRING', + Blockly.VariablesDynamic.onCreateVariableButtonClick_String); + workspace.registerButtonCallback('CREATE_VARIABLE_NUMBER', + Blockly.VariablesDynamic.onCreateVariableButtonClick_Number); + workspace.registerButtonCallback('CREATE_VARIABLE_COLOR', + Blockly.VariablesDynamic.onCreateVariableButtonClick_Color); + var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace); xmlList = xmlList.concat(blockList); @@ -95,26 +89,20 @@ Blockly.VariablesDynamic.flyoutCategoryBlocks = function(workspace) { var xmlList = []; if (variableModelList.length > 0) { - var varTypes = workspace.getVariableTypes(); - if (Blockly.Blocks['variables_set_dynamic']) { - for (var i in varTypes) { - var varType = varTypes[i]; - var variableModelListOfType = workspace.getVariablesOfType(varType); - var firstVariable = variableModelListOfType[0]; - var gap = i == varTypes.length - 1 ? 24 : 8; + for (var i = 0, variable; variable = variableModelList[i]; i++) { + if (Blockly.Blocks['variables_get_dynamic']) { var blockText = '' + - '' + - Blockly.Variables.generateVariableFieldXml_(firstVariable) + + '' + + Blockly.Variables.generateVariableFieldXml_(variable) + '' + ''; var block = Blockly.Xml.textToDom(blockText).firstChild; xmlList.push(block); } - } - if (Blockly.Blocks['variables_get_dynamic']) { - for (var i = 0, variable; variable = variableModelList[i]; i++) { + if (Blockly.Blocks['variables_set_dynamic']) { + var gap = Blockly.Blocks['variables_get'] ? 20 : 8; var blockText = '' + - '' + + '' + Blockly.Variables.generateVariableFieldXml_(variable) + '' + ''; diff --git a/tests/playground.html b/tests/playground.html index a9d746476..1b7599717 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -737,7 +737,7 @@ h1 { - +