Custom Input Module Working mostly accurately
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
let {
|
||||
value = $bindable(''),
|
||||
blinkDelay = 500,
|
||||
blinkDelay = 250,
|
||||
autofocus = true,
|
||||
oninput = () => {}
|
||||
}: {
|
||||
@@ -15,18 +15,11 @@
|
||||
|
||||
let container: HTMLDivElement | undefined;
|
||||
let cursorPos: number = $derived(value.length);
|
||||
let text: string = $derived(value);
|
||||
let text: string = $state('');
|
||||
let focused: boolean = $state(false);
|
||||
let blinkPaused: boolean = $state(false); // true → cursor steady, false → blinking
|
||||
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
$effect(() => {
|
||||
if (value !== text) {
|
||||
text = value;
|
||||
cursorPos = Math.min(cursorPos, text.length);
|
||||
}
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (autofocus && container) {
|
||||
container.focus();
|
||||
@@ -52,11 +45,14 @@
|
||||
}
|
||||
|
||||
function handleFocus() {
|
||||
console.log('focused');
|
||||
focused = true;
|
||||
blinkPaused = false; // start blinking immediately
|
||||
}
|
||||
|
||||
function handleBlur() {
|
||||
console.log('blured');
|
||||
|
||||
focused = false;
|
||||
if (blinkTimer) {
|
||||
clearTimeout(blinkTimer);
|
||||
@@ -95,7 +91,8 @@
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
// Ctrl + A / Ctrl + E
|
||||
console.log(e.key);
|
||||
|
||||
if (e.ctrlKey) {
|
||||
if (e.key === 'a' || e.key === 'A') {
|
||||
e.preventDefault();
|
||||
@@ -109,6 +106,24 @@
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (e.key === 'd' || e.key === 'D') {
|
||||
e.preventDefault();
|
||||
deleteCharAfter();
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.key === 'ArrowLeft') {
|
||||
e.preventDefault();
|
||||
cursorPos = 0;
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (e.key === 'ArrowRight') {
|
||||
e.preventDefault();
|
||||
cursorPos = text.length;
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
// ignore other Ctrl combos
|
||||
return;
|
||||
}
|
||||
@@ -158,7 +173,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// Everything else (Enter, Tab, etc.) is ignored.
|
||||
// Everything else (Enter, Tab, etc.) is ignored.EEE
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
@@ -170,7 +185,7 @@
|
||||
|
||||
<div
|
||||
id="terminal-input"
|
||||
class=" relative inline-block cursor-text overflow-hidden whitespace-nowrap"
|
||||
class=" relative inline-block font-terminal whitespace-nowrap outline-none"
|
||||
bind:this={container}
|
||||
tabindex="0"
|
||||
role="textbox"
|
||||
@@ -178,18 +193,22 @@
|
||||
onblur={handleBlur}
|
||||
onfocus={handleFocus}
|
||||
onkeydown={handleKeydown}
|
||||
style="--cursor-index: {cursorPos}; --pad: 8px;"
|
||||
style="--cursor-index: {cursorPos};"
|
||||
>
|
||||
<span>$ </span>
|
||||
<span class="text inline" aria-hidden="true">
|
||||
<span class="prompt inline-block w-[1ch] text-center">$</span>
|
||||
<span class="inline" aria-hidden="true">
|
||||
{#each text.split('') as char}
|
||||
<span class="char inline-block w-[1ch] text-center">{char}</span>
|
||||
<span class="inline-block w-[1ch] text-center">{char}</span>
|
||||
{/each}
|
||||
</span>
|
||||
{#if focused}
|
||||
<span
|
||||
class="cursor absolute bottom-[0.15rem] h-0.5 w-[1ch] animate-cursor-blink bg-amber-50"
|
||||
class:blink={!blinkPaused}
|
||||
></span>
|
||||
{/if}
|
||||
<div
|
||||
class="cursor absolute bottom-[0.2rem] h-px w-[1ch] bg-text-primary"
|
||||
class:animate-cursor-blink={!blinkPaused}
|
||||
></div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.cursor {
|
||||
left: calc(var(--cursor-index) * 1ch + 8.5px + 1ch);
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user