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

Keep line breaks in quoted messages #4356

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- "Disappearing Messages" dialog not reflecting the actual current value #4327
- accessibility: make settings keyboard-navigable #4319
- Fix documentation for --allow-unsafe-core-replacement #4341
- fix missing linebreaks in quotes #4345

<a id="1_48_0"></a>

Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/components/message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ export const Quote = ({
}
disableJumbomoji
nonInteractiveContent
isQuote={true}
/>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/frontend/src/components/message/MessageBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function MessageBody({
text,
disableJumbomoji,
nonInteractiveContent = false,
isQuote = false,
}: {
text: string
disableJumbomoji?: boolean
Expand All @@ -18,6 +19,7 @@ function MessageBody({
* display them as regular text.
*/
nonInteractiveContent?: boolean
isQuote?: boolean
}): JSX.Element {
if (text.length >= UPPER_LIMIT_FOR_PARSED_MESSAGES) {
return <>{text}</>
Expand All @@ -34,7 +36,7 @@ function MessageBody({
)
}
}
return message2React(emojifiedText, nonInteractiveContent)
return message2React(emojifiedText, nonInteractiveContent, isQuote)
}
const trimRegex = /^[\s\uFEFF\xA0\n\t]+|[\s\uFEFF\xA0\n\t]+$/g

Expand Down
75 changes: 71 additions & 4 deletions packages/frontend/src/components/message/MessageMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ SettingsStoreInstance.subscribe(newState => {
}
})

/**
* convert elements to HTML elements
*/
function renderElement(elm: ParsedElement, key?: number): JSX.Element {
switch (elm.t) {
case 'CodeBlock':
Expand Down Expand Up @@ -98,8 +101,11 @@ function renderElement(elm: ParsedElement, key?: number): JSX.Element {
}
}

/** render in preview mode for ChatListItem summary and for quoted messages,
* not interactive (links can not be clicked) just looks more similar to the message in the chatview/message-list */
/**
* render in preview mode for ChatListItem summary,
* not interactive (links can not be clicked) just
* looks more similar to the message in the chatview/message-list
*/
function renderElementPreview(elm: ParsedElement, key?: number): JSX.Element {
switch (elm.t) {
case 'CodeBlock':
Expand Down Expand Up @@ -144,10 +150,71 @@ function renderElementPreview(elm: ParsedElement, key?: number): JSX.Element {
}
}

export function message2React(message: string, preview: boolean): JSX.Element {
/**
* render in preview mode for quoted messages
*
* not interactive (links can not be clicked),
* but line breaks are preserved
*/
function renderElementQuotePreview(
elm: ParsedElement,
key?: number
): JSX.Element {
switch (elm.t) {
case 'CodeBlock':
case 'InlineCode':
return (
<code className='mm-inline-code' key={key}>
{elm.c.content}
</code>
)

case 'StrikeThrough':
return <s key={key}>{elm.c.map(renderElementQuotePreview)}</s>

case 'Italics':
return <i key={key}>{elm.c.map(renderElementQuotePreview)}</i>

case 'Bold':
return <b key={key}>{elm.c.map(renderElementQuotePreview)}</b>

case 'Link':
return <span key={key}>{elm.c.destination.target}</span>

case 'LabeledLink':
return (
<span key={key}>{elm.c.label.map(renderElementQuotePreview)} </span>
)

case 'Linebreak':
return <span key={key}>{'\n'}</span>

case 'Tag':
case 'EmailAddress':
case 'BotCommandSuggestion':
case 'Text':
return <span key={key}>{elm.c}</span>
default:
//@ts-ignore
log.error(`type ${elm.t} not known/implemented yet`, elm)
return (
<div key={key} style={{ color: 'red' }}>
{JSON.stringify(elm)}
</div>
)
}
}

export function message2React(
message: string,
preview: boolean,
quoteView: boolean = false
): JSX.Element {
try {
const elements = parseMessage(message)
return preview ? (
return quoteView ? (
<div className='truncated'>{elements.map(renderElementQuotePreview)}</div>
) : preview ? (
<div className='truncated'>{elements.map(renderElementPreview)}</div>
) : (
<>{elements.map(renderElement)}</>
Expand Down
Loading