Skip to content

Fix variable validation to show only one alert #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 101 additions & 17 deletions src/components/Blockly/generator/variables.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,91 @@
import * as Blockly from "blockly";

// Flag to prevent multiple alerts
let alertShown = false;

/**
* @param {string} name
* @param {!Blockly.Workspace} workspace
* @return {Blockly.VariableModel|null}
*/
function validateVariable(name, workspace) {
if (!name) {
return null;
}

name = name.trim();

if (!name) {
return null;
}

// Sammle alle Validierungsfehler
let errorMessage = null;

const reservedWords = Blockly.Generator.Arduino.RESERVED_WORDS_
? Blockly.Generator.Arduino.RESERVED_WORDS_.split(",")
: [];

if (reservedWords.includes(name)) {
errorMessage =
"The name '" + name + "' is a reserved word and cannot be used.";
} else if (/^\d/.test(name)) {
errorMessage =
"The name '" + name + "' starts with a number and cannot be used.";
} else if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {
errorMessage =
"The name '" + name + "' contains invalid characters and cannot be used.";
}

// Zeige nur einen Alert an, wenn ein Fehler gefunden wurde
if (errorMessage && !alertShown) {
alertShown = true;
if (Blockly.Msg.PROCEDURES_INVALID_NAME) {
alert(Blockly.Msg.PROCEDURES_INVALID_NAME.replace("%1", name));
} else {
alert(errorMessage);
}
// Reset the flag after a short delay
setTimeout(() => {
alertShown = false;
}, 1000);
return null;
}

let variable = workspace.getVariable(name);
if (!variable) {
variable = workspace.createVariable(name);
}
return variable;
}

const setVariableFunction = function (defaultValue) {
return function (block) {
var id = block.getFieldValue("VAR");

const variableName = Blockly.Variables.getVariable(
const variable = Blockly.Variables.getVariable(
Blockly.getMainWorkspace(),
id,
).name;
);

// const variableName = Blockly.Generator.Arduino.nameDB_.getName(
// id,
// Blockly.Variables.NAME_TYPE
// );
if (!variable) {
return "";
}

const validatedVariable = validateVariable(
variable.name,
Blockly.getMainWorkspace(),
);
if (!validatedVariable) {
return "";
}

const variableName = validatedVariable.name;
const variableValue = Blockly.Generator.Arduino.valueToCode(
block,
"VALUE",
Blockly.Generator.Arduino.ORDER_ATOMIC,
);

const allVars = Blockly.getMainWorkspace()
.getVariableMap()
Expand All @@ -29,13 +102,12 @@ const setVariableFunction = function (defaultValue) {
Blockly.Generator.Arduino.variables_[variableName + myVar.type] =
`uint16_t ${variableName}[96];\n`;
if (variableValue != "") {
code =`memcpy(${variableName}, ${variableValue}, sizeof(${variableName}));\n`;
code = `memcpy(${variableName}, ${variableValue}, sizeof(${variableName}));\n`;
}
} else {
Blockly.Generator.Arduino.variables_[variableName + myVar.type] =
`${myVar.type} ${variableName};\n`;
code =
`${variableName} = ${(variableValue || defaultValue)};\n`;
code = `${variableName} = ${variableValue || defaultValue};\n`;
}
}
return code;
Expand All @@ -45,20 +117,32 @@ const setVariableFunction = function (defaultValue) {
const getVariableFunction = function (block) {
var id = block.getFieldValue("VAR");

const variableName = Blockly.Variables.getVariable(
const variable = Blockly.Variables.getVariable(
Blockly.getMainWorkspace(),
id,
).name;
);

if (!variable) {
return ["0", Blockly.Generator.Arduino.ORDER_ATOMIC];
}

const validatedVariable = validateVariable(
variable.name,
Blockly.getMainWorkspace(),
);
if (!validatedVariable) {
return ["0", Blockly.Generator.Arduino.ORDER_ATOMIC];
}

const variableName = validatedVariable.name;

const allVars = Blockly.getMainWorkspace().getVariableMap().getAllVariables();
const myVar = allVars.filter((v) => v.name === variableName)[0];
// const variableName = Blockly.Generator.Arduino.nameDB_.getName(
// block.getFieldValue("VAR"),
// Blockly.Variables.NAME_TYPE
// );
var code = myVar.name.replace(/_/g, "__").replace(/[^a-zA-Z0-9_]/g, "_");
return [code, Blockly.Generator.Arduino.ORDER_ATOMIC];
};

Blockly.Generator.Arduino.forBlock["variables_set_dynamic"] = setVariableFunction();
Blockly.Generator.Arduino.forBlock["variables_get_dynamic"] = getVariableFunction;
Blockly.Generator.Arduino.forBlock["variables_set_dynamic"] =
setVariableFunction();
Blockly.Generator.Arduino.forBlock["variables_get_dynamic"] =
getVariableFunction;
2 changes: 2 additions & 0 deletions src/components/Blockly/msg/de_old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1460,5 +1460,7 @@ Blockly.Msg.faq_q3_question = `Ich habe einen Fehler gefunden oder etwas funktio
Blockly.Msg.faq_q3_answer = `
Am besten legst du dazu ein Issue auf [Github](https://github.com/sensebox/React-Ardublockly/issues) an. Alternativ kannst du uns auch eine Email an info(at)sensebox.de senden
`;
Blockly.Msg.PROCEDURES_INVALID_NAME =
"Der Name '%1' ist ungültig und kann nicht verwendet werden.";

export const De = Blockly.Msg;
3 changes: 3 additions & 0 deletions src/components/Blockly/msg/en_old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1235,4 +1235,7 @@ Blockly.Msg.faq_q3_answer = `
The best way to do this is to create an issue on [Github](https://github.com/sensebox/React-Ardublockly/issues). Alternatively you can send us an email to info(at)sensebox.de
`;

Blockly.Msg.PROCEDURES_INVALID_NAME =
"The name '%1' is invalid and cannot be used.";

export const En = Blockly.Msg;
Loading