forked from lichess-org/lila
-
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.
Merge branch 'master' into fully-show-notation-on-refresh
- Loading branch information
Showing
14 changed files
with
126 additions
and
40 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
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
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
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
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 |
---|---|---|
|
@@ -61,7 +61,7 @@ | |
} | ||
|
||
&__result { | ||
font-weight: normal; | ||
font-weight: bold; | ||
margin: 0 1ch; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* Ported to typescript from https://github.com/marmelab/highlight-search-term/blob/main/src/index.js */ | ||
|
||
export const highlightSearchTerm = (search: string, selector: string): void => { | ||
const highlightName = 'lichess-highlight'; | ||
|
||
console.log(search, CSS.highlights, highlightName); | ||
|
||
if (!CSS.highlights) return; // disable feature on Firefox as it does not support CSS Custom Highlight API | ||
|
||
// remove previous highlight | ||
CSS.highlights.delete(highlightName); | ||
if (!search) { | ||
// nothing to highlight | ||
return; | ||
} | ||
// find all text nodes containing the search term | ||
const ranges: AbstractRange[] = []; | ||
try { | ||
const elements = document.querySelectorAll<HTMLElement>(selector); | ||
Array.from(elements).map(element => { | ||
getTextNodesInElementContainingText(element, search).forEach(node => { | ||
node.parentElement && ranges.push(...getRangesForSearchTermInElement(node.parentElement, search)); | ||
}); | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
if (ranges.length === 0) return; | ||
// create a CSS highlight that can be styled with the ::highlight(search) pseudo-element | ||
const highlight = new Highlight(...ranges); | ||
CSS.highlights.set(highlightName, highlight); | ||
}; | ||
|
||
const getTextNodesInElementContainingText = (element: HTMLElement, text: string) => { | ||
const lowerCaseText = text.toLowerCase(); | ||
const nodes = []; | ||
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT); | ||
let node; | ||
while ((node = walker.nextNode())) { | ||
if (node.textContent?.toLowerCase().includes(lowerCaseText)) { | ||
nodes.push(node); | ||
} | ||
} | ||
return nodes; | ||
}; | ||
|
||
const getRangesForSearchTermInElement = (element: HTMLElement, search: string) => { | ||
const ranges: AbstractRange[] = []; | ||
const lowerCaseSearch = search.toLowerCase(); | ||
if (element.childNodes.length === 0) return ranges; | ||
// In some frameworks like React, when combining static text with dynamic text, the element may have multiple Text child nodes. | ||
// To avoid errors, we must find the child node that actually contains the search term. | ||
const childWithSearchTerm = Array.from(element.childNodes).find(node => | ||
node.textContent?.toLowerCase().includes(lowerCaseSearch), | ||
); | ||
if (!childWithSearchTerm) return ranges; | ||
const text = childWithSearchTerm.textContent?.toLowerCase() || ''; | ||
let start = 0; | ||
let index; | ||
while ((index = text.indexOf(lowerCaseSearch, start)) >= 0) { | ||
const range = new Range(); | ||
range.setStart(childWithSearchTerm, index); | ||
range.setEnd(childWithSearchTerm, index + search.length); | ||
ranges.push(range); | ||
start = index + search.length; | ||
} | ||
return ranges; | ||
}; |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
a { | ||
@extend %base-font, %page-font; | ||
font-weight: normal; | ||
} | ||
|
||
&:hover a { | ||
|
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