From 4a1ddd0f98cc38396fae246f04a94ddff3dbe400 Mon Sep 17 00:00:00 2001 From: gornodox Date: Fri, 26 Jun 2026 19:34:44 +0200 Subject: [PATCH] Prototyped custom input module code --- src/modules/Terminal.svelte | 4 +- src/modules/terminal/Input.svelte | 254 +++++++++++++++++++++--------- 2 files changed, 178 insertions(+), 80 deletions(-) diff --git a/src/modules/Terminal.svelte b/src/modules/Terminal.svelte index 66b9d72..155aeba 100644 --- a/src/modules/Terminal.svelte +++ b/src/modules/Terminal.svelte @@ -101,7 +101,7 @@ const hostname = 'terminal'; //TODO: Change this to pull from config on db //let isProcessActive = $state(false); - let inputValue = $state(''); + let inputValue = $state(''); diff --git a/src/modules/terminal/Input.svelte b/src/modules/terminal/Input.svelte index a7533e2..b6660f7 100644 --- a/src/modules/terminal/Input.svelte +++ b/src/modules/terminal/Input.svelte @@ -2,96 +2,194 @@ import { onMount } from 'svelte'; let { - inputValue = $bindable(), - isFocused, - cwd - }: { inputValue: string; isFocused?: boolean; cwd: string } = $props(); + value = $bindable(''), + blinkDelay = 500, + autofocus = true, + oninput = () => {} + }: { + value?: string; + blinkDelay?: number; + autofocus?: boolean; + oninput?: (value: string) => void; + } = $props(); - let cursor: any; - let input: HTMLInputElement; - let cursorPos: number = $state(0); - let locked: boolean = $state(false); + let container: HTMLDivElement | undefined; + let cursorPos: number = $derived(value.length); + let text: string = $derived(value); + let focused: boolean = $state(false); + let blinkPaused: boolean = $state(false); // true → cursor steady, false → blinking + let blinkTimer: ReturnType | null = null; - onMount(() => { - cursor = document.getElementById('cursor')!; - input = document.getElementById('input')! as HTMLInputElement; - - $inspect(cursorPos); + $effect(() => { + if (value !== text) { + text = value; + cursorPos = Math.min(cursorPos, text.length); + } }); - function shiftCursor(direction: 0 | 1) { - if (cursorPos === 0 && direction === 1) return; - if (cursorPos === input.value.length && direction === 0) return; - - if (direction === 0) { - cursorPos += 1; - } else { - cursorPos -= 1; + $effect(() => { + if (autofocus && container) { + container.focus(); } + }); - 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) { - cursor.style.opacity = '1'; - locked = true; - inputValue = (event.target as HTMLInputElement).value; + function handleFocus() { + focused = true; + blinkPaused = false; // start blinking immediately + } - switch (event.key) { - case 'Enter': - inputValue = ''; - cursor.style.transform = `translateX(0px)`; - break; - case 'ArrowRight': - shiftCursor(1); - break; - case 'ArrowLeft': - shiftCursor(0); - break; + function handleBlur() { + focused = false; + if (blinkTimer) { + clearTimeout(blinkTimer); + blinkTimer = null; + } + blinkPaused = false; + } + + function insertChar(char: string) { + 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(() => { - locked = false; - }, 100); + // Arrow keys + if (e.key === 'ArrowLeft') { + 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(); + } + }); -
-
-
- -
- keyDown(e)} - onfocus={() => (isFocused = true)} - onblur={() => (isFocused = false)} - type="text" - autocapitalize="off" - autocomplete="off" - autocorrect="off" - class=" pointer-events-none absolute left-0 m-0 w-0 border-none p-0 opacity-0" - id="input" - /> -

{cwd}

-
- $ - - - {#each inputValue.split('') as char} - {char} - {/each} - +
+ $ + + {#if focused} _ -
-
- - + class="cursor absolute bottom-[0.15rem] h-0.5 w-[1ch] animate-cursor-blink bg-amber-50" + class:blink={!blinkPaused} + > + {/if} +
\ No newline at end of file