diff --git a/src/main/index.html b/src/main/index.html index 930e055..54fbb2c 100644 --- a/src/main/index.html +++ b/src/main/index.html @@ -5,7 +5,7 @@ - + @@ -43,7 +43,7 @@

Converter

Convert numbers to and from various bases. - Separate values by commas to convert multiple at the same time. + Use commas to separate multiple values.

diff --git a/src/main/js/main.js b/src/main/js/main.js index 8620b41..01eb5cb 100644 --- a/src/main/js/main.js +++ b/src/main/js/main.js @@ -139,15 +139,28 @@ class NumeralSystemInput { const label = stringToHtml(``); this.wrapper.appendChild(label); - this.textarea = stringToHtml(``); + this.textarea = stringToHtml(``); this.textarea.addEventListener("beforeinput", event => { - if (!this.numeralSystem.isLegal(event.data)) + if ( + ( + event.data == null && event.inputType === "insertLineBreak" && + !this.numeralSystem.alphabet.includes("\n") + ) || + ( + event.data != null && !this.numeralSystem.isLegal(event.data) + ) + ) { event.preventDefault(); + } }); this.textarea.addEventListener("input", () => { - if (this.textarea.value === "") return; + clearInputValidity(this.textarea); - updateAllInputs(this, this.numeralSystem.basesToDecimals(this.getValues())); + try { + updateAllInputs(this, this.numeralSystem.basesToDecimals(this.getValues())); + } catch (error) { + showInputInvalid(this.textarea, error.message); + } }); this.wrapper.appendChild(this.textarea); @@ -180,6 +193,7 @@ class NumeralSystemInput { * @param decimalNumbers {bigInt[]} the decimal numbers to represent in the input */ setValues(decimalNumbers) { + clearInputValidity(this.textarea); this.textarea.value = this.numeralSystem.decimalsToBases(decimalNumbers).join(","); } } @@ -234,12 +248,14 @@ class Base64NumeralSystem extends NumeralSystem { * @returns {bigInt} the decimal representation of `baseString` */ baseToDecimal(baseString) { - if (baseString.length % 4 === 1) throw new Error("Invalid input string length."); - - const normalBaseString = stringReplaceAlls(baseString, this.alphabet, Base64NumeralSystem.defaultAlphabet()); - const hex = Array.from(atob(normalBaseString)) - .map(char => char.charCodeAt(0).toString(16).padStart(2, "0")).join(""); - return bigInt(hex, 16); + try { + const normalBaseString = stringReplaceAlls(baseString, this.alphabet, Base64NumeralSystem.defaultAlphabet()); + const hex = Array.from(atob(normalBaseString)) + .map(char => char.charCodeAt(0).toString(16).padStart(2, "0")).join(""); + return bigInt(hex, 16); + } catch (error) { + throw new Error("Invalid base64 string. Maybe the padding is wrong?"); + } } } @@ -316,7 +332,6 @@ class NumeralSystemInputWithToggleableSeparator extends NumeralSystemInput { console.warn("Toggleable separator input incorrectly used on numeral system without comma in alphabet."); super(name, numeralSystem); - this.textarea.addEventListener("input", () => clearInputValidity(this.textarea)); const id = `b${this.numeralSystem.base}-separator-toggle`; const checkboxContainer = document.createElement("div");