Moved to a new clean project on pnpm
This commit is contained in:
@@ -0,0 +1,350 @@
|
||||
<script lang="ts">
|
||||
import { petals } from '#lib/stores/petals.ts';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
let {
|
||||
value = $bindable(''),
|
||||
blinkDelay = 250,
|
||||
autofocus = true,
|
||||
oninput = () => {}
|
||||
}: {
|
||||
value?: string;
|
||||
blinkDelay?: number;
|
||||
autofocus?: boolean;
|
||||
oninput?: (value: string) => void;
|
||||
} = $props();
|
||||
|
||||
let container: HTMLElement;
|
||||
let cursorPos: number = $state(value.length);
|
||||
let anchorPos: number | null = $state(null);
|
||||
let isDragging: boolean = $state(false);
|
||||
let focused: boolean = $state(false);
|
||||
let blinkPaused: boolean = $state(false);
|
||||
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const mods: Record<string, (e: KeyboardEvent) => void> = {
|
||||
a: () => {
|
||||
anchorPos = 0;
|
||||
cursorPos = value.length;
|
||||
pauseBlink();
|
||||
},
|
||||
c: () => copySelection(),
|
||||
x: () => cutSelection(),
|
||||
e: () => {
|
||||
anchorPos = null;
|
||||
cursorPos = value.length;
|
||||
pauseBlink();
|
||||
},
|
||||
d: () => deleteCharAfter()
|
||||
};
|
||||
const base: Record<string, (e: KeyboardEvent) => void> = {
|
||||
Home: (e) => moveTo(0, e.shiftKey),
|
||||
End: (e) => moveTo(value.length, e.shiftKey),
|
||||
ArrowLeft: (e) =>
|
||||
e.ctrlKey ? moveTo(prevWordBoundary(cursorPos), e.shiftKey) : moveCursor(-1, e.shiftKey),
|
||||
ArrowRight: (e) =>
|
||||
e.ctrlKey ? moveTo(nextWordBoundary(cursorPos), e.shiftKey) : moveCursor(1, e.shiftKey),
|
||||
Backspace: () => deleteCharBefore(),
|
||||
Delete: () => deleteCharAfter(),
|
||||
Enter: () => gust()
|
||||
};
|
||||
|
||||
let selection = $derived.by(() => {
|
||||
if (anchorPos === null || anchorPos === cursorPos) {
|
||||
return { active: false, start: -1, end: -1 };
|
||||
}
|
||||
return {
|
||||
active: true,
|
||||
start: Math.min(anchorPos, cursorPos),
|
||||
end: Math.max(anchorPos, cursorPos)
|
||||
};
|
||||
});
|
||||
|
||||
function isSelected(i: number): boolean {
|
||||
return selection.active && i >= selection.start && i < selection.end;
|
||||
}
|
||||
|
||||
function gust(): void {
|
||||
if (!container) return;
|
||||
petals(container, { duration: 4000, swayAmp: 26, hideOriginals: false });
|
||||
value = '';
|
||||
cursorPos = 0;
|
||||
anchorPos = null;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (autofocus && container) {
|
||||
container.focus();
|
||||
}
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
return () => {
|
||||
if (blinkTimer) {
|
||||
clearTimeout(blinkTimer);
|
||||
blinkTimer = null;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (cursorPos > value.length) cursorPos = value.length;
|
||||
});
|
||||
|
||||
// Global mousemove / mouseup during drag.
|
||||
$effect(() => {
|
||||
if (!isDragging) return;
|
||||
|
||||
const onMove = (e: MouseEvent) => {
|
||||
const index = charIndexFromEvent(e);
|
||||
if (index === null) return;
|
||||
cursorPos = index;
|
||||
pauseBlink();
|
||||
};
|
||||
|
||||
window.addEventListener('mousemove', onMove);
|
||||
window.addEventListener('mouseup', () => (isDragging = false));
|
||||
return () => {
|
||||
window.removeEventListener('mousemove', onMove);
|
||||
window.removeEventListener('mouseup', () => (isDragging = false));
|
||||
};
|
||||
});
|
||||
|
||||
export function focusInput(): void {
|
||||
container?.focus();
|
||||
}
|
||||
|
||||
function pauseBlink() {
|
||||
blinkPaused = true;
|
||||
if (blinkTimer) clearTimeout(blinkTimer);
|
||||
blinkTimer = setTimeout(() => {
|
||||
blinkPaused = false;
|
||||
blinkTimer = null;
|
||||
}, blinkDelay);
|
||||
}
|
||||
|
||||
function handleFocus() {
|
||||
focused = true;
|
||||
blinkPaused = false;
|
||||
}
|
||||
|
||||
function handleBlur() {
|
||||
focused = false;
|
||||
if (blinkTimer) {
|
||||
clearTimeout(blinkTimer);
|
||||
blinkTimer = null;
|
||||
}
|
||||
blinkPaused = true;
|
||||
}
|
||||
|
||||
function charIndexFromEvent(e: MouseEvent): number | null {
|
||||
const elem = (e.target as HTMLElement | null)?.closest?.(
|
||||
'[data-char-index]'
|
||||
) as HTMLElement | null;
|
||||
if (!elem) return null;
|
||||
const raw = elem.getAttribute('data-char-index');
|
||||
return raw === null ? null : Number(raw);
|
||||
}
|
||||
|
||||
function getSelectedText(): string {
|
||||
if (!selection.active) return '';
|
||||
return value.slice(selection.start, selection.end);
|
||||
}
|
||||
|
||||
function deleteSelection(): void {
|
||||
if (!selection.active) return;
|
||||
value = value.slice(0, selection.start) + value.slice(selection.end);
|
||||
cursorPos = selection.start;
|
||||
anchorPos = null;
|
||||
oninput(value);
|
||||
}
|
||||
|
||||
function insertChar(char: string): void {
|
||||
if (selection.active) {
|
||||
value = value.slice(0, selection.start) + char + value.slice(selection.end);
|
||||
cursorPos = selection.start + char.length;
|
||||
anchorPos = null;
|
||||
} else {
|
||||
value = value.slice(0, cursorPos) + char + value.slice(cursorPos);
|
||||
cursorPos += char.length;
|
||||
}
|
||||
oninput(value);
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function deleteCharBefore() {
|
||||
if (selection.active) {
|
||||
deleteSelection();
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (cursorPos > 0) {
|
||||
value = value.slice(0, cursorPos - 1) + value.slice(cursorPos);
|
||||
oninput(value);
|
||||
cursorPos--;
|
||||
pauseBlink();
|
||||
}
|
||||
}
|
||||
|
||||
function deleteCharAfter() {
|
||||
if (selection.active) {
|
||||
deleteSelection();
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (cursorPos < value.length) {
|
||||
value = value.slice(0, cursorPos) + value.slice(cursorPos + 1);
|
||||
oninput(value);
|
||||
pauseBlink();
|
||||
}
|
||||
}
|
||||
|
||||
function moveCursor(delta: number, extend: boolean) {
|
||||
const newPos = cursorPos + delta;
|
||||
if (newPos < 0 || newPos > value.length) return;
|
||||
if (extend) {
|
||||
if (anchorPos === null) anchorPos = cursorPos;
|
||||
} else {
|
||||
anchorPos = null;
|
||||
}
|
||||
cursorPos = newPos;
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function moveTo(pos: number, extend: boolean) {
|
||||
if (extend) {
|
||||
if (anchorPos === null) anchorPos = cursorPos;
|
||||
} else {
|
||||
anchorPos = null;
|
||||
}
|
||||
cursorPos = pos;
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function prevWordBoundary(pos: number): number {
|
||||
let i = pos;
|
||||
while (i > 0 && !/\s/.test(value[i - 1])) i--;
|
||||
while (i > 0 && /\s/.test(value[i - 1])) i--;
|
||||
return i;
|
||||
}
|
||||
|
||||
function nextWordBoundary(pos: number): number {
|
||||
let i = pos;
|
||||
while (i < value.length && !/\s/.test(value[i])) i++;
|
||||
while (i < value.length && /\s/.test(value[i])) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
function handleMouseDown(e: MouseEvent): void {
|
||||
e.preventDefault();
|
||||
const idx = charIndexFromEvent(e);
|
||||
const clickPos = idx ?? value.length;
|
||||
|
||||
if (e.shiftKey) {
|
||||
if (anchorPos === null) anchorPos = cursorPos;
|
||||
cursorPos = clickPos;
|
||||
} else {
|
||||
anchorPos = clickPos;
|
||||
cursorPos = clickPos;
|
||||
}
|
||||
isDragging = true;
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function handlePaste(e: ClipboardEvent): void {
|
||||
e.preventDefault();
|
||||
const text = e.clipboardData?.getData('text/plain') ?? '';
|
||||
if (text) insertChar(text);
|
||||
}
|
||||
|
||||
function copySelection(): void {
|
||||
if (!selection.active) return;
|
||||
void navigator.clipboard.writeText(getSelectedText());
|
||||
}
|
||||
|
||||
function cutSelection(): void {
|
||||
if (!selection.active) return;
|
||||
void navigator.clipboard.writeText(getSelectedText());
|
||||
deleteSelection();
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
const isMod = e.ctrlKey || e.metaKey;
|
||||
const key = e.key.length === 1 ? e.key.toLowerCase() : e.key;
|
||||
|
||||
function insertIfChar(e: KeyboardEvent): ((e: KeyboardEvent) => void) | undefined {
|
||||
if (key.length !== 1 || e.altKey || e.metaKey) return undefined;
|
||||
return (e) => insertChar(e.key);
|
||||
}
|
||||
|
||||
const handler = isMod ? mods[key] : (base[key] ?? insertIfChar(e));
|
||||
if (!handler) return;
|
||||
|
||||
e.preventDefault();
|
||||
handler(e);
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
if (autofocus && container) {
|
||||
container.focus();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div
|
||||
id="terminal-input"
|
||||
class="font-terminal relative inline-block whitespace-nowrap outline-none select-none"
|
||||
bind:this={container}
|
||||
tabindex="1"
|
||||
role="textbox"
|
||||
aria-label="terminal input"
|
||||
onblur={handleBlur}
|
||||
onfocus={handleFocus}
|
||||
onkeydown={handleKeydown}
|
||||
onpaste={handlePaste}
|
||||
onmousedown={handleMouseDown}
|
||||
style="--cursor-index: {cursorPos};"
|
||||
>
|
||||
<span class="prompt inline-block w-[1ch] text-center">$</span>
|
||||
<span class="inline cursor-text" aria-hidden="true">
|
||||
{#each value.split('') as char, i}
|
||||
<span
|
||||
class="char inline-block w-[1ch] text-center whitespace-pre"
|
||||
class:selected={isSelected(i)}
|
||||
data-char-index={i}>{char}</span
|
||||
>
|
||||
{/each}
|
||||
</span>
|
||||
<div
|
||||
id="curs"
|
||||
class="cursor bg-text-primary absolute bottom-[0.2rem] h-px w-[1ch]"
|
||||
class:animate-cursor-blink={!blinkPaused && !selection.active}
|
||||
></div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.cursor {
|
||||
left: calc(var(--cursor-index) * 1ch + 8.5px + 1ch);
|
||||
transition: all 0.1s ease;
|
||||
}
|
||||
.char {
|
||||
transition: all linear 0.1s;
|
||||
}
|
||||
|
||||
.char.selected {
|
||||
transition: none;
|
||||
background-color: var(--color-highlight);
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.char.selected + .char.selected {
|
||||
border-top-left-radius: 0;
|
||||
border-bottom-left-radius: 0;
|
||||
}
|
||||
|
||||
.char.selected:has(+ .char.selected) {
|
||||
border-top-right-radius: 0;
|
||||
border-bottom-right-radius: 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user