getStringFromNode: Add parent/nextSibling for fewer newlines

This commit is contained in:
Scott Nonnenberg 2023-07-14 11:09:02 -07:00 committed by Fedor Indutnyy
parent b6445a6af0
commit 3f399b1329
3 changed files with 30 additions and 10 deletions

View file

@ -741,8 +741,14 @@ export class Message extends React.PureComponent<Props, State> {
}
private canRenderStickerLikeEmoji(): boolean {
const { text, quote, storyReplyContext, attachments, previews } =
this.props;
const {
attachments,
bodyRanges,
previews,
quote,
storyReplyContext,
text,
} = this.props;
return Boolean(
text &&
@ -751,6 +757,7 @@ export class Message extends React.PureComponent<Props, State> {
!quote &&
!storyReplyContext &&
(!attachments || !attachments.length) &&
(!bodyRanges || !bodyRanges.length) &&
(!previews || !previews.length)
);
}

View file

@ -58,6 +58,8 @@ export function MessageBodyReadMore({
BUFFER
);
const disableJumbomoji = bodyRanges?.length ? true : undefined;
const onIncreaseTextLength = hasReadMore
? () => {
messageExpanded(id, maxLength + INCREMENT_COUNT);
@ -68,6 +70,7 @@ export function MessageBodyReadMore({
<MessageBody
bodyRanges={bodyRanges}
direction={direction}
disableJumbomoji={disableJumbomoji}
disableLinks={disableLinks}
i18n={i18n}
isSpoilerExpanded={isSpoilerExpanded}

View file

@ -56,7 +56,11 @@ export function createEventHandler({
};
}
function getStringFromNode(node: Node): string {
function getStringFromNode(
node: Node,
parent?: Node,
nextSibling?: Node
): string {
if (node.nodeType === Node.TEXT_NODE) {
if (node.textContent === QUILL_EMBED_GUARD) {
return '';
@ -75,20 +79,26 @@ function getStringFromNode(node: Node): string {
) {
return element.ariaLabel || '';
}
if (element.nodeName === 'BR') {
if (nextSibling && element.nodeName === 'BR') {
return '\n';
}
if (element.childNodes.length === 0) {
const childCount = element.childNodes.length;
if (childCount === 0) {
return element.textContent || '';
}
let result = '';
for (const child of element.childNodes) {
result += getStringFromNode(child);
for (let i = 0; i < childCount; i += 1) {
const child = element.childNodes[i];
const nextChild = element.childNodes[i + 1];
result += getStringFromNode(child, node, nextChild);
}
if (
element.nodeName === 'P' ||
element.nodeName === 'DIV' ||
element.nodeName === 'TIME'
parent &&
parent.childNodes.length > 1 &&
(element.nodeName === 'P' ||
element.nodeName === 'DIV' ||
element.nodeName === 'TIME')
) {
if (result.length > 0 && !result.endsWith('\n\n')) {
result += '\n';