From 79ba0399d8d70bc655269fc3318455a70d14e180 Mon Sep 17 00:00:00 2001 From: EllangoK Date: Mon, 17 Apr 2023 19:02:08 -0400 Subject: [PATCH 1/2] selects current word automatically --- web/extensions/core/editAttention.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/web/extensions/core/editAttention.js b/web/extensions/core/editAttention.js index fe395c3c..55201953 100644 --- a/web/extensions/core/editAttention.js +++ b/web/extensions/core/editAttention.js @@ -70,7 +70,7 @@ name:id, let end = inputField.selectionEnd; let selectedText = inputField.value.substring(start, end); - // If there is no selection, attempt to find the nearest enclosure + // If there is no selection, attempt to find the nearest enclosure, or select the current word if (!selectedText) { const nearestEnclosure = findNearestEnclosure(inputField.value, start); if (nearestEnclosure) { @@ -78,7 +78,18 @@ name:id, end = nearestEnclosure.end; selectedText = inputField.value.substring(start, end); } else { - return; + // Select the current word, find the start and end of the word (first space before and after) + start = inputField.value.substring(0, start).lastIndexOf(" ") + 1; + end = inputField.value.substring(end).indexOf(" ") + end; + // Remove all punctuation at the end and beginning of the word + while (inputField.value[start].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) { + start++; + } + while (inputField.value[end - 1].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) { + end--; + } + selectedText = inputField.value.substring(start, end); + if (!selectedText) return; } } @@ -97,7 +108,6 @@ name:id, // If the selection is not enclosed in parentheses, add them if (selectedText[0] !== "(" || selectedText[selectedText.length - 1] !== ")") { - console.log("adding parentheses", inputField.value[start], inputField.value[end], selectedText); selectedText = `(${selectedText})`; } From a962222992479057b104cdd06bf399d2a2cae2fa Mon Sep 17 00:00:00 2001 From: EllangoK Date: Mon, 17 Apr 2023 23:40:44 -0400 Subject: [PATCH 2/2] correctly checks end of the text --- web/extensions/core/editAttention.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/web/extensions/core/editAttention.js b/web/extensions/core/editAttention.js index 55201953..206d0830 100644 --- a/web/extensions/core/editAttention.js +++ b/web/extensions/core/editAttention.js @@ -79,8 +79,16 @@ name:id, selectedText = inputField.value.substring(start, end); } else { // Select the current word, find the start and end of the word (first space before and after) - start = inputField.value.substring(0, start).lastIndexOf(" ") + 1; - end = inputField.value.substring(end).indexOf(" ") + end; + const wordStart = inputField.value.substring(0, start).lastIndexOf(" ") + 1; + const wordEnd = inputField.value.substring(end).indexOf(" "); + // If there is no space after the word, select to the end of the string + if (wordEnd === -1) { + end = inputField.value.length; + } else { + end += wordEnd; + } + start = wordStart; + // Remove all punctuation at the end and beginning of the word while (inputField.value[start].match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/)) { start++;