From 42d2e94fe66fdebed5fbebea66813b3e62d10ec4 Mon Sep 17 00:00:00 2001 From: filifa Date: Thu, 11 Dec 2025 23:49:34 -0500 Subject: [PATCH] try disabling buttons when modulus is focused --- main.js | 57 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/main.js b/main.js index f4fc4a9..b1e1adc 100644 --- a/main.js +++ b/main.js @@ -31,55 +31,52 @@ function evaluate() { } } -let currentInput = document.querySelector("#expr"); +const expr = document.querySelector("#expr"); let selectionStart = 0; let selectionEnd = 0; -function setCurrentInput() { - currentInput = this; - selectionStart = this.selectionStart; - selectionEnd = this.selectionEnd; +function setupExprInput() { + selectionStart = expr.selectionStart; + selectionEnd = expr.selectionEnd; + switchButtons(true); } function clear() { - currentInput.value = ""; - currentInput.focus(); + expr.value = ""; + expr.focus(); } function keyPress(c) { - // FIXME not working with modulus - // errors in logs on each key press - // wonder if it's an issue with it being a number type - currentInput.focus(); - const begin = currentInput.value.slice(0, selectionStart); - const end = currentInput.value.slice(selectionEnd); - currentInput.value = begin + c + end; - currentInput.setSelectionRange(selectionStart+c.length, selectionStart+c.length); + expr.focus(); + const begin = expr.value.slice(0, selectionStart); + const end = expr.value.slice(selectionEnd); + expr.value = begin + c + end; + expr.setSelectionRange(selectionStart+c.length, selectionStart+c.length); } function backspace() { - currentInput.focus(); + expr.focus(); if (selectionEnd === 0) { return; } let begin = null; if (selectionStart === selectionEnd) { - begin = currentInput.value.slice(0, selectionStart-1); + begin = expr.value.slice(0, selectionStart-1); } else { - begin = currentInput.value.slice(0, selectionStart); + begin = expr.value.slice(0, selectionStart); } - const end = currentInput.value.slice(selectionEnd); + const end = expr.value.slice(selectionEnd); - currentInput.value = begin + end; + expr.value = begin + end; if (selectionStart === 0) { - currentInput.setSelectionRange(0, 0); + expr.setSelectionRange(0, 0); } else if (selectionStart === selectionEnd) { - currentInput.setSelectionRange(selectionStart-1, selectionStart-1); + expr.setSelectionRange(selectionStart-1, selectionStart-1); } else { - currentInput.setSelectionRange(selectionStart, selectionStart); + expr.setSelectionRange(selectionStart, selectionStart); } } @@ -89,10 +86,20 @@ function evalOnEnter(e) { } } -document.querySelector("#expr").addEventListener("focus", setCurrentInput); +function switchButtons(enabled) { + for (const btn of document.querySelectorAll("#main-buttons button")) { + btn.disabled = !enabled; + } + + for (const btn of document.querySelectorAll("#extra-buttons button")) { + btn.disabled = !enabled; + } +} + +document.querySelector("#expr").addEventListener("focus", setupExprInput); document.querySelector("#expr").addEventListener("keyup", (e) => evalOnEnter(e)); -document.querySelector("#modulus").addEventListener("focus", setCurrentInput); +document.querySelector("#modulus").addEventListener("focus", () => switchButtons(false)); document.querySelector("#modulus").addEventListener("keyup", (e) => evalOnEnter(e)); document.querySelector("#zero").addEventListener("click", () => keyPress("0"));