-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: change search function from list.js to fuse.js
use debounced fuse.js extended search in search function
- Loading branch information
Showing
8 changed files
with
81 additions
and
2,066 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* global Fuse:false */ | ||
|
||
(function () { | ||
'use strict' | ||
|
||
const iconsBody = document.querySelector('#icons-body') | ||
|
||
if (!iconsBody) return | ||
|
||
const searchInput = iconsBody.querySelector('#search') | ||
const iconListContainer = iconsBody.querySelector('#icons-list') | ||
const iconElementList = Array.from(iconListContainer.children) | ||
|
||
const iconDataList = iconElementList.map(element => ({ | ||
name: element.dataset.name, | ||
categories: element.dataset.categories.split(' '), | ||
tags: element.dataset.tags.split(' ') | ||
})) | ||
|
||
const fuse = new Fuse(iconDataList, { | ||
ignoreLocation: true, | ||
useExtendedSearch: true, | ||
shouldSort: false, | ||
keys: ['name', 'categories', 'tags'], | ||
threshold: 0 | ||
}) | ||
|
||
function search(searchTerm) { | ||
const searchResult = fuse.search(searchTerm) | ||
|
||
iconListContainer.innerHTML = '' | ||
if (searchTerm.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) | ||
} else { | ||
newUrl.searchParams.delete('q') | ||
} | ||
|
||
window.history.replaceState(null, null, newUrl) | ||
} | ||
|
||
let timeout | ||
searchInput.addEventListener('input', () => { | ||
clearTimeout(timeout) | ||
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() | ||
} | ||
})() |
Oops, something went wrong.