Created
June 19, 2014 20:20
-
-
Save rynbyjn/ae1ff64a379588a9beb3 to your computer and use it in GitHub Desktop.
Gets the closest word backwards from the cursor position within a text node.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getWordFromSelection: (e) -> | |
selection = document.getSelection() | |
word = [] | |
text = selection.anchorNode?.wholeText || ' ' | |
wordArr = text.split('') | |
anchorPos = selection.anchorOffset - 1 | |
anchorPos = 0 if anchorPos < 0 | |
for index in [anchorPos..0] | |
letter = wordArr[index] | |
if letter.match(/\s/) | |
break | |
else word.unshift(letter) | |
word.join('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment