Commit e735be8b authored by missionfloyd's avatar missionfloyd
Browse files

Automatically select current word

parent 22bcc7be
Loading
Loading
Loading
Loading
+30 −5
Original line number Diff line number Diff line
@@ -44,9 +44,34 @@ function keyupEditAttention(event){
		return true;
    }
    
	// If the user hasn't selected anything, let's select their current parenthesis block
    if(! selectCurrentParenthesisBlock('<', '>')){
        selectCurrentParenthesisBlock('(', ')')
    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 or word
    if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) {
        selectCurrentWord();
    }

	event.preventDefault();