signal-desktop/ts/components/conversation/Emojify.tsx

102 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import classNames from 'classnames';
import emojiRegex from 'emoji-regex';
import { RenderTextCallbackType } from '../../types/Util';
import { emojiToImage, SizeClassType } from '../emoji/lib';
// Some of this logic taken from emoji-js/replacement
// the DOM structure for this getImageTag should match the other emoji implementations:
// ts/components/emoji/Emoji.tsx
// ts/quill/emoji/blot.tsx
function getImageTag({
match,
sizeClass,
key,
}: {
2020-09-14 19:51:27 +00:00
match: RegExpExecArray;
2019-01-02 19:56:33 +00:00
sizeClass?: SizeClassType;
key: string | number;
}) {
const img = emojiToImage(match[0]);
if (!img) {
return match[0];
}
return (
<img
key={key}
src={img}
aria-label={match[0]}
className={classNames('emoji', sizeClass)}
title={match[0]}
/>
);
}
export type Props = {
text: string;
/** A class name to be added to the generated emoji images */
2019-01-02 19:56:33 +00:00
sizeClass?: SizeClassType;
/** Allows you to customize now non-newlines are rendered. Simplest is just a <span>. */
2019-01-14 21:49:58 +00:00
renderNonEmoji?: RenderTextCallbackType;
};
export class Emojify extends React.Component<Props> {
public static defaultProps: Partial<Props> = {
renderNonEmoji: ({ text }) => text,
};
2020-09-14 19:51:27 +00:00
public render():
| JSX.Element
| string
| null
| Array<JSX.Element | string | null> {
const { text, sizeClass, renderNonEmoji } = this.props;
2020-09-14 19:51:27 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const results: Array<any> = [];
const regex = emojiRegex();
// 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;
}
let match = regex.exec(text);
let last = 0;
let count = 1;
if (!match) {
return renderNonEmoji({ text, key: 0 });
}
while (match) {
if (last < match.index) {
const textWithNoEmoji = text.slice(last, match.index);
2020-09-14 19:51:27 +00:00
count += 1;
results.push(renderNonEmoji({ text: textWithNoEmoji, key: count }));
}
2020-09-14 19:51:27 +00:00
count += 1;
results.push(getImageTag({ match, sizeClass, key: count }));
last = regex.lastIndex;
match = regex.exec(text);
}
if (last < text.length) {
2020-09-14 19:51:27 +00:00
count += 1;
results.push(renderNonEmoji({ text: text.slice(last), key: count }));
}
return results;
}
}