2018-05-14 20:52:10 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
2019-01-02 19:56:33 +00:00
|
|
|
import { getSizeClass, SizeClassType } from '../../util/emoji';
|
2018-05-18 19:00:46 +00:00
|
|
|
import { Emojify } from './Emojify';
|
2018-05-18 21:48:20 +00:00
|
|
|
import { AddNewLines } from './AddNewLines';
|
|
|
|
import { Linkify } from './Linkify';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
import { Localizer, RenderTextCallback } from '../../types/Util';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
text: string;
|
2018-05-18 21:48:20 +00:00
|
|
|
/** If set, all emoji will be the same size. Otherwise, just one emoji will be large. */
|
2018-05-14 20:52:10 +00:00
|
|
|
disableJumbomoji?: boolean;
|
2018-05-18 21:48:20 +00:00
|
|
|
/** If set, links will be left alone instead of turned into clickable `<a>` tags. */
|
2018-05-14 20:52:10 +00:00
|
|
|
disableLinks?: boolean;
|
2018-05-22 19:31:43 +00:00
|
|
|
i18n: Localizer;
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 21:48:20 +00:00
|
|
|
const renderNewLines: RenderTextCallback = ({
|
|
|
|
text: textWithNewLines,
|
|
|
|
key,
|
|
|
|
}) => <AddNewLines key={key} text={textWithNewLines} />;
|
|
|
|
|
2019-01-02 19:56:33 +00:00
|
|
|
const renderEmoji = ({
|
|
|
|
i18n,
|
|
|
|
text,
|
|
|
|
key,
|
|
|
|
sizeClass,
|
|
|
|
renderNonEmoji,
|
|
|
|
}: {
|
|
|
|
i18n: Localizer;
|
|
|
|
text: string;
|
|
|
|
key: number;
|
|
|
|
sizeClass?: SizeClassType;
|
|
|
|
renderNonEmoji: RenderTextCallback;
|
|
|
|
}) => (
|
|
|
|
<Emojify
|
|
|
|
i18n={i18n}
|
|
|
|
key={key}
|
|
|
|
text={text}
|
|
|
|
sizeClass={sizeClass}
|
|
|
|
renderNonEmoji={renderNonEmoji}
|
|
|
|
/>
|
2018-05-18 21:48:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This component makes it very easy to use all three of our message formatting
|
|
|
|
* components: `Emojify`, `Linkify`, and `AddNewLines`. Because each of them is fully
|
|
|
|
* configurable with their `renderXXX` props, this component will assemble all three of
|
|
|
|
* them for you.
|
|
|
|
*/
|
2018-05-22 19:31:43 +00:00
|
|
|
export class MessageBody extends React.Component<Props> {
|
2018-05-14 20:52:10 +00:00
|
|
|
public render() {
|
2018-05-22 19:31:43 +00:00
|
|
|
const { text, disableJumbomoji, disableLinks, i18n } = this.props;
|
2019-01-02 19:56:33 +00:00
|
|
|
const sizeClass = disableJumbomoji ? undefined : getSizeClass(text);
|
|
|
|
|
|
|
|
if (disableLinks) {
|
|
|
|
return renderEmoji({
|
|
|
|
i18n,
|
|
|
|
text,
|
|
|
|
sizeClass,
|
|
|
|
key: 0,
|
|
|
|
renderNonEmoji: renderNewLines,
|
|
|
|
});
|
|
|
|
}
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-05-18 21:48:20 +00:00
|
|
|
return (
|
2019-01-02 19:56:33 +00:00
|
|
|
<Linkify
|
2018-05-18 21:48:20 +00:00
|
|
|
text={text}
|
2019-01-02 19:56:33 +00:00
|
|
|
renderNonLink={({ key, text: nonLinkText }) => {
|
|
|
|
return renderEmoji({
|
|
|
|
i18n,
|
|
|
|
text: nonLinkText,
|
|
|
|
sizeClass,
|
|
|
|
key,
|
|
|
|
renderNonEmoji: renderNewLines,
|
|
|
|
});
|
|
|
|
}}
|
2018-05-18 21:48:20 +00:00
|
|
|
/>
|
2018-05-14 20:52:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|