2022-03-03 20:23:10 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-10-20 20:46:42 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-03-03 20:23:10 +00:00
|
|
|
import React from 'react';
|
2021-10-20 20:46:42 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Props as MessageBodyPropsType } from './MessageBody';
|
|
|
|
import { MessageBody } from './MessageBody';
|
2021-10-20 20:46:42 +00:00
|
|
|
|
|
|
|
export type Props = Pick<
|
|
|
|
MessageBodyPropsType,
|
|
|
|
| 'direction'
|
|
|
|
| 'text'
|
|
|
|
| 'textPending'
|
|
|
|
| 'disableLinks'
|
|
|
|
| 'i18n'
|
|
|
|
| 'bodyRanges'
|
|
|
|
| 'openConversation'
|
|
|
|
> & {
|
2021-11-11 23:45:47 +00:00
|
|
|
id: string;
|
|
|
|
displayLimit?: number;
|
|
|
|
messageExpanded: (id: string, displayLimit: number) => unknown;
|
2021-10-20 20:46:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const INITIAL_LENGTH = 800;
|
|
|
|
const INCREMENT_COUNT = 3000;
|
2021-11-29 15:42:26 +00:00
|
|
|
const BUFFER = 100;
|
2021-10-20 20:46:42 +00:00
|
|
|
|
2021-12-06 21:52:47 +00:00
|
|
|
export function doesMessageBodyOverflow(str: string): boolean {
|
|
|
|
return str.length > INITIAL_LENGTH + BUFFER;
|
|
|
|
}
|
|
|
|
|
2021-10-20 20:46:42 +00:00
|
|
|
function graphemeAwareSlice(
|
|
|
|
str: string,
|
|
|
|
length: number
|
|
|
|
): {
|
|
|
|
hasReadMore: boolean;
|
|
|
|
text: string;
|
|
|
|
} {
|
2021-11-29 15:42:26 +00:00
|
|
|
if (str.length <= length + BUFFER) {
|
2021-10-20 20:46:42 +00:00
|
|
|
return { text: str, hasReadMore: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
let text: string | undefined;
|
|
|
|
|
|
|
|
for (const { index } of new Intl.Segmenter().segment(str)) {
|
|
|
|
if (!text && index >= length) {
|
|
|
|
text = str.slice(0, index);
|
|
|
|
}
|
|
|
|
if (text && index > length) {
|
|
|
|
return {
|
|
|
|
text,
|
|
|
|
hasReadMore: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
text: str,
|
|
|
|
hasReadMore: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function MessageBodyReadMore({
|
|
|
|
bodyRanges,
|
|
|
|
direction,
|
|
|
|
disableLinks,
|
2021-11-11 23:45:47 +00:00
|
|
|
displayLimit,
|
2021-10-20 20:46:42 +00:00
|
|
|
i18n,
|
2021-11-11 23:45:47 +00:00
|
|
|
id,
|
|
|
|
messageExpanded,
|
2021-10-20 20:46:42 +00:00
|
|
|
openConversation,
|
|
|
|
text,
|
|
|
|
textPending,
|
|
|
|
}: Props): JSX.Element {
|
2021-11-11 23:45:47 +00:00
|
|
|
const maxLength = displayLimit || INITIAL_LENGTH;
|
2021-10-20 20:46:42 +00:00
|
|
|
|
|
|
|
const { hasReadMore, text: slicedText } = graphemeAwareSlice(text, maxLength);
|
|
|
|
|
|
|
|
const onIncreaseTextLength = hasReadMore
|
|
|
|
? () => {
|
2021-11-11 23:45:47 +00:00
|
|
|
messageExpanded(id, maxLength + INCREMENT_COUNT);
|
2021-10-20 20:46:42 +00:00
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MessageBody
|
|
|
|
bodyRanges={bodyRanges}
|
|
|
|
disableLinks={disableLinks}
|
|
|
|
direction={direction}
|
|
|
|
i18n={i18n}
|
|
|
|
onIncreaseTextLength={onIncreaseTextLength}
|
|
|
|
openConversation={openConversation}
|
|
|
|
text={slicedText}
|
|
|
|
textPending={textPending}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|