From 32af5af854d43016b7e76562f4c6757a14e7f88c Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Mon, 21 Nov 2022 15:59:12 +0800 Subject: [PATCH] website: add search enter shortcat key to jump to the page. --- scripts/js/main.js | 22 ++++++++++++++++------ scripts/style/style.css | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/scripts/js/main.js b/scripts/js/main.js index 7218907..93ba431 100644 --- a/scripts/js/main.js +++ b/scripts/js/main.js @@ -55,7 +55,9 @@ const closeBtn = document.getElementById('mysearch-close'); const searchMenu = document.getElementById('mysearch-menu'); const searchContent = document.getElementById('mysearch-content'); const isHome = document.body.classList.contains('home'); - +function getDocUrl(url = '') { + return isHome ? url : url.replace('docs/', ''); +} searchBtn.addEventListener('click', (ev) => { ev.preventDefault(); showSearch(); @@ -65,15 +67,22 @@ closeBtn.addEventListener('click', hideSearch); searchBox.addEventListener('click', hideSearch); searchBox.firstChild.addEventListener('click', (ev) => ev.stopPropagation()); searchInput.addEventListener('input', (evn) => searchResult(evn.target.value)); + +let activeMenu = {} +let result = [] +let inputValue = ''; + document.addEventListener('keydown', (ev) => { + console.log('ev::', ev) if (ev.metaKey && ev.key.toLocaleLowerCase() === 'k') { searchBox.classList.contains('show') ? hideSearch() : showSearch(); } + if (ev.key.toLocaleLowerCase() === 'enter') { + console.log('activeMenu:', activeMenu) + window.location.href = getDocUrl(activeMenu.path) + } }); -let result = [] -let inputValue = ''; - function showSearch() { document.body.classList.add('search'); searchBox.classList.add('show'); @@ -105,6 +114,7 @@ function searchResult(value) { }) const href = isHome ? item.item.path : item.item.path.replace('docs/', ''); if (idx === 0) { + activeMenu = item.item; menuHTML += `${label}${tags}`; } else { menuHTML += `${label}${tags}`; @@ -117,6 +127,7 @@ function searchResult(value) { anchor.onmouseenter = (evn) => { data.forEach(item => item.classList.remove('active')); evn.target.classList.add('active'); + activeMenu = result[idx]; searchSectionsResult(idx); } }); @@ -125,7 +136,6 @@ function searchResult(value) { item.addEventListener('click', hideSearch); }) } - function searchSectionsResult(idx = 0) { const data = result[idx] || []; const title = (data.item?.intro || '').replace(getValueReg(inputValue), (txt) => { @@ -137,7 +147,7 @@ function searchSectionsResult(idx = 0) { const label = item.t.replace(getValueReg(inputValue), (txt) => { return `${txt}` }) - const href = isHome ? data.item.path : data.item.path.replace('docs/', ''); + const href = getDocUrl(data.item.path); if (item.l < 3) { sectionHTML += `
  • ${label}
    ` } else { diff --git a/scripts/style/style.css b/scripts/style/style.css index 57c1a50..3447166 100644 --- a/scripts/style/style.css +++ b/scripts/style/style.css @@ -549,7 +549,7 @@ a.text-grey { .header-nav .menu .searchbtn span:last-child { transition: all 0.3s; border: 1px solid var(--color-border-default); - border-radius: 3px; + border-radius: 0.4rem; padding: 1px 1px 1px 3px; letter-spacing: 3px; }