-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtextFit.min.js
1 lines (1 loc) · 3.11 KB
/
textFit.min.js
1
((t,e)=>{"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.textFit=e()})("object"==typeof global?global:this,function(){var r={alignVert:!1,alignHoriz:!1,multiLine:!1,detectMultiLine:!0,minFontSize:6,maxFontSize:80,reProcess:!0,widthOnly:!1,alignVertWithFlexbox:!1};return function(t,e){e=e||{};var i,n={};for(i in r)e.hasOwnProperty(i)?n[i]=e[i]:n[i]=r[i];"function"==typeof t.toArray&&(t=t.toArray());var l=Object.prototype.toString.call(t);"[object Array]"!==l&&"[object NodeList]"!==l&&"[object HTMLCollection]"!==l&&(t=[t]);for(var o=0;o<t.length;o++)((t,e)=>{if(!(t=>"object"==typeof HTMLElement?t instanceof HTMLElement:t&&"object"==typeof t&&null!==t&&1===t.nodeType&&"string"==typeof t.nodeName)(t)||!e.reProcess&&t.getAttribute("textFitted"))return;var i,n,l,o,r,a;if(e.reProcess||t.setAttribute("textFitted",1),s=t.innerHTML,l=(t=>{var e=window.getComputedStyle(t,null);return t.getBoundingClientRect().width-parseInt(e.getPropertyValue("padding-left"),10)-parseInt(e.getPropertyValue("padding-right"),10)})(t),n=(t=>{var e=window.getComputedStyle(t,null);return t.getBoundingClientRect().height-parseInt(e.getPropertyValue("padding-top"),10)-parseInt(e.getPropertyValue("padding-bottom"),10)})(t),!l||!e.widthOnly&&!n)throw e.widthOnly?new Error("Set a static width on the target element "+t.outerHTML+" before using textFit!"):new Error("Set a static height and width on the target element "+t.outerHTML+" before using textFit!");-1===s.indexOf("textFitted")?((i=document.createElement("span")).className="textFitted",i.style.display="inline-block",i.innerHTML=s,t.innerHTML="",t.appendChild(i)):c(i=t.querySelector("span.textFitted"),"textFitAlignVert")&&(i.className=i.className.replace("textFitAlignVert",""),i.style.height="",t.className.replace("textFitAlignVertFlex","")),e.alignHoriz&&(t.style["text-align"]="center",i.style["text-align"]="center");for(var s=e.multiLine,d=((s=e.detectMultiLine&&!s&&i.getBoundingClientRect().height>=2*parseInt(window.getComputedStyle(i)["font-size"],10)?!0:s)||(t.style["white-space"]="nowrap"),a=e.maxFontSize,o=e.minFontSize);o<=a;){r=a+o>>1,i.style.fontSize=r+"px";var g=i.getBoundingClientRect();g.width<=l&&(e.widthOnly||g.height<=n)?o=1+(d=r):a=r-1}i.style.fontSize!=d+"px"&&(i.style.fontSize=d+"px"),e.alignVert&&((()=>{var t,e;document.getElementById("textFitStyleSheet")||(t=[".textFitAlignVert{","position: absolute;","top: 0; right: 0; bottom: 0; left: 0;","margin: auto;","display: flex;","justify-content: center;","flex-direction: column;","}",".textFitAlignVertFlex{","display: flex;","}",".textFitAlignVertFlex .textFitAlignVert{","position: static;","}"].join(""),(e=document.createElement("style")).type="text/css",e.id="textFitStyleSheet",e.innerHTML=t,document.body.appendChild(e))})(),s=i.scrollHeight,"static"===window.getComputedStyle(t).position&&(t.style.position="relative"),c(i,"textFitAlignVert")||(i.className=i.className+" textFitAlignVert"),i.style.height=s+"px",e.alignVertWithFlexbox)&&!c(t,"textFitAlignVertFlex")&&(t.className=t.className+" textFitAlignVertFlex")})(t[o],n)};function c(t,e){return-1<(" "+t.className+" ").indexOf(" "+e+" ")}});