From e735be8b5b455aa2bd02be0b3c6fe0596ad4ec52 Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Wed, 19 Apr 2023 22:04:34 -0600 Subject: Automatically select current word --- javascript/edit-attention.js | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'javascript/edit-attention.js') diff --git a/javascript/edit-attention.js b/javascript/edit-attention.js index 20a5aadf..1c9f6737 100644 --- a/javascript/edit-attention.js +++ b/javascript/edit-attention.js @@ -17,7 +17,7 @@ function keyupEditAttention(event){ // Find opening parenthesis around current cursor const before = text.substring(0, selectionStart); let beforeParen = before.lastIndexOf(OPEN); - if (beforeParen == -1) return false; + if (beforeParen == -1) return false; let beforeParenClose = before.lastIndexOf(CLOSE); while (beforeParenClose !== -1 && beforeParenClose > beforeParen) { beforeParen = before.lastIndexOf(OPEN, beforeParen - 1); @@ -27,7 +27,7 @@ function keyupEditAttention(event){ // Find closing parenthesis around current cursor const after = text.substring(selectionStart); let afterParen = after.indexOf(CLOSE); - if (afterParen == -1) return false; + if (afterParen == -1) return false; let afterParenOpen = after.indexOf(OPEN); while (afterParenOpen !== -1 && afterParen > afterParenOpen) { afterParen = after.indexOf(CLOSE, afterParen + 1); @@ -43,10 +43,35 @@ function keyupEditAttention(event){ target.setSelectionRange(selectionStart, selectionEnd); return true; } + + function selectCurrentWord(){ + if (selectionStart !== selectionEnd) return false; + + // Select the current word, find the start and end of the word (first space before and after) + const wordStart = text.substring(0, selectionStart).lastIndexOf(" ") + 1; + const wordEnd = text.substring(selectionEnd).indexOf(" "); + // If there is no space after the word, select to the end of the string + if (wordEnd === -1) { + selectionEnd = text.length; + } else { + selectionEnd += wordEnd; + } + selectionStart = wordStart; + + // Remove all punctuation at the end and beginning of the word + while (text[selectionStart].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) { + selectionStart++; + } + while (text[selectionEnd - 1].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) { + selectionEnd--; + } + target.setSelectionRange(selectionStart, selectionEnd); + return true; + } - // If the user hasn't selected anything, let's select their current parenthesis block - if(! selectCurrentParenthesisBlock('<', '>')){ - selectCurrentParenthesisBlock('(', ')') + // If the user hasn't selected anything, let's select their current parenthesis block or word + if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) { + selectCurrentWord(); } event.preventDefault(); -- cgit v1.2.1 From fbd34a68478caa528507fab830e3ac33a26fc6f4 Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Thu, 20 Apr 2023 01:12:59 -0600 Subject: Use string.contains() instead of regex --- javascript/edit-attention.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'javascript/edit-attention.js') diff --git a/javascript/edit-attention.js b/javascript/edit-attention.js index 1c9f6737..01d37074 100644 --- a/javascript/edit-attention.js +++ b/javascript/edit-attention.js @@ -46,6 +46,7 @@ function keyupEditAttention(event){ function selectCurrentWord(){ if (selectionStart !== selectionEnd) return false; + const delimiters = ".,\/#!$%\^&\*;:{}=\-_`~()"; // Select the current word, find the start and end of the word (first space before and after) const wordStart = text.substring(0, selectionStart).lastIndexOf(" ") + 1; @@ -59,10 +60,10 @@ function keyupEditAttention(event){ selectionStart = wordStart; // Remove all punctuation at the end and beginning of the word - while (text[selectionStart].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) { + while (delimiters.includes(text[selectionStart])) { selectionStart++; } - while (text[selectionEnd - 1].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) { + while (delimiters.includes(text[selectionEnd - 1])) { selectionEnd--; } target.setSelectionRange(selectionStart, selectionEnd); -- cgit v1.2.1 From ee172c0fc13aeb3a8c1f4a8da63551dcb93fc5ec Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Thu, 20 Apr 2023 01:34:13 -0600 Subject: Simplify finding word boundaries This also makes it work with prompts without spaces between words --- javascript/edit-attention.js | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'javascript/edit-attention.js') diff --git a/javascript/edit-attention.js b/javascript/edit-attention.js index 01d37074..80d7c01c 100644 --- a/javascript/edit-attention.js +++ b/javascript/edit-attention.js @@ -46,26 +46,18 @@ function keyupEditAttention(event){ function selectCurrentWord(){ if (selectionStart !== selectionEnd) return false; - const delimiters = ".,\/#!$%\^&\*;:{}=\-_`~()"; + const delimiters = ".,\/#!$%\^&\*;:{}=\-_`~() "; - // Select the current word, find the start and end of the word (first space before and after) - const wordStart = text.substring(0, selectionStart).lastIndexOf(" ") + 1; - const wordEnd = text.substring(selectionEnd).indexOf(" "); - // If there is no space after the word, select to the end of the string - if (wordEnd === -1) { - selectionEnd = text.length; - } else { - selectionEnd += wordEnd; + // seek backward until to find beggining + while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) { + selectionStart--; } - selectionStart = wordStart; - - // Remove all punctuation at the end and beginning of the word - while (delimiters.includes(text[selectionStart])) { - selectionStart++; - } - while (delimiters.includes(text[selectionEnd - 1])) { - selectionEnd--; + + // seek forward to find end + while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) { + selectionEnd++; } + target.setSelectionRange(selectionStart, selectionEnd); return true; } -- cgit v1.2.1 From 7ef5551634f8d06301929cb43069f5bf37ee8f1a Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Thu, 20 Apr 2023 02:24:38 -0600 Subject: Update delimiters --- javascript/edit-attention.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'javascript/edit-attention.js') diff --git a/javascript/edit-attention.js b/javascript/edit-attention.js index 80d7c01c..5fe1117c 100644 --- a/javascript/edit-attention.js +++ b/javascript/edit-attention.js @@ -46,7 +46,7 @@ function keyupEditAttention(event){ function selectCurrentWord(){ if (selectionStart !== selectionEnd) return false; - const delimiters = ".,\/#!$%\^&\*;:{}=\-_`~() "; + const delimiters = " .,\\/!?%^*;:{}=-_`~()\r\n\t"; // seek backward until to find beggining while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) { -- cgit v1.2.1 From 27d02597c78a1b189c57feea12ba522d61a56c2e Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Fri, 21 Apr 2023 01:37:29 -0600 Subject: Remove parentheses if weight == 1 --- javascript/edit-attention.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'javascript/edit-attention.js') diff --git a/javascript/edit-attention.js b/javascript/edit-attention.js index 5fe1117c..c02d292f 100644 --- a/javascript/edit-attention.js +++ b/javascript/edit-attention.js @@ -99,7 +99,13 @@ function keyupEditAttention(event){ weight = parseFloat(weight.toPrecision(12)); if(String(weight).length == 1) weight += ".0" - text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + 1 + end - 1); + if (closeCharacter == ')' && weight == 1) { + text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + 5); + selectionStart--; + selectionEnd--; + } else { + text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + 1 + end - 1); + } target.focus(); target.value = text; @@ -111,4 +117,4 @@ function keyupEditAttention(event){ addEventListener('keydown', (event) => { keyupEditAttention(event); -}); \ No newline at end of file +}); -- cgit v1.2.1 From c1fdba59045c2191433f9cdf82a1c86dc98e623b Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Fri, 21 Apr 2023 13:44:31 -0600 Subject: Remove hyphen, underscore delimiters --- javascript/edit-attention.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'javascript/edit-attention.js') diff --git a/javascript/edit-attention.js b/javascript/edit-attention.js index c02d292f..2307fd7a 100644 --- a/javascript/edit-attention.js +++ b/javascript/edit-attention.js @@ -46,7 +46,7 @@ function keyupEditAttention(event){ function selectCurrentWord(){ if (selectionStart !== selectionEnd) return false; - const delimiters = " .,\\/!?%^*;:{}=-_`~()\r\n\t"; + const delimiters = " .,\\/!?%^*;:{}=`~()\r\n\t"; // seek backward until to find beggining while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) { -- cgit v1.2.1 From 0e071ae504a8219934b2f35a1d88dcb1f628d7c3 Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Fri, 21 Apr 2023 20:19:58 -0600 Subject: Custom delimiters --- javascript/edit-attention.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'javascript/edit-attention.js') diff --git a/javascript/edit-attention.js b/javascript/edit-attention.js index 2307fd7a..588c7b77 100644 --- a/javascript/edit-attention.js +++ b/javascript/edit-attention.js @@ -46,7 +46,7 @@ function keyupEditAttention(event){ function selectCurrentWord(){ if (selectionStart !== selectionEnd) return false; - const delimiters = " .,\\/!?%^*;:{}=`~()\r\n\t"; + const delimiters = opts.keyedit_delimiters + " \r\n\t"; // seek backward until to find beggining while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) { -- cgit v1.2.1