-
Notifications
You must be signed in to change notification settings - Fork 0
/
zh-cn.search.min.8ba6fec238dfec5b38824b4c8d7153cd98d1f0348ee98934dea7320dca0e9694.js
8 lines (8 loc) · 5.26 KB
/
zh-cn.search.min.8ba6fec238dfec5b38824b4c8d7153cd98d1f0348ee98934dea7320dca0e9694.js
1
2
3
4
5
6
7
8
document.addEventListener("DOMContentLoaded",function(){if(/iPad|iPhone|Macintosh/.test(navigator.userAgent)){const e=document.querySelectorAll(".search-wrapper kbd");e.forEach(e=>{e.innerHTML='<span class="hx-text-xs">⌘</span>K'})}}),function(){const h="/zh-cn.search-data.json",u=document.querySelectorAll(".search-input");for(const e of u)e.addEventListener("focus",o),e.addEventListener("keyup",p),e.addEventListener("keydown",n),e.addEventListener("input",m);const d=document.querySelectorAll(".search-wrapper kbd");function a(e){d.forEach(t=>{t.style.opacity=e})}function m(e){const t=e.target.value.length>0?0:100;a(t)}function e(){const e=Array.from(document.querySelectorAll(".search-wrapper")).filter(e=>e.clientHeight>0);return e.length===1?{wrapper:e[0],inputElement:e[0].querySelector(".search-input"),resultsElement:e[0].querySelector(".search-results")}:void 0}const l=["input","select","button","textarea"];document.addEventListener("keydown",function(t){const{inputElement:n}=e();if(!n)return;const s=document.activeElement,o=s&&s.tagName;if(n===s||!o||l.includes(o)||s&&s.isContentEditable)return;t.key==="/"||t.key==="k"&&(t.metaKey||t.ctrlKey)?(t.preventDefault(),n.focus()):t.key==="Escape"&&n.value&&n.blur()}),document.addEventListener("mousedown",function(n){const{inputElement:o,resultsElement:s}=e();if(!o||!s)return;n.target!==o&&n.target!==s&&!s.contains(n.target)&&(a(100),t())});function r(){const{resultsElement:n}=e();if(!n)return{result:void 0,index:-1};const t=n.querySelector(".active");if(!t)return{result:void 0,index:-1};const s=parseInt(t.dataset.index,10);return{result:t,index:s}}function s(t){const{resultsElement:s}=e();if(!s)return;const{result:o}=r();o&&o.classList.remove("active");const n=s.querySelector(`[data-index="${t}"]`);n&&(n.classList.add("active"),n.focus())}function c(){const{resultsElement:t}=e();return t?t.dataset.count:0}function i(){const{inputElement:n}=e();if(!n)return;t(),n.value="",n.blur()}function t(){const{resultsElement:t}=e();if(!t)return;t.classList.add("hx-hidden")}function n(n){const{inputElement:o}=e();if(!o)return;const d=c(),{result:l,index:a}=r();switch(n.key){case"ArrowUp":n.preventDefault(),a>0&&s(a-1);break;case"ArrowDown":n.preventDefault(),a+1<d&&s(a+1);break;case"Enter":n.preventDefault(),l&&l.click(),i();case"Escape":n.preventDefault(),t(),o.value="",o.dispatchEvent(new Event("input")),o.blur();break}}function o(e){e.target.removeEventListener("focus",o),window.pageIndex&&window.sectionIndex||f()}async function f(){const n="forward";window.pageIndex=new FlexSearch.Document({tokenize:n,cache:100,document:{id:"id",store:["title"],index:"content"}}),window.sectionIndex=new FlexSearch.Document({tokenize:n,cache:100,document:{id:"id",store:["title","content","url","display"],index:"content",tag:"pageId"}});const s=await fetch(h),e=await s.json();let t=0;for(const n in e){let s="";++t;for(const r in e[n].data){const[c,d]=r.split("#"),o=n.trimEnd("/")+(c?"#"+c:""),i=d||e[n].title,l=e[n].data[r]||"",a=l.split(`
`).filter(Boolean);sectionIndex.add({id:o,url:o,title:i,pageId:`page_${t}`,content:i,...a[0]&&{display:a[0]}});for(let e=0;e<a.length;e++)sectionIndex.add({id:`${o}_${e}`,url:o,title:i,pageId:`page_${t}`,content:a[e]});s+=` ${i} ${l}`}window.pageIndex.add({id:t,title:e[n].title,content:s})}}function p(n){const i=n.target.value;if(!n.target.value){t();return}const{resultsElement:o}=e();for(;o.firstChild;)o.removeChild(o.firstChild);o.classList.remove("hx-hidden");const a=window.pageIndex.search(i,5,{enrich:!0,suggest:!0})[0]?.result||[],r=[],s={};for(let e=0;e<a.length;e++){const t=a[e];s[e]=0;const n=window.sectionIndex.search(i,5,{enrich:!0,suggest:!0,tag:`page_${t.id}`})[0]?.result||[];let o=!0;const c={};for(let i=0;i<n.length;i++){const{doc:a}=n[i],u=a.display!==void 0;u&&s[e]++;const{url:l,title:h}=a,d=a.display||a.content;if(c[l+"@"+d])continue;c[l+"@"+d]=!0,r.push({_page_rk:e,_section_rk:i,route:l,prefix:o?t.doc.title:void 0,children:{title:h,content:d}}),o=!1}}const c=r.sort((e,t)=>e._page_rk===t._page_rk?e._section_rk-t._section_rk:s[e._page_rk]!==s[t._page_rk]?s[t._page_rk]-s[e._page_rk]:e._page_rk-t._page_rk).map(e=>({id:`${e._page_rk}_${e._section_rk}`,route:e.route,prefix:e.prefix,children:e.children}));g(c,i)}function g(t,s){const{resultsElement:o}=e();if(!o)return;if(!t.length){o.innerHTML=`<span class="no-result">无结果</span>`;return}function r(e,t){const n=t.replace(/[-\\^$*+?.()|[\]{}]/g,"\\$&"),s=new RegExp(n,"gi");return e.replace(s,e=>`<span class="match">${e}</span>`)}function c(e){const t=document.createElement("div");return t.innerHTML=e.trim(),t.firstChild}function l(e){const t=e.target.closest("a");if(t){const e=o.querySelector("a.active");e&&e.classList.remove("active"),t.classList.add("active")}}const a=document.createDocumentFragment();for(let o=0;o<t.length;o++){const e=t[o];e.prefix&&a.appendChild(c(`
<div class="prefix">${e.prefix}</div>`));let d=c(`
<li>
<a data-index="${o}" href="${e.route}" class=${o===0?"active":""}>
<div class="title">`+r(e.children.title,s)+`</div>`+(e.children.content?`<div class="excerpt">`+r(e.children.content,s)+`</div>`:"")+`
</a>
</li>`);d.addEventListener("mousemove",l),d.addEventListener("keydown",n),d.querySelector("a").addEventListener("click",i),a.appendChild(d)}o.appendChild(a),o.dataset.count=t.length}}()