This repository has been archived on 2024-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
2023-10-12 14:01:05 +02:00

1 line
2.8 KiB
JavaScript

(function(e,t){"function"==typeof define&&define.amd?define([],t()):"object"==typeof module&&module.exports?module.exports=t():e.Watermark=t()})(this,function(){function e(t={}){var n=e.prototype;this.options=t,s(this),d(this,this.options),h(this),p(this),n.upload=function(e){if(e){o.disconnect(),i.disconnect(),this.options.content=e;for(const t of this._container.querySelectorAll(".cell-watermark"))t.innerHTML=e;o.observe(this._container.parentNode,{childList:!0,subtree:!0}),i.observe(this._container,{attributes:!0,childList:!0,characterData:!0,subtree:!0})}},n.render=function(e={}){l(this,Object.assign(this.options,e))},n.destroy=function(){i.disconnect(),o.disconnect(),window.removeEventListener("resize",r),this._container.parentNode.removeChild(this._container)}}var t,n,i,o,r,c=window.outerWidth,a=window.outerHeight,s=function(e){e._container=document.createElement("div"),e._container.classList.add("cell-watermark-container"),e._container.style.cssText="display: block; pointer-events: none;",e._container.setAttribute("aria-hidden",!0),t=document.querySelector(e.options.appendTo)||document.body,t.appendChild(e._container)},d=function(e,t){t.rowSpacing=t.rowSpacing||60,t.colSpacing=t.colSpacing||30,t.width=t.width||150,t.height=t.height||20;let i=parseInt(a/(t.height+t.rowSpacing)),o=parseInt(c/(t.width+t.colSpacing)),r=(c-t.width*o-t.colSpacing*(o-1))/2,s=(a-t.height*i-t.rowSpacing*(i-1))/2,d=document.createElement("div");d.classList.add("cell-watermark"),d.style.cssText="transform: rotate(15deg); opacity: 0.1; font-size: 0.85rem; text-align: center;position: fixed; user-select: none; word-break: break-all; overflow: hidden; z-index: 999999;";for(let n=0;n<i;n++){let i=s+(t.rowSpacing+t.height)*n,c=o;n%2!=0&&c++;for(let n=0;n<c;n++){let a=r+(t.colSpacing+t.width)*n;c!==o&&(a-=(t.colSpacing+t.width)/2);let s=d.cloneNode();s.style.cssText+=`left: ${a}px; top: ${i}px; width: ${t.width}px; height: ${t.height}px`,s.style.transform=`rotate(${t.rotate}deg)`,s.style.opacity=t.opacity,s.style.fontSize=`${t.fontSize}rem`,s.style.fontFamily=t.fontFamily,s.innerHTML=t.content,e._container.appendChild(s)}}n=e._container},l=function(e,t){i.disconnect(),e._container.innerHTML="",d(e,t),i.observe(e._container,{attributes:!0,childList:!0,characterData:!0,subtree:!0})},h=function(e){i=new MutationObserver(function(t,n){l(e,e.options)}),i.observe(e._container,{attributes:!0,childList:!0,characterData:!0,subtree:!0}),o=new MutationObserver(function(e){for(let i of e)"childList"===i.type&&i.removedNodes.length>0&&0===document.querySelectorAll(".cell-watermark-container").length&&t.appendChild(n)}),o.observe(e._container.parentNode,{childList:!0,subtree:!0})},p=function(e){r=function(){window.outerHeight===a&&window.outerWidth===c||(a=window.outerHeight,c=window.outerWidth,l(e,e.options))},window.addEventListener("resize",r)};return e});