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 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"));