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
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
|
2018-05-18 19:00:46 +00:00
|
|
|
import {
|
|
|
|
findImage,
|
|
|
|
getRegex,
|
|
|
|
getReplacementData,
|
|
|
|
getTitle,
|
|
|
|
} from '../../util/emoji';
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
import { Localizer, RenderTextCallback } from '../../types/Util';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
|
|
|
// Some of this logic taken from emoji-js/replacement
|
|
|
|
function getImageTag({
|
|
|
|
match,
|
|
|
|
sizeClass,
|
|
|
|
key,
|
2018-05-22 19:31:43 +00:00
|
|
|
i18n,
|
2018-05-14 20:52:10 +00:00
|
|
|
}: {
|
|
|
|
match: any;
|
|
|
|
sizeClass: string | undefined;
|
|
|
|
key: string | number;
|
2018-05-22 19:31:43 +00:00
|
|
|
i18n: Localizer;
|
2018-05-14 20:52:10 +00:00
|
|
|
}) {
|
2018-05-18 19:00:46 +00:00
|
|
|
const result = getReplacementData(match[0], match[1], match[2]);
|
2018-05-14 20:52:10 +00:00
|
|
|
|
|
|
|
if (is.string(result)) {
|
|
|
|
return <span key={key}>{match[0]}</span>;
|
|
|
|
}
|
|
|
|
|
2018-05-18 19:00:46 +00:00
|
|
|
const img = findImage(result.value, result.variation);
|
|
|
|
const title = getTitle(result.value);
|
2018-05-14 20:52:10 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<img
|
|
|
|
key={key}
|
|
|
|
src={img.path}
|
2018-05-22 19:31:43 +00:00
|
|
|
alt={i18n('emojiAlt', [title || ''])}
|
|
|
|
className={classNames('emoji', sizeClass)}
|
2018-05-14 20:52:10 +00:00
|
|
|
data-codepoints={img.full_idx}
|
|
|
|
title={`:${title}:`}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
text: string;
|
2018-05-18 21:48:20 +00:00
|
|
|
/** A class name to be added to the generated emoji images */
|
|
|
|
sizeClass?: '' | 'small' | 'medium' | 'large' | 'jumbo';
|
|
|
|
/** Allows you to customize now non-newlines are rendered. Simplest is just a <span>. */
|
|
|
|
renderNonEmoji?: RenderTextCallback;
|
2018-05-22 19:31:43 +00:00
|
|
|
i18n: Localizer;
|
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> = {
|
|
|
|
renderNonEmoji: ({ text, key }) => <span key={key}>{text}</span>,
|
|
|
|
};
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
public render() {
|
2018-05-22 19:31:43 +00:00
|
|
|
const { text, sizeClass, renderNonEmoji, i18n } = this.props;
|
2018-05-14 20:52:10 +00:00
|
|
|
const results: Array<any> = [];
|
2018-05-18 19:00:46 +00:00
|
|
|
const regex = getRegex();
|
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
|
|
|
}
|
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
results.push(getImageTag({ match, sizeClass, key: count++, i18n }));
|
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
|
|
|
}
|
|
|
|
}
|