Skip to content

Instantly share code, notes, and snippets.

@Rplus
Last active November 25, 2016 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rplus/ba16bda29255002c17be3e634f458029 to your computer and use it in GitHub Desktop.
Save Rplus/ba16bda29255002c17be3e634f458029 to your computer and use it in GitHub Desktop.
detection for the direction of selecting text
// ref: http://stackoverflow.com/a/16265811
// 用 createRange 創一新的選擇範圍來判斷圈選的方向性
// anchorNode 是開頭圈選的 node, focusNode 是停止圈的位置
// 當圈選起始點在後面,那這個生成的 range 會壓縮成一個疊合的位置,`range.toString()` 時會變成空字串 ''
// 不過我這邊調整了一下,直接輸出 rg.collapsed 比較原生,不用再轉一次 `toString()`
function isBackwards (sel) {
var rg = document.createRange();
rg.setStart(sel.anchorNode, sel.anchorOffset);
rg.setEnd(sel.focusNode, sel.focusOffset);
return rg.collapsed;
};
@Rplus
Copy link
Author

Rplus commented Nov 25, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment