forked from Auto-Plugin/autofit.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autofit.min.js
1 lines (1 loc) · 5.44 KB
/
autofit.min.js
1
!function(t,e){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).autofit=e()}(this,(function(){"use strict";function t(t,n){var o="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!o){if(Array.isArray(t)||(o=function(t,n){if(t){if("string"==typeof t)return e(t,n);var o={}.toString.call(t).slice(8,-1);return"Object"===o&&t.constructor&&(o=t.constructor.name),"Map"===o||"Set"===o?Array.from(t):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?e(t,n):void 0}}(t))||n&&t&&"number"==typeof t.length){o&&(t=o);var i=0,r=function(){};return{s:r,n:function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var c,l=!0,a=!1;return{s:function(){o=o.call(t)},n:function(){var t=o.next();return l=t.done,t},e:function(t){a=!0,c=t},f:function(){try{l||null==o.return||o.return()}finally{if(a)throw c}}}}function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=Array(e);n<e;n++)o[n]=t[n];return o}function n(t){return(n="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)}var o=null,i="",r="",c="",l=null,a=null,u=1,s=!1,f={isAutofitRunnig:!1,init:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};(!(arguments.length>1&&void 0!==arguments[1])||arguments[1])&&console.log("autofit.js is running");var e=t.dw,n=void 0===e?1920:e,u=t.dh,f=void 0===u?1080:u,m=t.el,h=void 0===m?"string"==typeof t?t:"body":m,v=t.resize,g=void 0===v||v,p=t.ignore,b=void 0===p?[]:p,S=t.transition,w=void 0===S?"none":S,x=t.delay,A=void 0===x?0:x,j=t.limit,T=void 0===j?.1:j,M=t.cssMode,H=void 0===M?"scale":M;o=h;var R=document.querySelector(h);if(R){var q=document.createElement("style"),z=document.createElement("style");q.lang="text/css",z.lang="text/css",q.id="autofit-style",z.id="ignoreStyle",q.innerHTML="body {overflow: hidden;}";var E=document.querySelector("body");E.appendChild(q),E.appendChild(z),R.style.height="".concat(f,"px"),R.style.width="".concat(n,"px"),R.style.transformOrigin="0 0",R.style.overflow="hidden",y(n,f,R,b,T,H),l=function(){clearTimeout(a),0!=A?a=setTimeout((function(){y(n,f,R,b,T,H),s&&d(i,c,r)}),A):(y(n,f,R,b,T,H),s&&d(i,c,r))},g&&window.addEventListener("resize",l),this.isAutofitRunnig=!0,setTimeout((function(){R.style.transition="".concat(w,"s")}))}else console.error("autofit: '".concat(h,"' is not exist"))},off:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"body";try{var e;s=!1,window.removeEventListener("resize",l),null===(e=document.querySelector("#autofit-style"))||void 0===e||e.remove();var n=document.querySelector("#ignoreStyle");n&&n.remove();var r=document.querySelector(o||t);r&&(r.style=""),s&&function(){if(!i)return;for(var t=0,e=Array.from(document.querySelectorAll(i));t<e.length;t++){var n=e[t];n.style.width="",n.style.height="",n.style.transform=""}}()}catch(t){console.error("autofit: Failed to remove normally",t),this.isAutofitRunnig=!1}this.isAutofitRunnig&&console.log("autofit.js is off")},elRectification:null};function d(t){var e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1;f.isAutofitRunnig||console.error("autofit.js:autofit has not been initialized yet"),!t&&console.error("autofit.js:bad selector: ".concat(t)),i=t,r=n,c=e;var o=Array.from(document.querySelectorAll(t));if(0!=o.length){for(var l=0,a=o;l<a.length;l++){var d=a[l],y=1==u?1:Number(u)*Number(n);s||(d.originalWidth=d.clientWidth,d.originalHeight=d.clientHeight),e?(d.style.width="".concat(d.originalWidth*y,"px"),d.style.height="".concat(d.originalHeight*y,"px")):(d.style.width="".concat(100*y,"%"),d.style.height="".concat(100*y,"%")),d.style.transform="scale(".concat(1/Number(u),")"),d.style.transformOrigin="0 0"}s=!0}else console.error("autofit.js:elRectification found no element")}function y(e,o,i,r,c){var l=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"scale",a=document.documentElement.clientHeight,s=document.documentElement.clientWidth;u=s/a<e/o?s/e:a/o,u=Math.abs(1-u)>c?u.toFixed(2):1;var f=Math.round(a/Number(u)),d=Math.round(s/Number(u));i.style.height="".concat(f,"px"),i.style.width="".concat(d,"px"),"zoom"===l?i.style.zoom="".concat(u):i.style.transform="scale(".concat(u,")");var y=document.querySelector("#ignoreStyle");y.innerHTML="";var m,h=t(r);try{for(h.s();!(m=h.n()).done;){var v=m.value,g=v.el||v.dom;if("string"==typeof v&&(g=v),g&&("object"!==n(g)||Object.keys(g).length)){var p=v.scale?v.scale:1/Number(u),b=p!=u?v.fontSize:"autofit",S=p!=u?v.width:"autofit",w=p!=u?v.height:"autofit";new RegExp("".concat(g,"( |{)"),"gm").test(y.innerHTML)||(y.innerHTML+="\n".concat(g," { \n transform: scale(").concat(p,")!important;\n transform-origin: 0 0;\n width: ").concat(S,"!important;\n height: ").concat(w,"!important;\n }"),b&&(y.innerHTML+="\n".concat(g," div ,").concat(g," span,").concat(g," a,").concat(g," * {\n font-size: ").concat(b,"px;\n }")))}else console.error("autofit: found invalid or empty selector/object: ".concat(g))}}catch(t){h.e(t)}finally{h.f()}}return f.elRectification=d,f}));