2018-05-14 20:52:10 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
|
2018-05-18 19:00:46 +00:00
|
|
|
import {
|
|
|
|
findImage,
|
|
|
|
getRegex,
|
|
|
|
getReplacementData,
|
|
|
|
getTitle,
|
|
|
|
} from '../../util/emoji';
|
2018-05-14 20:52:10 +00:00
|
|
|
import { AddNewLines } from './AddNewLines';
|
|
|
|
|
|
|
|
// Some of this logic taken from emoji-js/replacement
|
|
|
|
function getImageTag({
|
|
|
|
match,
|
|
|
|
sizeClass,
|
|
|
|
key,
|
|
|
|
}: {
|
|
|
|
match: any;
|
|
|
|
sizeClass: string | undefined;
|
|
|
|
key: string | number;
|
|
|
|
}) {
|
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}
|
|
|
|
className={classnames('emoji', sizeClass)}
|
|
|
|
data-codepoints={img.full_idx}
|
|
|
|
title={`:${title}:`}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
text: string;
|
|
|
|
sizeClass?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Emojify extends React.Component<Props, {}> {
|
|
|
|
public render() {
|
|
|
|
const { text, sizeClass } = this.props;
|
|
|
|
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 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) {
|
|
|
|
return <AddNewLines text={text} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (match) {
|
|
|
|
if (last < match.index) {
|
|
|
|
const textWithNoEmoji = text.slice(last, match.index);
|
|
|
|
results.push(<AddNewLines key={count++} text={textWithNoEmoji} />);
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(getImageTag({ match, sizeClass, key: count++ }));
|
|
|
|
|
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) {
|
|
|
|
results.push(<AddNewLines key={count++} text={text.slice(last)} />);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <span>{results}</span>;
|
|
|
|
}
|
|
|
|
}
|