-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
139 additions
and
142 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
apps/meteor/client/components/message/MessageBodyRender/BigEmoji.tsx
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
apps/meteor/client/components/message/MessageBodyRender/Heading.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
apps/meteor/client/components/message/MessageBodyRender/MessageBodyRender.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as MessageParser from '@rocket.chat/message-parser'; | ||
import React, { memo, MouseEvent, ReactElement } from 'react'; | ||
|
||
import Code from './Code'; | ||
import OrderedList from './OrderedList'; | ||
import Quote from './Quote'; | ||
import TaskList from './TaskList'; | ||
import UnorderedList from './UnorderedList'; | ||
import BigEmojiBlock from './blocks/BigEmojiBlock'; | ||
import HeadingBlock from './blocks/HeadingBlock'; | ||
import ParagraphBlock from './blocks/ParagraphBlock'; | ||
import { MessageBodyContext } from './contexts/MessageBodyContext'; | ||
import { ChannelMention } from './definitions/ChannelMention'; | ||
import { UserMention } from './definitions/UserMention'; | ||
|
||
type MessageBodyRenderProps = { | ||
tokens: MessageParser.MarkdownAST; | ||
mentions: UserMention[]; | ||
channels: ChannelMention[]; | ||
onUserMentionClick?: (username: string) => (e: MouseEvent<HTMLDivElement>) => void; | ||
onChannelMentionClick?: (id: string) => (e: MouseEvent<HTMLDivElement>) => void; | ||
isThreadPreview?: boolean; | ||
}; | ||
|
||
const MessageBodyRender = ({ | ||
tokens, | ||
mentions = [], | ||
channels = [], | ||
onUserMentionClick, | ||
onChannelMentionClick, | ||
isThreadPreview, | ||
}: MessageBodyRenderProps): ReactElement => ( | ||
<MessageBodyContext.Provider value={{ mentions, channels, onUserMentionClick, onChannelMentionClick, isThreadPreview }}> | ||
{tokens.map((block, index) => { | ||
switch (block.type) { | ||
case 'BIG_EMOJI': | ||
return <BigEmojiBlock key={index} emojis={block.value} isThreadPreview={isThreadPreview} />; | ||
|
||
case 'PARAGRAPH': | ||
return <ParagraphBlock key={index} chunks={block.value} />; | ||
|
||
case 'HEADING': | ||
return <HeadingBlock key={index} chunks={block.value} level={block.level} />; | ||
|
||
case 'UNORDERED_LIST': | ||
return <UnorderedList key={index} value={block.value} />; | ||
|
||
case 'ORDERED_LIST': | ||
return <OrderedList key={index} value={block.value} />; | ||
|
||
case 'TASKS': | ||
return <TaskList key={index} value={block.value} />; | ||
|
||
case 'QUOTE': | ||
return <Quote key={index} value={block.value} />; | ||
|
||
case 'CODE': | ||
return <Code key={index} {...block} />; | ||
|
||
case 'LINE_BREAK': | ||
return <br key={index} />; | ||
|
||
default: | ||
return null; | ||
} | ||
})} | ||
</MessageBodyContext.Provider> | ||
); | ||
|
||
export default memo(MessageBodyRender); |
12 changes: 0 additions & 12 deletions
12
apps/meteor/client/components/message/MessageBodyRender/Paragraph.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
apps/meteor/client/components/message/MessageBodyRender/blocks/BigEmojiBlock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as MessageParser from '@rocket.chat/message-parser'; | ||
import React, { ReactElement } from 'react'; | ||
|
||
import MessageEmoji from '../../MessageEmoji'; | ||
|
||
type BigEmojiBlockProps = { | ||
emojis: MessageParser.Emoji[]; | ||
isThreadPreview?: boolean; | ||
}; | ||
|
||
const BigEmojiBlock = ({ emojis, isThreadPreview }: BigEmojiBlockProps): ReactElement => ( | ||
<> | ||
{emojis.map(({ value: { value: handle } }, index) => ( | ||
<MessageEmoji key={index} handle={handle} big isThreadPreview={isThreadPreview} /> | ||
))} | ||
</> | ||
); | ||
|
||
export default BigEmojiBlock; |
23 changes: 23 additions & 0 deletions
23
apps/meteor/client/components/message/MessageBodyRender/blocks/HeadingBlock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as MessageParser from '@rocket.chat/message-parser'; | ||
import React, { ReactElement } from 'react'; | ||
|
||
import PlainText from '../PlainText'; | ||
|
||
type HeadingBlockProps = { | ||
chunks?: MessageParser.Plain[]; | ||
level?: 1 | 2 | 3 | 4; | ||
}; | ||
|
||
const HeadingBlock = ({ chunks = [], level = 1 }: HeadingBlockProps): ReactElement => { | ||
const HeadingTag = `h${level}` as const; | ||
|
||
return ( | ||
<HeadingTag> | ||
{chunks.map((block, index) => ( | ||
<PlainText key={index} value={block.value} /> | ||
))} | ||
</HeadingTag> | ||
); | ||
}; | ||
|
||
export default HeadingBlock; |
16 changes: 16 additions & 0 deletions
16
apps/meteor/client/components/message/MessageBodyRender/blocks/ParagraphBlock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as MessageParser from '@rocket.chat/message-parser'; | ||
import React, { ReactElement } from 'react'; | ||
|
||
import Inline from '../Inline'; | ||
|
||
type ParagraphBlockProps = { | ||
chunks: MessageParser.Inlines[]; | ||
}; | ||
|
||
const ParagraphBlock = ({ chunks: value = [] }: ParagraphBlockProps): ReactElement => ( | ||
<p> | ||
<Inline value={value} /> | ||
</p> | ||
); | ||
|
||
export default ParagraphBlock; |
80 changes: 1 addition & 79 deletions
80
apps/meteor/client/components/message/MessageBodyRender/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1 @@ | ||
import { BigEmoji as ASTBigEmoji, MarkdownAST } from '@rocket.chat/message-parser'; | ||
import React, { FC, memo, MouseEvent } from 'react'; | ||
|
||
import BigEmoji from './BigEmoji'; | ||
import Code from './Code'; | ||
import Heading from './Heading'; | ||
import OrderedList from './OrderedList'; | ||
import Paragraph from './Paragraph'; | ||
import Quote from './Quote'; | ||
import TaskList from './TaskList'; | ||
import UnorderedList from './UnorderedList'; | ||
import { MessageBodyContext } from './contexts/MessageBodyContext'; | ||
import { ChannelMention } from './definitions/ChannelMention'; | ||
import { UserMention } from './definitions/UserMention'; | ||
|
||
type BodyProps = { | ||
tokens: MarkdownAST; | ||
mentions: UserMention[]; | ||
channels: ChannelMention[]; | ||
onUserMentionClick?: (username: string) => (e: MouseEvent<HTMLDivElement>) => void; | ||
onChannelMentionClick?: (id: string) => (e: MouseEvent<HTMLDivElement>) => void; | ||
isThreadPreview?: boolean; | ||
}; | ||
|
||
const isBigEmoji = (tokens: MarkdownAST): tokens is [ASTBigEmoji] => tokens.length === 1 && tokens[0].type === 'BIG_EMOJI'; | ||
|
||
const MessageBodyRender: FC<BodyProps> = ({ | ||
tokens, | ||
mentions = [], | ||
channels = [], | ||
onUserMentionClick, | ||
onChannelMentionClick, | ||
isThreadPreview, | ||
}) => { | ||
if (isBigEmoji(tokens)) { | ||
return <BigEmoji value={tokens[0].value} isThreadPreview={isThreadPreview} />; | ||
} | ||
|
||
return ( | ||
<MessageBodyContext.Provider value={{ mentions, channels, onUserMentionClick, onChannelMentionClick, isThreadPreview }}> | ||
{tokens.map((block, index) => { | ||
if (block.type === 'UNORDERED_LIST') { | ||
return <UnorderedList value={block.value} key={index} />; | ||
} | ||
|
||
if (block.type === 'QUOTE') { | ||
return <Quote value={block.value} key={index} />; | ||
} | ||
if (block.type === 'TASKS') { | ||
return <TaskList value={block.value} key={index} />; | ||
} | ||
|
||
if (block.type === 'ORDERED_LIST') { | ||
return <OrderedList value={block.value} key={index} />; | ||
} | ||
|
||
if (block.type === 'PARAGRAPH') { | ||
return <Paragraph value={block.value} key={index} />; | ||
} | ||
|
||
if (block.type === 'CODE') { | ||
return <Code {...block} key={index} />; | ||
} | ||
|
||
if (block.type === 'HEADING') { | ||
return <Heading value={block.value} level={block.level} key={index} />; | ||
} | ||
|
||
if (block.type === 'LINE_BREAK') { | ||
return <br key={index} />; | ||
} | ||
|
||
return null; | ||
})} | ||
</MessageBodyContext.Provider> | ||
); | ||
}; | ||
|
||
export default memo(MessageBodyRender); | ||
export { default } from './MessageBodyRender'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters