Created
March 14, 2017 21:52
-
-
Save tyler-johnson/0a3e8818de3f115b2a2dc47468ac0099 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
if (window.Selection && !Selection.prototype.extend) { | |
Selection.prototype.extend = function(el, offset) { | |
const range = document.createRange(); | |
const anchor = document.createRange(); | |
anchor.setStart(this.anchorNode, this.anchorOffset); | |
const focus = document.createRange(); | |
focus.setStart(el, offset); | |
const v = focus.compareBoundaryPoints(Range.START_TO_START, anchor); | |
if (v >= 0) { // focus is after anchor | |
range.setStart(this.anchorNode, this.anchorOffset); | |
range.setEnd(el, offset); | |
} else { // anchor is after focus | |
range.setStart(el, offset); | |
range.setEnd(this.anchorNode, this.anchorOffset); | |
} | |
this.removeAllRanges(); | |
this.addRange(range); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment