Selaa lähdekoodia

Reactive toolbar

Yedidya Kennard 8 vuotta sitten
vanhempi
commit
48da796fa7
4 muutettua tiedostoa jossa 55 lisäystä ja 32 poistoa
  1. 15
    0
      src/RichTextEditor.js
  2. 13
    4
      src/RichTextToolbar.js
  3. 2
    6
      src/ZSSRichTextEditor/ZSSRichTextEditor.js
  4. 25
    22
      src/const.js

+ 15
- 0
src/RichTextEditor.js Näytä tiedosto

19
   constructor(props) {
19
   constructor(props) {
20
     super(props);
20
     super(props);
21
     this._sendAction = this._sendAction.bind(this);
21
     this._sendAction = this._sendAction.bind(this);
22
+    this.registerToolbar = this.registerToolbar.bind(this);
23
+    this.onBridgeMessage = this.onBridgeMessage.bind(this);
24
+    this.state = {
25
+      listeners: []
26
+    };
22
   }
27
   }
23
 
28
 
24
   onBridgeMessage(str){
29
   onBridgeMessage(str){
65
         case messages.CONTENT_FOCUSED:
70
         case messages.CONTENT_FOCUSED:
66
           this.contentFocusHandler && this.contentFocusHandler();
71
           this.contentFocusHandler && this.contentFocusHandler();
67
           break;
72
           break;
73
+        case messages.SELECTION_CHANGE:
74
+          const items = message.data.items;
75
+          this.state.listeners.map((listener) => listener(items));
76
+          break
68
       }
77
       }
69
     } catch(e) {
78
     } catch(e) {
70
       //alert('NON JSON MESSAGE');
79
       //alert('NON JSON MESSAGE');
91
   //-------------------------------------------------------------------------------
100
   //-------------------------------------------------------------------------------
92
   //--------------- Public API
101
   //--------------- Public API
93
 
102
 
103
+  registerToolbar(listener) {
104
+    this.setState({
105
+      listeners: [...this.state.listeners, listener]
106
+    });
107
+  }
108
+
94
   setTitleHTML(html) {
109
   setTitleHTML(html) {
95
     this._sendAction(actions.setTitleHtml, html);
110
     this._sendAction(actions.setTitleHtml, html);
96
   }
111
   }

+ 13
- 4
src/RichTextToolbar.js Näytä tiedosto

22
   return texts;
22
   return texts;
23
 }
23
 }
24
 
24
 
25
-
26
 export default class RichTextToolbar extends Component {
25
 export default class RichTextToolbar extends Component {
27
 
26
 
28
   static propTypes = {
27
   static propTypes = {
32
   constructor(props) {
31
   constructor(props) {
33
     super(props);
32
     super(props);
34
     this.state = {
33
     this.state = {
35
-      editor: undefined
34
+      editor: undefined,
35
+      selectedItems: []
36
     };
36
     };
37
   }
37
   }
38
 
38
 
41
     if (!editor) {
41
     if (!editor) {
42
       throw new Error('Toolbar has no editor!');
42
       throw new Error('Toolbar has no editor!');
43
     } else {
43
     } else {
44
+      editor.registerToolbar((selectedItems) => this.setSelectedItems(selectedItems));
44
       this.setState({editor});
45
       this.setState({editor});
45
     }
46
     }
46
   }
47
   }
47
 
48
 
49
+  setSelectedItems(selectedItems) {
50
+    this.setState({
51
+      selectedItems
52
+    });
53
+  }
54
+
55
+
56
+
48
   _getButton(action, selected) {
57
   _getButton(action, selected) {
49
     return (
58
     return (
50
       <TouchableOpacity
59
       <TouchableOpacity
51
           key={action}
60
           key={action}
52
-          style={{flex: 1, backgroundColor: '#D3D3D3', justifyContent: 'center'}}
61
+          style={{flex: 1, backgroundColor: selected? 'red' : '#D3D3D3', justifyContent: 'center'}}
53
           onPress={() => this._onPress(action)}
62
           onPress={() => this._onPress(action)}
54
       >
63
       >
55
         <Text style={{textAlign: 'center'}}>
64
         <Text style={{textAlign: 'center'}}>
62
   render() {
71
   render() {
63
     return (
72
     return (
64
       <View style={{flexDirection: 'row', height: 50}}>
73
       <View style={{flexDirection: 'row', height: 50}}>
65
-        {defaultActions.map((action) => this._getButton(action, false))}
74
+        {defaultActions.map((action) => this._getButton(action, this.state.selectedItems.includes(action)))}
66
       </View>
75
       </View>
67
     );
76
     );
68
   }
77
   }

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

698
         }
698
         }
699
         
699
         
700
     }
700
     }
701
-    
702
-    if (items.length > 0) {
703
-        WebViewBridge.send('callback://0/"+items.join(',')');
704
-    } else {
705
-        WebViewBridge.send('zss-callback/');
706
-    }
701
+
702
+    WebViewBridge.send(JSON.stringify({type: 'SELECTION_CHANGE', data: {items}}))
707
 }
703
 }
