archives_blog/public/ts/main.js
2025-06-05 10:17:40 +02:00

11 lines
6.9 KiB
JavaScript

(()=>{var l=class{galleryUID;items=[];constructor(t,i=1){if(window.PhotoSwipe==null||window.PhotoSwipeUI_Default==null){console.error("PhotoSwipe lib not loaded.");return}this.galleryUID=i,l.createGallery(t),this.loadItems(t),this.bindClick()}loadItems(t){this.items=[];let i=t.querySelectorAll("figure.gallery-image");for(let o of i){let r=o.querySelector("figcaption"),n=o.querySelector("img"),s={w:parseInt(n.getAttribute("width")),h:parseInt(n.getAttribute("height")),src:n.src,msrc:n.getAttribute("data-thumb")||n.src,el:o};r&&(s.title=r.innerHTML),this.items.push(s)}}static createGallery(t){let i=t.querySelectorAll("figure.gallery-image"),o=[];for(let r of i)o.length?r.previousElementSibling===o[o.length-1]?o.push(r):o.length&&(l.wrap(o),o=[r]):o=[r];o.length>0&&l.wrap(o)}static wrap(t){let i=document.createElement("div");i.className="gallery";let o=t[0].parentNode,r=t[0];o.insertBefore(i,r);for(let n of t)i.appendChild(n)}open(t){let i=document.querySelector(".pswp");new window.PhotoSwipe(i,window.PhotoSwipeUI_Default,this.items,{index:t,galleryUID:this.galleryUID,getThumbBoundsFn:r=>{let n=this.items[r].el.getElementsByTagName("img")[0],s=window.pageYOffset||document.documentElement.scrollTop,c=n.getBoundingClientRect();return{x:c.left,y:c.top+s,w:c.width}}}).init()}bindClick(){for(let[t,i]of this.items.entries())i.el.querySelector("a").addEventListener("click",r=>{r.preventDefault(),this.open(t)})}},g=l;var a={};if(localStorage.hasOwnProperty("StackColorsCache"))try{a=JSON.parse(localStorage.getItem("StackColorsCache"))}catch{a={}}async function f(e,t,i){if(!e)return await Vibrant.from(i).getPalette();if(!a.hasOwnProperty(e)||a[e].hash!==t){let o=await Vibrant.from(i).getPalette();a[e]={hash:t,Vibrant:{hex:o.Vibrant.hex,rgb:o.Vibrant.rgb,bodyTextColor:o.Vibrant.bodyTextColor},DarkMuted:{hex:o.DarkMuted.hex,rgb:o.DarkMuted.rgb,bodyTextColor:o.DarkMuted.bodyTextColor}},localStorage.setItem("StackColorsCache",JSON.stringify(a))}return a[e]}var k=(e,t=500)=>{e.classList.add("transiting"),e.style.transitionProperty="height, margin, padding",e.style.transitionDuration=t+"ms",e.style.height=e.offsetHeight+"px",e.offsetHeight,e.style.overflow="hidden",e.style.height="0",e.style.paddingTop="0",e.style.paddingBottom="0",e.style.marginTop="0",e.style.marginBottom="0",window.setTimeout(()=>{e.classList.remove("show"),e.style.removeProperty("height"),e.style.removeProperty("padding-top"),e.style.removeProperty("padding-bottom"),e.style.removeProperty("margin-top"),e.style.removeProperty("margin-bottom"),e.style.removeProperty("overflow"),e.style.removeProperty("transition-duration"),e.style.removeProperty("transition-property"),e.classList.remove("transiting")},t)},L=(e,t=500)=>{e.classList.add("transiting"),e.style.removeProperty("display"),e.classList.add("show");let i=e.offsetHeight;e.style.overflow="hidden",e.style.height="0",e.style.paddingTop="0",e.style.paddingBottom="0",e.style.marginTop="0",e.style.marginBottom="0",e.offsetHeight,e.style.transitionProperty="height, margin, padding",e.style.transitionDuration=t+"ms",e.style.height=i+"px",e.style.removeProperty("padding-top"),e.style.removeProperty("padding-bottom"),e.style.removeProperty("margin-top"),e.style.removeProperty("margin-bottom"),window.setTimeout(()=>{e.style.removeProperty("height"),e.style.removeProperty("overflow"),e.style.removeProperty("transition-duration"),e.style.removeProperty("transition-property"),e.classList.remove("transiting")},t)},x=(e,t=500)=>window.getComputedStyle(e).display==="none"?L(e,t):k(e,t);function b(){let e=document.getElementById("toggle-menu");e&&e.addEventListener("click",()=>{document.getElementById("main-menu").classList.contains("transiting")||(document.body.classList.toggle("show-menu"),x(document.getElementById("main-menu"),300),e.classList.toggle("is-active"))})}function M(e,t,i){var o=document.createElement(e);for(let r in t)if(r&&t.hasOwnProperty(r)){let n=t[r];r=="dangerouslySetInnerHTML"?o.innerHTML=n.__html:n===!0?o.setAttribute(r,r):n!==!1&&n!=null&&o.setAttribute(r,n.toString())}for(let r=2;r<arguments.length;r++){let n=arguments[r];n&&o.appendChild(n.nodeType==null?document.createTextNode(n.toString()):n)}return o}var S=M;var p=class{localStorageKey="StackColorScheme";currentScheme;systemPreferScheme;constructor(t){this.bindMatchMedia(),this.currentScheme=this.getSavedScheme(),this.dispatchEvent(document.documentElement.dataset.scheme),t&&this.bindClick(t),document.body.style.transition==""&&document.body.style.setProperty("transition","background-color .3s ease")}saveScheme(){localStorage.setItem(this.localStorageKey,this.currentScheme)}bindClick(t){t.addEventListener("click",i=>{this.isDark()?this.currentScheme="light":this.currentScheme="dark",this.setBodyClass(),this.currentScheme==this.systemPreferScheme&&(this.currentScheme="auto"),this.saveScheme()})}isDark(){return this.currentScheme=="dark"||this.currentScheme=="auto"&&this.systemPreferScheme=="dark"}dispatchEvent(t){let i=new CustomEvent("onColorSchemeChange",{detail:t});window.dispatchEvent(i)}setBodyClass(){this.isDark()?document.documentElement.dataset.scheme="dark":document.documentElement.dataset.scheme="light",this.dispatchEvent(document.documentElement.dataset.scheme)}getSavedScheme(){let t=localStorage.getItem(this.localStorageKey);return t=="light"||t=="dark"||t=="auto"?t:"auto"}bindMatchMedia(){window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",t=>{t.matches?this.systemPreferScheme="dark":this.systemPreferScheme="light",this.setBodyClass()})}},w=p;var v={init:()=>{b();let e=document.querySelector(".article-content");e&&new g(e);let t=document.querySelector(".article-list--tile");t&&new IntersectionObserver(async(s,c)=>{s.forEach(d=>{if(!d.isIntersecting)return;c.unobserve(d.target),d.target.querySelectorAll("article.has-image").forEach(async y=>{let u=y.querySelector("img"),E=u.src,T=u.getAttribute("data-key"),P=u.getAttribute("data-hash"),C=y.querySelector(".article-details"),m=await f(T,P,E);C.style.background=`
linear-gradient(0deg,
rgba(${m.DarkMuted.rgb[0]}, ${m.DarkMuted.rgb[1]}, ${m.DarkMuted.rgb[2]}, 0.5) 0%,
rgba(${m.Vibrant.rgb[0]}, ${m.Vibrant.rgb[1]}, ${m.Vibrant.rgb[2]}, 0.75) 100%)`})})}).observe(t);let i=document.querySelectorAll(".article-content .highlight"),o="Copy",r="Copied!";i.forEach(n=>{let s=document.createElement("button");s.innerHTML=o,s.classList.add("copyCodeButton"),n.appendChild(s);let d=n.getElementsByTagName("pre")[0].textContent;s.addEventListener("click",()=>{navigator.clipboard.writeText(d).then(()=>{s.textContent=r,setTimeout(()=>{s.textContent=o},1e3)}).catch(h=>{alert(h),console.log("Something went wrong",h)})})}),new w(document.getElementById("dark-mode-toggle"))}};window.addEventListener("load",()=>{setTimeout(function(){v.init()},0)});window.Stack=v;window.createElement=S;})();
/*!
* Hugo Theme Stack
*
* @author: Jimmy Cai
* @website: https://jimmycai.com
* @link: https://github.com/CaiJimmy/hugo-theme-stack
*/