2021-10-20 20:46:42 +00:00
|
|
|
// Copyright 2018-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { KeyboardEvent } from 'react';
|
|
|
|
import React from 'react';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SizeClassType } from '../emoji/lib';
|
|
|
|
import { getSizeClass } from '../emoji/lib';
|
2020-09-16 22:42:48 +00:00
|
|
|
import { AtMentionify } from './AtMentionify';
|
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
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2020-09-16 22:42:48 +00:00
|
|
|
BodyRangesType,
|
|
|
|
LocalizerType,
|
|
|
|
RenderTextCallbackType,
|
|
|
|
} from '../../types/Util';
|
|
|
|
|
|
|
|
type OpenConversationActionType = (
|
|
|
|
conversationId: string,
|
|
|
|
messageId?: string
|
|
|
|
) => void;
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type Props = {
|
2020-09-16 22:42:48 +00:00
|
|
|
direction?: 'incoming' | 'outgoing';
|
2018-05-14 20:52:10 +00:00
|
|
|
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;
|
2020-09-16 22:42:48 +00:00
|
|
|
bodyRanges?: BodyRangesType;
|
2021-10-20 20:46:42 +00:00
|
|
|
onIncreaseTextLength?: () => unknown;
|
2020-09-16 22:42:48 +00:00
|
|
|
openConversation?: OpenConversationActionType;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-05-14 20:52:10 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2021-10-20 20:46:42 +00:00
|
|
|
export function MessageBody({
|
|
|
|
bodyRanges,
|
|
|
|
direction,
|
|
|
|
disableJumbomoji,
|
|
|
|
disableLinks,
|
|
|
|
i18n,
|
|
|
|
onIncreaseTextLength,
|
|
|
|
openConversation,
|
|
|
|
text,
|
|
|
|
textPending,
|
|
|
|
}: Props): JSX.Element {
|
|
|
|
const hasReadMore = Boolean(onIncreaseTextLength);
|
|
|
|
const textWithSuffix = textPending || hasReadMore ? `${text}...` : text;
|
|
|
|
|
|
|
|
const sizeClass = disableJumbomoji ? undefined : getSizeClass(text);
|
|
|
|
const processedText = AtMentionify.preprocessMentions(
|
|
|
|
textWithSuffix,
|
|
|
|
bodyRanges
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderNewLines: RenderTextCallbackType = ({
|
2020-09-16 22:42:48 +00:00
|
|
|
text: textWithNewLines,
|
|
|
|
key,
|
|
|
|
}) => {
|
|
|
|
return (
|
|
|
|
<AddNewLines
|
|
|
|
key={key}
|
|
|
|
text={textWithNewLines}
|
2021-10-20 20:46:42 +00:00
|
|
|
renderNonNewLine={({ text: innerText, 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}
|
2021-10-20 20:46:42 +00:00
|
|
|
text={innerText}
|
2020-09-16 22:42:48 +00:00
|
|
|
bodyRanges={bodyRanges}
|
|
|
|
openConversation={openConversation}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-10-20 20:46:42 +00:00
|
|
|
return (
|
2022-03-23 00:12:06 +00:00
|
|
|
<span>
|
2021-10-20 20:46:42 +00:00
|
|
|
{disableLinks ? (
|
2019-03-13 20:38:28 +00:00
|
|
|
renderEmoji({
|
|
|
|
i18n,
|
2021-10-20 20:46:42 +00:00
|
|
|
text: processedText,
|
2019-03-13 20:38:28 +00:00
|
|
|
sizeClass,
|
|
|
|
key: 0,
|
2021-10-20 20:46:42 +00:00
|
|
|
renderNonEmoji: renderNewLines,
|
2019-03-13 20:38:28 +00:00
|
|
|
})
|
2021-10-20 20:46:42 +00:00
|
|
|
) : (
|
|
|
|
<Linkify
|
|
|
|
text={processedText}
|
|
|
|
renderNonLink={({ key, text: nonLinkText }) => {
|
|
|
|
return renderEmoji({
|
|
|
|
i18n,
|
|
|
|
text: nonLinkText,
|
|
|
|
sizeClass,
|
|
|
|
key,
|
|
|
|
renderNonEmoji: renderNewLines,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{textPending ? (
|
|
|
|
<span className="MessageBody__highlight"> {i18n('downloading')}</span>
|
|
|
|
) : null}
|
|
|
|
{onIncreaseTextLength ? (
|
|
|
|
<button
|
|
|
|
className="MessageBody__read-more"
|
|
|
|
onClick={() => {
|
|
|
|
onIncreaseTextLength();
|
|
|
|
}}
|
|
|
|
onKeyDown={(ev: KeyboardEvent) => {
|
|
|
|
if (ev.key === 'Space' || ev.key === 'Enter') {
|
|
|
|
onIncreaseTextLength();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
tabIndex={0}
|
|
|
|
type="button"
|
|
|
|
>
|
|
|
|
{' '}
|
|
|
|
{i18n('MessageBody--read-more')}
|
|
|
|
</button>
|
|
|
|
) : null}
|
|
|
|
</span>
|
|
|
|
);
|
2018-05-14 20:52:10 +00:00
|
|
|
}
|