Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bf593a7

Browse filesBrowse files
committed
fix(search): add lazy input
1 parent 3b127a1 commit bf593a7
Copy full SHA for bf593a7

2 files changed

+13-11Lines changed: 13 additions & 11 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/plugins/search/component.js‎

Copy file name to clipboardExpand all lines: src/plugins/search/component.js
+10-8Lines changed: 10 additions & 8 deletions
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

Collapse file

‎src/plugins/search/search.js‎

Copy file name to clipboardExpand all lines: src/plugins/search/search.js
+3-3Lines changed: 3 additions & 3 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.