2021-10-20 16:32:58 +00:00
|
|
|
import React, { useContext } from 'react';
|
2022-03-29 16:09:44 +00:00
|
|
|
import { Text } from 'react-native';
|
2021-10-20 16:32:58 +00:00
|
|
|
import { Link as LinkProps } from '@rocket.chat/message-parser';
|
2022-03-29 16:09:44 +00:00
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
2021-10-20 16:32:58 +00:00
|
|
|
|
|
|
|
import styles from '../styles';
|
|
|
|
import I18n from '../../../i18n';
|
|
|
|
import { LISTENER } from '../../Toast';
|
|
|
|
import { useTheme } from '../../../theme';
|
2022-06-06 14:17:51 +00:00
|
|
|
import openLink from '../../../lib/methods/helpers/openLink';
|
|
|
|
import EventEmitter from '../../../lib/methods/helpers/events';
|
2021-10-20 16:32:58 +00:00
|
|
|
import Strike from './Strike';
|
|
|
|
import Italic from './Italic';
|
|
|
|
import Bold from './Bold';
|
|
|
|
import MarkdownContext from './MarkdownContext';
|
|
|
|
|
|
|
|
interface ILinkProps {
|
|
|
|
value: LinkProps['value'];
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:20:09 +00:00
|
|
|
const Link = ({ value }: ILinkProps) => {
|
2022-06-27 21:27:22 +00:00
|
|
|
const { theme, colors } = useTheme();
|
2021-10-20 16:32:58 +00:00
|
|
|
const { onLinkPress } = useContext(MarkdownContext);
|
|
|
|
const { src, label } = value;
|
|
|
|
const handlePress = () => {
|
|
|
|
if (!src.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (onLinkPress) {
|
|
|
|
return onLinkPress(src.value);
|
|
|
|
}
|
|
|
|
openLink(src.value, theme);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onLongPress = () => {
|
|
|
|
Clipboard.setString(src.value);
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-06-27 21:27:22 +00:00
|
|
|
<Text onPress={handlePress} onLongPress={onLongPress} style={[styles.link, { color: colors.actionTintColor }]}>
|
2021-10-20 16:32:58 +00:00
|
|
|
{(block => {
|
|
|
|
switch (block.type) {
|
|
|
|
case 'PLAIN_TEXT':
|
|
|
|
return block.value;
|
|
|
|
case 'STRIKE':
|
|
|
|
return <Strike value={block.value} />;
|
|
|
|
case 'ITALIC':
|
|
|
|
return <Italic value={block.value} />;
|
|
|
|
case 'BOLD':
|
|
|
|
return <Bold value={block.value} />;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})(label)}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Link;
|