forward
如果用户从左到右选择文本,则以下函数返回。在另一种情况下它会返回backward
。
const getDirection = function () {
const selection = window.getSelection();
const range = document.createRange();
range.setStart(selection.anchorNode, selection.anchorOffset);
range.setEnd(selection.focusNode, selection.focusOffset);
return range.collapsed ? 'backward' : 'forward';
};