-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
190 lines (175 loc) · 5.85 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Rich Editor Example
* @author tangzehua
* @since 2019-06-24 14:52
*/
import React from 'react';
import {
Appearance,
Button,
KeyboardAvoidingView,
SafeAreaView,
ScrollView,
StyleSheet,
Text,
TextInput,
View,
} from 'react-native';
import {RichEditor, RichToolbar} from 'react-native-pell-rich-editor';
const initHTML = `<br/>
<center><b>Pell.js Rich Editor</b></center>
<center>React Native</center>
<br/>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1024px-React-icon.svg.png" ></br></br>
</br></br>
`;
class Example extends React.Component {
richText = React.createRef();
constructor(props) {
super(props);
const that = this;
const theme = props.theme || Appearance.getColorScheme();
const contentStyle = that.createContentStyle(theme);
that.state = {theme: theme, contentStyle};
that.onHome = that.onHome;
that.save = that.save;
that.onTheme = that.onTheme;
that.onPressAddImage = that.onPressAddImage;
that.themeChange = that.themeChange;
}
componentDidMount() {
Appearance.addChangeListener(this.themeChange);
}
componentWillUnmount() {
Appearance.removeChangeListener(this.themeChange);
}
/**
* theme change to editor color
* @param colorScheme
*/
themeChange({colorScheme}) {
const theme = colorScheme;
const contentStyle = this.createContentStyle(theme);
this.setState({theme, contentStyle});
}
async save() {
// Get the data here and call the interface to save the data
let html = await this.richText.current?.getContentHtml();
// console.log(html);
alert(html);
}
onPressAddImage() {
// insert URL
this.richText.current?.insertImage(
'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1024px-React-icon.svg.png',
);
// insert base64
// this.richText.current?.insertImage(`data:${image.mime};base64,${image.data}`);
this.richText.current?.blurContentEditor();
}
onHome() {
this.props.navigation.push('index');
}
createContentStyle(theme) {
const contentStyle = {backgroundColor: '#000033', color: '#fff', placeholderColor: 'gray'};
if (theme === 'light') {
contentStyle.backgroundColor = '#fff';
contentStyle.color = '#000033';
contentStyle.placeholderColor = '#a9a9a9';
}
return contentStyle;
}
onTheme() {
let {theme} = this.state;
theme = theme === 'light' ? 'dark' : 'light';
let contentStyle = this.createContentStyle(theme);
this.setState({theme, contentStyle});
}
render() {
let that = this;
const {contentStyle, theme} = that.state;
const {backgroundColor, color, placeholderColor} = contentStyle;
const themeBg = {backgroundColor};
return (
<SafeAreaView style={[styles.container, themeBg]}>
<View style={styles.nav}>
<Button title={'HOME'} onPress={that.onHome} />
<Button title={theme} onPress={that.onTheme} />
<Button title="Save" onPress={that.save} />
</View>
<ScrollView style={[styles.scroll, themeBg]} keyboardDismissMode={'none'}>
<View>
<View style={styles.item}>
<Text style={{color}}>To: </Text>
<TextInput
style={[styles.input, {color}]}
placeholderTextColor={placeholderColor}
placeholder={'stulip@126.com'}
/>
</View>
<View style={styles.item}>
<Text style={{color}}>Subject: </Text>
<TextInput
style={[styles.input, {color}]}
placeholderTextColor={placeholderColor}
placeholder="Rich Editor Bug 😀"
/>
</View>
</View>
<RichEditor
editorStyle={contentStyle}
containerStyle={themeBg}
ref={that.richText}
style={[styles.rich, themeBg]}
placeholder={'please input content'}
initialContentHTML={initHTML}
/>
</ScrollView>
<KeyboardAvoidingView behavior={'padding'}>
<RichToolbar
style={[styles.richBar, themeBg]}
editor={that.richText}
iconTint={color}
selectedIconTint={'#2095F2'}
selectedButtonStyle={{backgroundColor: 'transparent'}}
onPressAddImage={that.onPressAddImage}
/>
</KeyboardAvoidingView>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
nav: {
flexDirection: 'row',
justifyContent: 'space-between',
marginHorizontal: 5,
},
rich: {
minHeight: 300,
flex: 1,
},
richBar: {
height: 50,
backgroundColor: '#F5FCFF',
},
scroll: {
backgroundColor: '#ffffff',
},
item: {
borderBottomWidth: StyleSheet.hairlineWidth,
borderColor: '#eee',
flexDirection: 'row',
height: 40,
alignItems: 'center',
paddingHorizontal: 15,
},
input: {
flex: 1,
},
});
export {Example};