diff --git a/package.json b/package.json index c8453a0..2d53ff0 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,8 @@ "#lib/*": "./src/lib/*" }, "dependencies": { - "@lucide/svelte": "^1.66.0" + "@lucide/svelte": "^1.66.0", + "@wterm/dom": "^0.5.9", + "@wterm/svelte": "^0.5.9" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 990e988..229fd22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,12 @@ importers: '@lucide/svelte': specifier: ^1.66.0 version: 1.66.0(svelte@5.57.3(@typescript-eslint/types@8.72.1)) + '@wterm/dom': + specifier: ^0.5.9 + version: 0.5.9 + '@wterm/svelte': + specifier: ^0.5.9 + version: 0.5.9(@wterm/dom@0.5.9)(svelte@5.57.3(@typescript-eslint/types@8.72.1)) devDependencies: '@eslint/js': specifier: ^10.0.1 @@ -477,6 +483,18 @@ packages: resolution: {integrity: sha512-SB8xqipSzZHUePCCLNeYxqnxq83X61/qzNhjQGbTfjxGZIeOEP7tBgWYX05Tcd6CgE02WdTJnh5b8kMZJ/+bnQ==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + '@wterm/core@0.5.9': + resolution: {integrity: sha512-+S5VA/CGmbU60PnN8PrE8i0hWBbzUzdrbjRYsnaioMum7alubzgRQ7IJ+SWCeymX6lKA6SfzfdkdYsNffTLA1Q==} + + '@wterm/dom@0.5.9': + resolution: {integrity: sha512-0Ghz0FVbtr6g4Frk8F3I92H9UjCsIbhjQ1X8U+tly9oF172qDq685mrRPBeI8XZ5OIXK2bLr+/XAEYPUmxzwBA==} + + '@wterm/svelte@0.5.9': + resolution: {integrity: sha512-keIpy5jH2bDh/p+F5sAjf0OSQEVnufCHfEXL/VqdbmqyB21+Dw+wr+y2S1WBZVTxaGgEp2F6GRWF0bvt3XM1wA==} + peerDependencies: + '@wterm/dom': 0.5.9 + svelte: ^5.57.0 + acorn-jsx@5.3.2: resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==} peerDependencies: @@ -1603,6 +1621,17 @@ snapshots: '@typescript-eslint/types': 8.72.1 eslint-visitor-keys: 5.0.1 + '@wterm/core@0.5.9': {} + + '@wterm/dom@0.5.9': + dependencies: + '@wterm/core': 0.5.9 + + '@wterm/svelte@0.5.9(@wterm/dom@0.5.9)(svelte@5.57.3(@typescript-eslint/types@8.72.1))': + dependencies: + '@wterm/dom': 0.5.9 + svelte: 5.57.3(@typescript-eslint/types@8.72.1) + acorn-jsx@5.3.2(acorn@8.19.0): dependencies: acorn: 8.19.0 diff --git a/src/lib/modules/Input.svelte b/src/lib/modules/Input.svelte index 1331819..d391bcb 100644 --- a/src/lib/modules/Input.svelte +++ b/src/lib/modules/Input.svelte @@ -2,13 +2,17 @@ import { onMount } from 'svelte'; let { + children, value = $bindable(''), + cursorPrompt = '$', blinkDelay = 250, autofocus = true, oninput = () => {}, onsubmit = () => {} }: { + children: any; value?: string; + cursorPrompt?: string; blinkDelay?: number; autofocus?: boolean; oninput?: (value: string) => void; @@ -16,6 +20,8 @@ } = $props(); let container: HTMLElement; + let textField: HTMLElement; + let cursor: HTMLElement; let cursorPos: number = $state(value.length); let anchorPos: number | null = $state(null); let isDragging: boolean = $state(false); @@ -80,6 +86,62 @@ anchorPos = null; } + // ---- cursor alignment --------------------------------------------------- + + /** Move the caret to the exact pixel where the character at cursorPos lives. */ + function syncCursor() { + if (!textField || !cursor) return; + + /* const containerRect = container.getBoundingClientRect(); + if (containerRect.width === 0) return; + + const chars = container.querySelectorAll('.char'); + // With the placeholder, chars is never empty — but guard anyway. + if (chars.length === 0) return; */ + + const containerRect = textField.getBoundingClientRect(); + const chars = textField.querySelectorAll('.char'); + + if (chars.length === 0) { + cursor.style.transform = `translateX(0px)`; + return; + } + + let refRect: DOMRect; + let x: number; + + if (cursorPos >= chars.length) { + refRect = chars[chars.length - 1].getBoundingClientRect(); + x = refRect.right - containerRect.left; + } else { + refRect = chars[cursorPos].getBoundingClientRect(); + x = refRect.left - containerRect.left; + if (chars[cursorPos].classList.contains('selected')) { + cursor.style.backgroundColor = 'var(--color-bg-hard)'; + } else { + cursor.style.backgroundColor = 'var(--color-fg1)'; + } + } + + cursor.style.transform = `translateX(${x}px)`; + } + + // Re-sync after every value / cursor movement (runs after DOM update). + $effect(() => { + void value; + void cursorPos; + syncCursor(); + }); + + // Re-sync on webfont load and on resize (font metrics can change). + $effect(() => { + if (!container) return; + const ro = new ResizeObserver(() => syncCursor()); + ro.observe(container); + document.fonts?.ready.then(() => syncCursor()); + return () => ro.disconnect(); + }); + $effect(() => { if (autofocus && container) { container.focus(); @@ -303,7 +365,7 @@
- - $ -
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index a4173ad..aa4c6b2 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -102,14 +102,14 @@
diff --git a/src/routes/test/+page.svelte b/src/routes/test/+page.svelte index 16a7e0e..c913b31 100644 --- a/src/routes/test/+page.svelte +++ b/src/routes/test/+page.svelte @@ -27,7 +27,7 @@ wrapper, username: 'gornodox', hostname: 'termos', - working_dir: '~', + working_dir: '~/Documents/Projects/gornodox.dev', mwin: api, close: remove } diff --git a/src/routes/test2/+page.svelte b/src/routes/test2/+page.svelte new file mode 100644 index 0000000..d66f316 --- /dev/null +++ b/src/routes/test2/+page.svelte @@ -0,0 +1,19 @@ + + + diff --git a/src/style/global.css b/src/style/global.css index d446c66..b0625d0 100644 --- a/src/style/global.css +++ b/src/style/global.css @@ -135,10 +135,6 @@ --font-ibm: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } -@utility glow { - text-shadow: 0 0 6px var(--glow-color); -} - @utility bg-pattern-dots { background-image: var(--background-image-pattern-dots); background-size: 20px 20px;