@xnocss/core
Version:
short class online parse
6 lines (5 loc) • 9.42 kB
JavaScript
(function(u,c){typeof exports=="object"&&typeof module!="undefined"?module.exports=c():typeof define=="function"&&define.amd?define(c):(u=typeof globalThis!="undefined"?globalThis:u||self,u.XClass=c())})(this,function(){var c,d,x,y,R,C,m,S,p,A;"use strict";var k=Object.defineProperty;var M=(u,c,d)=>c in u?k(u,c,{enumerable:!0,configurable:!0,writable:!0,value:d}):u[c]=d;var o=(u,c,d)=>(M(u,typeof c!="symbol"?c+"":c,d),d),L=(u,c,d)=>{if(!c.has(u))throw TypeError("Cannot "+d)};var a=(u,c,d)=>(L(u,c,"read from private field"),d?d.call(u):c.get(u)),v=(u,c,d)=>{if(c.has(u))throw TypeError("Cannot add the same private member more than once");c instanceof WeakSet?c.add(u):c.set(u,d)},E=(u,c,d,x)=>(L(u,c,"write to private field"),x?x.call(u,d):c.set(u,d),d);var D=(u,c,d)=>(L(u,c,"access private method"),d);const w=class{constructor(e={}){v(this,p);o(this,"title","XCLASS");o(this,"version","1.0.0");o(this,"isClearCache",!0);o(this,"cacheExpire",-1);o(this,"pseudoClassDefine",{});o(this,"responsiveDefine",{});o(this,"shortDefine",{});o(this,"rules",[]);o(this,"themes",{});o(this,"initialRenderNum",1e3);o(this,"debug",!1);v(this,c,new Map);v(this,d,new Map);v(this,x,new Map);v(this,y,null);v(this,R,new Map);v(this,C,null);v(this,m,{});v(this,S,new Map);o(this,"intersectionCallback",e=>{e.forEach(t=>{let i=t.target;t.isIntersecting&&(this.initNode(i),a(this,y).unobserve(i))})});var t,i,h,n,r,s,l,f,g,b;this.title=(t=e==null?void 0:e.title)!=null?t:"XCLASS",this.version=(i=e==null?void 0:e.version)!=null?i:"1.0.0",this.isClearCache=(h=e==null?void 0:e.isClearCache)!=null?h:!0,this.cacheExpire=(n=e==null?void 0:e.cacheExpire)!=null?n:-1,this.pseudoClassDefine=(r=e==null?void 0:e.pseudoClassDefine)!=null?r:{},this.responsiveDefine=(s=e==null?void 0:e.responsiveDefine)!=null?s:{},this.shortDefine=(l=e==null?void 0:e.shortDefine)!=null?l:{},this.themes=(f=e==null?void 0:e.themes)!=null?f:{},this.rules=(g=e==null?void 0:e.rules)!=null?g:[],this.initialRenderNum=(e==null?void 0:e.initialRenderNum)||1e3,this.debug=(b=e==null?void 0:e.debug)!=null?b:!1,typeof window=="object"&&this.init()}init(){let e=this.title+"_"+this.version;this.isClearCache?w.removeStorages(new RegExp(`${this.title}_.*`)):w.removeStorages(new RegExp(`${this.title}_.*`),[e]);const t=function(r){const s=history[r],l=new Event(r);return function(){const f=s.apply(this,arguments);return l.arguments=arguments,window.dispatchEvent(l),f}};history.pushState=t("pushState"),history.replaceState=t("replaceState"),window.addEventListener("hashchange",()=>{a(this,S).clear()}),window.addEventListener("popstate",()=>{a(this,S).clear()}),window.addEventListener("pushState",()=>{a(this,S).clear()}),window.addEventListener("replaceState",()=>{a(this,S).clear()});let i=w.getStorage(e)||{};if(E(this,m,new Proxy(i,{get(r,s){return r[s]},set(r,s,l){return r[s]=l,w.setStorage(e,i,this.cacheExpire),!0}})),document.querySelectorAll(`style[title=${this.title}]`).length==0){let r=document.createElement("style");r.type="text/css",r.title=this.title,document.querySelector("head").appendChild(r)}let n=document.styleSheets;for(let r=n.length-1;r>=0;r--){let s=n.item(r);s.title==this.title&&E(this,C,s)}E(this,y,new IntersectionObserver(this.intersectionCallback,{rootMargin:"500px 0px"}))}bind(e,t){var i;if(a(this,S).set(e,t),e.getAttribute("guid")){this.initNode(e);return}a(this,S).size>this.initialRenderNum?(i=t==null?void 0:t.modifiers)!=null&&i.real||w.isInViewPort(e)?this.initNode(e):a(this,y).observe(e):this.initNode(e)}unbind(e){let t=e.getAttribute("uid");a(this,x).get(t)&&(a(this,x).get(t).disconnect(),a(this,x).delete(t))}initNode(e){let t=e.getAttribute("guid");if(t&&console.log("插件生成",t),!t){t=`${this.title}-${w.guid()}`;let n=document.createAttribute("uid");n.nodeValue=t,e.attributes.setNamedItem(n),this.handleDebug(a(this,S).get(e),t);let r=new Date().getTime();this.parseAndCreate(e),this.debug&&console.log("生成时间",t,new Date().getTime()-r)}var i={childList:!1,attributes:!0,subtree:!1};let h=new MutationObserver(()=>{let n=new Date().getTime();this.parseAndCreate(e),this.debug&&console.log("生成时间-",t,new Date().getTime()-n)});h.observe(e,i),a(this,x).set(t,h)}parseAndCreate(e){let t=this.parseStyle(e);this.createStyles(t,e)}parseStyle(e){let t=e.attributes,i=[];e.classList.forEach(s=>{i.push(s)});for(let s=0;s<t.length;s++)i.push(t[s].nodeName);let n=e.getAttribute("uid"),r=a(this,R).get(n);return r&&r.length==i.length&&r.filter(s=>!i.includes(s)).length==0?{}:(a(this,R).set(n,i.map(s=>s)),this.parseStyleNode(i,n))}createStyles(e,t){this.createStylesNode(e,t.getAttribute("uid"),t.tagName.toLocaleLowerCase()).forEach(h=>{h.forEach(n=>{this.insertStyle(n.selector,n.styleText,n.newStyleText)})}),this.debugConsole(t)}insertStyle(e,t,i){let h=a(this,C);if(h.title==this.title){let n=h.cssRules,r=!0;for(let s=0;s<n.length;s++){let l=n.item(s);if(!i&&l.type==1){if(l.selectorText==e&&l.cssText!=t){w.deleteRule(h,s),w.insertRule(h,e,t,s),r=!1;break}}else if(i&&l.type==4){for(let f=0;f<l.cssRules.length;f++){let g=l.cssRules.item(f);if(g.selectorText==e&&g.cssText!=t){w.deleteRule(h,s),w.insertRule(h,e,i,s),r=!1;break}}if(!r)break}}r&&w.insertRule(h,e,i||t,0)}}parseStyleNode(e,t){let i=[];e.forEach((n,r)=>{if(Object.keys(this.shortDefine).includes(n)){e.splice(r,1,"");let s=this.shortDefine[n]||[];typeof s=="string"&&(s=s.split(" ")),i.push(...s)}}),e=e.filter(n=>n),e.push(...i);let h={};return Object.keys(this.responsiveDefine).length>0?(Object.keys(this.responsiveDefine).forEach(n=>{h[this.responsiveDefine[n]]||(h[this.responsiveDefine[n]]=[]),e=e.filter(r=>r),h[this.responsiveDefine[n]].push(D(this,p,A).call(this,e.filter((r,s)=>{let l=r.startsWith(n);return l&&e.splice(s,1,""),l}).map(r=>r.replace(n,"")),t))}),e=e.filter(n=>n),h[""]=[D(this,p,A).call(this,e,t)]):h[""]=[D(this,p,A).call(this,e,t)],h}createStyleNode(e,t,i,h=""){var s;let n=`${i.toLocaleLowerCase()}[uid="${t}"]${h}`,r=((s=e==null?void 0:e.length)!=null?s:0)>0?`
${n}{
${e.join("")}
}
`:"";return r?{selector:n,styleText:r}:void 0}createStylesNode(e,t,i){return e?Object.keys(e).map(h=>{let r=e[h].reduce((s,l)=>(Object.keys(l).forEach(f=>{s[f]||(s[f]=[]),s[f].push(...l[f])}),s),{});return Object.keys(r).map(s=>{try{let l=this.createStyleNode(r[s],t,i,s||"");if(l)return h&&(l.newStyleText=h+`{${l.styleText}}`),l}catch(l){console.error(l)}}).filter(s=>s)}).filter(h=>h.length>0):[]}handleDebug(e,t){if(this.debug)if(e.arg=="test"){let i=e.value||[];typeof i=="string"&&(i=i.split(" ")),a(this,c).set(t,i||[])}else a(this,c).delete(t)}debugCollect(e,t){if(this.debug&&a(this,c).get(e)){let i=a(this,c).get(e)||[];if(t){let h=t(i);a(this,d).get(e)||a(this,d).set(e,[]),h&&a(this,d).get(e).push(h)}}}debugConsole(e){if(this.debug){let t=e.getAttribute("uid");a(this,c).get(t)&&(console.log("测试结果",t,e),console.log(a(this,d).get(t)))}}static insertRule(e,t,i,h){e.insertRule&&i?e.insertRule(i,h):e.addRule&&i&&e.addRule(t,i,h)}static deleteRule(e,t){e.deleteRule?e.deleteRule(t):e.removeRule&&e.removeRule(t)}};let u=w;return c=new WeakMap,d=new WeakMap,x=new WeakMap,y=new WeakMap,R=new WeakMap,C=new WeakMap,m=new WeakMap,S=new WeakMap,p=new WeakSet,A=function(e,t){let i={},h=e.map((r,s)=>{let l=a(this,m)[r];return l&&(this.debugCollect(t,function(f){if(f.length==0)return["规则生成缓存",r,l];if(f.includes(r))return["规则生成缓存",r,l]}),e.splice(s,1,"")),l}).filter(r=>r);Object.keys(this.pseudoClassDefine).forEach(r=>{i[this.pseudoClassDefine[r]]||(i[this.pseudoClassDefine[r]]=[]),e=e.filter(l=>l);let s=e.filter((l,f)=>{let g=l.startsWith(r);return g&&e.splice(f,1,""),g}).map(l=>{let f=l.replace(r,""),g=a(this,m)[f];return g?(this.debugCollect(t,function(b){if(b.length==0)return["规则生成-缓存",l,g];if(b.includes(l))return["规则生成-缓存",l,g]}),[g]):this.rules.filter(b=>b[0].test(f)).map(b=>(g=b[1](b[0].exec(f),f,this.themes),a(this,m)[f]=g,this.debugCollect(t,function(O){if(O.length==0)return["规则生成",b[0],l,g];if(O.includes(l))return["规则生成",b[0],l,g]}),g))}).flat(1/0);i[this.pseudoClassDefine[r]].push(...s)}),e=e.filter(r=>r);let n=this.rules.map(r=>e.filter(l=>r[0].test(l)).map(l=>{let f=r[1](r[0].exec(l),l,this.themes);return a(this,m)[l]=f,this.debugCollect(t,function(g){if(g.length==0)return["规则生成",r[0],l,f];if(g.includes(l))return["规则生成",r[0],l,f]}),f})).flat(1/0);return i[""]=h.concat(n),i},o(u,"guid",()=>"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=Math.random()*16|0,i=e=="x"?t:t&3|8;return i.toString(16)})),o(u,"setStorage",(e,t,i=72e5)=>{let h={value:t,expire:i,timestamp:Date.now(),isForever:i==-1};window.localStorage.setItem(e,JSON.stringify(h))}),o(u,"getStorage",e=>{let t=window.localStorage.getItem(e);if(!t)return null;let i=JSON.parse(t);return!i.isForever&&Date.now()>i.expire+i.timestamp?(window.localStorage.removeItem(e),null):i.value}),o(u,"removeStorage",e=>{window.localStorage.removeItem(e)}),o(u,"removeStorages",(e,t=[])=>{Object.keys(window.localStorage).filter(i=>e.test(i)&&!t.includes(i)).forEach(i=>{window.localStorage.removeItem(i)})}),o(u,"isInViewPort",e=>{const t=window.innerWidth||document.documentElement.clientWidth,i=window.innerHeight||document.documentElement.clientHeight,{top:h,right:n,bottom:r,left:s}=e.getBoundingClientRect();return h>=0&&s>=0&&n<=t&&r<=i}),u});