forked from the-teacher/rails7-startkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c316aa7
commit 9f5329f
Showing
5 changed files
with
99 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,3 +98,7 @@ section { | |
border: 1px solid rgb(187, 210, 165); | ||
padding: 10px; | ||
} | ||
|
||
em.tsh { | ||
background-color: yellow; | ||
} |
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 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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails | ||
import ArticlesIndex from './articlesIndex' | ||
import TheSearchHighlight from './theSearchHighlight' | ||
|
||
console.log("Hello World! This is `importmap` entry point") | ||
|
||
ArticlesIndex.init() | ||
TheSearchHighlight.init('.the-search-highlight') |
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,82 @@ | ||
// Simple implementation of search results highlighting on FE side | ||
const TheSearchHighlight = (() => { | ||
const SEARCH_WORDS_NAME = 'searchWords' | ||
|
||
const TMP_START_TAG = '!_A_!' | ||
const TMP_END_TAG = '!_Z_!' | ||
|
||
const START_TAG = '<em class="tsh">' | ||
const END_TAG = '</em>' | ||
|
||
let wordsToHighlight = [] | ||
|
||
const prepareSearchKeyword = (searchKeyword) => { | ||
return searchKeyword.trim().replace(/\s\s+/g, ' ') | ||
} | ||
|
||
const getFragmentsToHighlight = (highlightSelector) => | ||
document.querySelectorAll(highlightSelector) | ||
|
||
const getWordsToSearch = (fragment) => { | ||
const searchKeywords = JSON.parse(fragment.dataset[SEARCH_WORDS_NAME]) | ||
return searchKeywords.map((searchKeyword) => { | ||
return prepareSearchKeyword(searchKeyword).split(' ') | ||
}).flat() | ||
} | ||
|
||
const highlighterBuilder = (wordsToSearch) => { | ||
return (str) => { | ||
wordsToSearch.forEach(wordToSearch => { | ||
const regExp = new RegExp(wordToSearch, 'gi') | ||
const matches = [...str.matchAll(regExp)] | ||
const uniqMatchedWords = matches.map((aryItem) => aryItem[0]) | ||
const matchedWords = [...new Set(uniqMatchedWords)] | ||
|
||
if (matchedWords.length) { | ||
matchedWords.forEach((foundWord) => { | ||
const _regExp = new RegExp(foundWord, 'g') | ||
str = str.replaceAll(_regExp, `${TMP_START_TAG}${foundWord}${TMP_END_TAG}`) | ||
}) | ||
} | ||
}) | ||
return str | ||
} | ||
} | ||
|
||
const recursiveChildrenRunner = (element, highlighter) => { | ||
for (const child of element.children) { | ||
recursiveChildrenRunner(child, highlighter) | ||
|
||
for (var element of child.childNodes) { | ||
if (element.nodeType === Node.TEXT_NODE) { | ||
const oldText = element.nodeValue | ||
const newText = highlighter(element.nodeValue) | ||
if (oldText !== newText) { | ||
element.nodeValue = newText | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const highlightFragment = (fragment) => { | ||
const wordsToSearch = getWordsToSearch(fragment) | ||
if (!wordsToSearch.length) { return } | ||
const highlighter = highlighterBuilder(wordsToSearch) | ||
recursiveChildrenRunner(fragment, highlighter) | ||
} | ||
|
||
return { | ||
init: (highlightSelector) => { | ||
const fragments = getFragmentsToHighlight(highlightSelector) | ||
fragments.forEach((fragment) => { | ||
highlightFragment(fragment) | ||
fragment.innerHTML = fragment.innerHTML | ||
.replaceAll(TMP_START_TAG, START_TAG) | ||
.replaceAll(TMP_END_TAG, END_TAG); | ||
}) | ||
} | ||
} | ||
})() | ||
|
||
export default TheSearchHighlight |
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