revert to whitespace cursor, add input for copy & paste

This commit is contained in:
Baipyrus 2023-01-12 07:25:46 +01:00
parent b6953cb77c
commit b65b6c5cfd
3 changed files with 43 additions and 31 deletions

View File

@ -5,22 +5,21 @@ body {
}
#cursor {
position: absolute;
z-index: -1;
background-color: white;
display: inline-block;
opacity: 1;
border: none;
font-size: inherit;
max-width: 1ch;
text-align: center;
caret-color: transparent;
}
#cursor:focus {
outline: none;
#input {
opacity: 0;
}
#textbox {
color: lime;
}
#input {
#current {
color: lightgrey;
}

View File

@ -7,10 +7,10 @@
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<!-- <div id="cursor">&nbsp;</div>-->
<div id="cursor">&nbsp;</div>
<div id="textbox">
<a id="input"></a>
<input type="text" id="cursor">
<a id="current"></a>
<input type="text" id="input">
</div>
<script type="text/javascript" src="js/main.js"></script>
</body>

View File

@ -8,64 +8,71 @@ tbDiv.innerHTML = pretext+tbDiv.innerHTML;
const length = tbDiv.innerText.length+1;
startPosition = length;
cursorPosition = length;
const cursor = document.getElementById("cursor");
const textIn = document.getElementById("input");
const textCur = document.getElementById("current");
const cursor = document.getElementById("cursor");
cursor.style.transform = `translate(${length-1}ch)`;
cursor.oninput = () => {
textIn.textContent += cursor.value;
cursor.value = "";
textIn.oninput = () => {
textCur.textContent += textIn.value;
textIn.value = "";
cursorPosition++;
updateCursor();
};
cursor.focus();
textIn.focus();
document.addEventListener("selectionchange", e => {
if (e.target !== cursor) {
if (e.target !== textIn) {
const selection = window.getSelection();
if (selection.type === "Caret")
cursor.focus();
console.log(selection);
textIn.focus();
}
});
document.addEventListener("keydown", e => {
const index = cursorPosition - startPosition;
const text = textIn.textContent;
const text = textCur.textContent;
switch (e.key) {
case "Backspace":
// Ein Zeichen nach links löschen
if (index === 0)
return;
if (index <= textIn.textContent.length-1) {
if (index <= textCur.textContent.length-1) {
const a = text.substring(0, index-1);
const b = text.substring(index, text.length);
textIn.textContent=a+b;
textCur.textContent=a+b;
} else
textIn.textContent = text.substring(0,text.length-1);
textCur.textContent = text.substring(0,text.length-1);
cursorPosition--;
updateCursor();
break;
case "ArrowLeft":
// Ein Zeichen nach links
if (cursorPosition > startPosition)
if (cursorPosition > startPosition) {
cursorPosition--;
updateCursor();
}
break;
case "ArrowRight":
// Ein Zeichen nach reckts
if (index < textIn.textContent.length)
if (index < textCur.textContent.length) {
cursorPosition++;
updateCursor();
}
break;
case "Delete":
// Ein Zeichen nach rechts löschen
if (index === 0 || index >= textIn.textContent.length)
if (index === 0 || index >= textCur.textContent.length)
return;
const a = text.substring(0, index);
const b = text.substring(index+1, text.length);
textIn.textContent=a+b;
textCur.textContent=a+b;
break;
case "Enter":
// Befehl absenden / Zeilenumbruch
const cursor = document.getElementById("cursor");
tbDiv.removeChild(cursor);
const textIn = document.getElementById("input");
tbDiv.removeChild(textIn);
tbDiv.removeChild(textCur);
// Run command and return output???
let commandOutput = "";
@ -76,11 +83,12 @@ document.addEventListener("keydown", e => {
cursorYOffset++;
}
tbDiv.innerHTML += `${aTag(text)}<br>${commandOutput}${pretext}`;
textIn.textContent = "";
textCur.textContent = "";
tbDiv.appendChild(textCur);
tbDiv.appendChild(textIn);
tbDiv.appendChild(cursor);
cursorPosition = startPosition;
cursorYOffset++;
updateCursor();
break;
case "ArrowUp":
// Einen Befehl zurück in der History
@ -94,6 +102,11 @@ document.addEventListener("keydown", e => {
}
});
function updateCursor() {
let cursor = document.getElementById("cursor");
cursor.style.transform = `translate(${cursorPosition-1}ch, ${cursorYOffset}em)`;
}
setInterval(()=>{
cursor.style.opacity = !parseInt(cursor.style.opacity)+0;
},1000);