selects current word automatically

This commit is contained in:
EllangoK 2023-04-17 19:02:08 -04:00
parent b8c636b10d
commit 79ba0399d8
1 changed files with 13 additions and 3 deletions

View File

@ -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})`;
}