Updated components and added more Code components
This commit is contained in:
parent
1e7bb87c9d
commit
71aed0cde8
|
@ -1,18 +1,24 @@
|
||||||
|
/* eslint-disable react/no-array-index-key */
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Text } from 'react-native';
|
import { Text } from 'react-native';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
|
import CodeLine from './CodeLine';
|
||||||
|
import styles from '../styles';
|
||||||
|
|
||||||
import { themes } from '../../../constants/colors';
|
import { themes } from '../../../constants/colors';
|
||||||
import { useTheme } from '../../../theme';
|
import { useTheme } from '../../../theme';
|
||||||
|
|
||||||
const Code = ({
|
const Code = ({
|
||||||
type, styles, style, literal
|
value, style
|
||||||
}) => {
|
}) => {
|
||||||
const theme = useTheme();
|
const { theme } = useTheme();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Text
|
<Text
|
||||||
style={[
|
style={[
|
||||||
{
|
{
|
||||||
...(type === 'INLINE_CODE' ? styles.codeInline : styles.codeBlock),
|
...styles.codeBlock,
|
||||||
color: themes[theme].bodyText,
|
color: themes[theme].bodyText,
|
||||||
backgroundColor: themes[theme].bannerBackground,
|
backgroundColor: themes[theme].bannerBackground,
|
||||||
borderColor: themes[theme].bannerBackground
|
borderColor: themes[theme].bannerBackground
|
||||||
|
@ -20,15 +26,20 @@ const Code = ({
|
||||||
...style
|
...style
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
{literal}
|
{value.map((block, index) => {
|
||||||
|
switch (block.type) {
|
||||||
|
case 'CODE_LINE':
|
||||||
|
return <CodeLine key={index} value={block.value} />;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})}
|
||||||
</Text>
|
</Text>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
Code.propTypes = {
|
Code.propTypes = {
|
||||||
type: PropTypes.string,
|
value: PropTypes.array,
|
||||||
literal: PropTypes.string,
|
|
||||||
styles: PropTypes.object,
|
|
||||||
style: PropTypes.object
|
style: PropTypes.object
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
|
||||||
|
CodeLine.propTypes = {
|
||||||
|
value: PropTypes.object
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CodeLine;
|
|
@ -4,13 +4,16 @@ import PropTypes from 'prop-types';
|
||||||
|
|
||||||
import Link from './Link';
|
import Link from './Link';
|
||||||
import Plain from './Plain';
|
import Plain from './Plain';
|
||||||
import Code from './Code';
|
|
||||||
import Bold from './Bold';
|
import Bold from './Bold';
|
||||||
import Strike from './Strike';
|
import Strike from './Strike';
|
||||||
import Italic from './Italic';
|
import Italic from './Italic';
|
||||||
import Emoji from './Emoji';
|
import Emoji from './Emoji';
|
||||||
|
import Mention from './Mention';
|
||||||
|
import InlineCode from './InlineCode';
|
||||||
|
|
||||||
const Inline = ({ value }) => (
|
const Inline = ({
|
||||||
|
value, mentions, navToRoomInfo, style
|
||||||
|
}) => (
|
||||||
<Text>
|
<Text>
|
||||||
{value.map((block) => {
|
{value.map((block) => {
|
||||||
switch (block.type) {
|
switch (block.type) {
|
||||||
|
@ -25,15 +28,15 @@ const Inline = ({ value }) => (
|
||||||
case 'LINK':
|
case 'LINK':
|
||||||
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
||||||
return <Link value={block.value} />;
|
return <Link value={block.value} />;
|
||||||
// case 'MENTION_USER':
|
case 'MENTION_USER':
|
||||||
// return <Mention value={block.value} mentions={mentions} />;
|
return <Mention value={block.value} navToRoomInfo={navToRoomInfo} mentions={mentions} style={style} />;
|
||||||
case 'EMOJI':
|
case 'EMOJI':
|
||||||
return <Emoji emojiHandle={`:${ block.value.value }:`} />;
|
return <Emoji emojiHandle={`:${ block.value.value }:`} />;
|
||||||
// case 'MENTION_CHANNEL':
|
case 'MENTION_CHANNEL':
|
||||||
// // case 'COLOR':
|
// case 'COLOR':
|
||||||
// return <Plain value={block.value} />;
|
return <Plain value={`${ block.value.value }`} />;
|
||||||
case 'INLINE_CODE':
|
case 'INLINE_CODE':
|
||||||
return <Code value={block.value} />;
|
return <InlineCode value={block.value} style={style} />;
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -42,7 +45,10 @@ const Inline = ({ value }) => (
|
||||||
);
|
);
|
||||||
|
|
||||||
Inline.propTypes = {
|
Inline.propTypes = {
|
||||||
value: PropTypes.object
|
value: PropTypes.object,
|
||||||
|
mentions: PropTypes.array,
|
||||||
|
navToRoomInfo: PropTypes.func,
|
||||||
|
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object])
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Inline;
|
export default Inline;
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
import React from 'react';
|
||||||
|
import { Text } from 'react-native';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import { useTheme } from '@react-navigation/native';
|
||||||
|
|
||||||
|
import styles from '../styles';
|
||||||
|
import { themes } from '../../../constants/colors';
|
||||||
|
|
||||||
|
const InlineCode = ({ value, style }) => {
|
||||||
|
const { theme } = useTheme();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Text style={[
|
||||||
|
{
|
||||||
|
...styles.codeInline,
|
||||||
|
color: themes[theme].bodyText,
|
||||||
|
backgroundColor: themes[theme].bannerBackground,
|
||||||
|
borderColor: themes[theme].bannerBackground
|
||||||
|
},
|
||||||
|
...style
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
{((block) => {
|
||||||
|
switch (block.type) {
|
||||||
|
case 'PLAIN_TEXT':
|
||||||
|
return block.value;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})(value)}
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
InlineCode.propTypes = {
|
||||||
|
value: PropTypes.object,
|
||||||
|
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object])
|
||||||
|
};
|
||||||
|
|
||||||
|
export default InlineCode;
|
|
@ -32,7 +32,6 @@ const Link = ({ value }) => {
|
||||||
<Text
|
<Text
|
||||||
onPress={handlePress}
|
onPress={handlePress}
|
||||||
onLongPress={onLongPress}
|
onLongPress={onLongPress}
|
||||||
o
|
|
||||||
style={{ ...styles.link, color: themes[theme].actionTintColor }}
|
style={{ ...styles.link, color: themes[theme].actionTintColor }}
|
||||||
>
|
>
|
||||||
{((block) => {
|
{((block) => {
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
import React from 'react';
|
||||||
|
import { Text } from 'react-native';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
|
import styles from '../styles';
|
||||||
|
import { events, logEvent } from '../../../utils/log';
|
||||||
|
import { useTheme } from '../../../theme';
|
||||||
|
import { themes } from '../../../constants/colors';
|
||||||
|
|
||||||
|
const Mention = ({
|
||||||
|
value: mention, mentions, navToRoomInfo, style
|
||||||
|
}) => {
|
||||||
|
const { theme } = useTheme();
|
||||||
|
let mentionStyle = [];
|
||||||
|
const notMentionedStyle = [styles.text, { color: themes[theme].bodyText }, ...style];
|
||||||
|
const mentionedUser = mentions.find(mentioned => mentioned.username === mention.value);
|
||||||
|
|
||||||
|
if (mention === 'all' || mention === 'here') {
|
||||||
|
mentionStyle = [
|
||||||
|
{
|
||||||
|
color: themes[theme].mentionGroupColor
|
||||||
|
},
|
||||||
|
...style
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mention === mentionedUser) {
|
||||||
|
mentionStyle = {
|
||||||
|
color: themes[theme].mentionMeColor
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
mentionStyle = {
|
||||||
|
color: themes[theme].mentionOtherColor
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const handlePress = () => {
|
||||||
|
logEvent(events.ROOM_MENTION_GO_USER_INFO);
|
||||||
|
const navParam = {
|
||||||
|
t: 'd',
|
||||||
|
rid: mentionedUser && mentionedUser._id
|
||||||
|
};
|
||||||
|
navToRoomInfo(navParam);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Text
|
||||||
|
style={[styles.mention, (mention || mentionedUser) && mentionStyle, !(mention || mentionedUser) && notMentionedStyle, ...style]}
|
||||||
|
onPress={handlePress}
|
||||||
|
>
|
||||||
|
{mentionedUser ? mentionedUser.name || mention.value : `@{${ mention }}` }
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Mention.propTypes = {
|
||||||
|
value: PropTypes.string,
|
||||||
|
mentions: PropTypes.array,
|
||||||
|
navToRoomInfo: PropTypes.func,
|
||||||
|
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object])
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Mention;
|
|
@ -3,11 +3,15 @@ import PropTypes from 'prop-types';
|
||||||
|
|
||||||
import Inline from './Inline';
|
import Inline from './Inline';
|
||||||
|
|
||||||
const Paragraph = ({ value, mentions }) => <Inline value={value} mentions={mentions} />;
|
const Paragraph = ({
|
||||||
|
value, mentions, navToRoomInfo, style
|
||||||
|
}) => <Inline value={value} mentions={mentions} navToRoomInfo={navToRoomInfo} style={style} />;
|
||||||
|
|
||||||
Paragraph.propTypes = {
|
Paragraph.propTypes = {
|
||||||
value: PropTypes.string,
|
value: PropTypes.string,
|
||||||
mentions: PropTypes.string
|
mentions: PropTypes.string,
|
||||||
|
navToRoomInfo: PropTypes.func,
|
||||||
|
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object])
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Paragraph;
|
export default Paragraph;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { useTheme } from '../../../theme';
|
||||||
import styles from '../styles';
|
import styles from '../styles';
|
||||||
|
|
||||||
const Quote = ({ value }) => {
|
const Quote = ({ value }) => {
|
||||||
const theme = useTheme();
|
const { theme } = useTheme();
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
|
|
|
@ -9,14 +9,14 @@ import Heading from './Heading';
|
||||||
import Code from './Code';
|
import Code from './Code';
|
||||||
import Link from './Link';
|
import Link from './Link';
|
||||||
import BigEmoji from './BigEmoji';
|
import BigEmoji from './BigEmoji';
|
||||||
import { useTheme } from '../../../theme';
|
|
||||||
|
|
||||||
const isBigEmoji = tokens => tokens.length === 1 && tokens[0].type === 'BIG_EMOJI';
|
const isBigEmoji = tokens => tokens.length === 1 && tokens[0].type === 'BIG_EMOJI';
|
||||||
|
|
||||||
const Body = ({ tokens, mentions }) => {
|
const Body = ({
|
||||||
const { theme } = useTheme();
|
tokens, mentions, navToRoomInfo, style
|
||||||
|
}) => {
|
||||||
if (isBigEmoji(tokens)) {
|
if (isBigEmoji(tokens)) {
|
||||||
return <BigEmoji value={tokens[0].value} theme={theme} />;
|
return <BigEmoji value={tokens[0].value} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -32,9 +32,9 @@ const Body = ({ tokens, mentions }) => {
|
||||||
case 'QUOTE':
|
case 'QUOTE':
|
||||||
return <Quote key={index} value={block.value} />;
|
return <Quote key={index} value={block.value} />;
|
||||||
case 'PARAGRAPH':
|
case 'PARAGRAPH':
|
||||||
return <Paragraph key={index} value={block.value} mentions={mentions} />;
|
return <Paragraph key={index} value={block.value} navToRoomInfo={navToRoomInfo} mentions={mentions} style={style} />;
|
||||||
case 'CODE':
|
case 'CODE':
|
||||||
return <Code key={index} value={block.value} />;
|
return <Code key={index} value={block.value} style={style} />;
|
||||||
case 'LINK':
|
case 'LINK':
|
||||||
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
||||||
return <Link key={index} value={block.value} />;
|
return <Link key={index} value={block.value} />;
|
||||||
|
@ -50,7 +50,9 @@ const Body = ({ tokens, mentions }) => {
|
||||||
|
|
||||||
Body.propTypes = {
|
Body.propTypes = {
|
||||||
tokens: PropTypes.array,
|
tokens: PropTypes.array,
|
||||||
mentions: PropTypes.array
|
mentions: PropTypes.array,
|
||||||
|
navToRoomInfo: PropTypes.func,
|
||||||
|
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object])
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Body;
|
export default Body;
|
||||||
|
|
|
@ -376,7 +376,7 @@ class Markdown extends PureComponent {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
msg, md, numberOfLines, preview = false, theme, style = [], testID, user, mentions
|
msg, md, numberOfLines, preview = false, theme, style = [], testID, user, mentions, navToRoomInfo
|
||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
if (!msg) {
|
if (!msg) {
|
||||||
|
@ -384,7 +384,7 @@ class Markdown extends PureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.enableMessageParserEarlyAdoption && md) {
|
if (user.enableMessageParserEarlyAdoption && md) {
|
||||||
return <MessageBody tokens={md} theme={theme} style={style} mentions={mentions} />;
|
return <MessageBody tokens={md} theme={theme} style={style} mentions={mentions} navToRoomInfo={navToRoomInfo} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
let m = formatText(msg);
|
let m = formatText(msg);
|
||||||
|
|
Loading…
Reference in New Issue