+var k="theme";function A(t){return t||k}function m(t){return t||null}function f(t){return(t||"").split(",").map(n=>n.trim()).filter(Boolean)}function E(t,n){if(!t.length)return null;let e=t.indexOf(n||"");return e===-1?t[0]:t[(e+1)%t.length]}function L(t,n){let e=f(t);return e.length>1?E(e,n):m(t)}function b(t,n){let e=f(t);return n?e.length>1?e.includes(n):t===n:t===""}function y(t){let n=(t||"").trim();if(!n)return null;let e=n.indexOf(":"),o=(e===-1?n:n.slice(0,e)).trim();return o?{name:o,value:e===-1?"":n.slice(e+1)}:null}var $=A("");function s(t){return A(t&&t.getAttribute("data-key"))}function B(t,n){t?(document.documentElement.setAttribute("data-theme",t),localStorage.setItem(n,t)):(document.documentElement.removeAttribute("data-theme"),localStorage.removeItem(n))}function p(t){return localStorage.getItem(t)||null}function I(t,n){return s(t)===n}function C(t){return[...document.querySelectorAll("[data-set-theme]")].filter(n=>I(n,t))}var v=new WeakSet;function S(t,n,e,o){n.has(t)||(n.add(t),t.addEventListener(e,o))}function K(t,n){let e=t.getAttribute("data-act-class");e&&(n?t.classList.add(e):t.classList.remove(e))}function g(t,n){let e=y(t.getAttribute("data-act-attribute"));e&&(n?t.setAttribute(e.name,e.value):t.removeAttribute(e.name))}function l(t,n){C(n).forEach(o=>{if(o instanceof HTMLSelectElement){let a=t||"";o.querySelector(`[value="${a}"]`)&&(o.value=a);return}if(o instanceof HTMLInputElement&&o.type==="checkbox"){o.checked=!!t&&o.value===t;return}let c=o.getAttribute("data-set-theme")||"",r=b(c,t);K(o,r),g(o,r)})}function u(t,n){B(t,n),l(t,n)}function H(t){let n=C(t);if(!n.length)return;let e=p(t);e&&document.documentElement.setAttribute("data-theme",e),l(e,t),n.forEach(o=>{if(o instanceof HTMLSelectElement){S(o,v,"change",function(){u(m(this.value),t)});return}if(o instanceof HTMLInputElement&&o.type==="checkbox"){S(o,v,"change",function(){let c=m(this.value);u(this.checked?c:null,t)});return}S(o,v,"click",function(){let c=this.getAttribute("data-set-theme")||"",r=document.documentElement.getAttribute("data-theme");u(L(c,r),t)})})}function M(){let t=[...document.querySelectorAll("[data-set-theme]")];if(!t.length)return;[...new Set(t.map(e=>s(e)))].forEach(e=>H(e))}var D=new WeakSet;function F(){let t=[...document.querySelectorAll("[data-toggle-theme]")];if(!t.length)return;[...new Set(t.map(e=>s(e)))].forEach(e=>{let o=t.filter(r=>s(r)===e),c=p(e);c&&(document.documentElement.setAttribute("data-theme",c),o.forEach(r=>{let a=f(r.getAttribute("data-toggle-theme"))[0]===c,i=r.getAttribute("data-act-class");i&&r.classList.toggle(i,a),g(r,a)})),l(c,e),o.forEach(r=>{D.has(r)||(D.add(r),r.addEventListener("click",function(){let a=f(this.getAttribute("data-toggle-theme")),i=document.documentElement.getAttribute("data-theme"),d=E(a,i);u(d,e),o.forEach(h=>{let T=f(h.getAttribute("data-toggle-theme"))[0]===d,x=h.getAttribute("data-act-class");x&&h.classList.toggle(x,T),g(h,T)})}))})})}function q(){M()}var w=new WeakSet;function O(){let t=[...document.querySelectorAll("select[data-choose-theme]")];if(!t.length)return;[...new Set(t.map(e=>s(e)))].forEach(e=>{let o=t.filter(r=>s(r)===e),c=p(e);c&&(document.documentElement.setAttribute("data-theme",c),o.forEach(r=>{r.querySelector(`[value="${c}"]`)&&(r.value=c)})),l(c,e),o.forEach(r=>{w.has(r)||(w.add(r),r.addEventListener("change",function(){let a=m(this.value);u(a,e),o.forEach(i=>{let d=a||"";i.querySelector(`[value="${d}"]`)&&(i.value=d)})}))})})}function Z(t=!0){function n(){q(),F(),O()}t?document.addEventListener("DOMContentLoaded",n):n()}export{Z as themeChange};
0 commit comments