Skip to content

Commit

Permalink
feat: more reactive local search and suitable color
Browse files Browse the repository at this point in the history
refer to #21
  • Loading branch information
sshwy committed Jul 9, 2021
1 parent 51f3ba1 commit 06638fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion source/css/_partial/search.styl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
color: var(--link-hover-color);

span.search-key-word
color: red;
color: var(--link-hover-color);
font-weight: bold;

.search-post-tags
Expand Down
20 changes: 18 additions & 2 deletions source/js/module/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ function renderSearchData (keyword, counterNode, resultNode) {
}
}

var initializing_search_data = false;
var search_status = 'none';

function initializeSearchData () {
if (initializing_search_data) return;

initializing_search_data = true;

if (window.searchData === undefined) {
fetch('/search.json')
.then(res => res.json())
Expand All @@ -61,6 +68,7 @@ function bindEvent () {

const searchCloseBtn = document.querySelector('.header-inner .search .search-close-icon');
searchCloseBtn.addEventListener('click', () => searchClose('desktop', true), false);

const mobileSearchCloseBtn = document.querySelector('.header-inner .mobile-search .search-close-icon');
mobileSearchCloseBtn.addEventListener('click', () => searchClose('mobile', true), false);

Expand All @@ -70,6 +78,8 @@ function bindEvent () {
});
}
function searchOpen (type) {
search_status = type;

if (type === 'mobile') {
headerDiv.classList.add('mobile-search-active');
searchBox.classList.add('active', 'mobile');
Expand All @@ -81,6 +91,8 @@ function searchOpen (type) {
if (!window.searchData) initializeSearchData();
}
function searchClose (type, clearAll) {
search_status = 'none';

if (type === 'mobile') {
headerDiv.classList.remove('mobile-search-active');
searchBox.classList.remove('active', 'mobile');
Expand All @@ -102,6 +114,8 @@ function searchClose (type, clearAll) {
}

function searchSubmit (e) {
if (search_status === 'none') searchOpen('desktop');

if (e && e.keyCode === 27) {
searchClose('desktop', false);
e.target.blur();
Expand All @@ -110,11 +124,13 @@ function searchSubmit (e) {
if (window.searchData) {
renderSearchData(str, searchCounter, searchResult);
} else {
console.error('searchData not defined!');
initializeSearchData();
}
}

function mobileSearchSubmit (e) {
if (search_status === 'none') searchOpen('mobile');

if (e && e.keyCode === 27) {
searchClose('mobile', false);
e.target.blur();
Expand All @@ -123,7 +139,7 @@ function mobileSearchSubmit (e) {
if (window.searchData) {
renderSearchData(str, searchCounter, searchResult);
} else {
console.error('searchData not defined!');
initializeSearchData();
}
}

Expand Down

0 comments on commit 06638fc

Please sign in to comment.