瀏覽代碼

Reactive toolbar

Yedidya Kennard 8 年之前
父節點
當前提交
48da796fa7
共有 4 個文件被更改,包括 55 次插入32 次删除
  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 查看文件

@@ -19,6 +19,11 @@ export default class RichTextEditor extends Component {
19 19
   constructor(props) {
20 20
     super(props);
21 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 29
   onBridgeMessage(str){
@@ -65,6 +70,10 @@ export default class RichTextEditor extends Component {
65 70
         case messages.CONTENT_FOCUSED:
66 71
           this.contentFocusHandler && this.contentFocusHandler();
67 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 78
     } catch(e) {
70 79
       //alert('NON JSON MESSAGE');
@@ -91,6 +100,12 @@ export default class RichTextEditor extends Component {
91 100
   //-------------------------------------------------------------------------------
92 101
   //--------------- Public API
93 102
 
103
+  registerToolbar(listener) {
104
+    this.setState({
105
+      listeners: [...this.state.listeners, listener]
106
+    });
107
+  }
108
+
94 109
   setTitleHTML(html) {
95 110
     this._sendAction(actions.setTitleHtml, html);
96 111
   }

+ 13
- 4
src/RichTextToolbar.js 查看文件

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

+ 2
- 6
src/ZSSRichTextEditor/ZSSRichTextEditor.js 查看文件

@@ -698,12 +698,8 @@ zss_editor.enabledEditingItems = function(e) {
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 705
 zss_editor.focusEditor = function(editorId) {

+ 25
- 22
src/const.js 查看文件

@@ -5,37 +5,39 @@ export const actions = {
5 5
   getContentHtml: 'GET_CONTENT_HTML',
6 6
   blurTitleEditor: 'BLUR_TITLE_EDITOR',
7 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 18
   setParagraph: 'SET_PARAGRAPH',
18 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 26
   insertLink: 'INST_LINK',
26 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 34
   setTitlePlaceholder: 'SET_TITLE_PLACEHOLDER',
34 35
   setContentPlaceholder: 'SET_TITLE_PLACEHOLDER',
35 36
   setTitleFocusHandler: 'SET_TITLE_FOCUS_HANDLER',
36 37
   setContentFocusHandler: 'SET_CONTENT_FOCUS_HANDLER'
37 38
 };
38 39
 
40
+
39 41
 export const messages = {
40 42
   TITLE_HTML_RESPONSE: 'TITLE_HTML_RESPONSE',
41 43
   CONTENT_HTML_RESPONSE: 'CONTENT_HTML_RESPONSE',
@@ -43,5 +45,6 @@ export const messages = {
43 45
   SCROLL: 'SCROLL',
44 46
   LOG: 'LOG',
45 47
   TITLE_FOCUSED: 'TITLE_FOCUSED',
46
-  CONTENT_FOCUSED: 'CONTENT_FOCUSED'
48
+  CONTENT_FOCUSED: 'CONTENT_FOCUSED',
49
+  SELECTION_CHANGE: 'SELECTION_CHANGE'
47 50
 }