UNPKG

animatry

Version:
3 lines (2 loc) 2.05 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).splitText=t()}(this,(function(){"use strict";const e=n=>{if("string"==typeof n){const e=n.match(/\w+\[(\d+)\]/);if(e){const t=parseInt(e[1],10);return[document.querySelectorAll(n.replace(`[${e[1]}]`,""))[t]]}return Array.from(document.querySelectorAll(n))}return n instanceof Element?[n]:n instanceof NodeList||n instanceof Array?Array.from(n).flatMap((t=>e(t))):n instanceof Document?[n.documentElement]:(t("Could not collect these elements:"+n),[])},t=e=>{console.warn(`%cAnimatry%c ${e}`,"background-color:rgba(0,0,0,0.5);padding:3px 7.5px;","")};return function(t,n){t=e(t);const o=/char/.test(n),c=/word/.test(n),i=/line/.test(n),l=[],s=[],r=[];return t.forEach((e=>{e.style.minWidth=e.clientWidth+"px";const t=e.cloneNode(!0),n=e=>{var t;if(e.nodeType===Node.TEXT_NODE){const n=null!==(t=e.textContent)&&void 0!==t?t:"";if(""===n.trim())return;const c=n.split(/(\s+)/),i=document.createDocumentFragment();c.forEach((e=>{if(""!==e.trim()){const t=document.createElement("span");t.classList.add("word"),t.style.display="inline-block",o?e.split("").forEach((e=>{const n=document.createElement("span");n.classList.add("char"),n.style.display="inline-block",n.textContent=e,t.appendChild(n),r.push(n)})):t.textContent=e,i.appendChild(t),s.push(t)}else i.appendChild(document.createTextNode(" "))})),e.replaceWith(i)}else e.nodeType===Node.ELEMENT_NODE&&Array.from(e.childNodes).forEach(n)};(c||o||i)&&n(t);i?l.push(...(()=>{const n=Array.from(t.childNodes);e.innerHTML="";const o=[];let c=0;function i(t){const n=document.createElement("div");n.classList.add("line"),n.appendChild(t),e.appendChild(n),c=n.clientHeight,o.push(n)}for(n.length>0&&i(n.shift());n.length>0;){const e=o[o.length-1];e.appendChild(n.shift()),e.clientHeight>c&&i(e.lastChild)}return o})()):e.replaceWith(t)})),{lines:l,words:s,chars:r}}})); //# sourceMappingURL=split-text.min.js.map