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