UNPKG

halo-theme-dream2.0-plus

Version:

梦之城,童话梦境,动漫类型博客主题。

1 lines 3.27 kB
(()=>{function s(t,e){this.tocList=t,this.elementList=function t(e,n){if(null==e)return null;var i=e.children;if(0===i.length)return null;for(var s=[],o=0;o<i.length;o++){var l=i[o];-1!==n.indexOf(l.tagName)&&s.push(l),s.push.apply(s,t(l,n))}return s}(e,this.tocList),this.eIndex=0,this.build=function(){return null==this.elementList||0===this.elementList.length?"":this.analysis(-1,this.tocList.indexOf(this.elementList[this.eIndex].tagName))},this.analysis=function(t,e){for(var n="<ul class='menu-list'>";this.eIndex<this.elementList.length;){var i=this.elementList[this.eIndex],s=this.tocList.indexOf(i.tagName);if(t<s&&s<=e){e=s;var o=i.id,l=i.textContent.trim();if(null!=o&&""!==o||(o=l+"_"+this.eIndex,i.setAttribute("id",o)),n+=`<li><a data-id="#${o}" aria-label="${l}"><i class="ri-attachment-2"></i>${l}</a>`,this.eIndex++,this.eIndex>=this.elementList.length){n+="</li>";break}(s=this.tocList.indexOf(this.elementList[this.eIndex].tagName))>e&&(n+=this.analysis(e,s)),n+="</li>"}else if(s<=t)break}return n+"</ul>"}}const n=[];function t(i){const t=new Set,s=new Map,o=Array.from(i.querySelectorAll(".menu-list > li > a"));for(const i of o){const n=i.getAttribute("data-id").trim().slice(1),t=document.getElementById(n);if(t){s.set(t,i);const n=i.nextElementSibling;n&&n.classList.contains("menu-list")?i.addEventListener("click",function(t){var e=this.getBoundingClientRect(),n=parseFloat(getComputedStyle(this).fontSize),e=e.right-2.5*n;t.clientX>e&&!i.classList.contains("is-active")?(t.preventDefault(),t.stopPropagation(),this.classList.toggle("is-expand")):l(i)}):i.addEventListener("click",function(t){l(i)})}s.has(t)&&(t.style.scrollMargin="1em")}function l(e){e=document.getElementById(e.getAttribute("data-id").substring(1));if(e){let n,t=e.getBoundingClientRect(),i=window.scrollY,s=i+t.top-75,o=(s-i)/20,l=i>s?-1:1;window.requestAnimationFrame(function t(e){(i+=o)*l<s*l?(window.scrollTo(0,i),n=window.requestAnimationFrame(t)):(window.scrollTo(0,s),window.cancelAnimationFrame(n))})}}const e=new IntersectionObserver(e=>{for(const n of e)n.isIntersecting?t.add(n.target):t.delete(n.target);let n;if(t.size?n=[...t].sort((t,e)=>t.offsetTop-e.offsetTop)[0]:a.length&&(n=a.filter(t=>t.offsetTop<window.scrollY).sort((t,e)=>e.offsetTop-t.offsetTop)[0]),n&&s.has(n)){o.forEach(t=>t.classList.remove("is-active"));const e=s.get(n);e.classList.add("is-active"),e.classList.remove("is-expand");let t=e.parentElement.parentElement;for(;t.classList.contains("menu-list")&&"li"===t.parentElement.tagName.toLowerCase();)t.parentElement.children[0].classList.add("is-active"),t.parentElement.children[0].classList.remove("is-expand"),t=t.parentElement.parentElement}},{threshold:0}),a=Array.from(s.keys());for(const i of a)e.observe(i);n.push(e)}s.init=function(t){var e=t.tocList,n=t.contentElement,t=t.tocElement;if(null==e||0===e.length||null==n)return $(t).children().remove(),!1;for(var i=0;i<e.length;i++)e[i]=e[i].toUpperCase();n=new s(e,n).build();$(t).html(n)},window.tocPjax=function(){n.forEach(t=>{t.disconnect()}),n.splice(0),s.init({tocList:["h1","h2","h3","h4","h5","h6"],contentElement:$(".main-content:not(.not-toc)")[0],tocElement:".toc-content"}),void 0!==window.IntersectionObserver&&document.querySelectorAll(".toc-content").forEach(t)}})();