-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVE] Respect convert ascii to emoji preference for new message t…
…emplate (#27038)
- Loading branch information
1 parent
c2e93cc
commit 9d42405
Showing
5 changed files
with
90 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,28 @@ | ||
import { MessageEmoji, ThreadMessageEmoji } from '@rocket.chat/fuselage'; | ||
import type * as MessageParser from '@rocket.chat/message-parser'; | ||
import { ReactElement, useMemo, useContext, memo } from 'react'; | ||
|
||
import { MarkupInteractionContext } from '../MarkupInteractionContext'; | ||
import PlainSpan from '../elements/PlainSpan'; | ||
import EmojiRenderer from './EmojiRenderer'; | ||
|
||
type EmojiProps = MessageParser.Emoji & { | ||
big?: boolean; | ||
preview?: boolean; | ||
}; | ||
|
||
const Emoji = ({ big = false, preview = false, ...emoji }: EmojiProps): ReactElement => { | ||
const { detectEmoji } = useContext(MarkupInteractionContext); | ||
const { convertAsciiToEmoji } = useContext(MarkupInteractionContext); | ||
|
||
const fallback = useMemo(() => ('unicode' in emoji ? emoji.unicode : `:${emoji.shortCode ?? emoji.value.value}:`), [emoji]); | ||
const asciiEmoji = useMemo( | ||
() => ('shortCode' in emoji && emoji.value.value !== emoji.shortCode ? emoji.value.value : undefined), | ||
[emoji], | ||
); | ||
|
||
const descriptors = useMemo(() => { | ||
const detected = detectEmoji?.(fallback); | ||
return detected?.length !== 0 ? detected : undefined; | ||
}, [detectEmoji, fallback]); | ||
if (!convertAsciiToEmoji && asciiEmoji) { | ||
return <PlainSpan text={asciiEmoji} />; | ||
} | ||
|
||
return ( | ||
<> | ||
{descriptors?.map(({ name, className, image, content }, i) => ( | ||
<span key={i} title={name}> | ||
{preview ? ( | ||
<ThreadMessageEmoji className={className} name={name} image={image}> | ||
{content} | ||
</ThreadMessageEmoji> | ||
) : ( | ||
<MessageEmoji big={big} className={className} name={name} image={image}> | ||
{content} | ||
</MessageEmoji> | ||
)} | ||
</span> | ||
)) ?? ( | ||
<span role='img' aria-label={fallback.charAt(0) === ':' ? fallback : undefined}> | ||
{fallback} | ||
</span> | ||
)} | ||
</> | ||
); | ||
return <EmojiRenderer big={big} preview={preview} {...emoji} />; | ||
}; | ||
|
||
export default memo(Emoji); |
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,45 @@ | ||
import { MessageEmoji, ThreadMessageEmoji } from '@rocket.chat/fuselage'; | ||
import type * as MessageParser from '@rocket.chat/message-parser'; | ||
import { ReactElement, useMemo, useContext, memo } from 'react'; | ||
|
||
import { MarkupInteractionContext } from '../MarkupInteractionContext'; | ||
|
||
type EmojiProps = MessageParser.Emoji & { | ||
big?: boolean; | ||
preview?: boolean; | ||
}; | ||
|
||
const EmojiRenderer = ({ big = false, preview = false, ...emoji }: EmojiProps): ReactElement => { | ||
const { detectEmoji } = useContext(MarkupInteractionContext); | ||
|
||
const fallback = useMemo(() => ('unicode' in emoji ? emoji.unicode : `:${emoji.shortCode ?? emoji.value.value}:`), [emoji]); | ||
|
||
const descriptors = useMemo(() => { | ||
const detected = detectEmoji?.(fallback); | ||
return detected?.length !== 0 ? detected : undefined; | ||
}, [detectEmoji, fallback]); | ||
|
||
return ( | ||
<> | ||
{descriptors?.map(({ name, className, image, content }, i) => ( | ||
<span key={i} title={name}> | ||
{preview ? ( | ||
<ThreadMessageEmoji className={className} name={name} image={image}> | ||
{content} | ||
</ThreadMessageEmoji> | ||
) : ( | ||
<MessageEmoji big={big} className={className} name={name} image={image}> | ||
{content} | ||
</MessageEmoji> | ||
)} | ||
</span> | ||
)) ?? ( | ||
<span role='img' aria-label={fallback.charAt(0) === ':' ? fallback : undefined}> | ||
{fallback} | ||
</span> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(EmojiRenderer); |