From e735be8b5b455aa2bd02be0b3c6fe0596ad4ec52 Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Wed, 19 Apr 2023 22:04:34 -0600 Subject: [PATCH 1/7] Automatically select current word --- javascript/edit-attention.js | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) 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; - // If the user hasn't selected anything, let's select their current parenthesis block - if(! selectCurrentParenthesisBlock('<', '>')){ - selectCurrentParenthesisBlock('(', ')') + // 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 or word + if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) { + selectCurrentWord(); } event.preventDefault(); From fbd34a68478caa528507fab830e3ac33a26fc6f4 Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Thu, 20 Apr 2023 01:12:59 -0600 Subject: [PATCH 2/7] Use string.contains() instead of regex --- javascript/edit-attention.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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); From ee172c0fc13aeb3a8c1f4a8da63551dcb93fc5ec Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Thu, 20 Apr 2023 01:34:13 -0600 Subject: [PATCH 3/7] 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(-) 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--; + } + + // seek forward to find end + while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) { + selectionEnd++; } - 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--; - } target.setSelectionRange(selectionStart, selectionEnd); return true; } From 7ef5551634f8d06301929cb43069f5bf37ee8f1a Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Thu, 20 Apr 2023 02:24:38 -0600 Subject: [PATCH 4/7] Update delimiters --- javascript/edit-attention.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From 27d02597c78a1b189c57feea12ba522d61a56c2e Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Fri, 21 Apr 2023 01:37:29 -0600 Subject: [PATCH 5/7] Remove parentheses if weight == 1 --- javascript/edit-attention.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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 +}); From c1fdba59045c2191433f9cdf82a1c86dc98e623b Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Fri, 21 Apr 2023 13:44:31 -0600 Subject: [PATCH 6/7] Remove hyphen, underscore delimiters --- javascript/edit-attention.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From 0e071ae504a8219934b2f35a1d88dcb1f628d7c3 Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Fri, 21 Apr 2023 20:19:58 -0600 Subject: [PATCH 7/7] Custom delimiters --- javascript/edit-attention.js | 2 +- modules/shared.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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) { diff --git a/modules/shared.py b/modules/shared.py index 5fd0eecb..3cc2f8ba 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -382,6 +382,7 @@ options_templates.update(options_section(('ui', "User interface"), { "dimensions_and_batch_together": OptionInfo(True, "Show Width/Height and Batch sliders in same row"), "keyedit_precision_attention": OptionInfo(0.1, "Ctrl+up/down precision when editing (attention:1.1)", gr.Slider, {"minimum": 0.01, "maximum": 0.2, "step": 0.001}), "keyedit_precision_extra": OptionInfo(0.05, "Ctrl+up/down precision when editing ", gr.Slider, {"minimum": 0.01, "maximum": 0.2, "step": 0.001}), + "keyedit_delimiters": OptionInfo(".,\/!?%^*;:{}=`~()", "Ctrl+up/down word delimiters"), "quicksettings": OptionInfo("sd_model_checkpoint", "Quicksettings list"), "hidden_tabs": OptionInfo([], "Hidden UI tabs (requires restart)", ui_components.DropdownMulti, lambda: {"choices": [x for x in tab_names]}), "ui_reorder": OptionInfo(", ".join(ui_reorder_categories), "txt2img/img2img UI item order"),