Prototyped custom input module code
This commit is contained in:
@@ -101,7 +101,7 @@
|
||||
const hostname = 'terminal'; //TODO: Change this to pull from config on db
|
||||
//let isProcessActive = $state(false);
|
||||
|
||||
let inputValue = $state<string>('');
|
||||
let inputValue = $state('');
|
||||
</script>
|
||||
|
||||
<label for="input" class="w-11/12">
|
||||
@@ -126,7 +126,7 @@
|
||||
id="cout"
|
||||
>
|
||||
<div id="outputWrapper"></div>
|
||||
<Input {cwd} bind:inputValue />
|
||||
<Input autofocus bind:value={inputValue} blinkDelay={500} />
|
||||
</div>
|
||||
</div>
|
||||
</label>
|
||||
|
||||
@@ -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<typeof setTimeout> | 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;
|
||||
$effect(() => {
|
||||
if (autofocus && container) {
|
||||
container.focus();
|
||||
}
|
||||
});
|
||||
|
||||
if (direction === 0) {
|
||||
cursorPos += 1;
|
||||
} else {
|
||||
cursorPos -= 1;
|
||||
$effect(() => {
|
||||
return () => {
|
||||
if (blinkTimer) {
|
||||
clearTimeout(blinkTimer);
|
||||
blinkTimer = null;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
function pauseBlink() {
|
||||
blinkPaused = true;
|
||||
if (blinkTimer) clearTimeout(blinkTimer);
|
||||
blinkTimer = setTimeout(() => {
|
||||
blinkPaused = false;
|
||||
blinkTimer = null;
|
||||
}, blinkDelay);
|
||||
}
|
||||
|
||||
cursor.style.transform = `translateX(-${8 * cursorPos + cursorPos - 1}px)`;
|
||||
function handleFocus() {
|
||||
focused = true;
|
||||
blinkPaused = false; // start blinking immediately
|
||||
}
|
||||
|
||||
function keyDown(event: KeyboardEvent) {
|
||||
cursor.style.opacity = '1';
|
||||
locked = true;
|
||||
inputValue = (event.target as HTMLInputElement).value;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
locked = false;
|
||||
}, 100);
|
||||
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;
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="relative">
|
||||
<div id="terminal-input"></div>
|
||||
</div>
|
||||
|
||||
<div class=" relative">
|
||||
<input
|
||||
autofocus
|
||||
bind:value={inputValue}
|
||||
onkeydown={(e) => 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"
|
||||
/>
|
||||
<p class="cwd" id="cwd">{cwd}</p>
|
||||
<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>
|
||||
<div
|
||||
id="terminal-input"
|
||||
class=" relative inline-block cursor-text overflow-hidden whitespace-nowrap"
|
||||
bind:this={container}
|
||||
tabindex="0"
|
||||
role="textbox"
|
||||
aria-label="terminal input"
|
||||
onblur={handleBlur}
|
||||
onfocus={handleFocus}
|
||||
onkeydown={handleKeydown}
|
||||
style="--cursor-index: {cursorPos}; --pad: 8px;"
|
||||
>
|
||||
<span>$ </span>
|
||||
<span class="text inline" aria-hidden="true">
|
||||
{#each text.split('') as char}
|
||||
<span class="char inline-block w-[1ch] text-center">{char}</span>
|
||||
{/each}
|
||||
</span>
|
||||
{#if focused}
|
||||
<span
|
||||
id="cursor"
|
||||
class=" transition-transform duration-150 ease-out {isFocused && !locked
|
||||
? 'animate-cursor-blink'
|
||||
: ''}">_</span
|
||||
>
|
||||
</div>
|
||||
class="cursor absolute bottom-[0.15rem] h-0.5 w-[1ch] animate-cursor-blink bg-amber-50"
|
||||
class:blink={!blinkPaused}
|
||||
></span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.char
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user