Skip to content

Commit faa010c

Browse files
committed
[ptmt#3] Add markdown parser
1 parent 76cd977 commit faa010c

File tree

12 files changed

+357
-75
lines changed

12 files changed

+357
-75
lines changed

Examples/SimpleChatClient/SimpleChatClient/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ - (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge
105105
* then add the `main.jsbundle` file to your project and uncomment this line:
106106
*/
107107

108-
//sourceURL = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
108+
sourceURL = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
109109

110110
#if RUNNING_ON_CI
111111
sourceURL = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

Examples/SimpleChatClient/SimpleChatClient/main.jsbundle

Lines changed: 18 additions & 17 deletions
Large diffs are not rendered by default.

Examples/SimpleChatClient/components/ChatLayout.js

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var {
1414
} = React;
1515

1616
import LoadingIndicator from './LoadingIndicator';
17+
import Message from './Message';
1718

1819
class Channel extends React.Component {
1920
constructor() {
@@ -67,18 +68,10 @@ class ChatLayout extends React.Component {
6768
);
6869
});
6970

70-
const messages = this.props.messages && this.props.messages.map(message => {
71-
const date = new Date(message.timestamp);
72-
var time = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
73-
return (
74-
<View style={[styles.message]} key={message.id}>
75-
<Text style={styles.messageTimestamp}>{time}</Text>
76-
<Text style={styles.messageUsername}>{'<'}{message.author.username}{'>'}</Text>
77-
<Text style={styles.messageText}>{message.content}</Text>
78-
</View>
79-
);
71+
const messages = this.props.messages && this.props.messages.map((message, i) => {
72+
return <Message {...message} key={i} />
8073
});
81-
console.log(!messages)
74+
8275
return (
8376
<View style={styles.container}>
8477
<View style={styles.channels}>
@@ -90,12 +83,11 @@ class ChatLayout extends React.Component {
9083
</ScrollView>
9184
</View>
9285
<View style={styles.messages}>
93-
{!messages && <LoadingIndicator>Loading messages..</LoadingIndicator>}
9486
<ScrollView
9587
style={[styles.messagesScrollContainer]}
9688
autoScrollToBottom={true}
9789
showsVerticalScrollIndicator={true}>
98-
{messages}
90+
{messages || <LoadingIndicator>Loading...</LoadingIndicator>}
9991
</ScrollView>
10092
<View style={styles.inputWrapper}>
10193
<TextInput
@@ -192,46 +184,10 @@ var styles = StyleSheet.create({
192184
// --------- messages
193185
messages: {
194186
flex: 1,
195-
backgroundColor: 'white',
196-
top: 0,
187+
backgroundColor: 'white'
197188
},
198189
messagesScrollContainer: {
199190
height: Dimensions.get('window').height - 150,
200191
},
201-
verticallyInverted: {
202-
transform: [
203-
{ scaleY: 2 },
204-
],
205-
},
206-
message: {
207-
flex: 1,
208-
flexDirection: 'row',
209-
marginLeft: 10,
210-
marginBottom: 5,
211-
},
212-
messageTimestamp: {
213-
color: '#999',
214-
marginRight: 5,
215-
fontFamily: 'Monaco'
216-
//fontSize: 12
217-
},
218-
messageUsername: {
219-
color: '#222',
220-
width: 100,
221-
textAlign: 'right',
222-
fontWeight: '600',
223-
},
224-
messageText: {
225-
//position: 'absolute',
226-
color: '#222',
227-
marginLeft: 4,
228-
width: Dimensions.get('window').width - 700 // TODO: why it's not wrap automatically
229-
},
230-
messageLoader: {
231-
fontSize: 20,
232-
color: '#333',
233-
marginTop: 20,
234-
marginLeft: 100
235-
},
236192
});
237193
module.exports = ChatLayout;

Examples/SimpleChatClient/components/LoadingIndicator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class LoadingIndicator extends React.Component {
1515
}
1616
render(): ReactElement {
1717
return (
18-
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
18+
<View style={{flex: 1, marginTop: 100, alignItems: 'center', justifyContent: 'center'}}>
1919
<ActivityIndicatorIOS size="large" style={{width: 40, alignSelf: 'center'}}/>
2020
<Text>{this.props.children}</Text>
2121
</View>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* @flow */
2+
'use strict';
3+
4+
import React, {
5+
View,
6+
Text,
7+
Image,
8+
StyleSheet,
9+
Animated
10+
} from 'react-native-desktop';
11+
12+
import Markdown from './SuperSimpleMarkdown';
13+
14+
export default class Message extends React.Component {
15+
render(): ReactElement {
16+
const date = new Date(this.props.timestamp);
17+
const time = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
18+
const attachImage = this.props.attachments && this.props.attachments.length > 0 ?
19+
this.props.attachments[0].url : null;
20+
return (
21+
<View style={[styles.message]} key={this.props.id}>
22+
<Text style={styles.messageTimestamp}>{time}</Text>
23+
<Text style={styles.messageUsername} numberOfLines={1}>{'<'}{this.props.author.username}{'>'}</Text>
24+
{attachImage &&
25+
<Image resizeMode={'contain'} source={{uri: attachImage}} style={{width: 300, height: 300}} />
26+
}
27+
<Markdown>{parseMentions(this.props.content, this.props.mentions)}</Markdown>
28+
</View>
29+
);
30+
}
31+
}
32+
33+
function parseMentions(text: string, mentions: Array<any>): string {
34+
if (!mentions || mentions.length === 0) {
35+
return text;
36+
}
37+
const matches = text.match(/<@[^>]*>/g);
38+
matches.forEach(mention => {
39+
const id = mention.substring(2, mention.length - 1);
40+
text = text.replace(mention, '**@' + mentions.filter(m => m.id === id)[0].username + '**');
41+
});
42+
return text;
43+
}
44+
45+
const styles = StyleSheet.create({
46+
message: {
47+
flex: 1,
48+
flexDirection: 'row',
49+
marginLeft: 10,
50+
marginBottom: 5,
51+
},
52+
messageTimestamp: {
53+
color: '#999',
54+
marginRight: 5,
55+
fontFamily: 'Monaco',
56+
fontSize: 12,
57+
},
58+
messageUsername: {
59+
color: '#222',
60+
width: 100,
61+
fontSize: 12,
62+
textAlign: 'right',
63+
fontWeight: '600',
64+
},
65+
messageText: {
66+
color: '#222',
67+
marginLeft: 4,
68+
fontSize: 12,
69+
}
70+
});

0 commit comments

Comments
 (0)