From 2ae2227a4d85410c77c68b17bd0377d107edc9fa Mon Sep 17 00:00:00 2001 From: Botvid Johansson Date: Wed, 5 Feb 2025 14:37:16 +0100 Subject: [PATCH] =?UTF-8?q?lite=20hj=C3=A4lpfunktioner=20f=C3=B6r=20text?= =?UTF-8?q?=20och=20en=20textruta=20att=20skriva=20i?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/textarea/main.ts | 33 +++++++++++++++++ src/routes/+page.svelte | 9 +++-- src/routes/textarea.svelte | 72 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/lib/textarea/main.ts create mode 100644 src/routes/textarea.svelte diff --git a/src/lib/textarea/main.ts b/src/lib/textarea/main.ts new file mode 100644 index 0000000..629a65a --- /dev/null +++ b/src/lib/textarea/main.ts @@ -0,0 +1,33 @@ +export const insertText = (textarea, text) => { + const start = textarea.selectionStart; + const end = textarea.selectionStart + text.length; + textarea.setRangeText(text, start, end, "end"); +}; + +export const insertExpandedPhrase = (textarea, expander, len, text) => { + const start = textarea.selectionStart - len; + const end = textarea.selectionStart; + textarea.setRangeText(text.concat(expander), start, end, "end"); +}; + +const findIndexOfCurrentWord = (textarea) => { + const currentValue = textarea.value; + const cursorPos = textarea.selectionStart; + + let startIndex = cursorPos - 1; + while ( + startIndex >= 0 && + /\p{Letter}|\p{Number}/u.test(currentValue[startIndex]) + ) { + console.log(currentValue[startIndex]); + startIndex--; + } + return startIndex; +}; + +export const getCurrentWord = (textarea) => { + const currentValue = textarea.value; + const cursorPos = textarea.selectionStart; + const startIndex = findIndexOfCurrentWord(textarea); + return currentValue.substring(startIndex + 1, cursorPos); +}; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index cc88df0..8c1ae10 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,2 +1,7 @@ -

Welcome to SvelteKit

-

Visit svelte.dev/docs/kit to read the documentation

+ + +
+ +
+ +