-
Notifications
You must be signed in to change notification settings - Fork 0
Highlight whitespaces on renderer #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
50932c6
start to implement whitespaces highlighter
64ce28b
reformat code point for whitespaces
1b25058
refactor code
b56024c
fix checking spaces in the end of the string
af1adbe
refactoring and delete unnecessary highlighting
cbedbc3
replace fulltext highlighting logic to app, change logic fot whitespa…
41c9634
change logic for nested elements
3bb454e
refactor code
21c462e
rename functions and files and refactor code
fb9e11c
rename function
1bf8cc3
Merge remote-tracking branch 'origin/main' into feature/highlight-whi…
ed61d07
delete wrapper for highlighting
4c84dfe
replace all logic for highlighting fulltext here
5c7d495
refactor code
bf0d521
refactor code
6e9e5b2
Merge remote-tracking branch 'origin/main' into feature/highlight-whi…
52b361e
refactor code
f7cb5d9
refactor code
d385e66
add commet to code
a53b440
fix logic for whitespaces
0e238e1
add version to changelog and rename file
05abd47
refactor code
8e6bf9c
delete test from library
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,5 @@ | ||
| .renderer-whitespace { | ||
|
yanabel1996 marked this conversation as resolved.
|
||
| color: var(--slate-smoke); | ||
| -webkit-user-select: none; | ||
| user-select: none; | ||
| } | ||
This file contains hidden or 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,16 +1,19 @@ | ||
| import React from 'react'; | ||
| import React, { useMemo } from 'react'; | ||
| import { visualizeUnicodeChildren } from '../../utils/visualization/visualizeWhitespaces'; | ||
|
|
||
| // Renderer wraper | ||
| export function RendererWrapper({ | ||
| children, | ||
| component: Component = 'span', // Default tag, can be overloaded | ||
| ...rest | ||
| }) { | ||
| const HighlightedChildren = useMemo(() => visualizeUnicodeChildren(children), [children]); | ||
|
|
||
| return ( | ||
| <Component | ||
| {...rest} | ||
| > | ||
| {children} | ||
| {HighlightedChildren} | ||
| </Component> | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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,71 @@ | ||
| import React from 'react'; | ||
| import { highlightSearchedFulltexts } from './highlightSearchedFulltexts.js'; | ||
|
|
||
| const isPrimitiveHighlightValue = (value) => ( | ||
| value != null && (typeof value === 'string' || typeof value === 'number' || typeof value === 'bigint') | ||
| ); | ||
|
|
||
| const highlightText = (text, fulltextHighlightTerms) => ( | ||
| highlightSearchedFulltexts(String(text), fulltextHighlightTerms) | ||
| ); | ||
|
|
||
| // Apply fulltext highlight to children (string, whitespace node array, or nested elements) | ||
| export const highlightChildren = (children, fulltextHighlightTerms, dataValue) => { | ||
| if (!fulltextHighlightTerms?.length) { | ||
| return children; | ||
| } | ||
|
|
||
| if (typeof children === 'string' || typeof children === 'number') { | ||
| return highlightText(children, fulltextHighlightTerms); | ||
| } | ||
|
|
||
| // Whitespace renderer output: apply fulltext to string runs, keep unicode markers as-is | ||
| if (Array.isArray(children)) { | ||
| return children.flatMap((node) => { | ||
| if (typeof node === 'string' || typeof node === 'number') { | ||
| const highlighted = highlightText(node, fulltextHighlightTerms); | ||
| return Array.isArray(highlighted) ? highlighted : [highlighted]; | ||
| } | ||
|
|
||
| if (!React.isValidElement(node)) { | ||
| return [node]; | ||
| } | ||
|
|
||
| const highlightedNode = highlightChildren( | ||
| node, | ||
| fulltextHighlightTerms, | ||
| dataValue, | ||
| ); | ||
|
|
||
| return Array.isArray(highlightedNode) ? highlightedNode : [highlightedNode]; | ||
| }); | ||
| } | ||
|
|
||
| if (React.isValidElement(children)) { | ||
| const nestedDataValue = children.props?.['data-value'] ?? dataValue; | ||
|
|
||
| if (typeof children.type === 'string' || children.type === React.Fragment) { | ||
| return React.cloneElement( | ||
| children, | ||
| children.props, | ||
| highlightChildren( | ||
| children.props?.children, | ||
| fulltextHighlightTerms, | ||
| nestedDataValue, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| if (isPrimitiveHighlightValue(nestedDataValue)) { | ||
| return highlightText(nestedDataValue, fulltextHighlightTerms); | ||
| } | ||
|
|
||
| return children; | ||
| } | ||
|
|
||
| if (isPrimitiveHighlightValue(dataValue)) { | ||
| return highlightText(dataValue, fulltextHighlightTerms); | ||
| } | ||
|
|
||
| return children; | ||
| }; |
This file contains hidden or 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,51 @@ | ||
| import React from 'react'; | ||
|
|
||
| import './highlightSearchedFulltexts.scss'; | ||
|
|
||
| // Regex to escape special characters to prevent the app to crash in search value | ||
| const ESCAPE_REGEX = /[.*+?^${}()|[\]\\]/g; | ||
|
|
||
| // Normalize search terms | ||
| const normalizeSearchTerms = (searchValue) => { | ||
| if (Array.isArray(searchValue)) { | ||
| return searchValue | ||
| .map((term) => String(term).trim()) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| if (typeof searchValue === 'string') { | ||
| return searchValue | ||
| .trim() | ||
| .split(/\s+/) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| return []; | ||
| }; | ||
|
|
||
| // Highlight searched fulltexts in the text by replacing matched terms with a span | ||
| export const highlightSearchedFulltexts = (text, searchValue) => { | ||
| const sourceText = String(text ?? ''); | ||
| const terms = normalizeSearchTerms(searchValue); | ||
|
|
||
| if (terms.length === 0 || sourceText === '') { | ||
| return sourceText; | ||
| } | ||
|
|
||
| const escapedTerms = [...new Set(terms.map((term) => term.replace(ESCAPE_REGEX, '\\$&')))] | ||
| .sort((a, b) => b.length - a.length); | ||
| const matchRegex = new RegExp(`(${escapedTerms.join('|')})`, 'gi'); | ||
| const highlightedTerms = new Set(terms.map((term) => term.toLowerCase())); | ||
|
|
||
| return sourceText.split(matchRegex).map((part, index) => { | ||
| if (highlightedTerms.has(part.toLowerCase())) { | ||
| return ( | ||
| <span key={`${part}-${index}`} className='search-text-highlight'> | ||
| {part} | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| return part; | ||
| }); | ||
| }; |
This file contains hidden or 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,11 @@ | ||
| .search-text-highlight { | ||
| [data-bs-theme=dark] & { | ||
| color: var(--black); | ||
| background-color: rgba(var(--light-honey-rgb), 0.9); | ||
| } | ||
|
|
||
| [data-bs-theme=light] & { | ||
| color: var(--fiery-sunset); | ||
| background-color: rgba(var(--bright-orange-rgb), 0.25); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation