[NEW] Emphasis Elements (italic, strike and bold) in Message Parser Components (#4621)

* [NEW] Emphasis Elements (italic, strike and bold) in Message Parser Components

* update storyshot

* minor tweak and remove of isLink

* Add theme provider to new md stories

* Cleanup

* Update tests

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Reinaldo Neto 2022-11-07 13:42:42 -03:00 committed by GitHub
parent 0ad5545a4c
commit a5e538c13f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 32 deletions

File diff suppressed because one or more lines are too long

View File

@ -41,18 +41,21 @@ const Link = ({ value }: ILinkProps) => {
return ( return (
<Text onPress={handlePress} onLongPress={onLongPress} style={[styles.link, { color: themes[theme].actionTintColor }]}> <Text onPress={handlePress} onLongPress={onLongPress} style={[styles.link, { color: themes[theme].actionTintColor }]}>
{(block => { {(block => {
switch (block.type) { const blockArray = Array.isArray(block) ? block : [block];
case 'PLAIN_TEXT': return blockArray.map(blockInArray => {
return block.value; switch (blockInArray.type) {
case 'STRIKE': case 'PLAIN_TEXT':
return <Strike value={block.value} />; return blockInArray.value;
case 'ITALIC': case 'STRIKE':
return <Italic value={block.value} />; return <Strike value={blockInArray.value} />;
case 'BOLD': case 'ITALIC':
return <Bold value={block.value} />; return <Italic value={blockInArray.value} />;
default: case 'BOLD':
return null; return <Bold value={blockInArray.value} />;
} default:
return null;
}
});
})(label)} })(label)}
</Text> </Text>
); );

View File

@ -3,22 +3,25 @@ import { StyleSheet, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native'; import { NavigationContainer } from '@react-navigation/native';
import NewMarkdownComponent from '.'; import NewMarkdownComponent from '.';
import { themes } from '../../../lib/constants'; import { colors, themes } from '../../../lib/constants';
import { longText } from '../../../../.storybook/utils'; import { longText } from '../../../../.storybook/utils';
import { ThemeContext } from '../../../theme';
const theme = 'light';
export default { export default {
title: 'NewMarkdown', title: 'NewMarkdown',
decorators: [ decorators: [
(Story: any) => ( (Story: any) => (
<NavigationContainer> <NavigationContainer>
<Story /> <ThemeContext.Provider value={{ theme, colors: colors[theme] }}>
<Story />
</ThemeContext.Provider>
</NavigationContainer> </NavigationContainer>
) )
] ]
}; };
const theme = 'light';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
marginHorizontal: 15, marginHorizontal: 15,
@ -420,10 +423,73 @@ const markdownLink = [
} }
]; ];
const markdownLinkWithEmphasis = [
{
type: 'PARAGRAPH',
value: [
{
type: 'LINK',
value: {
src: {
type: 'PLAIN_TEXT',
value: 'https://rocket.chat/'
},
label: [
{
type: 'PLAIN_TEXT',
value: 'Normal Link - '
},
{
type: 'BOLD',
value: [
{
type: 'PLAIN_TEXT',
value: 'Bold'
}
]
},
{
type: 'PLAIN_TEXT',
value: ' '
},
{
type: 'STRIKE',
value: [
{
type: 'PLAIN_TEXT',
value: 'strike'
}
]
},
{
type: 'PLAIN_TEXT',
value: ' and '
},
{
type: 'ITALIC',
value: [
{
type: 'PLAIN_TEXT',
value: 'Italic'
}
]
},
{
type: 'PLAIN_TEXT',
value: ' Styles'
}
]
}
}
]
}
];
export const Links = () => ( export const Links = () => (
<View style={styles.container}> <View style={styles.container}>
<NewMarkdown tokens={rocketChatLink} /> <NewMarkdown tokens={rocketChatLink} />
<NewMarkdown tokens={markdownLink} /> <NewMarkdown tokens={markdownLink} />
<NewMarkdown tokens={markdownLinkWithEmphasis} />
</View> </View>
); );

View File

@ -3,20 +3,15 @@ import { Text } from 'react-native';
import { Plain as PlainProps } from '@rocket.chat/message-parser'; import { Plain as PlainProps } from '@rocket.chat/message-parser';
import styles from '../styles'; import styles from '../styles';
import { useTheme } from '../../../theme';
import { themes } from '../../../lib/constants';
interface IPlainProps { interface IPlainProps {
value: PlainProps['value']; value: PlainProps['value'];
} }
const Plain = ({ value }: IPlainProps) => { const Plain = ({ value }: IPlainProps) => (
const { theme } = useTheme(); <Text accessibilityLabel={value} style={styles.plainText}>
return ( {value}
<Text accessibilityLabel={value} style={[styles.plainText, { color: themes[theme].bodyText }]}> </Text>
{value} );
</Text>
);
};
export default Plain; export default Plain;