|
@@ -1,192 +0,0 @@
|
1
|
|
-import React, { Component } from 'react';
|
2
|
|
-import PropTypes from 'prop-types';
|
3
|
|
-import {ListView, View, TouchableOpacity, Image, StyleSheet} from 'react-native';
|
4
|
|
-import {actions} from './const';
|
5
|
|
-
|
6
|
|
-const defaultActions = [
|
7
|
|
- actions.insertImage,
|
8
|
|
- actions.setBold,
|
9
|
|
- actions.setItalic,
|
10
|
|
- actions.insertBulletsList,
|
11
|
|
- actions.insertOrderedList,
|
12
|
|
- actions.insertLink
|
13
|
|
-];
|
14
|
|
-
|
15
|
|
-function getDefaultIcon() {
|
16
|
|
- const texts = {};
|
17
|
|
- texts[actions.insertImage] = require('../img/icon_format_media.png');
|
18
|
|
- texts[actions.setBold] = require('../img/icon_format_bold.png');
|
19
|
|
- texts[actions.setItalic] = require('../img/icon_format_italic.png');
|
20
|
|
- texts[actions.insertBulletsList] = require('../img/icon_format_ul.png');
|
21
|
|
- texts[actions.insertOrderedList] = require('../img/icon_format_ol.png');
|
22
|
|
- texts[actions.insertLink] = require('../img/icon_format_link.png');
|
23
|
|
- return texts;
|
24
|
|
-}
|
25
|
|
-
|
26
|
|
-
|
27
|
|
-export default class RichTextToolbar extends Component {
|
28
|
|
-
|
29
|
|
- static propTypes = {
|
30
|
|
- getEditor: PropTypes.func.isRequired,
|
31
|
|
- actions: PropTypes.array,
|
32
|
|
- onPressAddLink: PropTypes.func,
|
33
|
|
- onPressAddImage: PropTypes.func,
|
34
|
|
- selectedButtonStyle: PropTypes.object,
|
35
|
|
- iconTint: PropTypes.any,
|
36
|
|
- selectedIconTint: PropTypes.any,
|
37
|
|
- unselectedButtonStyle: PropTypes.object,
|
38
|
|
- renderAction: PropTypes.func,
|
39
|
|
- iconMap: PropTypes.object,
|
40
|
|
- };
|
41
|
|
-
|
42
|
|
- constructor(props) {
|
43
|
|
- super(props);
|
44
|
|
- const actions = this.props.actions ? this.props.actions : defaultActions;
|
45
|
|
- this.state = {
|
46
|
|
- editor: undefined,
|
47
|
|
- selectedItems: [],
|
48
|
|
- actions,
|
49
|
|
- ds: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(this.getRows(actions, []))
|
50
|
|
- };
|
51
|
|
- }
|
52
|
|
-
|
53
|
|
- componentDidReceiveProps(newProps) {
|
54
|
|
- const actions = newProps.actions ? newProps.actions : defaultActions;
|
55
|
|
- this.setState({
|
56
|
|
- actions,
|
57
|
|
- ds: this.state.ds.cloneWithRows(this.getRows(actions, this.state.selectedItems))
|
58
|
|
- });
|
59
|
|
- }
|
60
|
|
-
|
61
|
|
- getRows(actions, selectedItems) {
|
62
|
|
- return actions.map((action) => {return {action, selected: selectedItems.includes(action)};});
|
63
|
|
- }
|
64
|
|
-
|
65
|
|
- componentDidMount() {
|
66
|
|
- const editor = this.props.getEditor();
|
67
|
|
- if (!editor) {
|
68
|
|
- throw new Error('Toolbar has no editor!');
|
69
|
|
- } else {
|
70
|
|
- editor.registerToolbar((selectedItems) => this.setSelectedItems(selectedItems));
|
71
|
|
- this.setState({editor});
|
72
|
|
- }
|
73
|
|
- }
|
74
|
|
-
|
75
|
|
- setSelectedItems(selectedItems) {
|
76
|
|
- if (selectedItems !== this.state.selectedItems) {
|
77
|
|
- this.setState({
|
78
|
|
- selectedItems,
|
79
|
|
- ds: this.state.ds.cloneWithRows(this.getRows(this.state.actions, selectedItems))
|
80
|
|
- });
|
81
|
|
- }
|
82
|
|
- }
|
83
|
|
-
|
84
|
|
- _getButtonSelectedStyle() {
|
85
|
|
- return this.props.selectedButtonStyle ? this.props.selectedButtonStyle : styles.defaultSelectedButton;
|
86
|
|
- }
|
87
|
|
-
|
88
|
|
- _getButtonUnselectedStyle() {
|
89
|
|
- return this.props.unselectedButtonStyle ? this.props.unselectedButtonStyle : styles.defaultUnselectedButton;
|
90
|
|
- }
|
91
|
|
-
|
92
|
|
- _getButtonIcon(action) {
|
93
|
|
- if (this.props.iconMap && this.props.iconMap[action]) {
|
94
|
|
- return this.props.iconMap[action];
|
95
|
|
- } else if (getDefaultIcon()[action]){
|
96
|
|
- return getDefaultIcon()[action];
|
97
|
|
- } else {
|
98
|
|
- return undefined;
|
99
|
|
- }
|
100
|
|
- }
|
101
|
|
-
|
102
|
|
- _defaultRenderAction(action, selected) {
|
103
|
|
- const icon = this._getButtonIcon(action);
|
104
|
|
- return (
|
105
|
|
- <TouchableOpacity
|
106
|
|
- key={action}
|
107
|
|
- style={[
|
108
|
|
- {height: 50, width: 50, justifyContent: 'center'},
|
109
|
|
- selected ? this._getButtonSelectedStyle() : this._getButtonUnselectedStyle()
|
110
|
|
- ]}
|
111
|
|
- onPress={() => this._onPress(action)}
|
112
|
|
- >
|
113
|
|
- {icon ? <Image source={icon} style={{tintColor: selected ? this.props.selectedIconTint : this.props.iconTint}}/> : null}
|
114
|
|
- </TouchableOpacity>
|
115
|
|
- );
|
116
|
|
- }
|
117
|
|
-
|
118
|
|
- _renderAction(action, selected) {
|
119
|
|
- return this.props.renderAction ?
|
120
|
|
- this.props.renderAction(action, selected) :
|
121
|
|
- this._defaultRenderAction(action, selected);
|
122
|
|
- }
|
123
|
|
-
|
124
|
|
- render() {
|
125
|
|
- return (
|
126
|
|
- <View
|
127
|
|
- style={[{height: 50, backgroundColor: '#D3D3D3', alignItems: 'center'}, this.props.style]}
|
128
|
|
- >
|
129
|
|
- <ListView
|
130
|
|
- horizontal
|
131
|
|
- contentContainerStyle={{flexDirection: 'row'}}
|
132
|
|
- dataSource={this.state.ds}
|
133
|
|
- renderRow= {(row) => this._renderAction(row.action, row.selected)}
|
134
|
|
- />
|
135
|
|
- </View>
|
136
|
|
- );
|
137
|
|
- }
|
138
|
|
-
|
139
|
|
- _onPress(action) {
|
140
|
|
- switch(action) {
|
141
|
|
- case actions.setBold:
|
142
|
|
- case actions.setItalic:
|
143
|
|
- case actions.insertBulletsList:
|
144
|
|
- case actions.insertOrderedList:
|
145
|
|
- case actions.setUnderline:
|
146
|
|
- case actions.heading1:
|
147
|
|
- case actions.heading2:
|
148
|
|
- case actions.heading3:
|
149
|
|
- case actions.heading4:
|
150
|
|
- case actions.heading5:
|
151
|
|
- case actions.heading6:
|
152
|
|
- case actions.setParagraph:
|
153
|
|
- case actions.removeFormat:
|
154
|
|
- case actions.alignLeft:
|
155
|
|
- case actions.alignCenter:
|
156
|
|
- case actions.alignRight:
|
157
|
|
- case actions.alignFull:
|
158
|
|
- case actions.setSubscript:
|
159
|
|
- case actions.setSuperscript:
|
160
|
|
- case actions.setStrikethrough:
|
161
|
|
- case actions.setHR:
|
162
|
|
- case actions.setIndent:
|
163
|
|
- case actions.setOutdent:
|
164
|
|
- this.state.editor._sendAction(action);
|
165
|
|
- break;
|
166
|
|
- case actions.insertLink:
|
167
|
|
- this.state.editor.prepareInsert();
|
168
|
|
- if(this.props.onPressAddLink) {
|
169
|
|
- this.props.onPressAddLink();
|
170
|
|
- } else {
|
171
|
|
- this.state.editor.getSelectedText().then(selectedText => {
|
172
|
|
- this.state.editor.showLinkDialog(selectedText);
|
173
|
|
- });
|
174
|
|
- }
|
175
|
|
- break;
|
176
|
|
- case actions.insertImage:
|
177
|
|
- this.state.editor.prepareInsert();
|
178
|
|
- if(this.props.onPressAddImage) {
|
179
|
|
- this.props.onPressAddImage();
|
180
|
|
- }
|
181
|
|
- break;
|
182
|
|
- break;
|
183
|
|
- }
|
184
|
|
- }
|
185
|
|
-}
|
186
|
|
-
|
187
|
|
-const styles = StyleSheet.create({
|
188
|
|
- defaultSelectedButton: {
|
189
|
|
- backgroundColor: 'red'
|
190
|
|
- },
|
191
|
|
- defaultUnselectedButton: {}
|
192
|
|
-});
|