Selaa lähdekoodia

Toolbar handles addImage, addLink, accepts actions

Yedidya Kennard 8 vuotta sitten
vanhempi
commit
e580df013f
3 muutettua tiedostoa jossa 51 lisäystä ja 14 poistoa
  1. 2
    1
      index.js
  2. 47
    11
      src/RichTextToolbar.js
  3. 2
    2
      src/ZSSRichTextEditor/ZSSRichTextEditor.js

+ 2
- 1
index.js Näytä tiedosto

1
 import RichTextEditor from './src/RichTextEditor';
1
 import RichTextEditor from './src/RichTextEditor';
2
 import RichTextToolbar from './src/RichTextToolbar';
2
 import RichTextToolbar from './src/RichTextToolbar';
3
+import {actions} from './src/const';
3
 
4
 
4
 module.exports = {
5
 module.exports = {
5
-  RichTextEditor, RichTextToolbar
6
+  RichTextEditor, RichTextToolbar, actions
6
 }
7
 }

+ 47
- 11
src/RichTextToolbar.js Näytä tiedosto

1
 import React, {Component, PropTypes} from 'react';
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
 import {actions} from './const';
3
 import {actions} from './const';
4
 
4
 
5
 const defaultActions = [
5
 const defaultActions = [
25
 export default class RichTextToolbar extends Component {
25
 export default class RichTextToolbar extends Component {
26
 
26
 
27
   static propTypes = {
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
   constructor(props) {
34
   constructor(props) {
32
     super(props);
35
     super(props);
36
+    const actions = this.props.actions ? this.props.actions : defaultActions;
33
     this.state = {
37
     this.state = {
34
       editor: undefined,
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
   componentDidMount() {
57
   componentDidMount() {
40
     const editor = this.props.getEditor();
58
     const editor = this.props.getEditor();
41
     if (!editor) {
59
     if (!editor) {
47
   }
65
   }
48
 
66
 
49
   setSelectedItems(selectedItems) {
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
     return (
79
     return (
59
       <TouchableOpacity
80
       <TouchableOpacity
60
           key={action}
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
           onPress={() => this._onPress(action)}
83
           onPress={() => this._onPress(action)}
63
       >
84
       >
64
         <Text style={{textAlign: 'center'}}>
85
         <Text style={{textAlign: 'center'}}>
65
-          {getDefaultIconText()[action]}
86
+          {getDefaultIconText()[action] ? getDefaultIconText()[action] : action.slice(0,1)}
66
         </Text>
87
         </Text>
67
       </TouchableOpacity>
88
       </TouchableOpacity>
68
     );
89
     );
70
 
91
 
71
   render() {
92
   render() {
72
     return (
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
       </View>
103
       </View>
76
     );
104
     );
77
   }
105
   }
104
         this.state.editor._sendAction(action);
132
         this.state.editor._sendAction(action);
105
         break;
133
         break;
106
       case actions.insertLink:
134
       case actions.insertLink:
135
+        if(this.props.onPressAddLink) {
136
+          this.props.onPressAddLink();
137
+        }
138
+        break;
107
       case actions.insertImage:
139
       case actions.insertImage:
140
+        if(this.props.onPressAddImage) {
141
+          this.props.onPressAddImage();
142
+        }
143
+        break;
108
         break;
144
         break;
109
     }
145
     }
110
   }
146
   }

+ 2
- 2
src/ZSSRichTextEditor/ZSSRichTextEditor.js Näytä tiedosto

284
     var t = current_selection.prop("tagName").toLowerCase();
284
     var t = current_selection.prop("tagName").toLowerCase();
285
     var is_heading = (t == 'h1' || t == 'h2' || t == 'h3' || t == 'h4' || t == 'h5' || t == 'h6');
285
     var is_heading = (t == 'h1' || t == 'h2' || t == 'h3' || t == 'h4' || t == 'h5' || t == 'h6');
286
     if (is_heading && heading == t) {
286
     if (is_heading && heading == t) {
287
-        var c = current_selection.html();
288
-        current_selection.replaceWith(c);
287
+        //var c = current_selection.html();
288
+        //current_selection.replaceWith(c);
289
     } else {
289
     } else {
290
         document.execCommand('formatBlock', false, '<'+heading+'>');
290
         document.execCommand('formatBlock', false, '<'+heading+'>');
291
     }
291
     }