2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
import classNames from 'classnames';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { RenderTextCallbackType } from '../../types/Util';
|
2021-06-30 17:00:02 +00:00
|
|
|
import { splitByEmoji } from '../../util/emoji';
|
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SizeClassType } from '../emoji/lib';
|
|
|
|
import { emojiToImage } from '../emoji/lib';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
|
|
|
// Some of this logic taken from emoji-js/replacement
|
2020-11-06 20:11:18 +00:00
|
|
|
// the DOM structure for this getImageTag should match the other emoji implementations:
|
|
|
|
// ts/components/emoji/Emoji.tsx
|
|
|
|
// ts/quill/emoji/blot.tsx
|
2018-05-14 20:52:10 +00:00
|
|
|
function getImageTag({
|
2023-04-10 16:31:45 +00:00
|
|
|
isInvisible,
|
|
|
|
key,
|
2018-05-14 20:52:10 +00:00
|
|
|
match,
|
|
|
|
sizeClass,
|
|
|
|
}: {
|
2023-04-10 16:31:45 +00:00
|
|
|
isInvisible?: boolean;
|
|
|
|
key: string | number;
|
2021-06-30 17:00:02 +00:00
|
|
|
match: string;
|
2019-01-02 19:56:33 +00:00
|
|
|
sizeClass?: SizeClassType;
|
2021-06-30 17:00:02 +00:00
|
|
|
}): JSX.Element | string {
|
|
|
|
const img = emojiToImage(match);
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
if (!img) {
|
2021-06-30 17:00:02 +00:00
|
|
|
return match;
|
2019-06-17 18:46:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
return (
|
|
|
|
<img
|
|
|
|
key={key}
|
2019-07-25 16:28:44 +00:00
|
|
|
src={img}
|
2021-06-30 17:00:02 +00:00
|
|
|
aria-label={match}
|
2023-04-10 16:31:45 +00:00
|
|
|
className={classNames(
|
|
|
|
'emoji',
|
|
|
|
sizeClass,
|
|
|
|
isInvisible ? 'emoji--invisible' : null
|
|
|
|
)}
|
2021-07-02 20:16:55 +00:00
|
|
|
alt={match}
|
2018-05-14 20:52:10 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type Props = {
|
2023-04-10 16:31:45 +00:00
|
|
|
/** When behind a spoiler, this emoji needs to be visibility: hidden */
|
|
|
|
isInvisible?: boolean;
|
2018-05-18 21:48:20 +00:00
|
|
|
/** A class name to be added to the generated emoji images */
|
2019-01-02 19:56:33 +00:00
|
|
|
sizeClass?: SizeClassType;
|
2018-05-18 21:48:20 +00:00
|
|
|
/** Allows you to customize now non-newlines are rendered. Simplest is just a <span>. */
|
2019-01-14 21:49:58 +00:00
|
|
|
renderNonEmoji?: RenderTextCallbackType;
|
2023-04-10 16:31:45 +00:00
|
|
|
text: string;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2022-11-10 04:59:36 +00:00
|
|
|
const defaultRenderNonEmoji: RenderTextCallbackType = ({ text }) => text;
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
export function Emojify({
|
|
|
|
isInvisible,
|
|
|
|
renderNonEmoji = defaultRenderNonEmoji,
|
|
|
|
sizeClass,
|
|
|
|
text,
|
|
|
|
}: Props): JSX.Element {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{splitByEmoji(text).map(({ type, value: match }, index) => {
|
|
|
|
if (type === 'emoji') {
|
|
|
|
return getImageTag({ isInvisible, match, sizeClass, key: index });
|
|
|
|
}
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
if (type === 'text') {
|
|
|
|
return renderNonEmoji({ text: match, key: index });
|
|
|
|
}
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
throw missingCaseError(type);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|