From f9f4b8be647653be953ce6b961af30d0d461247d Mon Sep 17 00:00:00 2001 From: Georgi-S <106542204+Georgi-S@users.noreply.github.com> Date: Tue, 29 Apr 2025 15:39:05 +0200 Subject: [PATCH] Fix variable validation to show only one alert --- src/components/Blockly/generator/variables.js | 118 +++++++++++++++--- src/components/Blockly/msg/de_old.js | 2 + src/components/Blockly/msg/en_old.js | 3 + 3 files changed, 106 insertions(+), 17 deletions(-) diff --git a/src/components/Blockly/generator/variables.js b/src/components/Blockly/generator/variables.js index 28f67dc3..cdeb62fc 100644 --- a/src/components/Blockly/generator/variables.js +++ b/src/components/Blockly/generator/variables.js @@ -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() @@ -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; @@ -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; diff --git a/src/components/Blockly/msg/de_old.js b/src/components/Blockly/msg/de_old.js index daf1930d..f0019be5 100644 --- a/src/components/Blockly/msg/de_old.js +++ b/src/components/Blockly/msg/de_old.js @@ -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; diff --git a/src/components/Blockly/msg/en_old.js b/src/components/Blockly/msg/en_old.js index 7da9f59e..5f8ee4b9 100644 --- a/src/components/Blockly/msg/en_old.js +++ b/src/components/Blockly/msg/en_old.js @@ -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;