I am emulating text editor in my project with custom caret, but native selection. Is there any way how to detect in which direction did user select the text? Lets say, that
This should work:
function isBackwards(sel) {
var rg = document.createRange();
rg.setStart(sel.anchorNode, sel.anchorOffset);
rg.setEnd(sel.focusNode, sel.focusOffset);
return !rg.toString();
}
Note: If you allow selections of nothing but breaks and whitespace you need to modify above function since it would return true
in such a case, no matter what.