caret-pos
Version:
Locate the current position of the caret. A plain JavaScript version of Caret.js.
3 lines (2 loc) • 4.82 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).caretPos={})}(this,function(t){"use strict";function d(e,t){var n,o,r,i=document.createElement("div");return n=t,o={position:"absolute",left:-9999,top:0,zIndex:-2e3},"TEXTAREA"===e.tagName&&a.push("width"),a.forEach(function(t){o[t]=getComputedStyle(e)[t]}),r=o,Object.keys(r).forEach(function(t){i.style[t]=r[t]}),i.innerHTML=n,e.parentNode.insertBefore(i,e.nextSibling),{rect:function(){var t=i.ownerDocument.getElementById("caret-position-marker"),e={left:t.offsetLeft,top:t.offsetTop,height:t.offsetHeight};return i.parentNode.removeChild(i),e}}}var a=["borderBottomWidth","borderLeftWidth","borderRightWidth","borderTopStyle","borderRightStyle","borderBottomStyle","borderLeftStyle","borderTopWidth","boxSizing","fontFamily","fontSize","fontWeight","height","letterSpacing","lineHeight","marginBottom","marginLeft","marginRight","marginTop","outlineWidth","overflow","overflowX","overflowY","paddingBottom","paddingLeft","paddingRight","paddingTop","textAlign","textOverflow","textTransform","whiteSpace","wordBreak","wordWrap"];function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function f(t){var e=0<arguments.length&&void 0!==t?t:{},n=e.customPos,o=e.iframe,r=e.noShadowCaret;return o?{iframe:o,window:o.contentWindow,document:o.contentDocument||o.contentWindow.document,noShadowCaret:r,customPos:n}:{window:window,document:document,noShadowCaret:r,customPos:n}}function r(t,e){var n=e&&e.window||window,o=e&&e.document||document,r=t.getBoundingClientRect(),i=o.documentElement,a=n.pageXOffset||i.scrollLeft,f=n.pageYOffset||i.scrollTop;return{top:r.top+f,left:r.left+a}}function c(t){return"object"===e(t)&&null!==t}function o(f,c){function l(){return f.selectionStart}var o=function(t){function e(t){return t.replace(/<|>|`|"|&/g,"?").replace(/\r\n|\r|\n/g,"<br/>")}!c.customPos&&0!==c.customPos||(t=c.customPos);var n=void 0===t?l():t,o=f.value.slice(0,n),r=f.value.slice(n),i='<span style="position: relative; display: inline;">'.concat(e(o),"</span>");i+='<span id="caret-position-marker" style="position: relative; display: inline;">|</span>',i+='<span style="position: relative; display: inline;">'.concat(e(r),"</span>");var a=d(f,i).rect();return a.pos=l(),a};return{getPos:l,setPos:function(t){return f.setSelectionRange(t,t),f},getOffset:function(t){var e=r(f),n=o(t);return{top:e.top+n.top+c.document.body.scrollTop,left:e.left+n.left+c.document.body.scrollLeft,height:n.height}},getPosition:o}}function i(d,u){function i(){var t=s(),e={height:0,left:0,right:0};if(!t)return e;var n=u.customPos||0===u.customPos;if(0<t.endOffset-1&&t.endContainer!==d||n){var o=t.cloneRange(),r=n?u.customPos:t.endOffset;o.setStart(t.endContainer,r-1<0?0:r-1),o.setEnd(t.endContainer,r);var i=o.getBoundingClientRect();e={height:i.height,left:i.left+i.width,top:i.top},o.detach()}if((!e||e&&0===e.height)&&!u.noShadowCaret){var a=t.cloneRange(),f=u.document.createTextNode("|");a.insertNode(f),a.selectNode(f);var c=a.getBoundingClientRect();e={height:c.height,left:c.left,top:c.top},f.parentNode.removeChild(f),a.detach()}if(e){var l=u.document.documentElement;e.top+=u.window.pageYOffset-(l.clientTop||0),e.left+=u.window.pageXOffset-(l.clientLeft||0)}return e}var s=function(){if(u.window.getSelection){var t=u.window.getSelection();return 0<t.rangeCount?t.getRangeAt(0):null}},a=function(){var t=s(),e=t.cloneRange();e.selectNodeContents(d),e.setEnd(t.endContainer,t.endOffset);var n=e.toString().length;return e.detach(),n};return{getPos:a,setPos:function(a){var f=u.window.getSelection();if(f){var c=0,l=!1;!function t(e,n){for(var o=0;o<n.childNodes.length;o++){var r=n.childNodes[o];if(l)break;if(3===r.nodeType){if(c+r.length>=e){l=!0;var i=u.document.createRange();i.setStart(r,e-c),f.removeAllRanges(),f.addRange(i);break}c+=r.length}else t(a,r)}}(a,d)}return d},getPosition:function(){var t=i(),e=a(),n=d.getBoundingClientRect(),o=n.top+u.document.body.scrollTop,r=n.left+u.document.body.scrollLeft;return t.left-=r,t.top-=o,t.pos=e,t},getOffset:i,getRange:s}}function l(t,e){return(((n=t).contentEditable?"true"===n.contentEditable:"true"===n.getAttribute("contenteditable"))?i:o)(t,e);var n}t.getOffset=r,t.offset=function(t,e,n){var o=2<arguments.length&&void 0!==n?n:{};c(e)&&(o=e,e=null);var r=f(o);return l(t,r).getOffset(e)},t.position=function(t,e,n){var o=2<arguments.length&&void 0!==n?n:{};c(e)&&(o=e,e=null);var r=f(o),i=l(t,r);return e||0===e?i.setPos(e):i.getPosition()},Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=main.min.js.map