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

- old
+ new

@@ -61,10 +61,11 @@ <category name="Literales"> <block type="math_number"></block> <block type="ColorSelector"></block> <block type="DireccionSelector"></block> <block type="BoolSelector"></block> + <block type="List"></block> </category> <category name="Expresiones primitivas"> <block type="hayBolitas"></block> <block type="puedeMover"></block> <block type="nroBolitas"></block> @@ -4175,11 +4176,11 @@ }, _addInit() { this.$init = true; - const icon = "minnus.png"; + const icon = "minus.png"; var removeButton = new Blockly.FieldImage( getLocalMediaUrl(this, icon), getLocalMediaSize(icon), getLocalMediaSize(icon), "Eliminar", @@ -4276,12 +4277,11 @@ type: "field_dropdown", name: "InteractiveBindingDropdownKey", options: keys.map(it => [it.name, it.code]), } ], - colour: Blockly.CUSTOM_COLORS.InteractiveBinding || Blockly.CUSTOM_COLORS.interactiveBinding, - tooltip: "Escoger una entrada", + colour: Blockly.CUSTOM_COLORS.InteractiveBinding || Blockly.CUSTOM_COLORS.interactiveBinding }); this.appendStatementInput('block').setCheck(["Statement"]); const self = this; @@ -4847,10 +4847,99 @@ Blockly.Blocks.ColorSelector = createLiteralSelectorBlock('Color',['Rojo','Verde','Negro','Azul']); Blockly.Blocks.DireccionSelector = createLiteralSelectorBlock('Direccion',['Este','Oeste','Norte','Sur']); Blockly.Blocks.BoolSelector = createLiteralSelectorBlock('Bool',['True','False']); +Blockly.Blocks.List = { + init: function () { + const type = "List"; + + this.jsonInit({ + type: type, + message0: "[", + args0: [], + output: type, + colour: Blockly.CUSTOM_COLORS.list || Blockly.CUSTOM_COLORS.literalExpression, + inputsInline: false + }); + + this._addAddButton(); + this.length = 0; + }, + + mutationToDom: function() { + var container = document.createElement('mutation'); + container.setAttribute('length', this.length); + return container; + }, + + domToMutation: function(xmlElement) { + var length = parseInt(xmlElement.getAttribute('length')) || 0; + for (let i = 0; i < length; i++) this._addElement(); + }, + + _addElement: function() { + this.length++; + this._removeAddButton(); + const input = this.appendValueInput('element' + this.length); + this._addRemoveButtonFor(input); + this._addAddButton(); + }, + + _removeElement: function(input) { + this.removeInput(input.name); + this.length--; + + let id = 1; + for (let input of this.inputList) { + if (input.name.startsWith("element")) { + input.name = "element" + id; + id++; + } + } + }, + + _addAddButton: function() { + const icon = "plus.png"; + var addButton = new Blockly.FieldImage( + getLocalMediaUrl(this, icon), + getLocalMediaSize(icon), + getLocalMediaSize(icon), + "Agregar elemento", + function() { + this._addElement(); + }.bind(this) + ); + const input = this.appendDummyInput(); + input.appendField(addButton); + input.name = "addButton"; + + const closingBracket = this.appendDummyInput(); + closingBracket.appendField("]"); + closingBracket.name = "closingBracket"; + }, + + _removeAddButton: function() { + this.removeInput("addButton") + this.removeInput("closingBracket"); + }, + + _addRemoveButtonFor: function(input) { + const icon = "minus.png"; + var removeButton = new Blockly.FieldImage( + getLocalMediaUrl(this, icon), + getLocalMediaSize(icon), + getLocalMediaSize(icon), + "Quitar elemento", + function() { + this._removeElement(input); + }.bind(this) + ); + input.appendField(removeButton); + } +}; + function createSingleParameterExpressionBlock(blockText,returnType, colorType = "operator"){ return { init: function () { this.jsonInit({ message0: blockText + ' %1', @@ -5476,10 +5565,21 @@ Blockly.GobstonesLanguage.ColorSelector = literalSelectorBlockCodeGenerator('Color'); Blockly.GobstonesLanguage.DireccionSelector = literalSelectorBlockCodeGenerator('Direccion'); Blockly.GobstonesLanguage.BoolSelector = literalSelectorBlockCodeGenerator('Bool'); +Blockly.GobstonesLanguage.List = function(block) { + const elements = block + .inputList + .filter((it) => it.name.startsWith("element")) + .map((it) => Blockly.GobstonesLanguage.valueToCode(block, it.name, Blockly.GobstonesLanguage.ORDER_NONE)) + .join(", "); + const code = `[${elements}]`; + + return [code, Blockly.GobstonesLanguage.ORDER_ATOMIC]; +}; + Blockly.GobstonesLanguage.OperadorDeComparacion = function (block) { var code = (Blockly.GobstonesLanguage.valueToCode(block, 'arg1', Blockly.GobstonesLanguage.ORDER_RELATIONAL) || '()') + ' ' + block.getFieldValue('RELATION') + @@ -6569,9 +6669,11 @@ }, _runWorkspaceXmlRetrocompatibilityFixes: function() { const oldAndOperator = `<field name="OPERATOR">&&</field>`; const newAndOperator = `<field name="OPERATOR">AND</field>`; + + if (!this.workspaceXml) return; if (this.workspaceXml.includes(oldAndOperator)) { this.workspaceXml = this.workspaceXml.replace(new RegExp(oldAndOperator, "g"), newAndOperator); return true; } \ No newline at end of file