app/assets/htmls/gs-element-blockly.html in gobstones-blockly-0.23.0 vs app/assets/htmls/gs-element-blockly.html in gobstones-blockly-0.24.0

- old
+ new

@@ -50,10 +50,11 @@ <block type="AlternativaCompleta"></block> </category> <category name="Repeticiones"> <block type="RepeticionSimple"></block> <block type="RepeticionCondicional"></block> + <block type="ForEach"></block> </category> <category name="AsignaciĆ³n"> <block type="Asignacion"></block> </category> </category> @@ -4064,10 +4065,19 @@ f(newBlock); newBlock.initSvg(); newBlock.render(); }; +const createVariable = (workspace, name) => { + Blockly.createBlockSvg(workspace, 'variables_get', b => { + b.setFieldValue(name, 'VAR'); + b.moveBy(10,5); + }); +} + +// --- + Blockly.Blocks.Program = { init: function () { this.jsonInit({ "type": "Program", "message0": "%1 %2 %3", @@ -4936,10 +4946,56 @@ ); input.appendField(removeButton); } }; +Blockly.Blocks.ForEach = { + init: function () { + this.jsonInit({ + type: "Statement", + previousStatement: "Statement", + nextStatement: "Statement", + message0: 'Repetir para cada %1 %2 %3', + args0: [ + { + "type": "field_input", + "name": "varName", + "text": "elemento" + }, + { + type: 'input_dummy' + }, + { + "type": "field_label", + "text": "en" + }, + ] + }); + + this.setColour(Blockly.CUSTOM_COLORS.ForEach || Blockly.CUSTOM_COLORS.controlStructure); + this.appendValueInput('list'); + this.appendStatementInput('block').setCheck(["Statement"]); + this.setInputsInline(true); + + var self = this; + + const handIcon = "hand.png"; + var createGetterButton = new Blockly.FieldImage( + getLocalMediaUrl(this, handIcon), + getLocalMediaSize(handIcon), + getLocalMediaSize(handIcon), + "Obtener variable", + function() { + var name = self.getFieldValue('varName'); + createVariable(self.workspace, name); + } + ); + + this.inputList[0].appendField(createGetterButton); + } +}; + function createSingleParameterExpressionBlock(blockText,returnType, colorType = "operator"){ return { init: function () { this.jsonInit({ message0: blockText + ' %1', @@ -5115,14 +5171,11 @@ this.createVariableBlock(name); }}); }, createVariableBlock: function(name) { - return Blockly.createBlockSvg(this.workspace, 'variables_get', b => { - b.setFieldValue(name, 'VAR'); - b.moveBy(10,5); - }); + return createVariable(this.workspace, name); } }; Blockly.Blocks.variables_get = { @@ -5574,9 +5627,17 @@ .map((it) => Blockly.GobstonesLanguage.valueToCode(block, it.name, Blockly.GobstonesLanguage.ORDER_NONE)) .join(", "); const code = `[${elements}]`; return [code, Blockly.GobstonesLanguage.ORDER_ATOMIC]; +}; + +Blockly.GobstonesLanguage.ForEach = function (block) { + let body = Blockly.GobstonesLanguage.statementToCode(block, 'block'); + var varName = block.getFieldValue('varName'); + var list = Blockly.GobstonesLanguage.valueToCode(block, 'list', Blockly.GobstonesLanguage.ORDER_NONE) || ''; + + return `foreach ${varName} in ${list} {\n${body}}\n`; }; Blockly.GobstonesLanguage.OperadorDeComparacion = function (block) { var code = (Blockly.GobstonesLanguage.valueToCode(block, 'arg1', Blockly.GobstonesLanguage.ORDER_RELATIONAL) || '()') + \ No newline at end of file