Renamed folder, add @rocket.chat/message-parser, migrate some files to TypeScript

This commit is contained in:
Gerzon Z 2021-09-21 18:42:01 -04:00
parent 7a88d8df62
commit 20d2db9aa3
23 changed files with 126 additions and 131 deletions

View File

@ -1,19 +0,0 @@
/* eslint-disable react/no-array-index-key */
import React from 'react';
import { View } from 'react-native';
import PropTypes from 'prop-types';
import Emoji from './Emoji';
const BigEmoji = ({ value }) => (
<View style={{ flexDirection: 'row' }}>
{value.map((block, index) => <Emoji key={index} emojiHandle={`:${ block.value.value }:`} isBigEmoji />)}
</View>
);
BigEmoji.propTypes = {
value: PropTypes.object
};
export default BigEmoji;

View File

@ -1,15 +0,0 @@
import React from 'react';
import { Text } from 'react-native';
import PropTypes from 'prop-types';
const Plain = ({ value }) => (
<Text accessibilityLabel={value}>
{value}
</Text>
);
Plain.propTypes = {
value: PropTypes.string
};
export default Plain;

View File

@ -4,6 +4,7 @@ import { Node, Parser } from 'commonmark';
import Renderer from 'commonmark-react-renderer';
import removeMarkdown from 'remove-markdown';
import { connect } from 'react-redux';
import { MarkdownAST } from '@rocket.chat/message-parser';
import shortnameToUnicode from '../../utils/shortnameToUnicode';
import I18n from '../../i18n';
@ -22,10 +23,16 @@ import mergeTextNodes from './mergeTextNodes';
import styles from './styles';
import { isValidURL } from '../../utils/url';
import { getUserSelector } from '../../selectors/login';
import MessageBody from './MessageBody';
import NewMarkdown from './new';
interface IMarkdownProps {
msg: string;
md: [
{
tokens: MarkdownAST;
mentions: object[];
}
];
getCustomEmoji: Function;
baseUrl: string;
username: string;
@ -39,6 +46,9 @@ interface IMarkdownProps {
_id: number;
}[];
mentions: object[];
user: {
enableMessageParserEarlyAdoption: boolean;
};
navToRoomInfo: Function;
preview: boolean;
theme: string;
@ -351,7 +361,7 @@ class Markdown extends PureComponent<IMarkdownProps, any> {
}
if (user.enableMessageParserEarlyAdoption && md) {
return <MessageBody tokens={md} style={style} mentions={mentions} channels={channels} navToRoomInfo={navToRoomInfo} />;
return <NewMarkdown tokens={md} style={style} mentions={mentions} channels={channels} navToRoomInfo={navToRoomInfo} />;
}
let m = formatText(msg);
@ -385,7 +395,7 @@ class Markdown extends PureComponent<IMarkdownProps, any> {
}
}
const mapStateToProps = state => ({
const mapStateToProps = (state: any) => ({
user: getUserSelector(state)
});

View File

@ -0,0 +1,25 @@
import React, { FC } from 'react';
import { StyleSheet, View } from 'react-native';
import { BigEmoji as BigEmojiProps } from '@rocket.chat/message-parser';
import Emoji from './Emoji';
interface IBigEmojiProps {
value: BigEmojiProps['value'];
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row'
}
});
const BigEmoji: FC<IBigEmojiProps> = ({ value }) => (
<View style={styles.container}>
{value.map(block => (
<Emoji emojiHandle={`:${block.value.value}:`} isBigEmoji />
))}
</View>
);
export default BigEmoji;

View File

