app/assets/htmls/gs-element-blockly.html in gobstones-blockly-0.18.0 vs app/assets/htmls/gs-element-blockly.html in gobstones-blockly-0.18.1
- old
+ new
@@ -60,11 +60,11 @@
<category name="Expresiones">
<category name="Literales">
<block type="math_number"></block>
<block type="ColorSelector"></block>
<block type="DireccionSelector"></block>
- <block type="BoolSelector"></block>
+ <block type="BoolSelector"></block>
</category>
<category name="Expresiones primitivas">
<block type="hayBolitas"></block>
<block type="puedeMover"></block>
<block type="nroBolitas"></block>
@@ -76,13 +76,13 @@
<category name="Operadores">
<block type="OperadorNumerico"></block>
<block type="OperadorDeComparacion"></block>
<block type="OperadorLogico"></block>
<block type="not"></block>
- <block type="siguiente"></block>
- <block type="previo"></block>
- <block type="opuesto"></block>
+ <block type="siguiente"></block>
+ <block type="previo"></block>
+ <block type="opuesto"></block>
</category>
</category>
<category name="Definiciones">
<category name="Programas">
<block type="Program"></block>
@@ -5070,19 +5070,22 @@
};
// Gobstones pragma BEGIN_REGION should avoid char 'at' ( @ )
Blockly.utils.genUid.soup_ = Blockly.utils.genUid.soup_.replace(/@/g,"a");
function getCustomTeacherDropdownValue(block) {
+ // TODO: Hackeada de compromiso
+
try {
- const inputs = block.inputList;
- const lastInput = inputs[inputs.length - 1];
- const fields = lastInput.fieldRow;
- const lastField = fields[fields.length - 1];
+ const getCustomTeacherDropdown = (field) => field.name === 'custom_teacher_dropdown';
- if (lastField.name === 'custom_teacher_dropdown')
- return lastField.getValue();
- } catch(e) { // TODO: Sacar esto
+ const input = block.inputList.find((input) =>
+ input.fieldRow.some(getCustomTeacherDropdown)
+ );
+ const field = input.fieldRow.find(getCustomTeacherDropdown);
+
+ return field.getValue();
+ } catch(e) {
return null;
}
}
/**
@@ -6139,13 +6142,13 @@
_getDropdownOptions(definition) {
const attributes = definition.attributes;
const dropdown = definition.attributes && definition.attributes.block_dropdown;
- const dropdownOptions = dropdown && dropdown.match(/\("\w+" *, *'[^']*'\)/g);
+ const dropdownOptions = dropdown && dropdown.match(/\("[^"]+" *, *'[^']*'\)/g);
return dropdownOptions && dropdownOptions.map((option) => {
- const parts = option.match(/^\("(\w+)" *, *'([^']*)'\)$/);
+ const parts = option.match(/^\("([^"]+)" *, *'([^']*)'\)$/);
return [parts[1], parts[2]];
});
},
/**
@@ -6159,56 +6162,39 @@
const finalName = customName || name;
const parts = (customName ? this._getParts : this._getPartsByConvention.bind(this, true))
.bind(this)(finalName);
+ const self = this;
+
// Bloque
Blockly.Blocks[name] = {
- init: function () {
- let argsIndex = 1;
- this.setColour(Blockly.CUSTOM_COLORS.primitiveProcedure);
+ init: function () {
+ this.setColour(Blockly.CUSTOM_COLORS.primitiveProcedure);
if (icon) {
this.appendDummyInput().appendField(new Blockly.FieldImage(
icon,
16,
16,
finalName
));
}
- for (var i in parts) {
- if (i == (parts.length - 1)) {
- this.appendDummyInput().appendField(parts[i]);
+ self._definePrimitiveHolesOrDropdown(this, parts, dropdownOptions);
- if (dropdownOptions) {
- this.removeInput('arg1');
- this.jsonInit({
- message0: parts[i - 1] + " %1",
- args0: [
- {
- type: 'field_dropdown',
- name: "custom_teacher_dropdown",
- options: dropdownOptions
- }
- ]
- });
- }
- } else {
- this.appendValueInput('arg' + argsIndex).appendField(parts[i]);
- argsIndex++;
- }
- }
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setInputsInline(true);
- }
+ this.setPreviousStatement(true);
+ this.setNextStatement(true);
+ this.setInputsInline(true);
+ }
};
+
let argsList = [];
for(var i=1;i<parts.length;i++) {
argsList.push('arg' + i);
}
+
// Generator
Blockly.GobstonesLanguage[name] = procBlockCodeGenerator(name, argsList);
},
_definePrimitiveProcedures: function () {
@@ -6233,14 +6219,15 @@
const finalName = customName || name;
const parts = (customName ? this._getParts : this._getPartsByConvention.bind(this, false))
.bind(this)(finalName);
+ const self = this;
+
// Bloque
Blockly.Blocks[name] = {
init: function () {
- let argsIndex = 1;
this.setColour(Blockly.CUSTOM_COLORS.primitiveFunction);
if (icon) {
this.appendDummyInput().appendField(new Blockly.FieldImage(
icon,
@@ -6248,42 +6235,24 @@
16,
finalName
));
}
- for (var i in parts) {
- if (i == (parts.length - 1)) {
- this.appendDummyInput().appendField(parts[i]);
+ self._definePrimitiveHolesOrDropdown(this, parts, dropdownOptions);
- if (dropdownOptions) {
- this.removeInput('arg1');
- this.jsonInit({
- message0: parts[i - 1] + " %1",
- args0: [
- {
- type: 'field_dropdown',
- name: "custom_teacher_dropdown",
- options: dropdownOptions
- }
- ]
- });
- }
- } else {
- this.appendValueInput('arg' + argsIndex).appendField(parts[i]);
- argsIndex++;
- }
- }
this.setPreviousStatement(false);
this.setNextStatement(false);
this.setInputsInline(true);
this.setOutput('var');
}
};
+
let argsList = [];
for(var i=1;i<parts.length;i++) {
argsList.push('arg' + i);
}
+
// Generator
Blockly.GobstonesLanguage[name] = functionBlockCodeGenerator(name, argsList);
},
_definePrimitiveFunctions: function () {
@@ -6294,10 +6263,40 @@
for(var i in this.primitiveFunctions) {
this._definePrimitiveFunction(this.primitiveFunctions[i]);
}
},
+ _definePrimitiveHolesOrDropdown: function (block, parts, dropdownOptions) {
+ if (dropdownOptions) {
+ if (parts.length !== 2) throw new Error("Only one parameter is supported when using block_dropdown");
+
+ // En Space (U+2002)
+ dropdownOptions.forEach((opt) => opt[0] = opt[0].replace(/ /g, " "));
+
+ block.jsonInit({
+ message0: parts.join('%1'),
+ args0: [
+ {
+ type: 'field_dropdown',
+ name: "custom_teacher_dropdown",
+ options: dropdownOptions
+ }
+ ]
+ });
+ } else {
+ let argsIndex = 1;
+ for (var i in parts) {
+ if (i == parts.length - 1) {
+ block.appendDummyInput().appendField(parts[i]);
+ } else {
+ block.appendValueInput('arg' + argsIndex).appendField(parts[i]);
+ argsIndex++;
+ }
+ }
+ }
+ },
+
_onBlocklyWorkspaceUpdate: function () {
let xml = Blockly.Xml.workspaceToDom(this.workspace);
this._blocklyWorkspaceXML = Blockly.Xml.domToText(xml);
this.workspaceXml = this._blocklyWorkspaceXML;
this._keepOnlyAProgram(xml);
@@ -6522,16 +6521,16 @@
},
attached: function() {
this._definePrimitiveProcedures();
this._definePrimitiveFunctions();
- // create workspace
+ // create workspace
var blocklyDiv = this.$$('#blocklyDiv');
this.blocklyDiv = blocklyDiv;
this._fixSize();
this._fixScroll();
- this.workspace = Blockly.inject(blocklyDiv, {
+ this.workspace = Blockly.inject(blocklyDiv, {
toolbox: this._createToolbox(),
media: this.get("media"),
toolboxPosition: "start",
scrollbars: true,
horizontalLayout: false,
@@ -6540,10 +6539,10 @@
readOnly: this.readOnly,
zoom: {
controls: true,
wheel: true
}
- });
+ });
var _this = this;
this.workspace.addChangeListener(function (a, b, c) {
Blockly.Events.disableOrphans(a, b, c);
_this._onBlocklyWorkspaceUpdate();
});
\ No newline at end of file