insert at last cursor position when pressing button
This commit is contained in:
parent
0bd3b5682d
commit
5d004370e3
16
main.js
16
main.js
|
|
@ -153,9 +153,13 @@ function calculate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let currentInput = null;
|
let currentInput = null;
|
||||||
|
let selectionStart = null;
|
||||||
|
let selectionEnd = null;
|
||||||
|
|
||||||
function setCurrentInput() {
|
function setCurrentInput() {
|
||||||
currentInput = this;
|
currentInput = this;
|
||||||
|
selectionStart = this.selectionStart;
|
||||||
|
selectionEnd = this.selectionEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
function clear() {
|
function clear() {
|
||||||
|
|
@ -164,8 +168,14 @@ function clear() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function keyPress(c) {
|
function keyPress(c) {
|
||||||
currentInput.value += 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();
|
currentInput.focus();
|
||||||
|
const begin = currentInput.value.slice(0, selectionStart);
|
||||||
|
const end = currentInput.value.slice(selectionEnd);
|
||||||
|
currentInput.value = begin + c + end;
|
||||||
|
currentInput.setSelectionRange(selectionEnd+1, selectionEnd+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.querySelector("#expr").addEventListener("focus", setCurrentInput);
|
document.querySelector("#expr").addEventListener("focus", setCurrentInput);
|
||||||
|
|
@ -195,5 +205,5 @@ document.querySelector("#pow").addEventListener("click", () => keyPress("^"));
|
||||||
document.querySelector("#clear").addEventListener("click", clear);
|
document.querySelector("#clear").addEventListener("click", clear);
|
||||||
document.querySelector("#enter").addEventListener("click", calculate);
|
document.querySelector("#enter").addEventListener("click", calculate);
|
||||||
|
|
||||||
// FIXME buttons don't work if you try to insert in the middle of the text
|
// TODO backspace might be tricky
|
||||||
// TODO backspace is probably going to be tricky
|
// TODO implement square root and inverse keys
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue