try disabling buttons when modulus is focused

This commit is contained in:
filifa 2025-12-11 23:49:34 -05:00
parent 5294860fea
commit 42d2e94fe6
1 changed files with 32 additions and 25 deletions

57
main.js
View File

@ -31,55 +31,52 @@ function evaluate() {
} }
} }
let currentInput = document.querySelector("#expr"); const expr = document.querySelector("#expr");
let selectionStart = 0; let selectionStart = 0;
let selectionEnd = 0; let selectionEnd = 0;
function setCurrentInput() { function setupExprInput() {
currentInput = this; selectionStart = expr.selectionStart;
selectionStart = this.selectionStart; selectionEnd = expr.selectionEnd;
selectionEnd = this.selectionEnd; switchButtons(true);
} }
function clear() { function clear() {
currentInput.value = ""; expr.value = "";
currentInput.focus(); expr.focus();
} }
function keyPress(c) { function keyPress(c) {
// FIXME not working with modulus expr.focus();
// errors in logs on each key press const begin = expr.value.slice(0, selectionStart);
// wonder if it's an issue with it being a number type const end = expr.value.slice(selectionEnd);
currentInput.focus(); expr.value = begin + c + end;
const begin = currentInput.value.slice(0, selectionStart); expr.setSelectionRange(selectionStart+c.length, selectionStart+c.length);
const end = currentInput.value.slice(selectionEnd);
currentInput.value = begin + c + end;
currentInput.setSelectionRange(selectionStart+c.length, selectionStart+c.length);
} }
function backspace() { function backspace() {
currentInput.focus(); expr.focus();
if (selectionEnd === 0) { if (selectionEnd === 0) {
return; return;
} }
let begin = null; let begin = null;
if (selectionStart === selectionEnd) { if (selectionStart === selectionEnd) {
begin = currentInput.value.slice(0, selectionStart-1); begin = expr.value.slice(0, selectionStart-1);
} else { } 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) { if (selectionStart === 0) {
currentInput.setSelectionRange(0, 0); expr.setSelectionRange(0, 0);
} else if (selectionStart === selectionEnd) { } else if (selectionStart === selectionEnd) {
currentInput.setSelectionRange(selectionStart-1, selectionStart-1); expr.setSelectionRange(selectionStart-1, selectionStart-1);
} else { } 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("#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("#modulus").addEventListener("keyup", (e) => evalOnEnter(e));
document.querySelector("#zero").addEventListener("click", () => keyPress("0")); document.querySelector("#zero").addEventListener("click", () => keyPress("0"));