TerminalHomepage/js/main.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-05-27 16:13:33 +00:00
const pretext = "root@baipyr.us:~# ";
let startPosition = 0;
let cursorPosition = 0;
2022-06-06 14:56:20 +00:00
let cursorYOffset = 0;
2022-05-27 16:13:33 +00:00
document.addEventListener("keypress", e=>{
if ((e.key == "Enter") || (e.code == "KeyV" && e.ctrlKey))
return;
let textbox = document.getElementById("input");
const index = cursorPosition - startPosition;
2022-06-06 14:59:58 +00:00
if (e.key == ' ' && (textbox.textContent[index-1] == ' ' || cursorPosition == startPosition))
2022-05-27 16:13:33 +00:00
return;
2022-06-06 14:59:58 +00:00
if (index <= textbox.textContent.length-1) {
const text = textbox.textContent;
2022-05-27 16:13:33 +00:00
const a = text.substring(0, index);
const b = text.substring(index, text.length);
2022-06-06 14:59:58 +00:00
textbox.textContent = a + e.key + b;
2022-05-27 16:13:33 +00:00
} else
2022-06-06 14:59:58 +00:00
textbox.textContent += e.key;
2022-05-27 16:13:33 +00:00
cursorPosition++;
updateCursor();
});
document.addEventListener("keydown", e=>{
let textbox = document.getElementById("input");
const index = cursorPosition - startPosition;
2022-06-06 14:59:58 +00:00
const text = textbox.textContent;
2022-05-27 16:13:33 +00:00
switch (e.key) {
case "Backspace":
// Ein Zeichen nach links löschen
if (index == 0)
return;
2022-06-06 14:59:58 +00:00
if (index <= textbox.textContent.length-1) {
2022-05-27 16:13:33 +00:00
const a = text.substring(0, index-1);
const b = text.substring(index, text.length);
2022-06-06 14:59:58 +00:00
textbox.textContent=a+b;
2022-05-27 16:13:33 +00:00
} else
2022-06-06 14:59:58 +00:00
textbox.textContent = text.substring(0,text.length-1);
2022-05-27 16:13:33 +00:00
cursorPosition--;
updateCursor();
break;
case "ArrowLeft":
// Ein Zeichen nach links
if (cursorPosition > startPosition) {
cursorPosition--;
updateCursor();
}
break;
case "ArrowRight":
// Ein Zeichen nach reckts
2022-06-06 14:59:58 +00:00
if (index < textbox.textContent.length) {
2022-05-27 16:13:33 +00:00
cursorPosition++;
updateCursor();
}
break;
case "Delete":
// Ein Zeichen nach rechts löschen
2022-06-06 14:59:58 +00:00
if (index == 0 || index >= textbox.textContent.length)
2022-05-27 16:13:33 +00:00
return;
const a = text.substring(0, index);
const b = text.substring(index+1, text.length);
2022-06-06 14:59:58 +00:00
textbox.textContent=a+b;
2022-05-27 16:13:33 +00:00
break;
case "Enter":
// Befehl absenden / Zeilenumbruch
const parent = document.getElementById("textbox");
parent.removeChild(textbox);
// Run command and return output???
const commandOutput = "";
2022-06-06 14:56:20 +00:00
parent.innerHTML += '<a style="color: lightgrey;">' + text + '</a><br>' + commandOutput + pretext;
2022-06-06 14:59:58 +00:00
textbox.textContent = "";
2022-05-27 16:13:33 +00:00
parent.appendChild(textbox);
cursorPosition = startPosition;
2022-06-06 14:56:20 +00:00
cursorYOffset++;
2022-05-27 16:13:33 +00:00
updateCursor();
break;
case "ArrowUp":
// Einen Befehl zurück in der History
break;
case "ArrowDown":
// Einen Befehl nach vorne in der History
break;
case "Tab":
// Autocomplete
break;
}
});
function updateCursor() {
let cursor = document.getElementById("cursor");
2022-06-06 14:56:20 +00:00
const transform = `translate(${cursorPosition}ch,${cursorYOffset}em)`;
2022-05-27 16:13:33 +00:00
cursor.style.transform = transform;
}
function preload() {
let cursor = document.getElementById("cursor");
let textbox = document.getElementById("textbox");
textbox.innerHTML = pretext+textbox.innerHTML;
const length = textbox.innerText.length+1;
const transform = `translate(${length}ch)`;
cursor.style.transform = transform;
startPosition = length;
cursorPosition = length;
}
setInterval(()=>{
let cursor = document.getElementById("cursor");
cursor.style.opacity = (cursor.style.opacity=="1")?"0":"1";
},1000);