Skip to content

Commit

Permalink
docs: trim query from search (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Dec 17, 2023
1 parent 2051e9f commit b9564a9
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions docs/assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ import Fuse from 'fuse.js'
})

function search(searchTerm) {
const searchResult = fuse.search(searchTerm)
const trimmedSearchTerm = searchTerm ? searchTerm.trim() : ''
const searchResult = fuse.search(trimmedSearchTerm)

iconListContainer.innerHTML = ''
if (searchTerm.length > 0) {
if (trimmedSearchTerm.length > 0) {
const resultElements = searchResult.map(result => iconElementList[result.refIndex])
iconListContainer.append(...resultElements)
} else {
iconListContainer.append(...iconElementList)
}

const newUrl = new URL(window.location)
if (searchTerm.length > 0) {
newUrl.searchParams.set('q', searchTerm)
if (trimmedSearchTerm.length > 0) {
newUrl.searchParams.set('q', trimmedSearchTerm)
} else {
newUrl.searchParams.delete('q')
}
Expand All @@ -47,13 +48,16 @@ import Fuse from 'fuse.js'
let timeout
searchInput.addEventListener('input', () => {
clearTimeout(timeout)
timeout = setTimeout(() => search(searchInput.value), 250)
timeout = setTimeout(() => {
search(searchInput.value)
}, 250)
})

const query = new URLSearchParams(window.location.search).get('q')
if (query) {
search(query)
searchInput.value = query
document.querySelector('#content').scrollIntoView()
}
if (!query || query.length === 0) return

const trimmedQuery = query.trim()
search(trimmedQuery)
searchInput.value = trimmedQuery
document.querySelector('#content').scrollIntoView()
})()

0 comments on commit b9564a9

Please sign in to comment.