Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: change search function from list.js to fuse.js #1653

Merged
merged 2 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ module:
target: assets/js/vendor/bootstrap.bundle.min.js
- source: node_modules/clipboard/dist/clipboard.min.js
target: assets/js/vendor/clipboard.min.js
- source: node_modules/fuse.js/dist/fuse.min.js
target: assets/js/vendor/fuse.min.js

params:
description: "Official open source SVG icon library for Bootstrap"
Expand Down
38 changes: 0 additions & 38 deletions docs/assets/js/list.js

This file was deleted.

61 changes: 61 additions & 0 deletions docs/assets/js/search.js
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)
XhmikosR marked this conversation as resolved.
Show resolved Hide resolved
}

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()
}
})()
Loading