Prototyped custom input module code

This commit is contained in:
2026-06-26 19:34:44 +02:00
parent 8cb42f07a2
commit 4a1ddd0f98
2 changed files with 178 additions and 80 deletions
+2 -2
View File
@@ -101,7 +101,7 @@
const hostname = 'terminal'; //TODO: Change this to pull from config on db const hostname = 'terminal'; //TODO: Change this to pull from config on db
//let isProcessActive = $state(false); //let isProcessActive = $state(false);
let inputValue = $state<string>(''); let inputValue = $state('');
</script> </script>
<label for="input" class="w-11/12"> <label for="input" class="w-11/12">
@@ -126,7 +126,7 @@
id="cout" id="cout"
> >
<div id="outputWrapper"></div> <div id="outputWrapper"></div>
<Input {cwd} bind:inputValue /> <Input autofocus bind:value={inputValue} blinkDelay={500} />
</div> </div>
</div> </div>
</label> </label>
+176 -78
View File
@@ -2,96 +2,194 @@
import { onMount } from 'svelte'; import { onMount } from 'svelte';
let { let {
inputValue = $bindable(), value = $bindable(''),
isFocused, blinkDelay = 500,
cwd autofocus = true,
}: { inputValue: string; isFocused?: boolean; cwd: string } = $props(); oninput = () => {}
}: {
value?: string;
blinkDelay?: number;
autofocus?: boolean;
oninput?: (value: string) => void;
} = $props();
let cursor: any; let container: HTMLDivElement | undefined;
let input: HTMLInputElement; let cursorPos: number = $derived(value.length);
let cursorPos: number = $state(0); let text: string = $derived(value);
let locked: boolean = $state(false); let focused: boolean = $state(false);
let blinkPaused: boolean = $state(false); // true → cursor steady, false → blinking
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
onMount(() => { $effect(() => {
cursor = document.getElementById('cursor')!; if (value !== text) {
input = document.getElementById('input')! as HTMLInputElement; text = value;
cursorPos = Math.min(cursorPos, text.length);
$inspect(cursorPos); }
}); });
function shiftCursor(direction: 0 | 1) { $effect(() => {
if (cursorPos === 0 && direction === 1) return; if (autofocus && container) {
if (cursorPos === input.value.length && direction === 0) return; container.focus();
if (direction === 0) {
cursorPos += 1;
} else {
cursorPos -= 1;
} }
});
cursor.style.transform = `translateX(-${8 * cursorPos + cursorPos - 1}px)`; $effect(() => {
return () => {
if (blinkTimer) {
clearTimeout(blinkTimer);
blinkTimer = null;
}
};
});
function pauseBlink() {
blinkPaused = true;
if (blinkTimer) clearTimeout(blinkTimer);
blinkTimer = setTimeout(() => {
blinkPaused = false;
blinkTimer = null;
}, blinkDelay);
} }
function keyDown(event: KeyboardEvent) { function handleFocus() {
cursor.style.opacity = '1'; focused = true;
locked = true; blinkPaused = false; // start blinking immediately
inputValue = (event.target as HTMLInputElement).value; }
switch (event.key) { function handleBlur() {
case 'Enter': focused = false;
inputValue = ''; if (blinkTimer) {
cursor.style.transform = `translateX(0px)`; clearTimeout(blinkTimer);
break; blinkTimer = null;
case 'ArrowRight': }
shiftCursor(1); blinkPaused = false;
break; }
case 'ArrowLeft':
shiftCursor(0); function insertChar(char: string) {
break; text = text.slice(0, cursorPos) + char + text.slice(cursorPos);
cursorPos += char.length;
pauseBlink();
}
function deleteCharBefore() {
if (cursorPos > 0) {
text = text.slice(0, cursorPos - 1) + text.slice(cursorPos);
cursorPos--;
pauseBlink();
}
}
function deleteCharAfter() {
if (cursorPos < text.length) {
text = text.slice(0, cursorPos) + text.slice(cursorPos + 1);
pauseBlink();
}
}
function moveCursor(delta: number) {
const newPos = cursorPos + delta;
if (newPos >= 0 && newPos <= text.length) {
cursorPos = newPos;
pauseBlink();
}
}
function handleKeydown(e: KeyboardEvent) {
// Ctrl + A / Ctrl + E
if (e.ctrlKey) {
if (e.key === 'a' || e.key === 'A') {
e.preventDefault();
cursorPos = 0;
pauseBlink();
return;
}
if (e.key === 'e' || e.key === 'E') {
e.preventDefault();
cursorPos = text.length;
pauseBlink();
return;
}
// ignore other Ctrl combos
return;
} }
setTimeout(() => { // Arrow keys
locked = false; if (e.key === 'ArrowLeft') {
}, 100); e.preventDefault();
moveCursor(-1);
return;
}
if (e.key === 'ArrowRight') {
e.preventDefault();
moveCursor(1);
return;
}
// Home / End
if (e.key === 'Home') {
e.preventDefault();
cursorPos = 0;
pauseBlink();
return;
}
if (e.key === 'End') {
e.preventDefault();
cursorPos = text.length;
pauseBlink();
return;
}
// Backspace / Delete
if (e.key === 'Backspace') {
e.preventDefault();
deleteCharBefore();
return;
}
if (e.key === 'Delete') {
e.preventDefault();
deleteCharAfter();
return;
}
// Printable characters (length === 1 and no modifier)
if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) {
e.preventDefault();
insertChar(e.key);
return;
}
// Everything else (Enter, Tab, etc.) is ignored.
} }
onMount(() => {
if (autofocus && container) {
container.focus();
}
});
</script> </script>
<div class="relative"> <div
<div id="terminal-input"></div> id="terminal-input"
</div> class=" relative inline-block cursor-text overflow-hidden whitespace-nowrap"
bind:this={container}
<div class=" relative"> tabindex="0"
<input role="textbox"
autofocus aria-label="terminal input"
bind:value={inputValue} onblur={handleBlur}
onkeydown={(e) => keyDown(e)} onfocus={handleFocus}
onfocus={() => (isFocused = true)} onkeydown={handleKeydown}
onblur={() => (isFocused = false)} style="--cursor-index: {cursorPos}; --pad: 8px;"
type="text" >
autocapitalize="off" <span>$ </span>
autocomplete="off" <span class="text inline" aria-hidden="true">
autocorrect="off" {#each text.split('') as char}
class=" pointer-events-none absolute left-0 m-0 w-0 border-none p-0 opacity-0" <span class="char inline-block w-[1ch] text-center">{char}</span>
id="input" {/each}
/> </span>
<p class="cwd" id="cwd">{cwd}</p> {#if focused}
<div class="w flex-column flex flex-row flex-wrap font-terminal">
<span class="pointer pr-2">$</span>
<!-- prettier-ignore -->
<span style="white-space: preserve;" class=" relative wrap-break-word">
{#each inputValue.split('') as char}
<span class=" w-[1ch]">{char}</span>
{/each}
</span>
<span <span
id="cursor" class="cursor absolute bottom-[0.15rem] h-0.5 w-[1ch] animate-cursor-blink bg-amber-50"
class=" transition-transform duration-150 ease-out {isFocused && !locked class:blink={!blinkPaused}
? 'animate-cursor-blink' ></span>
: ''}">_</span {/if}
> </div>
</div>
</div>
<style>
.char
</style>