From b41c170b0cc625e494ffe60c4b41740b73d92cc2 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Tue, 22 Nov 2022 00:18:06 +0800 Subject: [PATCH] website: add search arrow selection. --- scripts/js/main.js | 53 ++++++++++++++++++++++++++++------------ scripts/nodes/search.mjs | 7 +++++- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/scripts/js/main.js b/scripts/js/main.js index 1edf795..8d5b045 100644 --- a/scripts/js/main.js +++ b/scripts/js/main.js @@ -71,22 +71,47 @@ searchInput.addEventListener('input', (evn) => searchResult(evn.target.value)); let activeMenu = {} let result = [] let inputValue = ''; +let activeIndex = 0 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) } + if (ev.key.toLocaleLowerCase() === 'arrowdown') { + activeAnchorElm('down') + } + if (ev.key.toLocaleLowerCase() === 'arrowup') { + activeAnchorElm('up') + } }); +function activeAnchorElm(type) { + if (type === 'down') { + ++activeIndex; + } + if (type === 'up') { + --activeIndex; + } + const data = Array.from(searchMenu.children); + if (activeIndex < 0) activeIndex = 0; + if (activeIndex >= data.length) activeIndex = data.length - 1; + anchorElm = data[activeIndex]; + if (anchorElm) { + data.forEach(item => item.classList.remove('active')); + anchorElm.classList.add('active'); + activeMenu = result[activeIndex]; + activeIndex = activeIndex; + searchSectionsResult(activeIndex); + } +} + function showSearch() { document.body.classList.add('search'); searchBox.classList.add('show'); - searchResult('') + searchResult(searchInput.value || ''); searchInput.focus(); } @@ -107,13 +132,14 @@ function searchResult(value) { let menuHTML = ''; result.forEach((item, idx) => { const label = item.item.name.replace(getValueReg(value), (txt) => { - return `${txt}` + return `${txt}`; }) const tags = (item.item.tags || []).join(',').replace(getValueReg(value), (txt) => { - return `${txt}` + return `${txt}`; }) const href = isHome ? item.item.path : item.item.path.replace('docs/', ''); if (idx === 0) { + activeIndex = idx; activeMenu = item.item; menuHTML += `${label}${tags}`; } else { @@ -122,12 +148,13 @@ function searchResult(value) { }); searchMenu.innerHTML = menuHTML; searchSectionsResult(); - const data = Array.from(searchMenu.children) + const data = Array.from(searchMenu.children); data.forEach((anchor, idx) => { anchor.onmouseenter = (evn) => { data.forEach(item => item.classList.remove('active')); evn.target.classList.add('active'); activeMenu = result[idx]; + activeIndex = idx; searchSectionsResult(idx); } }); @@ -138,23 +165,19 @@ function searchResult(value) { } function searchSectionsResult(idx = 0) { const data = result[idx] || []; - const title = (data.item?.intro || '').replace(getValueReg(inputValue), (txt) => { - return `${txt}` - }); + const title = (data.item?.intro || '').replace(getValueReg(inputValue), (txt) => `${txt}`); let sectionHTML = `

${title}

    `; if (data && data.item && data.item.sections) { data.item.sections.forEach((item, idx) => { - const label = item.t.replace(getValueReg(inputValue), (txt) => { - return `${txt}` - }) + const label = item.t.replace(getValueReg(inputValue), (txt) => `${txt}`); const href = getDocUrl(data.item.path); if (item.l < 3) { - sectionHTML += `
  1. ${label}
    ` + sectionHTML += `
  2. ${label}
    `; } else { - sectionHTML += `${label}` + sectionHTML += `${label}`; } if (data.item.sections.length === idx + 1) { - sectionHTML += `
  3. ` + sectionHTML += ``; } }) } diff --git a/scripts/nodes/search.mjs b/scripts/nodes/search.mjs index 36d999d..c8f693f 100644 --- a/scripts/nodes/search.mjs +++ b/scripts/nodes/search.mjs @@ -62,7 +62,12 @@ export function search({ homePath = '', isHome } = {}) { { type: 'element', tagName: 'input', - properties: { id: ['mysearch-input'], type: 'search', placeholder: '搜索备忘清单' }, + properties: { + id: ['mysearch-input'], + type: 'search', + placeholder: '搜索备忘清单', + autocomplete: 'off', + }, children: [], }, {