unified-doc-dom
Version:
3 lines (2 loc) • 3.47 kB
JavaScript
import{saveAs as e}from"file-saver";import t from"resize-observer-polyfill";import n from"rangy";var r=function(e){try{return Promise.resolve(e.text()).then(function(t){var n=e.name,r=e.type,i="",o=n.split(".");return o.length>1&&(i=o.pop()),{content:t,extension:i,name:n,stem:o.join("."),type:r}})}catch(e){return Promise.reject(e)}};function i(e){return new File([e.content],e.name,{type:e.type})}function o(t){var n=i(t);e(n)}function u(){return(u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var l={background:"rgba(11, 95, 255, 0.2)",durationMs:5e3,highlighterClassName:"unified-doc-highlighter",selector:function(e){return"[data-mark-id='"+e+"']"}},s=function(){function e(e,t){var n=t.background,r=t.durationMs,i=t.highlighterClassName,o=document.querySelector("."+i);o&&o.remove(),this.highlighterElement=document.createElement("div"),this.highlighterElement.className=i,this.highlighterElement.style.background=n,this.highlighterElement.style.left="0",this.highlighterElement.style.position="fixed",this.highlighterElement.style.right="0",this.highlighterElement.style.top="0",this.highlighterElement.style.animationDuration=r+"ms",e&&e.append(this.highlighterElement)}var t=e.prototype;return t.destroy=function(){this.highlighterElement.remove()},t.update=function(e){var t=e.left,n=e.top,r=e.width;this.highlighterElement.style.height=e.height+"px",this.highlighterElement.style.left=t+"px",this.highlighterElement.style.top=n+"px",this.highlighterElement.style.width=r+"px"},e}();function h(e,n,r){var i=u({},l,r),o=i.durationMs,h=e.querySelectorAll((0,i.selector)(n)),a=h[0],c=h[h.length-1],m=new s(a,i),g=new t(function(){return v()});g.observe(e),window.addEventListener("scroll",v);var d=setTimeout(function(){return f()},o);function v(){a&&c&&m.update(function(e,t,n){var r=e.getBoundingClientRect(),i=t.getBoundingClientRect();return{height:n.getBoundingClientRect().bottom-i.top,left:r.left,top:i.top,width:r.width}}(e,a,c))}function f(){clearTimeout(d),m.destroy(),g.unobserve(e),window.removeEventListener("scroll",v)}return f}function a(e,t){}function c(e,t,n){void 0===n&&(n={});var r=e.querySelectorAll("[data-mark-id]"),i=n.onClick,o=void 0===i?a:i,u=n.onMouseEnter,l=void 0===u?a:u,s=n.onMouseOut,h=void 0===s?a:s,c=t.reduce(function(e,t){return e[t.id]={click:function(e){return o(e,t)},mouseenter:function(e){return l(e,t)},mouseout:function(e){return h(e,t)}},e},{});function m(e){var t=e.getAttribute("data-mark-id");return c[t]}return r.forEach(function(e){var t=m(e)||{},n=t.click,r=t.mouseenter,i=t.mouseout;n&&e.addEventListener("click",n),r&&e.addEventListener("mouseenter",r),i&&e.addEventListener("mouseout",i)}),function(){r.forEach(function(e){var t=m(e)||{},n=t.click,r=t.mouseenter,i=t.mouseout;n&&e.removeEventListener("click",n),r&&e.removeEventListener("mouseenter",r),i&&e.removeEventListener("mouseout",i)})}}var m={callback:function(e){}};function g(e,t){function r(){var r=n.getSelection(),i=r.getBookmark(e).rangeBookmarks[0],o=i.start,u=i.end;if(u>=o){var l=e.textContent.slice(o,u);t.callback({start:o,end:u,value:l})}r.removeAllRanges()}return void 0===t&&(t=m),e.addEventListener("mouseup",r),function(){e.removeEventListener("mouseup",r)}}export{r as fromFile,h as highlight,c as registerMarks,o as saveFile,g as selectText,i as toFile};
//# sourceMappingURL=index.module.js.map