2023-01-03 11:55:46 -08:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 15:34:04 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2018-05-14 13:52:10 -07:00
|
|
|
import React from 'react';
|
2021-10-26 14:15:33 -05:00
|
|
|
import type { RenderTextCallbackType } from '../../types/Util';
|
2021-06-30 10:00:02 -07:00
|
|
|
import { splitByEmoji } from '../../util/emoji';
|
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
2025-03-26 12:35:32 -07:00
|
|
|
import { FunInlineEmoji } from '../fun/FunEmoji';
|
|
|
|
import {
|
|
|
|
getEmojiVariantByKey,
|
|
|
|
getEmojiVariantKeyByValue,
|
|
|
|
isEmojiVariantValue,
|
2025-03-28 16:01:27 -07:00
|
|
|
isEmojiVariantValueNonQualified,
|
2025-03-26 12:35:32 -07:00
|
|
|
} from '../fun/data/emojis';
|
2025-06-16 11:59:31 -07:00
|
|
|
import { createLogger } from '../../logging/log';
|
2025-04-09 11:10:54 -07:00
|
|
|
import { useFunEmojiLocalizer } from '../fun/useFunEmojiLocalizer';
|
2018-05-14 13:52:10 -07:00
|
|
|
|
2025-06-16 11:59:31 -07:00
|
|
|
const log = createLogger('Emojify');
|
|
|
|
|
2021-01-14 12:07:05 -06:00
|
|
|
export type Props = {
|
2025-03-26 12:35:32 -07:00
|
|
|
fontSizeOverride?: number | null;
|
|
|
|
text: string;
|
2023-04-10 09:31:45 -07:00
|
|
|
/** When behind a spoiler, this emoji needs to be visibility: hidden */
|
|
|
|
isInvisible?: boolean;
|
2018-05-18 14:48:20 -07:00
|
|
|
/** Allows you to customize now non-newlines are rendered. Simplest is just a <span>. */
|
2019-01-14 13:49:58 -08:00
|
|
|
renderNonEmoji?: RenderTextCallbackType;
|
2021-01-14 12:07:05 -06:00
|
|
|
};
|
2018-05-14 13:52:10 -07:00
|
|
|
|
2022-11-09 20:59:36 -08:00
|
|
|
const defaultRenderNonEmoji: RenderTextCallbackType = ({ text }) => text;
|
2018-05-18 14:48:20 -07:00
|
|
|
|
2023-04-12 16:17:56 -07:00
|
|
|
export function Emojify({
|
2025-03-26 12:35:32 -07:00
|
|
|
fontSizeOverride,
|
2023-04-12 16:17:56 -07:00
|
|
|
text,
|
2025-03-26 12:35:32 -07:00
|
|
|
renderNonEmoji = defaultRenderNonEmoji,
|
2023-04-12 16:17:56 -07:00
|
|
|
}: Props): JSX.Element {
|
2025-04-09 11:10:54 -07:00
|
|
|
const emojiLocalizer = useFunEmojiLocalizer();
|
2023-04-12 16:17:56 -07:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{splitByEmoji(text).map(({ type, value: match }, index) => {
|
|
|
|
if (type === 'emoji') {
|
2025-03-28 16:01:27 -07:00
|
|
|
// If we don't recognize the emoji, render it as text.
|
|
|
|
if (!isEmojiVariantValue(match)) {
|
|
|
|
log.error(`Found emoji that we did not recognize: ${match}`);
|
|
|
|
return renderNonEmoji({ text: match, key: index });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render emoji as text if they are a non-qualified emoji value.
|
|
|
|
if (isEmojiVariantValueNonQualified(match)) {
|
|
|
|
return renderNonEmoji({ text: match, key: index });
|
|
|
|
}
|
2025-03-26 12:35:32 -07:00
|
|
|
|
|
|
|
const variantKey = getEmojiVariantKeyByValue(match);
|
|
|
|
const variant = getEmojiVariantByKey(variantKey);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FunInlineEmoji
|
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
key={index}
|
|
|
|
role="img"
|
2025-04-29 16:24:14 -07:00
|
|
|
aria-label={emojiLocalizer.getLocaleShortName(variantKey)}
|
2025-03-26 12:35:32 -07:00
|
|
|
emoji={variant}
|
|
|
|
size={fontSizeOverride}
|
|
|
|
/>
|
|
|
|
);
|
2023-04-12 16:17:56 -07:00
|
|
|
}
|
2018-05-14 13:52:10 -07:00
|
|
|
|
2023-04-12 16:17:56 -07:00
|
|
|
if (type === 'text') {
|
|
|
|
return renderNonEmoji({ text: match, key: index });
|
|
|
|
}
|
2018-05-14 13:52:10 -07:00
|
|
|
|
2023-04-12 16:17:56 -07:00
|
|
|
throw missingCaseError(type);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
2018-05-14 13:52:10 -07:00
|
|
|
}
|