This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD - Affliction icon besides text (#231)
Signed-off-by: RaenonX <[email protected]>
- Loading branch information
Showing
13 changed files
with
285 additions
and
5 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
1 change: 1 addition & 0 deletions
1
src/components/elements/markdown/transformers/text/icon/const.ts
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 @@ | ||
export const IDENTIFIER_SEPARATOR = '|'; |
5 changes: 5 additions & 0 deletions
5
src/components/elements/markdown/transformers/text/icon/main.module.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
src/components/elements/markdown/transformers/text/icon/main.module.css.map
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
src/components/elements/markdown/transformers/text/icon/main.module.scss
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,5 @@ | ||
img { | ||
&.afflictionIcon { | ||
margin: 0 0.2rem; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/components/elements/markdown/transformers/text/icon/main.tsx
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,42 @@ | ||
import React from 'react'; | ||
|
||
import {DepotPaths} from '../../../../../../api-def/resources'; | ||
import {AppReactContext} from '../../../../../../context/app/main'; | ||
import {useI18n} from '../../../../../../i18n/hook'; | ||
import {Image} from '../../../../common/image'; | ||
import {TextComponentProps} from '../types'; | ||
import {IDENTIFIER_SEPARATOR} from './const'; | ||
import styles from './main.module.css'; | ||
import {IconType} from './types'; | ||
|
||
|
||
export const Icon = ({children}: TextComponentProps) => { | ||
const {lang} = useI18n(); | ||
const context = React.useContext(AppReactContext); | ||
|
||
const [type, identifier] = children.split(IDENTIFIER_SEPARATOR, 2); | ||
|
||
if (type === IconType.AFFLICTION && context?.resources.afflictions.status.length) { | ||
const statusEnum = context?.resources.afflictions.status.find((entry) => entry.name === identifier); | ||
|
||
if (!statusEnum) { | ||
// Identifier invalid / not found | ||
return <>{children}</>; | ||
} | ||
|
||
if (!statusEnum.imagePath) { | ||
// No image to show | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<Image | ||
text={statusEnum.trans[lang]} | ||
src={DepotPaths.getImageURL(statusEnum.imagePath)} | ||
className={styles.afflictionIcon} | ||
/> | ||
); | ||
} | ||
|
||
return <>{children}</>; | ||
}; |
3 changes: 3 additions & 0 deletions
3
src/components/elements/markdown/transformers/text/icon/types.ts
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,3 @@ | ||
export const IconType = { | ||
AFFLICTION: 'aff', | ||
}; |
9 changes: 9 additions & 0 deletions
9
src/components/elements/markdown/transformers/text/icon/utils.ts
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,9 @@ | ||
import {EnumEntry} from '../../../../../../api-def/resources'; | ||
import {iconSyntax} from '../syntax'; | ||
import {IDENTIFIER_SEPARATOR} from './const'; | ||
import {IconType} from './types'; | ||
|
||
|
||
export const makeAfflictionIconMarkdown = (affliction: EnumEntry) => { | ||
return `${iconSyntax.start}${IconType.AFFLICTION}${IDENTIFIER_SEPARATOR}${affliction.name}${iconSyntax.end}`; | ||
}; |
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
72 changes: 72 additions & 0 deletions
72
src/components/elements/markdown/transformers/text/utils.test.ts
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,72 @@ | ||
import {expect} from '@jest/globals'; | ||
|
||
import {SupportedLanguages} from '../../../../../api-def/api'; | ||
import {StatusEnums} from '../../../../../api-def/resources'; | ||
import {makeAfflictionIconMarkdown} from './icon/utils'; | ||
import {injectMarkdownToText} from './utils'; | ||
|
||
|
||
describe('Text injection', () => { | ||
const afflictions: StatusEnums['status'] = [ | ||
{ | ||
name: 'POISON', | ||
code: 1, | ||
imagePath: 'poison.png', | ||
trans: { | ||
[SupportedLanguages.CHT]: 'Poison CHT', | ||
[SupportedLanguages.EN]: 'Poison', | ||
[SupportedLanguages.JP]: 'Poison JP', | ||
}, | ||
}, | ||
]; | ||
|
||
it('does not inject anything', async () => { | ||
const injected = injectMarkdownToText(SupportedLanguages.EN, 'Text', {afflictions}); | ||
|
||
expect(injected).toBe('Text'); | ||
}); | ||
|
||
it('injects affliction icon before affliction text', async () => { | ||
const injected = injectMarkdownToText(SupportedLanguages.EN, 'Poison', {afflictions}); | ||
|
||
expect(injected).toBe(`${makeAfflictionIconMarkdown(afflictions[0])}Poison`); | ||
}); | ||
|
||
it('injects affliction icon in a sentence', async () => { | ||
const injected = injectMarkdownToText(SupportedLanguages.EN, 'Afflicts Poison', {afflictions}); | ||
|
||
expect(injected).toBe(`Afflicts ${makeAfflictionIconMarkdown(afflictions[0])}Poison`); | ||
}); | ||
|
||
it('injects affliction icon in a continuous word', async () => { | ||
const injected = injectMarkdownToText(SupportedLanguages.EN, 'ApplyPoison', {afflictions}); | ||
|
||
expect(injected).toBe(`Apply${makeAfflictionIconMarkdown(afflictions[0])}Poison`); | ||
}); | ||
|
||
it('injects affliction icon case-insensitively', async () => { | ||
const injected = injectMarkdownToText(SupportedLanguages.EN, 'Afflicts poison', {afflictions}); | ||
|
||
expect(injected).toBe(`Afflicts ${makeAfflictionIconMarkdown(afflictions[0])}Poison`); | ||
}); | ||
|
||
it('does not inject affliction icon if already exists', async () => { | ||
const injected = injectMarkdownToText( | ||
SupportedLanguages.EN, | ||
`${makeAfflictionIconMarkdown(afflictions[0])}Poison`, | ||
{afflictions}, | ||
); | ||
|
||
expect(injected).toBe(`${makeAfflictionIconMarkdown(afflictions[0])}Poison`); | ||
}); | ||
|
||
it('does not inject affliction icon if already exists (precedes with space)', async () => { | ||
const injected = injectMarkdownToText( | ||
SupportedLanguages.EN, | ||
`${makeAfflictionIconMarkdown(afflictions[0])} Poison`, | ||
{afflictions}, | ||
); | ||
|
||
expect(injected).toBe(`${makeAfflictionIconMarkdown(afflictions[0])} Poison`); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/components/elements/markdown/transformers/text/utils.ts
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,24 @@ | ||
import {SupportedLanguages} from '../../../../../api-def/api'; | ||
import {StatusEnums} from '../../../../../api-def/resources'; | ||
import {IDENTIFIER_SEPARATOR} from './icon/const'; | ||
import {makeAfflictionIconMarkdown} from './icon/utils'; | ||
import {iconSyntax} from './syntax'; | ||
|
||
|
||
type Resources = { | ||
afflictions?: StatusEnums['status'], | ||
} | ||
|
||
export const injectMarkdownToText = (lang: SupportedLanguages, text: string, resources: Resources): string => { | ||
// Affliction icons | ||
if (resources.afflictions?.length) { | ||
resources.afflictions.forEach((entry) => { | ||
text = text.replace( | ||
new RegExp(`(^|([^${iconSyntax.end}${IDENTIFIER_SEPARATOR} ]) ?)${entry.trans[lang]}`, 'gi'), | ||
`$1${makeAfflictionIconMarkdown(entry)}${entry.trans[lang]}`, | ||
); | ||
}); | ||
} | ||
|
||
return text; | ||
}; |
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