2021-11-12 23:44:20 +00:00
|
|
|
// Copyright 2018-2021 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({
|
|
|
|
match,
|
|
|
|
sizeClass,
|
|
|
|
key,
|
|
|
|
}: {
|
2021-06-30 17:00:02 +00:00
|
|
|
match: string;
|
2019-01-02 19:56:33 +00:00
|
|
|
sizeClass?: SizeClassType;
|
2018-05-14 20:52:10 +00:00
|
|
|
key: string | number;
|
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}
|
2018-05-22 19:31:43 +00:00
|
|
|
className={classNames('emoji', sizeClass)}
|
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 = {
|
2018-05-14 20:52:10 +00:00
|
|
|
text: string;
|
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;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
export class Emojify extends React.Component<Props> {
|
2018-05-18 21:48:20 +00:00
|
|
|
public static defaultProps: Partial<Props> = {
|
2018-06-27 20:53:49 +00:00
|
|
|
renderNonEmoji: ({ text }) => text,
|
2018-05-18 21:48:20 +00:00
|
|
|
};
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
public override render(): null | Array<JSX.Element | string | null> {
|
2019-06-27 20:35:21 +00:00
|
|
|
const { text, sizeClass, renderNonEmoji } = this.props;
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-05-18 21:48:20 +00:00
|
|
|
// We have to do this, because renderNonEmoji is not required in our Props object,
|
|
|
|
// but it is always provided via defaultProps.
|
|
|
|
if (!renderNonEmoji) {
|
2020-09-14 19:51:27 +00:00
|
|
|
return null;
|
2018-05-18 21:48:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:00:02 +00:00
|
|
|
return splitByEmoji(text).map(({ type, value: match }, index) => {
|
|
|
|
if (type === 'emoji') {
|
|
|
|
return getImageTag({ match, sizeClass, key: index });
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:00:02 +00:00
|
|
|
if (type === 'text') {
|
|
|
|
return renderNonEmoji({ text: match, key: index });
|
|
|
|
}
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2021-06-30 17:00:02 +00:00
|
|
|
throw missingCaseError(type);
|
|
|
|
});
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
}
|