Skip to content

Commit

Permalink
feat: text formatting + clickable links
Browse files Browse the repository at this point in the history
  • Loading branch information
glouvigny committed Mar 15, 2019
1 parent 0cadf69 commit 4bbd239
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 27 deletions.
Expand Up @@ -15,7 +15,7 @@ export const ActionButtonLarge = ({ onPress, color, title, icon }) => <Text
icon={icon}
background={color}
color={color === colors.white ? colors.subtleGrey : colors.white}
margin={{ left: 8 }}
margin={{ left: 4, right: 4 }}
padding={{
vertical: 6,
horizontal: 4,
Expand Down
132 changes: 132 additions & 0 deletions client/react-native/common/components/Library/Markdown.js
@@ -0,0 +1,132 @@
import MarkdownLib, { styles as libStyles } from 'react-native-markdown-renderer'
import MarkdownIt from 'markdown-it'
import React from 'react'
import { StyleSheet, Text } from 'react-native'
import { monospaceFont } from '../../constants/styling'
import colors from '../../constants/colors'
import { openURL } from '../../helpers/linking'

export const styles = StyleSheet.create({
codeBlock: {
borderRadius: 5,
borderWidth: 0,
backgroundColor: colors.fakeBlack,
color: colors.white,
padding: 4,
fontFamily: monospaceFont,
},
codeInline: {
borderRadius: 5,
borderWidth: 0,
backgroundColor: colors.fakeBlack,
color: colors.white,
padding: 1,
fontFamily: monospaceFont,
},
blockquote: {
paddingVertical: 0,
paddingRight: 0,
paddingLeft: 5,
borderLeftWidth: 2,
borderLeftColor: colors.blue25,
margin: 0,
backgroundColor: 'transparent',
},
inlineCode: {
borderRadius: 3,
borderWidth: 1,
padding: 2,
fontFamily: monospaceFont,
fontWeight: 'bold',
},
list: {},
listItem: {
flex: 1,
flexWrap: 'wrap',
},
listUnorderedItem: {
flexDirection: 'row',
justifyContent: 'flex-start',
},
listUnorderedItemIcon: {
marginLeft: 10,
marginRight: 10,
lineHeight: 20,
},
listUnorderedItemText: {
fontSize: 20,
lineHeight: 20,
},
listOrderedItem: {
flexDirection: 'row',
},
listOrderedItemIcon: {
marginLeft: 10,
marginRight: 10,
lineHeight: 20,
},
listOrderedItemText: {
fontWeight: 'bold',
lineHeight: 20,
},
paragraph: {
marginTop: 0,
marginBottom: 0,
flexWrap: 'wrap',
flexDirection: 'row',
alignItems: 'flex-start',
justifyContent: 'flex-start',
},
hardbreak: {
width: '100%',
height: 1,
},
strong: {
fontWeight: 'bold',
},
text: {},
strikethrough: {
textDecorationLine: 'line-through',
},
link: {
textDecorationLine: 'underline',
},
blocklink: {
flex: 1,
borderColor: colors.blue,
borderBottomWidth: 1,
},
u: {
borderBottomWidth: 1,
},
})

const md = MarkdownIt({
typographer: true,
linkify: true,
}).disable([
'newline',
'image',
'link', // Avoid [https://test.com](https://olol.com)
'html_inline',
'table',
'hr',
'reference',
'html_block',
'heading',
])

const flattenedStyles = StyleSheet.flatten([libStyles, styles])

const link = (node, children, parent, styles) =>
<Text key={node.key} style={styles.link} accessibilityRole='link' onPress={() => openURL(node.attributes.href)}>
{children}
</Text>

const Markdown = ({ children, style, ...props }) =>
<MarkdownLib rules={{
link,
}} markdownit={md} style={style || flattenedStyles} {...props}>{children}</MarkdownLib>

export default Markdown
Markdown.styles = styles
6 changes: 3 additions & 3 deletions client/react-native/common/components/Library/Menu.js
Expand Up @@ -14,7 +14,7 @@ import { colors } from '../../constants'
import { isRTL } from '../../i18n'

export default class Menu extends Component {
static Header = ({ icon, title, description }) => (
static Header = ({ icon, color, descriptionColor, title, description }) => (
<Flex.Cols style={[{ marginTop: 33 }, paddingHorizontal]} align='center'>
{icon && (
<Flex.Rows size={1} align={title ? 'end' : 'center'}>
Expand All @@ -27,11 +27,11 @@ export default class Menu extends Component {
justify='between'
style={[paddingLeft, { height: 42 }]}
>
<Text medium color={colors.black}>
<Text medium color={color || colors.fakeBlack}>
{title}
</Text>
{description && (
<Text tiny color={colors.subtleGrey}>
<Text tiny color={descriptionColor || colors.subtleGrey}>
{description}
</Text>
)}
Expand Down
1 change: 1 addition & 0 deletions client/react-native/common/components/Library/index.js
Expand Up @@ -22,3 +22,4 @@ export EmptyList from './EmptyList'
export Animation from './Animation'
export MovableView from './MovableView'
export DebugStateBar from './DebugStateBar'
export Markdown from './Markdown'
Expand Up @@ -34,7 +34,7 @@ export default createMaterialTopTabNavigator(
animationEnabled: true,
tabBarOptions: {
labelStyle: {
color: colors.black,
color: colors.fakeBlack,
},
indicatorStyle: {
backgroundColor: colors.blue,
Expand Down
56 changes: 37 additions & 19 deletions client/react-native/common/components/Screens/Chats/Detail.js
@@ -1,14 +1,14 @@
import {
ActivityIndicator,
Platform,
Platform, StyleSheet,
TextInput as RNTextInput,
View,
} from 'react-native'
import { btoa } from 'b64-lite'
import { withNamespaces } from 'react-i18next'
import React, { PureComponent } from 'react'

import { Flex, Header, Icon, Screen, Text, Avatar } from '../../Library'
import { Flex, Header, Icon, Screen, Text, Avatar, Markdown } from '../../Library'
import { Pagination, QueryReducer, RelayContext } from '../../../relay'
import { colors } from '../../../constants'
import { fragments } from '../../../graphql'
Expand All @@ -20,6 +20,24 @@ import * as dateFns from '../../../i18n/dateFns'
import withRelayContext from '../../../helpers/withRelayContext'
import * as KeyboardContext from '../../../helpers/KeyboardContext'

const textStyles = StyleSheet.flatten([Markdown.styles, {
text: {
color: colors.white,
},
listUnorderedItemIcon: {
color: colors.white,
},
listOrderedItemIcon: {
color: colors.white,
},
blocklink: {
borderColor: colors.white,
},
u: {
borderColor: colors.white,
},
}])

class Message extends React.PureComponent {
static contextType = RelayContext

Expand Down Expand Up @@ -49,7 +67,7 @@ class Message extends React.PureComponent {
return (
<Flex.Rows
align={isMyself ? 'end' : 'start'}
style={{ marginHorizontal: 10, marginVertical: 10 }}
style={{ marginHorizontal: 10, marginVertical: 2 }}
>
{!isMyself && !isOneToOne ? (
<Flex.Cols
Expand All @@ -69,24 +87,21 @@ class Message extends React.PureComponent {
</Flex.Cols>
) : null}

<Text
padding={{
vertical: 6,
horizontal: 10,
}}
multiline
left={!isMyself}
right={isMyself}
background={colors.blue}
color={colors.white}
rounded={14.5}
margin={{
bottom: 4,
[isMyself ? 'left' : 'right']: 42,
<View
style={{
[isMyself ? 'marginLeft' : 'marginRight']: 42,
marginTop: 2,
marginBottom: 2,
backgroundColor: colors.blue,
padding: 10,
borderTopLeftRadius: isMyself ? 14.5 : 3,
borderTopRightRadius: 14.5,
borderBottomLeftRadius: 14.5,
borderBottomRightRadius: isMyself ? 3 : 14.5,
}}
>
{parseEmbedded(data.attributes).message.text}
</Text>
<Markdown style={textStyles}>{parseEmbedded(data.attributes).message.text}</Markdown>
</View>
<Text
left={!isMyself}
right={isMyself}
Expand Down Expand Up @@ -141,6 +156,8 @@ class TextInputBase extends PureComponent {
marginVertical: 8,
marginHorizontal: 0,
height: height,
color: colors.fakeBlack,
backgroundColor: colors.inputGrey,
},
Platform.OS === 'web' ? { paddingLeft: 16 } : {},
]}
Expand All @@ -152,6 +169,7 @@ class TextInputBase extends PureComponent {
onContentSizeChange={this.onContentSizeChange}
autoFocus
placeholder={t('chats.write-message')}
placeholderTextColor={colors.lightGrey}
onChangeText={this.props.onChangeText}
value={value}
multiline
Expand Down
Expand Up @@ -114,7 +114,7 @@ const ItemBase = fragments.Conversation(
justify='center'
style={[marginLeft]}
>
<Text color={colors.black} left middle bold={!isRead}>
<Text color={colors.fakeBlack} left middle bold={!isRead}>
{utils.getTitle(data)}
</Text>
<Flex.Cols size={1} justify='flex-start'>
Expand Down
Expand Up @@ -67,7 +67,7 @@ const Item = fragments.Contact(
<Avatar data={data} size={40} />
</Flex.Rows>
<Flex.Rows size={3} justify='start' style={[marginLeft]}>
<Text color={colors.black} left>
<Text color={colors.fakeBlack} left>
{overrideDisplayName || displayName}
</Text>
<Text color={colors.subtleGrey} left>
Expand Down
7 changes: 7 additions & 0 deletions client/react-native/common/helpers/linking.js
@@ -0,0 +1,7 @@
import { Linking } from 'react-native';

export const openURL = url => {
if (url) {
Linking.openURL(url)
}
}
9 changes: 9 additions & 0 deletions client/react-native/common/helpers/linking.web.js
@@ -0,0 +1,9 @@
import { NativeModules } from 'react-native'

const { CoreModule } = NativeModules

export const openURL = url => {
if (url) {
CoreModule.openURL(url)
}
}
1 change: 1 addition & 0 deletions client/react-native/common/helpers/patch-electron.js
Expand Up @@ -45,6 +45,7 @@ if (Platform.OS === 'web' &&
'updateNetworkConfig',
'setCurrentRoute',
'getLocalGRPCInfos',
'openURL',
]

for (let op of ops) {
Expand Down
3 changes: 3 additions & 0 deletions client/react-native/common/helpers/patch-web.js
Expand Up @@ -22,6 +22,9 @@ if (Platform.OS === 'web') {
getNetworkConfig: async () => console.warn('not implemented in web'),
updateNetworkConfig: async () => console.warn('not implemented in web'),
setCurrentRoute: () => {},
openURL: (url) => {
window.open(url, '_blank')
},
}
NativeModules.CoreModule = CoreModule

Expand Down
13 changes: 13 additions & 0 deletions client/react-native/desktop/coreinterface/core.go
Expand Up @@ -4,11 +4,14 @@ import (
"errors"
"fmt"

"go.uber.org/zap"

"berty.tech/core/pkg/notification"

"berty.tech/client/react-native/gomobile/core"

"berty.tech/core/pkg/deviceinfo"
"github.com/pkg/browser"
"github.com/shibukawa/configdir"
)

Expand Down Expand Up @@ -80,6 +83,16 @@ func UpdateNetworkConfig(config string) (interface{}, error) {
return nil, core.UpdateNetworkConfig(config)
}

func OpenURL(url string) (interface{}, error) {
logger().Info(fmt.Sprintf("opening URL %s", url))
err := browser.OpenURL(url)
if err != nil {
logger().Error(fmt.Sprintf("unable to open URL %s", url), zap.Error(err))
}

return nil, err
}

func IsBotRunning() (interface{}, error) {
return core.IsBotRunning(), nil
}
Expand Down
9 changes: 9 additions & 0 deletions client/react-native/desktop/coreinterface/logger.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/react-native/desktop/go.mod
Expand Up @@ -11,6 +11,7 @@ require (
github.com/asticode/go-astilectron-bootstrap v0.0.0-20180616141213-b3211646d205
github.com/asticode/go-astilectron-bundler v0.0.0-20190221164801-6b22eafb3c85 // indirect
github.com/asticode/go-bindata v0.0.0-20151023091102-a0ff2567cfb7 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.8.1
github.com/sam-kamerer/go-plister v0.0.0-20190202124357-57f251aa88ff // indirect
github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0
Expand Down

0 comments on commit 4bbd239

Please sign in to comment.