1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| import Fuse from './fuse.js'
async function initSearch() { const res = await fetch('/search.json') const data = await res.json()
const openBtn = document.getElementById('open-search') const closeBtn = document.getElementById('close-search') const modal = document.getElementById('search-modal') const input = document.getElementById('search-input') const resultsBox = document.getElementById( 'search-results' )
let lastFocusedElement = null
const fuse = new Fuse(data, { keys: ['title', 'content'], includeScore: true, threshold: 0.8, useExtendedSearch: true, minMatchCharLength: 0, })
readerResult(data)
openBtn.addEventListener('click', () => { lastFocusedElement = document.activeElement openModal() })
closeBtn.addEventListener('click', closeModal)
function openModal() { modal.classList.add('active') document.body.classList.add('modal-open') input.focus() }
function closeModal() { modal.classList.remove('active') document.body.classList.remove('modal-open') lastFocusedElement?.focus() }
modal.addEventListener('click', e => { if (e.target === modal) closeModal() })
document.addEventListener('keydown', e => { if (e.ctrlKey && e.shiftKey && e.key === 'K') { if (!modal.classList.contains('active')) { openModal() } else { input.focus() } } if (!modal.classList.contains('active')) return if (e.key === 'Escape') closeModal()
const focusable = modal.querySelectorAll( 'input, button, a' ) const first = focusable[0] const last = focusable[focusable.length - 1] if (e.key === 'Tab') { if (e.shiftKey && document.activeElement === first) { e.preventDefault() last.focus() } else if ( !e.shiftKey && document.activeElement === last ) { e.preventDefault() first.focus() } } })
function readerResult(results) { resultsBox.innerHTML = results .map(post => { const content = (post.content || '').slice( 0, Math.max( 120, parseInt( post.content.length * (1 - (results.length - 1) / data.length) ) ) ) return ` <div class="search-item"> <a href="${ post.url[1] === '\/' ? post.url.slice(1) : post.url }">${post.title}</a> <p>${content}${ post.content.length === content.length ? '' : '...' } </p> </div> ` }) .join('') }
function highlightKeyword(keywords) { if (!('CSS' in window) || !CSS.highlights) return CSS.highlights.clear() if (!keywords.length) return
const ranges = [] resultsBox .querySelectorAll('.search-item') .forEach(item => { const walker = document.createTreeWalker( item, NodeFilter.SHOW_TEXT ) let node while ((node = walker.nextNode())) { const text = node.textContent for (const word of keywords) { if (!word) continue const regex = new RegExp( word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi' ) let match while ((match = regex.exec(text))) { const range = new Range() range.setStart(node, match.index) range.setEnd( node, match.index + match[0].length ) ranges.push(range) } } } })
if (ranges.length) { const highlight = new Highlight(...ranges) CSS.highlights.set('search-highlight', highlight) } }
input.addEventListener('input', e => { const keyword = e.target.value.trim().toLowerCase() resultsBox.innerHTML = ''
if (!keyword) { readerResult(data) highlightKeyword([]) return }
const keywords = keyword.split(/\s+/).filter(Boolean) const results = fuse.search(keyword) const final = results.map(r => r.item) if (final.length === 0) { resultsBox.innerHTML = '<p>未找到相关文章</p>' highlightKeyword([]) return }
readerResult(final) highlightKeyword(keywords) }) }
initSearch()
|