|
@@ -1,5 +1,5 @@
|
1
|
1
|
import React, {Component, PropTypes} from 'react';
|
2
|
|
-import {View, TouchableOpacity, Text} from 'react-native';
|
|
2
|
+import {ListView, View, TouchableOpacity, Text} from 'react-native';
|
3
|
3
|
import {actions} from './const';
|
4
|
4
|
|
5
|
5
|
const defaultActions = [
|
|
@@ -25,17 +25,35 @@ function getDefaultIconText() {
|
25
|
25
|
export default class RichTextToolbar extends Component {
|
26
|
26
|
|
27
|
27
|
static propTypes = {
|
28
|
|
- getEditor: PropTypes.func.isRequired
|
|
28
|
+ getEditor: PropTypes.func.isRequired,
|
|
29
|
+ actions: PropTypes.array,
|
|
30
|
+ onPressAddLink: PropTypes.func,
|
|
31
|
+ onPressAddImage: PropTypes.func
|
29
|
32
|
};
|
30
|
33
|
|
31
|
34
|
constructor(props) {
|
32
|
35
|
super(props);
|
|
36
|
+ const actions = this.props.actions ? this.props.actions : defaultActions;
|
33
|
37
|
this.state = {
|
34
|
38
|
editor: undefined,
|
35
|
|
- selectedItems: []
|
|
39
|
+ selectedItems: [],
|
|
40
|
+ actions,
|
|
41
|
+ ds: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(this.getRows(actions, []))
|
36
|
42
|
};
|
37
|
43
|
}
|
38
|
44
|
|
|
45
|
+ componentDidReceiveProps(newProps) {
|
|
46
|
+ const actions = newProps.actions ? newProps.actions : defaultActions;
|
|
47
|
+ this.setState({
|
|
48
|
+ actions,
|
|
49
|
+ ds: this.state.ds.cloneWithRows(this.getRows(actions, this.state.selectedItems))
|
|
50
|
+ });
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ getRows(actions, selectedItems) {
|
|
54
|
+ return actions.map((action) => {return {action, selected: selectedItems.includes(action)};});
|
|
55
|
+ }
|
|
56
|
+
|
39
|
57
|
componentDidMount() {
|
40
|
58
|
const editor = this.props.getEditor();
|
41
|
59
|
if (!editor) {
|
|
@@ -47,22 +65,25 @@ export default class RichTextToolbar extends Component {
|
47
|
65
|
}
|
48
|
66
|
|
49
|
67
|
setSelectedItems(selectedItems) {
|
50
|
|
- this.setState({
|
51
|
|
- selectedItems
|
52
|
|
- });
|
|
68
|
+ if (selectedItems !== this.state.selectedItems) {
|
|
69
|
+ this.setState({
|
|
70
|
+ selectedItems,
|
|
71
|
+ ds: this.state.ds.cloneWithRows(this.getRows(this.state.actions, selectedItems))
|
|
72
|
+ });
|
|
73
|
+ }
|
53
|
74
|
}
|
54
|
75
|
|
55
|
76
|
|
56
|
77
|
|
57
|
|
- _getButton(action, selected) {
|
|
78
|
+ _renderAction(action, selected) {
|
58
|
79
|
return (
|
59
|
80
|
<TouchableOpacity
|
60
|
81
|
key={action}
|
61
|
|
- style={{flex: 1, backgroundColor: selected? 'red' : '#D3D3D3', justifyContent: 'center'}}
|
|
82
|
+ style={{height: 50, width: 50, backgroundColor: selected? 'red' : undefined, justifyContent: 'center'}}
|
62
|
83
|
onPress={() => this._onPress(action)}
|
63
|
84
|
>
|
64
|
85
|
<Text style={{textAlign: 'center'}}>
|
65
|
|
- {getDefaultIconText()[action]}
|
|
86
|
+ {getDefaultIconText()[action] ? getDefaultIconText()[action] : action.slice(0,1)}
|
66
|
87
|
</Text>
|
67
|
88
|
</TouchableOpacity>
|
68
|
89
|
);
|
|
@@ -70,8 +91,15 @@ export default class RichTextToolbar extends Component {
|
70
|
91
|
|
71
|
92
|
render() {
|
72
|
93
|
return (
|
73
|
|
- <View style={{flexDirection: 'row', height: 50}}>
|
74
|
|
- {defaultActions.map((action) => this._getButton(action, this.state.selectedItems.includes(action)))}
|
|
94
|
+ <View
|
|
95
|
+ style={[{height: 50, backgroundColor: '#D3D3D3', alignItems: 'center'}, this.props.style]}
|
|
96
|
+ >
|
|
97
|
+ <ListView
|
|
98
|
+ horizontal
|
|
99
|
+ contentContainerStyle={{flexDirection: 'row'}}
|
|
100
|
+ dataSource={this.state.ds}
|
|
101
|
+ renderRow= {(row) => this._renderAction(row.action, row.selected)}
|
|
102
|
+ />
|
75
|
103
|
</View>
|
76
|
104
|
);
|
77
|
105
|
}
|
|
@@ -104,7 +132,15 @@ export default class RichTextToolbar extends Component {
|
104
|
132
|
this.state.editor._sendAction(action);
|
105
|
133
|
break;
|
106
|
134
|
case actions.insertLink:
|
|
135
|
+ if(this.props.onPressAddLink) {
|
|
136
|
+ this.props.onPressAddLink();
|
|
137
|
+ }
|
|
138
|
+ break;
|
107
|
139
|
case actions.insertImage:
|
|
140
|
+ if(this.props.onPressAddImage) {
|
|
141
|
+ this.props.onPressAddImage();
|
|
142
|
+ }
|
|
143
|
+ break;
|
108
|
144
|
break;
|
109
|
145
|
}
|
110
|
146
|
}
|