Skip to content

Commit bf593a7

Browse files
committed
fix(search): add lazy input
1 parent 3b127a1 commit bf593a7

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/plugins/search/component.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ function bindEvents () {
8181
const $search = dom.find('div.search')
8282
const $input = dom.find($search, 'input')
8383
const $panel = dom.find($search, '.results-panel')
84-
85-
// Prevent to Fold sidebar
86-
dom.on($search, 'click',
87-
e => e.target.tagName !== 'A' && e.stopPropagation())
88-
89-
dom.on($input, 'input', e => {
90-
const value = e.target.value.trim()
84+
const doSearch = function (value) {
9185
if (!value) {
9286
$panel.classList.remove('show')
9387
$panel.innerHTML = ''
@@ -96,7 +90,6 @@ function bindEvents () {
9690
const matchs = search(value)
9791

9892
let html = ''
99-
10093
matchs.forEach(post => {
10194
html += `<div class="matching-post">
10295
<h2><a href="${post.url}">${post.title}</a></h2>
@@ -106,6 +99,15 @@ function bindEvents () {
10699

107100
$panel.classList.add('show')
108101
$panel.innerHTML = html || '<p class="empty">No Results!</p>'
102+
}
103+
104+
let timeId
105+
// Prevent to Fold sidebar
106+
dom.on($search, 'click',
107+
e => e.target.tagName !== 'A' && e.stopPropagation())
108+
dom.on($input, 'input', e => {
109+
clearTimeout(timeId)
110+
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 200)
109111
})
110112
}
111113

src/plugins/search/search.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ export function search (keywords) {
8282
const postContent = post.body && post.body.trim()
8383
const postUrl = post.slug || ''
8484

85-
if (postTitle !== '' && postContent !== '') {
85+
if (postTitle && postContent) {
8686
keywords.forEach((keyword, i) => {
8787
const regEx = new RegExp(keyword, 'gi')
8888
let indexTitle = -1
8989
let indexContent = -1
9090

91-
indexTitle = postTitle.search(regEx)
92-
indexContent = postContent.search(regEx)
91+
indexTitle = postTitle && postTitle.search(regEx)
92+
indexContent = postContent && postContent.search(regEx)
9393

9494
if (indexTitle < 0 && indexContent < 0) {
9595
isMatch = false

0 commit comments

Comments
 (0)