From c66d7c7386f2c190ee9545a5b037692b32a47c41 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 13 Jan 2023 07:27:13 +0100 Subject: [PATCH] bug fixes and clear command --- css/main.css | 4 ++++ js/commands.js | 5 +++++ js/main.js | 38 +++++++++++++++++++++++--------------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/css/main.css b/css/main.css index 2efb837..9091a23 100644 --- a/css/main.css +++ b/css/main.css @@ -16,6 +16,10 @@ body { opacity: 0; } +#input:hover { + cursor: default; +} + #textbox { color: lime; } diff --git a/js/commands.js b/js/commands.js index e8024a0..123c471 100644 --- a/js/commands.js +++ b/js/commands.js @@ -1,3 +1,8 @@ function cmd_help() { return "You need help? Sucks to be you! Hahahahaha"; +} + +function cmd_clear() { + window.location.reload(); + return ""; } \ No newline at end of file diff --git a/js/main.js b/js/main.js index dfcccb7..e7c00a9 100644 --- a/js/main.js +++ b/js/main.js @@ -6,16 +6,23 @@ let cursorYOffset = 7; const tbDiv = document.getElementById("textbox"); const length = pretext.length+1; startPosition = length; -cursorPosition = length; +cursorPosition = 0; const textIn = document.getElementById("input"); const textCur = document.getElementById("current"); const cursor = document.getElementById("cursor"); cursor.style.transform = `translate(${length-1}ch,${2.222*7-0.4}ch)`; textIn.oninput = () => { - textCur.textContent += textIn.value; + const text = textCur.textContent; + if (cursorPosition === text.length) + textCur.textContent += textIn.value; + else { + const left = text.substring(0, cursorPosition); + const right = text.substring(cursorPosition, text.length); + textCur.textContent = left + textIn.value + right; + } + cursorPosition += textIn.value.length; textIn.value = ""; - cursorPosition++; updateCursor(); }; textIn.focus(); @@ -29,16 +36,15 @@ document.addEventListener("selectionchange", e => { }); document.addEventListener("keydown", e => { - const index = cursorPosition - startPosition; const text = textCur.textContent; switch (e.key) { case "Backspace": // Ein Zeichen nach links löschen - if (index === 0) + if (cursorPosition === 0) return; - if (index <= textCur.textContent.length-1) { - const a = text.substring(0, index-1); - const b = text.substring(index, text.length); + if (cursorPosition <= textCur.textContent.length-1) { + const a = text.substring(0, cursorPosition-1); + const b = text.substring(cursorPosition, text.length); textCur.textContent=a+b; } else textCur.textContent = text.substring(0,text.length-1); @@ -47,24 +53,26 @@ document.addEventListener("keydown", e => { break; case "ArrowLeft": // Ein Zeichen nach links - if (cursorPosition > startPosition) { + if (cursorPosition > 0) { cursorPosition--; updateCursor(); } break; case "ArrowRight": // Ein Zeichen nach reckts - if (index < textCur.textContent.length) { + if (cursorPosition < textCur.textContent.length) { cursorPosition++; updateCursor(); } break; case "Delete": // Ein Zeichen nach rechts löschen - if (index === 0 || index >= textCur.textContent.length) + if (cursorPosition >= textCur.textContent.length) return; - const a = text.substring(0, index); - const b = text.substring(index+1, text.length); + if (cursorPosition === 0 && textCur.textContent.length === 0) + return; + const a = text.substring(0, cursorPosition); + const b = text.substring(cursorPosition+1, text.length); textCur.textContent=a+b; break; case "Enter": @@ -86,7 +94,7 @@ document.addEventListener("keydown", e => { textCur.textContent = ""; tbDiv.appendChild(textCur); tbDiv.appendChild(textIn); - cursorPosition = startPosition; + cursorPosition = 0; cursorYOffset++; updateCursor(); break; @@ -131,7 +139,7 @@ function runCommand(input) { function updateCursor() { let cursor = document.getElementById("cursor"); - cursor.style.transform = `translate(${cursorPosition-1}ch, ${2.222*cursorYOffset-0.4}ch)`; + cursor.style.transform = `translate(${startPosition+cursorPosition-1}ch, ${2.222*cursorYOffset-0.4}ch)`; } setInterval(()=>{