-
Notifications
You must be signed in to change notification settings - Fork 78
/
App.tsx
241 lines (226 loc) · 7.57 KB
/
App.tsx
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import * as React from 'react';
import {
StyleSheet,
StatusBar,
SafeAreaView,
TouchableOpacity,
TextInput,
Alert,
View,
Text,
} from 'react-native';
import { CustomContainer } from './CustomContainer';
import QuillEditor, { QuillToolbar } from 'react-native-cn-quill';
import type {
SelectionChangeData,
TextChangeData,
} from 'react-native-cn-quill';
import { customFonts } from './customFonts';
const clockIcon = require('../assets/icons/clock.png');
export default class App extends React.Component<any, any> {
private _editor: React.RefObject<QuillEditor>;
constructor(props: any) {
super(props);
this._editor = React.createRef();
this.state = {
disabled: false,
title: 'react-native-cn-quill',
};
}
getCurrentDate() {
let d = new Date(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
handleEnable = () => {
const { disabled } = this.state;
this._editor.current?.enable(disabled);
this.setState({ disabled: !disabled });
};
handleGetHtml = () => {
this._editor.current?.getHtml().then((res) => {
console.log('Html :', res);
Alert.alert(res);
});
};
handleSelectionChange = async (data: SelectionChangeData) => {
const { range } = data;
if (range) {
if (range.length === 0) {
console.log('User cursor is on', range.index);
} else {
var text = await this._editor.current?.getText(
range.index,
range.length
);
console.log('User has highlighted', text);
}
} else {
console.log('Cursor not in the editor');
}
};
handleTextChange = (data: TextChangeData) => {
if (data.source === 'api') {
console.log('An API call triggered this change.');
} else if (data.source === 'user') {
console.log('A user action triggered this change.');
}
};
customHandler = (name: string, value: any) => {
if (name === 'image') {
this._editor.current?.insertEmbed(
0,
'image',
'https://picsum.photos/200/300'
);
} else if (name === 'clock') {
this._editor.current?.insertText(0, `Today is ${this.getCurrentDate()}`, {
bold: true,
color: 'red',
});
} else {
console.log(`${name} clicked with value: ${value}`);
}
};
render() {
const { title, disabled } = this.state;
return (
<SafeAreaView
style={styles.root}
onTouchStart={() => this._editor.current?.blur()}
>
<StatusBar
animated={true}
backgroundColor="#61dafb"
barStyle={'dark-content'}
showHideTransition={'fade'}
hidden={false}
/>
<TextInput
style={[styles.input, styles.textbox]}
onChangeText={(text) => this.setState({ title: text })}
value={title}
/>
<QuillEditor
webview={{
nestedScrollEnabled: true,
}}
container={CustomContainer} // not required just to show how to pass cusom container
style={[styles.input, styles.editor]}
ref={this._editor}
onSelectionChange={this.handleSelectionChange}
onTextChange={this.handleTextChange}
onHtmlChange={({ html }) => console.log(html)}
quill={{
// not required just for to show how to pass this props
placeholder: 'this is placeholder',
modules: {
toolbar: false, // this is default value
},
theme: 'snow', // this is default value
}}
//Extending Blots (from Quill js website example)
//You can also extend existing formats.
//Here is a quick ES6 implementation of a list item that does not permit formatting its contents.
// Code blocks are implemented in exactly this way.
customJS={`
var ListItem = Quill.import('formats/list/item');
class PlainListItem extends ListItem {
formatAt(index, length, name, value) {
if (name === 'list') {
// Allow changing or removing list format
super.formatAt(name, value);
}
// Otherwise ignore
}
}
Quill.register(PlainListItem, true);
`}
defaultFontFamily={customFonts[0].name}
customFonts={customFonts}
import3rdParties="cdn" // default value is 'local'
initialHtml="<h1>Quill Editor for react-native</h1><img src='https://picsum.photos/200/300'/><br/><p>On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.</p>"
/>
<View style={styles.buttons}>
<TouchableOpacity onPress={this.handleEnable} style={styles.btn}>
<Text>{disabled === true ? 'Enable' : 'Disable'}</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.handleGetHtml} style={styles.btn}>
<Text>Html</Text>
</TouchableOpacity>
</View>
<QuillToolbar
editor={this._editor}
theme="light"
styles={{
toolbar: {
provider: (provided) => ({
...provided,
borderTopWidth: 0,
}),
root: (provided) => ({
...provided,
backgroundColor: 'orange',
}),
},
}}
options={[
['bold', 'italic', 'underline'],
[{ header: 1 }, { header: 2 }],
[{ align: [] }],
[
{ color: ['#000000', '#e60000', '#ff9900', 'yellow'] },
{ background: [] },
],
[{ font: ['', customFonts[1].name] }],
['image', 'clock'],
]}
custom={{
handler: this.customHandler,
actions: ['image', 'clock'],
icons: {
clock: clockIcon,
},
}}
/>
</SafeAreaView>
);
}
}
var styles = StyleSheet.create({
root: {
flex: 1,
marginTop: StatusBar.currentHeight || 0,
backgroundColor: '#eaeaea',
},
input: {
borderColor: 'gray',
borderWidth: 1,
marginHorizontal: 30,
marginVertical: 5,
backgroundColor: 'white',
},
textbox: {
height: 40,
paddingHorizontal: 20,
},
editor: {
flex: 1,
padding: 0,
},
buttons: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
btn: {
alignItems: 'center',
backgroundColor: '#ddd',
padding: 10,
margin: 3,
},
});