fix variable naming

This commit is contained in:
Mario Pesch 2021-03-30 12:26:26 +02:00
parent 255b1619b8
commit 6b12c501a0

View File

@ -1,52 +1,60 @@
import Blockly from 'blockly'; import Blockly from "blockly";
const setVariableFunction = function (defaultValue) { const setVariableFunction = function (defaultValue) {
return function (block) { return function (block) {
const variableName = Blockly['Arduino'].variableDB_.getName( const variableName = Blockly["Arduino"].variableDB_.getName(
block.getFieldValue('VAR'), block.getFieldValue("VAR"),
Blockly.Variables.NAME_TYPE Blockly.Variables.NAME_TYPE
); );
const variableValue = Blockly['Arduino'].valueToCode( const variableValue = Blockly["Arduino"].valueToCode(
block, block,
'VALUE', "VALUE",
Blockly['Arduino'].ORDER_ATOMIC Blockly["Arduino"].ORDER_ATOMIC
); );
const allVars = Blockly.getMainWorkspace().getVariableMap().getAllVariables(); const allVars = Blockly.getMainWorkspace()
const myVar = allVars.filter(v => v.name === variableName)[0] .getVariableMap()
var code = '' .getAllVariables();
const myVar = allVars.filter((v) => v.name === variableName)[0];
var code = "";
switch (myVar.type) { switch (myVar.type) {
default: default:
Blockly.Arduino.variables_[myVar + myVar.type] = myVar.type + " " + myVar.name + ';\n'; Blockly.Arduino.variables_[variableName + myVar.type] =
code = variableName + ' = ' + (variableValue || defaultValue) + ';\n'; myVar.type + " " + myVar.name + ";\n";
break; code = variableName + " = " + (variableValue || defaultValue) + ";\n";
case 'Array': break;
var arrayType; case "Array":
var number; var arrayType;
var number;
if (this.getChildren().length > 0) { if (this.getChildren().length > 0) {
if (this.getChildren()[0].type === 'lists_create_empty') { if (this.getChildren()[0].type === "lists_create_empty") {
arrayType = this.getChildren()[0].getFieldValue("type");
arrayType = this.getChildren()[0].getFieldValue('type'); number = Blockly.Arduino.valueToCode(
number = Blockly.Arduino.valueToCode(this.getChildren()[0], 'NUMBER', Blockly['Arduino'].ORDER_ATOMIC); this.getChildren()[0],
Blockly.Arduino.variables_[myVar + myVar.type] = `${arrayType} ${myVar.name} [${number}];\n`; "NUMBER",
} Blockly["Arduino"].ORDER_ATOMIC
} );
break; Blockly.Arduino.variables_[
myVar + myVar.type
] = `${arrayType} ${myVar.name} [${number}];\n`;
}
} }
return code; break;
}; }
return code;
};
}; };
const getVariableFunction = function (block) { const getVariableFunction = function (block) {
const variableName = Blockly['Arduino'].variableDB_.getName( const variableName = Blockly["Arduino"].variableDB_.getName(
block.getFieldValue('VAR'), block.getFieldValue("VAR"),
Blockly.Variables.NAME_TYPE Blockly.Variables.NAME_TYPE
); );
var code = variableName; var code = variableName;
return [code, Blockly['Arduino'].ORDER_ATOMIC]; return [code, Blockly["Arduino"].ORDER_ATOMIC];
}; };
Blockly['Arduino']['variables_set_dynamic'] = setVariableFunction() Blockly["Arduino"]["variables_set_dynamic"] = setVariableFunction();
Blockly['Arduino']['variables_get_dynamic'] = getVariableFunction; Blockly["Arduino"]["variables_get_dynamic"] = getVariableFunction;