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
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
import emojiRegex from 'emoji-regex';
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2019-06-27 20:35:21 +00:00
|
|
|
import { RenderTextCallbackType } from '../../types/Util';
|
2019-07-25 16:28:44 +00:00
|
|
|
import { emojiToImage, SizeClassType } from '../emoji/lib';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
|
|
|
// Some of this logic taken from emoji-js/replacement
|
|
|
|
function getImageTag({
|
|
|
|
match,
|
|
|
|
sizeClass,
|
|
|
|
key,
|
|
|
|
}: {
|
|
|
|
match: any;
|
2019-01-02 19:56:33 +00:00
|
|
|
sizeClass?: SizeClassType;
|
2018-05-14 20:52:10 +00:00
|
|
|
key: string | number;
|
|
|
|
}) {
|
2019-07-25 16:28:44 +00:00
|
|
|
const img = emojiToImage(match[0]);
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
if (!img) {
|
2019-06-17 18:46:42 +00:00
|
|
|
return match[0];
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
return (
|
2018-07-09 21:29:13 +00:00
|
|
|
// tslint:disable-next-line react-a11y-img-has-alt
|
2018-05-14 20:52:10 +00:00
|
|
|
<img
|
|
|
|
key={key}
|
2019-07-25 16:28:44 +00:00
|
|
|
src={img}
|
2019-06-27 20:35:21 +00:00
|
|
|
aria-label={match[0]}
|
2018-05-22 19:31:43 +00:00
|
|
|
className={classNames('emoji', sizeClass)}
|
2019-06-27 20:35:21 +00:00
|
|
|
title={match[0]}
|
2018-05-14 20:52:10 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
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;
|
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
|
|
|
};
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
public render() {
|
2019-06-27 20:35:21 +00:00
|
|
|
const { text, sizeClass, renderNonEmoji } = this.props;
|
2018-05-14 20:52:10 +00:00
|
|
|
const results: Array<any> = [];
|
2019-07-25 16:28:44 +00:00
|
|
|
const regex = emojiRegex();
|
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) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-18 19:00:46 +00:00
|
|
|
let match = regex.exec(text);
|
2018-05-14 20:52:10 +00:00
|
|
|
let last = 0;
|
|
|
|
let count = 1;
|
|
|
|
|
|
|
|
if (!match) {
|
2018-05-18 21:48:20 +00:00
|
|
|
return renderNonEmoji({ text, key: 0 });
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (match) {
|
|
|
|
if (last < match.index) {
|
|
|
|
const textWithNoEmoji = text.slice(last, match.index);
|
2018-05-18 21:48:20 +00:00
|
|
|
results.push(renderNonEmoji({ text: textWithNoEmoji, key: count++ }));
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 20:35:21 +00:00
|
|
|
results.push(getImageTag({ match, sizeClass, key: count++ }));
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-05-18 19:00:46 +00:00
|
|
|
last = regex.lastIndex;
|
|
|
|
match = regex.exec(text);
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (last < text.length) {
|
2018-05-18 21:48:20 +00:00
|
|
|
results.push(renderNonEmoji({ text: text.slice(last), key: count++ }));
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 21:48:20 +00:00
|
|
|
return results;
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
}
|