29 lines
28 KiB
JavaScript
29 lines
28 KiB
JavaScript
/** CharWrapper v2.0.2 | MIT License | github.com/rowild/charwrapper */
|
|
"use strict";var CharWrapperModule=(()=>{var dt=Object.defineProperty;var Xt=Object.getOwnPropertyDescriptor;var Qt=Object.getOwnPropertyNames;var Yt=Object.prototype.hasOwnProperty;var Ot=a=>{throw TypeError(a)};var te=(a,t)=>{for(var e in t)dt(a,e,{get:t[e],enumerable:!0})},ee=(a,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of Qt(t))!Yt.call(a,n)&&n!==e&&dt(a,n,{get:()=>t[n],enumerable:!(r=Xt(t,n))||r.enumerable});return a};var re=a=>ee(dt({},"__esModule",{value:!0}),a);var pt=(a,t,e)=>t.has(a)||Ot("Cannot "+e);var s=(a,t,e)=>(pt(a,t,"read from private field"),e?e.call(a):t.get(a)),y=(a,t,e)=>t.has(a)?Ot("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(a):t.set(a,e),d=(a,t,e,r)=>(pt(a,t,"write to private field"),r?r.call(a,e):t.set(a,e),e),p=(a,t,e)=>(pt(a,t,"access private method"),e);var oe={};te(oe,{CharWrapper:()=>lt,default:()=>ne});var se={wrap:{chars:!0,words:!1,spaces:!1,specialChars:!1},enumerate:{rootSet:!1,chars:!1,words:!1,attributeSets:!1,includeSpaces:!1,includeSpecialChars:!1},classes:{rootSet:"belongs-to-root-set",char:"char",word:"word",space:"char--space",special:"char--special",regular:"char--regular"},tags:{char:"span",word:"span"},dataAttributes:{rootSet:"rootSet",rootOrder:"rootOrder",setName:"setName",setOrder:"setOrder",setCharClass:"setCharClass",setWordClass:"setWordClass"},rootSet:{customSets:{},exposeEmptyAttributeSets:!1,autoDetectDataAttributes:!0},replaceSpaceWith:"\xA0",processing:{stripHTML:!0,trimWhitespace:!0,preserveStructure:!0,lazyWrap:!1,ordered:!1},performance:{useBatching:!0,cacheSelectors:!0},accessibility:{enabled:!0,ariaLabel:"auto",ariaHidden:!0,addTitle:!0},groups:{}},V={regular:/[\w\-+äüöÄÜÖßéèêëúùûüóòôöáàâäíìîïÉÈÊËÚÙÛÜÓÒÔÖÁÀÂÄÍÌÎÏçÇñÑ]/,special:/[<>?!:;,.$%€£¥\u2026\u00AB\u00BB|]/,space:/\s/,withSpaces:/[\w\-+\s]/,withSpecialChars:/[\w\-+<>?!:;,.$%€£¥\u2026\u00AB\u00BB|äüöÄÜÖßéèêëúùûüóòôöáàâäíìîïÉÈÊËÚÙÛÜÓÒÔÖÁÀÂÄÍÌÎÏçÇñÑ]/};function gt(a={}){let t=Wt(se,a);if(typeof t.wrap!="object")throw new Error("config.wrap must be an object");let e=["span","div","i","em","strong","mark"];if(!e.includes(t.tags.char))throw new Error(`config.tags.char must be one of: ${e.join(", ")}`);if(!e.includes(t.tags.word))throw new Error(`config.tags.word must be one of: ${e.join(", ")}`);return Object.entries(t.classes).forEach(([r,n])=>{if(typeof n!="string"||n.trim()==="")throw new Error(`config.classes.${r} must be a non-empty string`)}),t}function Wt(a,t){let e={...a};return ft(a)&&ft(t)&&Object.keys(t).forEach(r=>{let n=t[r];ft(n)?r in a?e[r]=Wt(a[r],n):Object.assign(e,{[r]:n}):Object.assign(e,{[r]:n})}),e}function ft(a){return a&&typeof a=="object"&&!Array.isArray(a)}function K(a){return String(a).padStart(3,"0")}function $t(a){let t=document.createElement("div");return t.innerHTML=a,t.textContent||t.innerText||""}function J(a,t=!0){let e=a.replace(/[\r\n\t]+/g," ").replace(/\s{2,}/g," ");return t?e.trim():e}function Mt(a="cw"){return`${a}-${Date.now()}-${Math.random().toString(36).substr(2,9)}`}function mt(a){let t=document.createDocumentFragment();return a.forEach(e=>t.appendChild(e)),t}function yt(a,t=document){let e=t.querySelector(a);if(!e)throw new Error(`Element not found: ${a}`);return e}function vt(a,t,e="asc"){return Array.from(a).sort((r,n)=>{let i=Number(r.dataset[t])||0,o=Number(n.dataset[t])||0;return e==="asc"?i-o:o-i})}function Dt(a){return`data-${a.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase()}`}function R(a,t){if(!(!a||!(a instanceof HTMLElement)))return a.dataset?.[t]}function jt(a,t,e){let r=a;for(;r;){if(r instanceof HTMLElement&&r.dataset?.[e]!==void 0)return r;if(r===t)break;r=r.parentElement}return null}function bt(...a){return a.filter(Boolean).join(" ")}var A={string:a=>typeof a=="string",number:a=>typeof a=="number"&&!isNaN(a),boolean:a=>typeof a=="boolean",function:a=>typeof a=="function",object:a=>a!==null&&typeof a=="object"&&!Array.isArray(a),array:a=>Array.isArray(a),element:a=>a instanceof Element,textNode:a=>a?.nodeType===Node.TEXT_NODE},f={error:(...a)=>console.error("[CharWrapper Error]",...a),warn:(...a)=>console.warn("[CharWrapper Warning]",...a),info:(...a)=>console.info("[CharWrapper]",...a),debug:(...a)=>{typeof DEBUG<"u"&&DEBUG&&console.log("[CharWrapper Debug]",...a)}};var l,O,j,Lt,et,wt,rt=class{constructor(t){y(this,j);y(this,l);y(this,O);d(this,l,t),d(this,O,{rootSet:0,char:0,word:0,attributeSets:{},attributeSetWords:{}})}resetCounters(){s(this,O).rootSet=0,s(this,O).char=0,s(this,O).word=0,s(this,O).attributeSets={},s(this,O).attributeSetWords={}}createCharElement(t,e={}){(typeof e!="object"||e===null)&&(e={});let{setName:r=null,setCharClass:n=null}=e,i=document.createElement(s(this,l).tags.char),o=[s(this,l).classes.char,s(this,l).classes.rootSet];if(s(this,l).enumerate.rootSet&&p(this,j,et).call(this,t,s(this,l).enumerate.rootSet)&&o.push(`${s(this,l).classes.rootSet}-${K(s(this,O).rootSet++)}`),s(this,l).enumerate.chars&&p(this,j,Lt).call(this,t)&&o.push(`${s(this,l).classes.char}-${K(s(this,O).char++)}`),n&&o.push(n),s(this,l).enumerate.attributeSets&&r&&n&&p(this,j,et).call(this,t,s(this,l).enumerate.attributeSets)){let g=s(this,O).attributeSets[r]??0;o.push(`${n}-${K(g)}`),s(this,O).attributeSets[r]=g+1}s(this,l).wrap.spaces&&V.space.test(t)&&o.push(s(this,l).classes.space),s(this,l).wrap.specialChars&&V.special.test(t)&&o.push(s(this,l).classes.special),V.regular.test(t)&&o.push(s(this,l).classes.regular),i.className=bt(...o);let u=t===" "&&s(this,l).replaceSpaceWith?s(this,l).replaceSpaceWith:t;return i.textContent=u,s(this,l).accessibility.enabled&&s(this,l).accessibility.ariaHidden&&i.setAttribute("aria-hidden","true"),i}createWordElement(t,e=[],r={}){(typeof r!="object"||r===null)&&(r={});let{setName:n=null,setWordClass:i=null}=r,o=document.createElement(s(this,l).tags.word),u=[s(this,l).classes.word];if(s(this,l).enumerate.words&&(!(t.trim()==="")||s(this,l).enumerate.includeSpaces)&&u.push(`${s(this,l).classes.word}-${K(s(this,O).word++)}`),i&&u.push(i),s(this,l).enumerate.attributeSets&&n&&i&&(!(t.trim()==="")||p(this,j,wt).call(this,s(this,l).enumerate.attributeSets).includeSpaces)){let g=s(this,O).attributeSetWords[n]??0;u.push(`${i}-${K(g)}`),s(this,O).attributeSetWords[n]=g+1}return o.className=bt(...u),e.forEach(c=>o.appendChild(c)),s(this,l).accessibility.enabled&&s(this,l).accessibility.ariaHidden&&o.setAttribute("aria-hidden","true"),o}createSpaceElement(){let t=document.createElement(s(this,l).tags.word);return t.className=s(this,l).classes.space,t.textContent=s(this,l).replaceSpaceWith,s(this,l).accessibility.enabled&&s(this,l).accessibility.ariaHidden&&t.setAttribute("aria-hidden","true"),t}wrapChars(t,e={}){return t?t.split("").map(n=>this.createCharElement(n,e)):[]}wrapWords(t,e={}){if(!t)return{words:[],chars:[]};let r=t.split(" "),n=[],i=[];return r.forEach((o,u)=>{let c=this.wrapChars(o,e);i.push(...c);let g=this.createWordElement(o,c,e);if(n.push(g),u<r.length-1){let h=this.createSpaceElement();n.push(h)}}),{words:n,chars:i}}getCounters(){return{...s(this,O)}}};l=new WeakMap,O=new WeakMap,j=new WeakSet,Lt=function(t){return p(this,j,et).call(this,t,{includeSpaces:s(this,l).enumerate.includeSpaces,includeSpecialChars:s(this,l).enumerate.includeSpecialChars})},et=function(t,e){if(!e)return!1;let r=p(this,j,wt).call(this,e),n=V.space.test(t),i=V.special.test(t);return r.includeSpaces&&r.includeSpecialChars?!0:!(n&&!r.includeSpaces||i&&!r.includeSpecialChars)},wt=function(t){return typeof t=="object"?t:{includeSpaces:s(this,l).enumerate.includeSpaces,includeSpecialChars:s(this,l).enumerate.includeSpecialChars}};var C,H,$,It,Pt,Rt,Ht,kt,Bt,st=class{constructor(t){y(this,$);y(this,C);y(this,H);d(this,C,t),d(this,H,new WeakMap)}extractText(t){if(!A.element(t))throw new TypeError("extractText requires a valid DOM element");if(s(this,C).performance.cacheSelectors&&s(this,H).has(t))return s(this,H).get(t);let e=t.textContent||"";return s(this,C).processing.stripHTML&&(e=$t(e)),s(this,C).processing.trimWhitespace&&(e=J(e,!0)),s(this,C).performance.cacheSelectors&&s(this,H).set(t,e),e}findTextNodes(t,e=[]){if(!t||!("childNodes"in t))return e;for(let r of Array.from(t.childNodes))A.textNode(r)?J(r.textContent,!0).length>0&&e.push(r):r.nodeType===Node.ELEMENT_NODE&&R(r,s(this,C).dataAttributes.setName)!=="_exclude_"&&this.findTextNodes(r,e);return e}replaceContent(t,e){if(!A.element(t))throw new TypeError("replaceContent requires a valid DOM element");if(!A.array(e))throw new TypeError("wrappedElements must be an array");if(t.textContent="",s(this,C).performance.useBatching){let r=mt(e);t.appendChild(r)}else e.forEach(r=>t.appendChild(r))}processTextNode(t,e,r={}){if(!A.textNode(t))return f.warn("processTextNode called with non-text node",t),null;let n=t.parentElement,i=!1;if(n){let S=Array.from(n.childNodes),b=S.indexOf(t);if(b!==-1){let W=S[b-1],T=S[b+1];(W&&W.nodeType===Node.ELEMENT_NODE||T&&T.nodeType===Node.ELEMENT_NODE)&&(i=!0)}}let o=s(this,C).processing.trimWhitespace&&!i,u=J(t.textContent,o);if(!u)return null;let c=s(this,C).wrap.words,g=s(this,C).wrap.chars,h={words:[],chars:[]};if(c)if(g)h=e.wrapWords(u,r);else{let S=u.split(" ");h.words=S.map((b,W)=>{let T=e.createWordElement(b,[],r);if(T.textContent=b,W<S.length-1){let P=e.createSpaceElement();return[T,P]}return T}).flat()}else if(g)h.chars=e.wrapChars(u,r);else return f.warn("No wrapping mode enabled (neither chars nor words)"),null;return h}processElement(t,e,r={}){let n=p(this,$,It).call(this,t),i=r.rootSetName||R(t,s(this,C).dataAttributes.rootSet)||"",o={name:i,element:t,chars:[],words:[],groups:{},attributeSets:{},customSets:{}};if(n.length===0)return f.warn("No text nodes found in element",t),{words:[],chars:[],rootSet:o,rootText:"",attributeSetTexts:{}};let u=p(this,$,Pt).call(this,n),c={},g=[];return u.forEach(h=>{let S={...r,rootSetName:i,setName:h.setName,setCharClass:h.setCharClass,setWordClass:h.setWordClass},b=this.processTextNode(h.textNode,e,S);if(!b)return;let W=p(this,$,kt).call(this,h.textNode);if(W&&(g.push(W),h.setName&&(c[h.setName]=`${c[h.setName]||""}${W}`)),b.words.length>0&&o.words.push(...b.words),b.chars.length>0&&o.chars.push(...b.chars),h.setName){let T=p(this,$,Rt).call(this,o,h);b.words.length>0&&T.words.push(...b.words),b.chars.length>0&&T.chars.push(...b.chars)}p(this,$,Ht).call(this,h,b)}),{words:o.words,chars:o.chars,rootSet:o,rootText:g.join(""),attributeSetTexts:c}}clearCache(){d(this,H,new WeakMap)}};C=new WeakMap,H=new WeakMap,$=new WeakSet,It=function(t){let e=[],r=0,n=i=>{if(!(!i||!("childNodes"in i))){for(let o of Array.from(i.childNodes))if(A.textNode(o)){if(J(o.textContent,!0).length>0&&o.parentElement){let c=jt(o.parentElement,t,s(this,C).dataAttributes.setName),g=R(c,s(this,C).dataAttributes.setName);g!=="_exclude_"&&e.push({textNode:o,parentElement:o.parentElement,setElement:c,setName:g,setOrder:p(this,$,Bt).call(this,R(c,s(this,C).dataAttributes.setOrder)),setCharClass:R(c,s(this,C).dataAttributes.setCharClass),setWordClass:R(c,s(this,C).dataAttributes.setWordClass),domIndex:r++})}}else if(o.nodeType===Node.ELEMENT_NODE){let u=o;R(u,s(this,C).dataAttributes.setName)!=="_exclude_"&&n(u)}}};return n(t),e},Pt=function(t){return s(this,C).processing.ordered?[...t].sort((e,r)=>{let n=typeof e.setOrder=="number",i=typeof r.setOrder=="number";return n&&i&&e.setOrder!==r.setOrder?e.setOrder-r.setOrder:n!==i?n?-1:1:e.domIndex-r.domIndex}):t},Rt=function(t,e){let r=e.setName;return t.attributeSets[r]||(t.attributeSets[r]={name:r,element:e.setElement||e.parentElement,chars:[],words:[],groups:{}}),t.attributeSets[r]},Ht=function(t,e){let r=t.textNode.parentElement;if(!r)return;let n=e.words.length>0?e.words:e.chars;if(s(this,C).performance.useBatching){let i=mt(n);r.replaceChild(i,t.textNode)}else n.forEach(i=>{r.insertBefore(i,t.textNode)}),r.removeChild(t.textNode)},kt=function(t){let e=t.parentElement,r=!1;if(e){let i=Array.from(e.childNodes),o=i.indexOf(t),u=i[o-1],c=i[o+1];r=!!(u&&u.nodeType===Node.ELEMENT_NODE||c&&c.nodeType===Node.ELEMENT_NODE)}let n=s(this,C).processing.trimWhitespace&&!r;return J(t.textContent,n)},Bt=function(t){if(t===void 0||t==="")return;let e=Number(t);return Number.isFinite(e)?e:void 0};var at=class{select(t,e){throw new Error("select() must be implemented by subclass")}},nt=class extends at{select(t){if(A.element(t))return t;if(A.string(t))return yt(t);throw new TypeError("Target must be a CSS selector string or DOM element")}},q,St=class extends at{constructor(e){super();y(this,q);d(this,q,e)}select(e,r={}){let{ordered:n=s(this,q).processing.ordered}=r,i=A.element(e)?e:yt(e),o=s(this,q).dataAttributes.setName,u=`[${Dt(o)}]`,c=Array.from(i.querySelectorAll(u));if(c.length===0)return f.warn(`No elements found with attribute: ${u}`),[];let g=c.filter(h=>h.dataset[o]!=="_exclude_");if(n){let h=s(this,q).dataAttributes.setOrder;return vt(g,h,"asc")}return g}};q=new WeakMap;var Ct=class{static create(t,e){return A.element(t)?new nt:A.string(t)&&/^[.#[]/.test(t)?new nt:(f.info("Using data attribute strategy"),new St(e))}},Q,Y,ot=class{constructor(t){y(this,Q);y(this,Y);d(this,Q,t)}select(t,e={}){return d(this,Y,Ct.create(t,s(this,Q))),s(this,Y).select(t,e)}isValid(t){if(A.element(t))return!0;if(A.string(t)&&t.trim()!=="")try{return document.querySelector(t),!0}catch{return!1}return!1}};Q=new WeakMap,Y=new WeakMap;var F,M,_t,qt,Ft,Et,Ut,it=class{constructor(t){y(this,M);y(this,F);d(this,F,t)}groupCharacters(t,e){let r={};if(!s(this,F)||Object.keys(s(this,F)).length===0)return r;let n=p(this,M,qt).call(this,e);for(let[i,o]of Object.entries(s(this,F)))r[i]=p(this,M,_t).call(this,t,e,o,n);return r}static validateGroupsConfig(t){for(let[e,r]of Object.entries(t))if(!(r instanceof RegExp)&&typeof r!="object"&&f.warn(`Invalid group definition for "${e}". Must be RegExp or GroupConfig object.`),typeof r=="object"&&!(r instanceof RegExp)){let n=r;n.nth&&(n.nth<1||!Number.isInteger(n.nth))&&f.warn(`Invalid nth value for group "${e}". Must be a positive integer.`),n.indices&&!Array.isArray(n.indices)&&f.warn(`Invalid indices for group "${e}". Must be an array of numbers.`),n.words&&!Array.isArray(n.words)&&f.warn(`Invalid words for group "${e}". Must be an array of strings.`)}}};F=new WeakMap,M=new WeakSet,_t=function(t,e,r,n){let i=[];if(r instanceof RegExp)return t.filter((u,c)=>{let g=e[c]||u.textContent||"";return r.test(g)});let o=r;return t.forEach((u,c)=>{let g=e[c]||u.textContent||"",h=p(this,M,Ft).call(this,g,c,n,e),S=!1;if(o.pattern&&o.pattern.test(g)&&(S=!0),o.nth&&(c+1)%o.nth===0&&(S=!0),o.indices&&o.indices.includes(c)&&(S=!0),o.words){let b=p(this,M,Ut).call(this,c,e,n);o.words.includes(b)&&(S=!0)}o.custom&&o.custom(g,c,h)&&(S=!0),S&&(o.class&&u.classList.add(o.class),i.push(u))}),i},qt=function(t){let e=[0];for(let r=0;r<t.length;r++)t[r]===" "&&r+1<t.length&&e.push(r+1);return e},Ft=function(t,e,r,n){let i=p(this,M,Et).call(this,e,r),o=r[i],u=r[i+1]?r[i+1]-1:n.length-1;return{char:t,index:e,isFirstInWord:e===o,isLastInWord:e===u||n[e+1]===" ",wordIndex:i}},Et=function(t,e){for(let r=e.length-1;r>=0;r--)if(t>=e[r])return r;return 0},Ut=function(t,e,r){let n=p(this,M,Et).call(this,t,r),i=r[n],o=r[n+1]||e.length;return e.substring(i,o).trim()};var w=new Map,v={duration:.8,stagger:.03,delay:0,ease:"power2.out"};w.set("fadeInStagger",(a,t)=>{let e={...v,...t};return gsap.from(a,{opacity:0,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("slideInUp",(a,t)=>{let e={...v,...t};return gsap.from(a,{opacity:0,y:50,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("slideInDown",(a,t)=>{let e={...v,...t};return gsap.from(a,{opacity:0,y:-50,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("slideInLeft",(a,t)=>{let e={...v,...t};return gsap.from(a,{opacity:0,x:-50,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("slideInRight",(a,t)=>{let e={...v,...t};return gsap.from(a,{opacity:0,x:50,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("scaleIn",(a,t)=>{let e={...v,ease:"back.out(1.7)",...t};return gsap.from(a,{opacity:0,scale:0,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("rotateIn",(a,t)=>{let e={...v,ease:"back.out(2)",...t};return gsap.from(a,{opacity:0,rotation:-180,scale:0,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("elasticBounce",(a,t)=>{let e={...v,ease:"elastic.out(1, 0.5)",duration:1.2,...t};return gsap.from(a,{opacity:0,y:-100,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("typewriter",(a,t)=>{let e={stagger:.05,duration:0,...t};return gsap.set(a,{opacity:0}),gsap.to(a,{opacity:1,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:"none"})});w.set("wave",(a,t)=>{let e={...v,stagger:.02,...t},r=t.amplitude||30;return gsap.from(a,{opacity:0,y:n=>Math.sin(n*.5)*r,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("glitch",(a,t)=>{let e={duration:.05,stagger:.01,...t},r=gsap.timeline();return a.forEach((n,i)=>{r.to(n,{x:()=>Math.random()*10-5,y:()=>Math.random()*10-5,duration:e.duration,repeat:5,yoyo:!0,ease:"none"},i*(e.stagger||.01))}),r});w.set("floatingWave",(a,t)=>{let e={duration:2,stagger:.05,...t},r=t.amplitude||10;return gsap.to(a,{y:n=>Math.sin(n*.5)*r,duration:e.duration,stagger:e.stagger,repeat:-1,yoyo:!0,ease:"sine.inOut"})});w.set("pulse",(a,t)=>{let e={duration:1.5,stagger:.02,...t};return gsap.to(a,{scale:1.1,duration:e.duration,stagger:e.stagger,repeat:-1,yoyo:!0,ease:"sine.inOut"})});w.set("colorCycle",(a,t)=>{let e={duration:2,stagger:.1,...t},r=t.colors||["#ff6b9d","#4ecdc4","#ffd93d","#95e1d3"],n=gsap.timeline({repeat:-1});return r.forEach(i=>{n.to(a,{color:i,duration:e.duration,stagger:e.stagger,ease:"none"})}),n});w.set("shimmer",(a,t)=>{let e={duration:.3,stagger:.03,...t};return gsap.to(a,{opacity:.3,duration:e.duration,stagger:e.stagger,repeat:-1,yoyo:!0,ease:"sine.inOut"})});w.set("fadeOut",(a,t)=>{let e={...v,...t};return gsap.to(a,{opacity:0,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("slideOutDown",(a,t)=>{let e={...v,...t};return gsap.to(a,{opacity:0,y:50,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("scaleOut",(a,t)=>{let e={...v,ease:"back.in(1.7)",...t};return gsap.to(a,{opacity:0,scale:0,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:e.ease})});w.set("explode",(a,t)=>{let e={...v,duration:1.2,...t};return gsap.to(a,{opacity:0,x:()=>(Math.random()-.5)*200,y:()=>(Math.random()-.5)*200,rotation:()=>(Math.random()-.5)*720,scale:0,duration:e.duration,stagger:e.stagger,delay:e.delay,ease:"power2.in"})});w.set("hoverBounce",(a,t)=>{let e={duration:.3,...t};return a.forEach(r=>{r.addEventListener("mouseenter",()=>{gsap.to(r,{y:-20,duration:e.duration,ease:"power2.out"}),gsap.to(r,{y:0,duration:.5,delay:e.duration,ease:"bounce.out"})})}),gsap.timeline()});w.set("clickSpin",(a,t)=>{let e={duration:.6,...t};return a.forEach(r=>{r.addEventListener("click",()=>{gsap.to(r,{rotation:"+=360",duration:e.duration,ease:"back.out(1.7)"})})}),gsap.timeline()});function zt(a,t){w.has(a)&&f.warn(`Preset "${a}" already exists. Overwriting.`),w.set(a,t),f.info(`Preset "${a}" registered successfully.`)}function ae(){return Array.from(w.keys())}function Gt(a,t,e={}){let r=w.get(a);return r?!t||t.length===0?(f.warn(`No elements provided for preset "${a}"`),null):(f.info(`Executing preset "${a}" on ${t.length} elements`),r(t,e)):(f.error(`Preset "${a}" not found. Available presets: ${ae().join(", ")}`),null)}var Vt={strategy:"smart",addDuration:.4,removeDuration:.4,stagger:.02,ease:"power2.out"},ct=class{static transition(t,e,r,n,i,o={}){let u={...Vt,...o};f.info(`Transitioning from "${e}" to "${r}" using ${u.strategy} strategy`),n.length>0&&gsap.killTweensOf(n);let c=gsap.timeline({onComplete:o.onComplete});return u.strategy==="sequential"?this.sequentialTransition(t,n,r,i,u,c):this.smartTransition(t,e,r,n,i,u,c)}static sequentialTransition(t,e,r,n,i,o){let u=[];return r.split("").forEach((c,g)=>{let h=n.createCharElement(c,g);gsap.set(h,{opacity:0,scale:0}),u.push(h)}),e.length>0&&o.to(e,{opacity:0,scale:0,duration:i.removeDuration,stagger:i.stagger,ease:i.ease}),o.call(()=>{t.innerHTML="",u.forEach(c=>t.appendChild(c))},void 0,i.removeDuration),o.to(u,{opacity:1,scale:1,duration:i.addDuration,stagger:i.stagger,ease:i.ease,clearProps:"transform"},`+=${i.removeDuration*.05}`),o}static smartTransition(t,e,r,n,i,o,u){let c=e.split(""),g=r.split(""),h=new Map;n.forEach((T,P)=>{let _=c[P];h.has(_)||h.set(_,[]),h.get(_).push(T)});let S=[],b=[],W=[];return g.forEach((T,P)=>{let _=h.get(T),I;_&&_.length>0?(I=_.shift(),gsap.set(I,{opacity:1,scale:1,x:0,y:0,rotation:0}),Array.from(I.classList).filter(ht=>/char-\d+/.test(ht)).forEach(ht=>I.classList.remove(ht)),I.classList.add(`char-${String(P+1).padStart(3,"0")}`)):(I=i.createCharElement(T,P),gsap.set(I,{opacity:0,scale:0}),b.push(I)),W.push(I)}),h.forEach(T=>{T.forEach(P=>S.push(P))}),S.length>0&&u.to(S,{opacity:0,scale:0,duration:o.removeDuration,stagger:o.stagger,ease:o.ease},0),u.call(()=>{t.innerHTML="",W.forEach(T=>t.appendChild(T))},void 0,o.removeDuration*.3),b.length>0&&u.to(b,{opacity:1,scale:1,duration:o.addDuration,stagger:o.stagger,ease:o.ease,clearProps:"transform"},o.removeDuration*.5),u}static replace(t,e,r,n,i={}){let o={...Vt,...i},u=gsap.timeline({onComplete:i.onComplete});return this.sequentialTransition(t,e,r,n,o,u)}};var x,U,z,k,B,E,tt,X,m,D,L,N,Kt,ut,xt,Nt,Jt,Tt,G,Zt,At,Z=class Z{constructor(t,e={}){y(this,N);y(this,x);y(this,U);y(this,z);y(this,k);y(this,B);y(this,E);y(this,tt);y(this,X);y(this,m);y(this,D);y(this,L);if(d(this,x,gt(e)),d(this,L,Mt("charwrapper")),d(this,U,new ot(s(this,x))),!s(this,U).isValid(t))throw new Error(`Invalid target: ${t}`);let r=s(this,U).select(t);d(this,E,Array.isArray(r)?r[0]:r),d(this,tt,s(this,E).innerHTML),d(this,X,s(this,E).textContent||""),d(this,z,new st(s(this,x))),d(this,k,new rt(s(this,x))),d(this,B,new it(s(this,x).groups)),d(this,m,null),d(this,D,!1),f.info(`CharWrapper instance created: ${s(this,L)}`,{target:t,config:s(this,x)})}wrap(t={}){if(s(this,D))return f.warn("Element already wrapped. Call unwrap() first or use rewrap()"),s(this,m);try{s(this,x).accessibility.enabled&&p(this,N,Kt).call(this),s(this,k).resetCounters();let e=s(this,z).processElement(s(this,E),s(this,k),{...t,rootSetName:t.rootSetName||p(this,N,ut).call(this)});return e.rootSet.groups=s(this,B).groupCharacters(e.rootSet.chars,e.rootText||p(this,N,xt).call(this,e.rootSet.chars)),Object.entries(e.rootSet.attributeSets).forEach(([r,n])=>{n.groups=s(this,B).groupCharacters(n.chars,e.attributeSetTexts[r]||p(this,N,xt).call(this,n.chars))}),e.rootSet.customSets=p(this,N,Nt).call(this,e.rootSet),d(this,m,e.rootSet),d(this,D,!0),f.info(`Text wrapped successfully: ${s(this,L)}`,{charCount:e.rootSet.chars.length,wordCount:e.rootSet.words.length,groupCount:Object.keys(e.rootSet.groups).length,attributeSetCount:Object.keys(e.rootSet.attributeSets).length,customSetCount:Object.keys(e.rootSet.customSets).length}),s(this,m)}catch(e){throw f.error("Failed to wrap text:",e),e}}unwrap(){if(!s(this,D)){f.warn("Element is not wrapped");return}try{s(this,E).innerHTML=s(this,tt),d(this,m,null),d(this,D,!1),f.info(`Text unwrapped: ${s(this,L)}`)}catch(t){throw f.error("Failed to unwrap text:",t),t}}rewrap(t={}){return this.unwrap(),this.wrap(t)}destroy(){s(this,D)&&this.unwrap(),s(this,z)?.clearCache(),d(this,m,null),d(this,E,null),d(this,z,null),d(this,k,null),d(this,B,null),d(this,U,null),f.info(`CharWrapper instance destroyed: ${s(this,L)}`)}getChars(){return[...s(this,m)?.chars||[]]}getWords(){return[...s(this,m)?.words||[]]}getChar(t){return s(this,m)?.chars[t]}getWord(t){return s(this,m)?.words[t]}isWrapped(){return s(this,D)}getRootElement(){return s(this,E)}getConfig(){return JSON.parse(JSON.stringify(s(this,x)))}getMetadata(){return{id:s(this,L),isWrapped:s(this,D),charCount:s(this,m)?.chars.length||0,wordCount:s(this,m)?.words.length||0,rootSetName:s(this,m)?.name||p(this,N,ut).call(this),attributeSetCount:Object.keys(s(this,m)?.attributeSets||{}).length,customSetCount:Object.keys(s(this,m)?.customSets||{}).length,rootElement:s(this,E)?.tagName}}filterCharsByClass(t){return(s(this,m)?.chars||[]).filter(e=>e.classList.contains(t))}getRootSet(){return s(this,m)}getAttributeSet(t){return s(this,m)?.attributeSets[t]}getAttributeSetChars(t){return[...this.getAttributeSet(t)?.chars||[]]}getAttributeSetWords(t){return[...this.getAttributeSet(t)?.words||[]]}getCustomSet(t){return[...s(this,m)?.customSets[t]||[]]}hasAttributeSet(t){return!!s(this,m)?.attributeSets[t]}getAttributeSetNames(){return Object.keys(s(this,m)?.attributeSets||{})}getCharsByType(t){let r={regular:s(this,x).classes.regular,space:s(this,x).classes.space,special:s(this,x).classes.special}[t];return r?this.filterCharsByClass(r):(f.warn(`Unknown type: ${t}. Use 'regular', 'space', or 'special'`),[])}animate(t,e={}){if(!s(this,D))return f.error("Cannot animate: element not wrapped. Call wrap() first."),null;let r=s(this,m).chars;return e.groups&&s(this,m).groups[e.groups]&&(r=s(this,m).groups[e.groups],f.info(`Animating group "${e.groups}" with preset "${t}"`)),Gt(t,r,e)}transitionTo(t,e={}){return s(this,D)?s(this,E)?ct.transition(s(this,E),s(this,X),t,s(this,m).chars,s(this,k),{...e,onComplete:()=>{d(this,X,t);let n=Array.from(s(this,E).querySelectorAll(`.${s(this,x).classes.char}`)),i=Array.from(s(this,E).querySelectorAll(`.${s(this,x).classes.word}`));d(this,m,{name:p(this,N,ut).call(this),element:s(this,E),chars:n,words:i,groups:{},attributeSets:{},customSets:{}}),Object.keys(s(this,x).groups).length>0&&(s(this,m).groups=s(this,B).groupCharacters(n,t)),s(this,m).customSets=p(this,N,Nt).call(this,s(this,m)),e.onComplete&&e.onComplete(),f.info(`Transition complete. New text: "${t}"`)}}):(f.error("Cannot transition: root element not found."),null):(f.error("Cannot transition: element not wrapped. Call wrap() first."),null)}static create(t,e={}){let r=new Z(t,e);return r.wrap(),r}static wrapMultiple(t,e={}){if(!A.array(t))throw new TypeError("targets must be an array");return t.map(r=>Z.create(r,e))}static wrapAll(t,e={}){let r=p(this,G,Zt).call(this,t),n=gt(e),i=n.dataAttributes.rootOrder;return(n.processing.ordered?[...r].sort((c,g)=>{let h=p(this,G,At).call(this,c.dataset?.[i]),S=p(this,G,At).call(this,g.dataset?.[i]),b=typeof h=="number",W=typeof S=="number";return b&&W&&h!==S?h-S:b!==W?b?-1:1:r.indexOf(c)-r.indexOf(g)}):r).map(c=>new Z(c,e).wrap())}static getRootSet(t,e){return t.find(r=>r.name===e)}static registerPreset(t,e){zt(t,e)}};x=new WeakMap,U=new WeakMap,z=new WeakMap,k=new WeakMap,B=new WeakMap,E=new WeakMap,tt=new WeakMap,X=new WeakMap,m=new WeakMap,D=new WeakMap,L=new WeakMap,N=new WeakSet,Kt=function(){let{ariaLabel:t,addTitle:e}=s(this,x).accessibility,n=(s(this,E).textContent?.trim()||"").replace(/[\r\n]+/g," ").replace(/\s{2,}/g," ").trim();if(t==="auto"&&n)s(this,E).setAttribute("aria-label",n);else if(t&&t!=="auto"&&t!=="none"){let i=t.replace(/[\r\n]+/g," ").replace(/\s{2,}/g," ").trim();s(this,E).setAttribute("aria-label",i)}e&&!s(this,E).hasAttribute("title")&&n&&s(this,E).setAttribute("title",n)},ut=function(){return(s(this,E)instanceof HTMLElement?s(this,E).dataset?.[s(this,x).dataAttributes.rootSet]:"")||s(this,L)},xt=function(t){return t.map(e=>e.textContent||"").join("")},Nt=function(t){let e={};return Object.entries(s(this,x).rootSet.customSets||{}).forEach(([r,n])=>{e[r]=p(this,N,Jt).call(this,t,n)}),e},Jt=function(t,e){return typeof e=="function"?[...e(t)]:e==="root"||e==="rootChars"?[...t.chars]:e==="rootWords"?[...t.words]:typeof e=="string"?[...t.attributeSets[e]?.chars||[]]:Array.isArray(e)?e.flatMap(r=>typeof r=="string"?t.attributeSets[r]?.chars||[]:p(this,N,Tt).call(this,t,r)):p(this,N,Tt).call(this,t,e)},Tt=function(t,e){let r=t.attributeSets[e.attributeSet];return r?e.target==="chars"?[...r.chars]:e.target==="words"?[...r.words]:[...r.chars,...r.words]:[]},G=new WeakSet,Zt=function(t){return Array.from(typeof t=="string"?document.querySelectorAll(t):t)},At=function(t){if(t===void 0||t==="")return;let e=Number(t);return Number.isFinite(e)?e:void 0},y(Z,G);var lt=Z,ne=lt;return re(oe);})();
|
|
/**
|
|
* CharWrapper - Modern ES6+ Text Character Wrapper
|
|
*
|
|
* Wraps text characters and words in HTML elements for animation purposes.
|
|
* Designed to work seamlessly with GSAP and other animation libraries.
|
|
*
|
|
* @version 2.0.2
|
|
* @author Robert Wildling
|
|
* @license MIT
|
|
*
|
|
* @example
|
|
* const wrapper = new CharWrapper('.my-text', {
|
|
* wrap: { chars: true, words: true },
|
|
* enumerate: { chars: true }
|
|
* });
|
|
*
|
|
* const { chars, words } = wrapper.wrap();
|
|
*
|
|
* // Animate with GSAP
|
|
* gsap.from(chars, { opacity: 0, stagger: 0.05 });
|
|
*
|
|
* // Clean up when done
|
|
* wrapper.destroy();
|
|
*/
|
|
if(typeof window!=='undefined'){window.CharWrapper=CharWrapperModule.default||CharWrapperModule.CharWrapper;}
|
|
//# sourceMappingURL=charwrapper.min.js.map
|