-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
14 changed files
with
2,311 additions
and
2,121 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
.coverage | ||
public/script.js |
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,16 +1,3 @@ | ||
{ | ||
"extends": ["./node_modules/@adhamu/zero/eslint/typescript-react"], | ||
"overrides": [ | ||
{ | ||
"files": ["./setupTests.ts"], | ||
"rules": { | ||
"import/no-extraneous-dependencies": [ | ||
"error", | ||
{ | ||
"devDependencies": true | ||
} | ||
] | ||
} | ||
} | ||
] | ||
"extends": ["./node_modules/@adhamu/zero/eslint"] | ||
} |
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 +1 @@ | ||
16.14 | ||
18.13 |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,54 +1,57 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import type { ReactElement } from 'react' | ||
import React, { Children, cloneElement } from 'react' | ||
|
||
import reactStringReplace from 'react-string-replace' | ||
|
||
export const getElementText = (node: React.ReactNode): string | undefined => { | ||
if (['string', 'number'].includes(typeof node)) { | ||
return node as string | ||
} | ||
|
||
if (node instanceof Array) { | ||
return [...new Set(node.map(getElementText))].join(' ') | ||
} | ||
|
||
if (typeof node === 'object' && node) { | ||
return getElementText((node as ReactElement).props.children) | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
const highlightKeyword = (content: string, keyword: string) => | ||
reactStringReplace(content, keyword, (match, key) => ( | ||
<mark key={key}>{match}</mark> | ||
)) | ||
|
||
const cloneChildren = (children: ReactElement[], keyword: string): any => | ||
Children.map(children, child => | ||
child.props | ||
? cloneElement(child, { | ||
children: highlightKeyword( | ||
cloneChildren(child.props.children, keyword), | ||
keyword | ||
), | ||
}) | ||
: child | ||
) | ||
|
||
export const wrapElementText = ( | ||
node: React.ReactNode, | ||
keyword: string | ||
): ReactElement | React.ReactNode => { | ||
const n = node as React.ReactElement | ||
const { | ||
props: { children }, | ||
} = n | ||
|
||
return React.cloneElement(n, { | ||
children: | ||
typeof children === 'string' | ||
? highlightKeyword(children, keyword) | ||
: cloneChildren(children, keyword), | ||
}) | ||
export const elementText = { | ||
get: (node: React.ReactNode): string | undefined => { | ||
if (['string', 'number'].includes(typeof node)) { | ||
return node as string | ||
} | ||
|
||
if (node instanceof Array) { | ||
return [...new Set(node.map(elementText.get))].join(' ') | ||
} | ||
|
||
if (typeof node === 'object' && node) { | ||
return elementText.get((node as ReactElement).props.children) | ||
} | ||
|
||
return undefined | ||
}, | ||
|
||
highlightKeyword: (content: string, keyword: string) => | ||
reactStringReplace(content, keyword, (match, key) => ( | ||
<mark key={key}>{match}</mark> | ||
)), | ||
|
||
cloneChildren: (children: ReactElement[], keyword: string): any => | ||
Children.map(children, child => | ||
child.props | ||
? cloneElement(child, { | ||
children: elementText.highlightKeyword( | ||
elementText.cloneChildren(child.props.children, keyword), | ||
keyword | ||
), | ||
}) | ||
: child | ||
), | ||
|
||
wrap: ( | ||
node: React.ReactNode, | ||
keyword: string | ||
): ReactElement | React.ReactNode => { | ||
const n = node as React.ReactElement | ||
const { | ||
props: { children }, | ||
} = n | ||
|
||
return React.cloneElement(n, { | ||
children: | ||
typeof children === 'string' | ||
? elementText.highlightKeyword(children, keyword) | ||
: elementText.cloneChildren(children, keyword), | ||
}) | ||
}, | ||
} |
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
Oops, something went wrong.