Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added truncation logic for text type message in rooms #2110

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"slate-history": "0.93.0",
"slate-react": "0.98.4",
"tippy.js": "6.3.7",
"truncate-html": "1.1.2",
"ua-parser-js": "1.0.35"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions src/app/components/message/MsgTypeRenderers.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { style } from '@vanilla-extract/css';

export const buttonStyle = style({
width: '15%',
color: '#5e9ecf',
backgroundColor: 'transparent',
border: 'none',
padding: '0',
textDecoration: 'underline',
cursor: 'pointer',
transition: 'color 0.3s, text-decoration 0.3s',

selectors: {
'&:hover': {
color: '#1e6e8c',
textDecoration: 'none',
backgroundColor: 'transparent',
},
},
});
36 changes: 33 additions & 3 deletions src/app/components/message/MsgTypeRenderers.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactNode } from 'react';
import React, { ReactNode, useState } from 'react';
import { Box, Chip, Icon, Icons, Text, toRem } from 'folds';
import { IContent } from 'matrix-js-sdk';
import { JUMBO_EMOJI_REG, URL_REG } from '../../utils/regex';
Expand Down Expand Up @@ -27,6 +27,11 @@ import { FALLBACK_MIMETYPE, getBlobSafeMimeType } from '../../utils/mimeTypes';
import { parseGeoUri, scaleYDimension } from '../../utils/common';
import { Attachment, AttachmentBox, AttachmentContent, AttachmentHeader } from './attachment';
import { FileHeader } from './FileHeader';
import { Button } from 'folds';
import { buttonStyle } from './MsgTypeRenderers.css';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pardon me for this silly mistake.

import truncateHtml from 'truncate-html';

const CHARACTER_LIMIT = 750;

export function MBadEncrypted() {
return (
Expand Down Expand Up @@ -63,6 +68,11 @@ export function BrokenContent() {
);
}

const truncateText = (text: string, limit: number) => {
if (text.length <= limit) return text;
return `${text.slice(0, limit).trim()}...`;
};

type RenderBodyProps = {
body: string;
customBody?: string;
Expand All @@ -80,6 +90,14 @@ export function MText({ edited, content, renderBody, renderUrlsPreview }: MTextP
const trimmedBody = trimReplyFromBody(body);
const urlsMatch = renderUrlsPreview && trimmedBody.match(URL_REG);
const urls = urlsMatch ? [...new Set(urlsMatch)] : undefined;
const [isExpanded, setIsExpanded] = useState(false);

const shouldTruncate =
trimmedBody.length > 750 || (typeof customBody === 'string' && customBody.length > 750);
const finalContent = isExpanded ? trimmedBody : truncateText(trimmedBody, CHARACTER_LIMIT);
const customFinalContent = isExpanded
? (customBody as string)
: truncateHtml(customBody as string, CHARACTER_LIMIT);

return (
<>
Expand All @@ -88,12 +106,24 @@ export function MText({ edited, content, renderBody, renderUrlsPreview }: MTextP
jumboEmoji={JUMBO_EMOJI_REG.test(trimmedBody)}
>
{renderBody({
body: trimmedBody,
customBody: typeof customBody === 'string' ? customBody : undefined,
body: finalContent,
customBody: typeof customBody === 'string' ? customFinalContent : undefined,
})}
{edited && <MessageEditedContent />}
</MessageTextBody>
{renderUrlsPreview && urls && urls.length > 0 && renderUrlsPreview(urls)}

{shouldTruncate && (
<Button
size="400"
fill="None"
radii="0"
className={buttonStyle}
onClick={() => setIsExpanded(!isExpanded)}
>
{isExpanded ? 'Show Less' : 'Show More'}
</Button>
)}
</>
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,20 @@ textarea {
color: inherit;
word-spacing: inherit;
}

.show-more {
cursor: pointer;
color: #5e9ecf;
padding: 5px;
border-radius: 5px;
text-decoration: underline;

}

.show-more:hover {
color: #085fa7;
}

.noselect {
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
Expand Down
Loading