-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.min.js
More file actions
10 lines (7 loc) · 5.92 KB
/
Copy pathinit.min.js
File metadata and controls
10 lines (7 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
/*----------------------------------------------------
textBobber() by @glenthemes
* visual novel typewriter effect plugin
* github.com/ht-devx/textBobber
2024 | All Rights Reserved
-----------------------------------------------------*/
window.textBobber=function(e){let t=e=>{let t=e.wrapper,r=e.initialDelay,o=e.textSelectors,b=e.textSelectorsFadeSpeed,s=e.characterAnimationSpeed,l=e.characterDelay,i=e.nextButton,a=e.nextButtonAnimationSpeed;function n(e){return({}).toString.call(e).match(/\s([a-zA-Z]+)/)[1].toLowerCase()}function c(e){let t,r=Number(e.replace(/[^\d\.]*/g,""));return"s"==e.toLowerCase().replace(/[^a-z]/g,"")?1e3*r:r}function d(e,t){let r,o=e.style[`${t}`],b=getComputedStyle(e).getPropertyValue(`${t}`);return""==o?b:o}let u=0,h=0,p=0,x=0,m=0;!window.matchMedia("(prefers-reduced-motion: reduce)").matches&&s&&"string"==n(s)&&""!==s.trim()&&l&&"string"==n(l)&&""!==l.trim()&&(u=c(r.trim()),h=c(b.trim()),p=c(s.trim()),x=c(l.trim()),m=c(a.trim()));let f=[];if(t){let y=n(t);"string"==y&&""!==y.trim()?document.querySelector(t)&&document.querySelector(t).setAttribute("text-bobber-wrapper",""):y.startsWith("html")&&y.endsWith("element")?t.setAttribute("text-bobber-wrapper",""):"nodelist"==y&&document.querySelectorAll(t)?.forEach(e=>{e.setAttribute("text-bobber-wrapper","")})}if(o){let A=n(o);"string"==A&&""!==A.trim()?document.querySelector(o)&&document.querySelectorAll(o)?.forEach((e,t)=>{t+=1,e.setAttribute("text-bobber",""),e.setAttribute("text-bobber-order",t)}):A.startsWith("html")&&A.endsWith("element")?(o.setAttribute("text-bobber",""),sel.setAttribute("text-bobber-order",1)):"nodelist"==A&&document.querySelectorAll(o)?.forEach((e,t)=>{t+=1,e.setAttribute("text-bobber",""),e.setAttribute("text-bobber-order",t)})}document.querySelectorAll("[text-bobber-wrapper]")?.forEach(e=>{if(e.querySelector("[text-bobber]")&&(e.setAttribute("has-bobs",""),e.querySelector("[text-bobber-order]:not([text-bobber-order=''])"))){e.setAttribute("current-text",1);let t;function r(r){if(!r.matches(".end")){r.classList.add("end"),r.querySelectorAll("span[text-bobber-chara]:not([text-bobber-whitespace])")?.forEach((e,t)=>{e.matches(".bob-it")||e.classList.add("bob-it")});let o=Number(r.getAttribute("text-bobber-order"));e.querySelector(`[text-bobber][text-bobber-order='${o+1}']`)&&t&&(t.matches(".show")||t.classList.add("show"))}}"string"==n(i)?t=e.querySelector(i):i.startsWith("html")&&selsType.endsWith("element")?t=i:"nodelist"==n(i)&&(t=i[0]),t.style.setProperty("--Text-Bobber-Next-Arrow-Animation-Speed",`${m}ms`);setTimeout(()=>{(function o(b){if(e.querySelector(b)){let s=e.querySelector(b),l=Number(s.getAttribute("text-bobber-order"))||1;1==l?s.classList.add("show"):l>1&&setTimeout(()=>{s.classList.add("show")},h),e.setAttribute("current-text",l),setTimeout(()=>{t&&t.matches(".show")&&t.classList.remove("show")},0);let i="span[text-bobber-chara]:not([text-bobber-whitespace])";if(s.querySelector(i)){let a=s.querySelectorAll(i).length;s.setAttribute("total-characters",a),s.querySelectorAll(i)?.forEach((e,t)=>{setTimeout(()=>{e.classList.add("bob-it")},x*t)});setTimeout(()=>{r(s)},x*(a-1)+p)}e.addEventListener("click",()=>{s.matches(".end")?s.matches(".pseudo-ended")||(e.querySelector(`[text-bobber][text-bobber-order='${l+1}']`)&&function t(r){let b=Number(r.current);if(!isNaN(b)){let s=e.querySelector(`[text-bobber][text-bobber-order='${b}']`);s&&s.classList.remove("show"),o(`[text-bobber][text-bobber-order="${b+1}"]`)}}({current:l}),s.classList.add("pseudo-ended")):r(s)})}})("[text-bobber][text-bobber-order='1']")},u)}}),document.querySelectorAll("[text-bobber]")?.forEach(e=>{if(!e.matches(".text-bobbed")){let t=e.offsetHeight;f.push(t),e.style.setProperty("--Text-Bobber-Dialogue-Fade-Speed",`${h}ms`),e.ariaLabel=e.textContent.trim(),e.style.setProperty("--Text-Bobber-Animation-Speed",`${p}ms`);let r=[e];for(;r.length>0;){let o=r.pop();if(3===o.nodeType&&o.data.trim().length>0&&!o.parentNode?.matches("span.temp-node")){let b=document.createElement("span");b.classList.add("temp-node"),o.before(b),b.appendChild(o)}else if(o.childNodes.length>0)for(let s=o.childNodes.length-1;s>=0;s--)r.push(o.childNodes[s])}e.querySelectorAll("span.temp-node")?.forEach(e=>{let t=Array.from(e.innerHTML);for(let r=0;r<t.length;r++)""==t[r].trim()?t[r]=`<span text-bobber-chara text-bobber-whitespace>${t[r]}</span>`:t[r]=`<span text-bobber-chara>${t[r]}</span>`;e.innerHTML=t.join("")}),e.querySelectorAll("span.temp-node")?.forEach(e=>{"temp-node"==e.classList.value.trim()?e.removeAttribute("class"):e.classList.remove("temp-node")}),e.classList.add("text-bobbed")}});let S=Math.max(...f);function g(){f=[],document.querySelectorAll("[text-bobber-parent]")?.forEach(e=>{e.style.removeProperty("--Text-Bobber-Max-Height",""),e.querySelectorAll("[text-bobber]")?.forEach(e=>{if(e.matches("[initialized]")){e.removeAttribute("initialized");let t=e.offsetHeight;f.push(t),e.setAttribute("initialized","")}});let t=Math.max(...f);e.style.setProperty("--Text-Bobber-Max-Height",`${t}px`)})}document.querySelectorAll("[text-bobber]")?.forEach(e=>{e.setAttribute("initialized","");let t=e.parentElement;"relative"==d(t,"position")||"absolute"==d(t,"position")||"fixed"==d(t,"position")||(t.style.position="relative"),t.matches("[text-bobber-parent]")||t.setAttribute("text-bobber-parent",""),t.style.setProperty("--Text-Bobber-Max-Height",`${S}px`)});let q=Date.now(),L=setInterval(()=>{Date.now()-q>3e3?(clearInterval(L),g()):("loading"==document.readyState||clearInterval(L),g())},0);Promise.all([...document.scripts].filter(e=>!e.complete).map(e=>new Promise(t=>{e.onload=e.onerror=t}))).then(()=>{g()});let w=["mouseenter","mouseover","click","keydown","scroll","touchstart"];function E(){g(),w.forEach(e=>{window.removeEventListener(e,E)})}w.forEach(e=>{window.addEventListener(e,E,{once:!0})}),window.addEventListener("resize",()=>{g()})};"loading"==document.readyState?document.addEventListener("DOMContentLoaded",()=>t(e)):t(e)};