2018-05-14 20:52:10 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
import { getSizeClass, SizeClassType } from '../emoji/lib';
|
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
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
import { LocalizerType, RenderTextCallbackType } from '../../types/Util';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
text: string;
|
2019-03-13 20:38:28 +00:00
|
|
|
textPending?: boolean;
|
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;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
const renderNewLines: RenderTextCallbackType = ({
|
2018-05-18 21:48:20 +00:00
|
|
|
text: textWithNewLines,
|
|
|
|
key,
|
|
|
|
}) => <AddNewLines key={key} text={textWithNewLines} />;
|
|
|
|
|
2019-01-02 19:56:33 +00:00
|
|
|
const renderEmoji = ({
|
|
|
|
text,
|
|
|
|
key,
|
|
|
|
sizeClass,
|
|
|
|
renderNonEmoji,
|
|
|
|
}: {
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2019-01-02 19:56:33 +00:00
|
|
|
text: string;
|
|
|
|
key: number;
|
|
|
|
sizeClass?: SizeClassType;
|
2019-01-14 21:49:58 +00:00
|
|
|
renderNonEmoji: RenderTextCallbackType;
|
2019-01-02 19:56:33 +00:00
|
|
|
}) => (
|
|
|
|
<Emojify
|
|
|
|
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> {
|
2019-03-13 20:38:28 +00:00
|
|
|
public addDownloading(jsx: JSX.Element): JSX.Element {
|
|
|
|
const { i18n, textPending } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{jsx}
|
|
|
|
{textPending ? (
|
|
|
|
<span className="module-message-body__highlight">
|
|
|
|
{' '}
|
|
|
|
{i18n('downloading')}
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
public render() {
|
2019-03-13 20:38:28 +00:00
|
|
|
const {
|
|
|
|
text,
|
|
|
|
textPending,
|
|
|
|
disableJumbomoji,
|
|
|
|
disableLinks,
|
|
|
|
i18n,
|
|
|
|
} = this.props;
|
2019-01-02 19:56:33 +00:00
|
|
|
const sizeClass = disableJumbomoji ? undefined : getSizeClass(text);
|
2019-03-13 20:38:28 +00:00
|
|
|
const textWithPending = textPending ? `${text}...` : text;
|
2019-01-02 19:56:33 +00:00
|
|
|
|
|
|
|
if (disableLinks) {
|
2019-03-13 20:38:28 +00:00
|
|
|
return this.addDownloading(
|
|
|
|
renderEmoji({
|
|
|
|
i18n,
|
|
|
|
text: textWithPending,
|
|
|
|
sizeClass,
|
|
|
|
key: 0,
|
|
|
|
renderNonEmoji: renderNewLines,
|
|
|
|
})
|
|
|
|
);
|
2019-01-02 19:56:33 +00:00
|
|
|
}
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2019-03-13 20:38:28 +00:00
|
|
|
return this.addDownloading(
|
2019-01-02 19:56:33 +00:00
|
|
|
<Linkify
|
2019-03-13 20:38:28 +00:00
|
|
|
text={textWithPending}
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|