signal-desktop/ts/components/conversation/MessageBody.tsx

144 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import { getSizeClass, SizeClassType } from '../emoji/lib';
2020-09-16 22:42:48 +00:00
import { AtMentionify } from './AtMentionify';
import { Emojify } from './Emojify';
import { AddNewLines } from './AddNewLines';
import { Linkify } from './Linkify';
2020-09-16 22:42:48 +00:00
import {
BodyRangesType,
LocalizerType,
RenderTextCallbackType,
} from '../../types/Util';
type OpenConversationActionType = (
conversationId: string,
messageId?: string
) => void;
export type Props = {
2020-09-16 22:42:48 +00:00
direction?: 'incoming' | 'outgoing';
text: string;
textPending?: boolean;
/** If set, all emoji will be the same size. Otherwise, just one emoji will be large. */
disableJumbomoji?: boolean;
/** If set, links will be left alone instead of turned into clickable `<a>` tags. */
disableLinks?: boolean;
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
2020-09-16 22:42:48 +00:00
bodyRanges?: BodyRangesType;
openConversation?: OpenConversationActionType;
};
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}
/>
);
/**
* 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.
*/
export class MessageBody extends React.Component<Props> {
2020-09-16 22:42:48 +00:00
private readonly renderNewLines: RenderTextCallbackType = ({
text: textWithNewLines,
key,
}) => {
const { bodyRanges, direction, openConversation } = this.props;
return (
<AddNewLines
key={key}
text={textWithNewLines}
2020-09-25 16:59:45 +00:00
renderNonNewLine={({ text, key: innerKey }) => (
2020-09-16 22:42:48 +00:00
<AtMentionify
2020-09-25 16:59:45 +00:00
key={innerKey}
2020-09-16 22:42:48 +00:00
direction={direction}
text={text}
bodyRanges={bodyRanges}
openConversation={openConversation}
/>
)}
/>
);
};
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>
);
}
2020-09-14 19:51:27 +00:00
public render(): JSX.Element {
const {
2020-09-16 22:42:48 +00:00
bodyRanges,
text,
textPending,
disableJumbomoji,
disableLinks,
i18n,
} = this.props;
2019-01-02 19:56:33 +00:00
const sizeClass = disableJumbomoji ? undefined : getSizeClass(text);
2020-09-16 22:42:48 +00:00
const textWithPending = AtMentionify.preprocessMentions(
textPending ? `${text}...` : text,
bodyRanges
);
2019-01-02 19:56:33 +00:00
if (disableLinks) {
return this.addDownloading(
renderEmoji({
i18n,
text: textWithPending,
sizeClass,
key: 0,
2020-09-16 22:42:48 +00:00
renderNonEmoji: this.renderNewLines,
})
);
2019-01-02 19:56:33 +00:00
}
return this.addDownloading(
2019-01-02 19:56:33 +00:00
<Linkify
text={textWithPending}
2019-01-02 19:56:33 +00:00
renderNonLink={({ key, text: nonLinkText }) => {
return renderEmoji({
i18n,
text: nonLinkText,
sizeClass,
key,
2020-09-16 22:42:48 +00:00
renderNonEmoji: this.renderNewLines,
2019-01-02 19:56:33 +00:00
});
}}
/>
);
}
}