bug fixes and clear command
This commit is contained in:
parent
1d88bfa1d3
commit
c66d7c7386
@ -16,6 +16,10 @@ body {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
#input:hover {
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
#textbox {
|
||||
color: lime;
|
||||
}
|
||||
|
@ -1,3 +1,8 @@
|
||||
function cmd_help() {
|
||||
return "You need help? Sucks to be you! Hahahahaha";
|
||||
}
|
||||
|
||||
function cmd_clear() {
|
||||
window.location.reload();
|
||||
return "";
|
||||
}
|
38
js/main.js
38
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(()=>{
|
||||
|
Loading…
Reference in New Issue
Block a user