@ -1,28 +1,32 @@
/* eslint-disable react/no-array-index-key */
import React from 'react';
import React, { FC } from 'react';
import { StyleSheet, Text } from 'react-native';
import PropTypes from 'prop-types';
import { Bold as BoldProps } from '@rocket.chat/message-parser';
import Strike from './Strike';
import Italic from './Italic';
import Plain from './Plain';
interface IBoldProps {
value: BoldProps['value'];
}
const styles = StyleSheet.create({
text: {
fontWeight: 'bold'
}
});
const Bold = ({ value }) => (
const Bold: FC<IBoldProps> = ({ value }) => (
<Text style={styles.text}>
{value.map((block, index) => {
{value.map(block => {
switch (block.type) {
case 'PLAIN_TEXT':
return <Plain value={block.value} />;
case 'STRIKE':
return <Strike key={index} value={block.value} />;
return <Strike value={block.value} />;
case 'ITALIC':
return <Italic key={index} value={block.value} />;
return <Italic value={block.value} />;
default:
return null;
}
@ -30,8 +34,4 @@ const Bold = ({ value }) => (
</Text>
);
Bold.propTypes = {
value: PropTypes.string
};
export default Bold;

View File

@ -3,15 +3,12 @@ import React from 'react';
import { Text } from 'react-native';
import PropTypes from 'prop-types';
import CodeLine from './CodeLine';
import styles from '../styles';
import { themes } from '../../../constants/colors';
import { useTheme } from '../../../theme';
import CodeLine from './CodeLine';
const Code = ({
value, style
}) => {
const Code = ({ value, style }) => {
const { theme } = useTheme();
return (
@ -24,8 +21,7 @@ const Code = ({
borderColor: themes[theme].borderColor
},
...style
]}
>
]}>
{value.map((block, index) => {
switch (block.type) {
case 'CODE_LINE':

View File

@ -2,9 +2,7 @@ import React from 'react';
import { Text } from 'react-native';
import PropTypes from 'prop-types';
const CodeLine = ({ value }) => (
<Text>{value.type === 'PLAIN_TEXT' && value.value}</Text>
);
const CodeLine = ({ value }) => <Text>{value.type === 'PLAIN_TEXT' && value.value}</Text>;
CodeLine.propTypes = {
value: PropTypes.object

View File

@ -11,15 +11,7 @@ const Emoji = ({ emojiHandle, style, isBigEmoji }) => {
const { theme } = useTheme();
const emojiUnicode = shortnameToUnicode(emojiHandle);
return (
<Text
style={[
{ color: themes[theme].bodyText },
isBigEmoji ? styles.textBig : styles.text,
style
]}
>
{emojiUnicode}
</Text>
<Text style={[{ color: themes[theme].bodyText }, isBigEmoji ? styles.textBig : styles.text, style]}>{emojiUnicode}</Text>
);
};

View File

@ -7,18 +7,14 @@ import { useTheme } from '../../../theme';
const Heading = ({ value, level }) => {
const { theme } = useTheme();
const textStyle = styles[`heading${ level }`];
const textStyle = styles[`heading${level}`];
return (
<>
{value.map((block) => {
{value.map(block => {
switch (block.type) {
case 'PLAIN_TEXT':
return (
<Text style={[textStyle, { color: themes[theme].bodyText }]}>
{block.value}
</Text>
);
return <Text style={[textStyle, { color: themes[theme].bodyText }]}>{block.value}</Text>;
default:
return null;
}

View File

@ -2,6 +2,8 @@ import React from 'react';
import { Text } from 'react-native';
import PropTypes from 'prop-types';
import Hashtag from '../Hashtag';
import Link from './Link';
import Plain from './Plain';
import Bold from './Bold';
@ -10,14 +12,10 @@ import Italic from './Italic';
import Emoji from './Emoji';
import Mention from './Mention';
import InlineCode from './InlineCode';
import Hashtag from '../Hashtag';
const Inline = ({
value, mentions, channels, navToRoomInfo, style
}) => (
const Inline = ({ value, mentions, channels, navToRoomInfo, style }) => (
<Text>
{value.map((block) => {
{value.map(block => {
switch (block.type) {
case 'PLAIN_TEXT':
return <Plain value={block.value} />;
@ -33,7 +31,7 @@ const Inline = ({
case 'MENTION_USER':
return <Mention value={block.value} navToRoomInfo={navToRoomInfo} mentions={mentions} style={style} />;
case 'EMOJI':
return <Emoji emojiHandle={`:${ block.value.value }:`} />;
return <Emoji emojiHandle={`:${block.value.value}:`} />;
case 'MENTION_CHANNEL':
return <Hashtag hashtag={block.value.value} navToRoomInfo={navToRoomInfo} channels={channels} style={style} />;
case 'INLINE_CODE':

View File

@ -10,17 +10,17 @@ const InlineCode = ({ value, style }) => {
const { theme } = useTheme();
return (
<Text style={[
{
...styles.codeInline,
color: themes[theme].bodyText,
backgroundColor: themes[theme].bannerBackground,
borderColor: themes[theme].borderColor
},
...style
]}
>
{((block) => {
<Text
style={[
{
...styles.codeInline,
color: themes[theme].bodyText,
backgroundColor: themes[theme].bannerBackground,
borderColor: themes[theme].borderColor
},
...style
]}>
{(block => {
switch (block.type) {
case 'PLAIN_TEXT':
return block.value;

View File

@ -1,19 +1,23 @@
/* eslint-disable react/no-array-index-key */
import React from 'react';
import React, { FC } from 'react';
import { StyleSheet, Text } from 'react-native';
import PropTypes from 'prop-types';
import { Italic as ItalicProps } from '@rocket.chat/message-parser';
import Strike from './Strike';
import Bold from './Bold';
import Plain from './Plain';
interface IItalicProps {
value: ItalicProps['value'];
}
const styles = StyleSheet.create({
text: {
fontStyle: 'italic'
}
});
const Italic = ({ value }) => (
const Italic: FC<IItalicProps> = ({ value }) => (
<Text style={styles.text}>
{value.map((block, index) => {
switch (block.type) {
@ -30,8 +34,4 @@ const Italic = ({ value }) => (
</Text>
);
Italic.propTypes = {
value: PropTypes.string
};
export default Italic;

View File

@ -2,9 +2,6 @@ import React from 'react';
import { Text, Clipboard } from 'react-native';
import PropTypes from 'prop-types';
import Strike from './Strike';
import Italic from './Italic';
import Bold from './Bold';
import styles from '../styles';
import I18n from '../../../i18n';
import { LISTENER } from '../../Toast';
@ -13,6 +10,10 @@ import openLink from '../../../utils/openLink';
import EventEmitter from '../../../utils/events';
import { themes } from '../../../constants/colors';
import Strike from './Strike';
import Italic from './Italic';
import Bold from './Bold';
const Link = ({ value }) => {
const { theme } = useTheme();
const { src, label } = value;
@ -29,12 +30,8 @@ const Link = ({ value }) => {
};
return (
<Text
onPress={handlePress}
onLongPress={onLongPress}
style={{ ...styles.link, color: themes[theme].actionTintColor }}
>
{((block) => {
<Text onPress={handlePress} onLongPress={onLongPress} style={{ ...styles.link, color: themes[theme].actionTintColor }}>
{(block => {
switch (block.type) {
case 'PLAIN_TEXT':
return block.value;

View File

@ -7,9 +7,7 @@ import { events, logEvent } from '../../../utils/log';
import { useTheme } from '../../../theme';
import { themes } from '../../../constants/colors';
const Mention = ({
value: mention, mentions, navToRoomInfo, style
}) => {
const Mention = ({ value: mention, mentions, navToRoomInfo, style }) => {
const { theme } = useTheme();
let mentionStyle = [];
const notMentionedStyle = [styles.text, { color: themes[theme].bodyText }, ...style];
@ -43,10 +41,14 @@ const Mention = ({
return (
<Text
style={[styles.mention, (mention || mentionedUser) && mentionStyle, !(mention || mentionedUser) && notMentionedStyle, ...style]}
onPress={handlePress}
>
{mentionedUser ? mentionedUser.name || mention.value : `@{${ mention }}` }
style={[
styles.mention,
(mention || mentionedUser) && mentionStyle,
!(mention || mentionedUser) && notMentionedStyle,
...style
]}
onPress={handlePress}>
{mentionedUser ? mentionedUser.name || mention.value : `@{${mention}}`}
</Text>
);
};

View File

@ -3,9 +3,9 @@ import PropTypes from 'prop-types';
import Inline from './Inline';
const Paragraph = ({
value, mentions, channels, navToRoomInfo, style
}) => <Inline value={value} mentions={mentions} channels={channels} navToRoomInfo={navToRoomInfo} style={style} />;
const Paragraph = ({ value, mentions, channels, navToRoomInfo, style }) => (
<Inline value={value} mentions={mentions} channels={channels} navToRoomInfo={navToRoomInfo} style={style} />
);
Paragraph.propTypes = {
value: PropTypes.string,

View File

@ -0,0 +1,11 @@
import React, { FC } from 'react';
import { Text } from 'react-native';
import { Plain as PlainProps } from '@rocket.chat/message-parser';
interface IPlainProps {
value: PlainProps['value'];
}
const Plain: FC<IPlainProps> = ({ value }) => <Text accessibilityLabel={value}>{value}</Text>;
export default Plain;

View File

@ -1,28 +1,32 @@
/* eslint-disable react/no-array-index-key */
import React from 'react';
import React, { FC } from 'react';
import { StyleSheet, Text } from 'react-native';
import PropTypes from 'prop-types';
import { Strike as StrikeProps } from '@rocket.chat/message-parser';
import Bold from './Bold';
import Italic from './Italic';
import Plain from './Plain';
interface IStrikeProps {
value: StrikeProps['value'];
}
const styles = StyleSheet.create({
text: {
textDecorationLine: 'line-through'
}
});
const Strike = ({ value }) => (
const Strike: FC<IStrikeProps> = ({ value }) => (
<Text style={styles.text}>
{value.map((block, index) => {
{value.map(block => {
switch (block.type) {
case 'PLAIN_TEXT':
return <Plain key={index} value={block.value} />;
return <Plain value={block.value} />;
case 'BOLD':
return <Bold key={index} value={block.value} />;
return <Bold value={block.value} />;
case 'ITALIC':
return <Italic key={index} value={block.value} />;
return <Italic value={block.value} />;
default:
return null;
}
@ -30,8 +34,4 @@ const Strike = ({ value }) => (
</Text>
);
Strike.propTypes = {
value: PropTypes.string
};
export default Strike;

View File

@ -13,9 +13,7 @@ import UnorderedList from './UnorderedList';
const isBigEmoji = tokens => tokens.length === 1 && tokens[0].type === 'BIG_EMOJI';
const Body = ({
tokens, mentions, channels, navToRoomInfo, style
}) => {
const Body = ({ tokens, mentions, channels, navToRoomInfo, style }) => {
if (isBigEmoji(tokens)) {
return <BigEmoji value={tokens[0].value} />;
}

View File

@ -47,6 +47,7 @@
"@react-navigation/drawer": "5.12.5",
"@react-navigation/native": "5.9.4",
"@react-navigation/stack": "5.14.5",
"@rocket.chat/message-parser": "^0.29.0",
"@rocket.chat/react-native-fast-image": "^8.2.0",
"@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#mobile",
"@rocket.chat/ui-kit": "0.13.0",

View File

@ -3203,6 +3203,11 @@
dependencies:
eslint-plugin-import "^2.17.2"
"@rocket.chat/message-parser@^0.29.0":
version "0.29.0"
resolved "https://registry.yarnpkg.com/@rocket.chat/message-parser/-/message-parser-0.29.0.tgz#9577f354756653e6271b9fe2290fadafcb5d4293"
integrity sha512-2qEPTNSHeWRCFfxCETqz9Asju6bf5dR98+AQUYwNus5911WW4tuehvrfvoqLYrlYqs4w5Qj6q9m2THCXqN27Gw==
"@rocket.chat/react-native-fast-image@^8.2.0":
version "8.2.0"
resolved "https://registry.yarnpkg.com/@rocket.chat/react-native-fast-image/-/react-native-fast-image-8.2.0.tgz#4f48858f95f40afcb10b39cee9b1239c150d6c51"