708
 
704
 
709
 zss_editor.focusEditor = function(editorId) {
705
 zss_editor.focusEditor = function(editorId) {

+ 25
- 22
src/const.js Näytä tiedosto

5
   getContentHtml: 'GET_CONTENT_HTML',
5
   getContentHtml: 'GET_CONTENT_HTML',
6
   blurTitleEditor: 'BLUR_TITLE_EDITOR',
6
   blurTitleEditor: 'BLUR_TITLE_EDITOR',
7
   blurContentEditor: 'BLUR_CONTENT_EDITOR',
7
   blurContentEditor: 'BLUR_CONTENT_EDITOR',
8
-  setBold: 'SET_BOLD',
9
-  setItalic: 'SET_ITALIC',
10
-  setUnderline: 'SET_UNDERLINE',
11
-  heading1: 'HEADING1',
12
-  heading2: 'HEADING2',
13
-  heading3: 'HEADING3',
14
-  heading4: 'HEADING4',
15
-  heading5: 'HEADING5',
16
-  heading6: 'HEADING6',
8
+
9
+  setBold: 'bold',
10
+  setItalic: 'italic',
11
+  setUnderline: 'underline',
12
+  heading1: 'h1',
13
+  heading2: 'h2',
14
+  heading3: 'h3',
15
+  heading4: 'h4',
16
+  heading5: 'h5',
17
+  heading6: 'h6',
17
   setParagraph: 'SET_PARAGRAPH',
18
   setParagraph: 'SET_PARAGRAPH',
18
   removeFormat: 'REMOVE_FORMAT',
19
   removeFormat: 'REMOVE_FORMAT',
19
-  alignLeft: 'ALIGN_LEFT',
20
-  alignCenter: 'ALIGN_CENTER',
21
-  alignRight: 'ALIGN_RIGHT',
22
-  alignFull: 'ALIGN_FULL',
23
-  insertBulletsList: 'INST_BULLETS',
24
-  insertOrderedList: 'INST_ORDERED',
20
+  alignLeft: 'justifyLeft',
21
+  alignCenter: 'justifyCenter',
22
+  alignRight: 'justifyRight',
23
+  alignFull: 'justifyFull',
24
+  insertBulletsList: 'unorderedList',
25
+  insertOrderedList: 'orderedList',
25
   insertLink: 'INST_LINK',
26
   insertLink: 'INST_LINK',
26
   insertImage: 'INST_IMAGE',
27
   insertImage: 'INST_IMAGE',
27
-  setSubscript: 'SET_SUB',
28
-  setSuperscript: 'SET_SUPER',
29
-  setStrikethrough: 'SET_STRIKETHRU',
30
-  setHR: 'SET_HR',
31
-  setIndent: 'SET_INDENT',
32
-  setOutdent: 'SET_OUTDENT',
28
+  setSubscript: 'subscript',
29
+  setSuperscript: 'superscript',
30
+  setStrikethrough: 'strikeThrough',
31
+  setHR: 'horizontalRule',
32
+  setIndent: 'indent',
33
+  setOutdent: 'outdent',
33
   setTitlePlaceholder: 'SET_TITLE_PLACEHOLDER',
34
   setTitlePlaceholder: 'SET_TITLE_PLACEHOLDER',
34
   setContentPlaceholder: 'SET_TITLE_PLACEHOLDER',
35
   setContentPlaceholder: 'SET_TITLE_PLACEHOLDER',
35
   setTitleFocusHandler: 'SET_TITLE_FOCUS_HANDLER',
36
   setTitleFocusHandler: 'SET_TITLE_FOCUS_HANDLER',
36
   setContentFocusHandler: 'SET_CONTENT_FOCUS_HANDLER'
37
   setContentFocusHandler: 'SET_CONTENT_FOCUS_HANDLER'
37
 };
38
 };
38
 
39
 
40
+
39
 export const messages = {
41
 export const messages = {
40
   TITLE_HTML_RESPONSE: 'TITLE_HTML_RESPONSE',
42
   TITLE_HTML_RESPONSE: 'TITLE_HTML_RESPONSE',
41
   CONTENT_HTML_RESPONSE: 'CONTENT_HTML_RESPONSE',
43
   CONTENT_HTML_RESPONSE: 'CONTENT_HTML_RESPONSE',
43
   SCROLL: 'SCROLL',
45
   SCROLL: 'SCROLL',
44
   LOG: 'LOG',
46
   LOG: 'LOG',
45
   TITLE_FOCUSED: 'TITLE_FOCUSED',
47
   TITLE_FOCUSED: 'TITLE_FOCUSED',
46
-  CONTENT_FOCUSED: 'CONTENT_FOCUSED'
48
+  CONTENT_FOCUSED: 'CONTENT_FOCUSED',
49
+  SELECTION_CHANGE: 'SELECTION_CHANGE'
47
 }
50
 }