zhangchao 4 gadus atpakaļ
vecāks
revīzija
d160e2abdd
4 mainītis faili ar 146 papildinājumiem un 206 dzēšanām
  1. 0
    3
      package.json
  2. 22
    21
      src/RichTextEditor.js
  3. 115
    173
      src/WebviewMessageHandler.js
  4. 9
    9
      src/editor.html

+ 0
- 3
package.json Parādīt failu

@@ -8,9 +8,6 @@
8 8
   "description": "React Native Wrapper for ZSSRichTextEditor",
9 9
   "main": "index.js",
10 10
   "license": "SEE LICENSE IN LICENSE",
11
-  "dependencies": {
12
-    "react-native-webview-bridge-updated": "^1.0.0"
13
-  },
14 11
   "peerDependencies": {
15 12
     "react": "*",
16 13
     "react-native": ">=0.31.0"

+ 22
- 21
src/RichTextEditor.js Parādīt failu

@@ -1,15 +1,16 @@
1 1
 import React, {Component} from 'react';
2 2
 import PropTypes from 'prop-types';
3
-import WebViewBridge from 'react-native-webview-bridge-updated';
4
-import {InjectedMessageHandler} from './WebviewMessageHandler';
3
+// import WebViewBridge from 'react-native-webview-bridge-updated';
4
+import WebView from 'react-native-webview';
5
+import {MessageConverter} from './WebviewMessageHandler';
5 6
 import {actions, messages} from './const';
6 7
 import {Modal, View, Text, StyleSheet, TextInput, TouchableOpacity, Platform, PixelRatio, Keyboard, Dimensions} from 'react-native';
7 8
 
8
-const injectScript = `
9
-  (function () {
10
-    ${InjectedMessageHandler}
11
-  }());
12
-`;
9
+// const injectScript = `
10
+//   (function () {
11
+//     ${InjectedMessageHandler}
12
+//   }());
13
+// `;
13 14
 
14 15
 const PlatformIOS = Platform.OS === 'ios';
15 16
 
@@ -36,7 +37,7 @@ export default class RichTextEditor extends Component {
36 37
     super(props);
37 38
     this._sendAction = this._sendAction.bind(this);
38 39
     this.registerToolbar = this.registerToolbar.bind(this);
39
-    this.onBridgeMessage = this.onBridgeMessage.bind(this);
40
+    this.onMessage = this.onMessage.bind(this);
40 41
     this._onKeyboardWillShow = this._onKeyboardWillShow.bind(this);
41 42
     this._onKeyboardWillHide = this._onKeyboardWillHide.bind(this);
42 43
     this.state = {
@@ -51,7 +52,7 @@ export default class RichTextEditor extends Component {
51 52
     this._selectedTextChangeListeners = [];
52 53
   }
53 54
 
54
-  componentWillMount() {
55
+  componentDidMount() {
55 56
     if(PlatformIOS) {
56 57
       this.keyboardEventListeners = [
57 58
         Keyboard.addListener('keyboardWillShow', this._onKeyboardWillShow),
@@ -94,7 +95,8 @@ export default class RichTextEditor extends Component {
94 95
     this.setEditorHeight(editorAvailableHeight);
95 96
   }
96 97
 
97
-  onBridgeMessage(str){
98
+  onMessage({ nativeEvent }){
99
+    const { data: str } = nativeEvent;
98 100
     try {
99 101
       const message = JSON.parse(str);
100 102
 
@@ -149,8 +151,8 @@ export default class RichTextEditor extends Component {
149 151
           }
150 152
           this.setTitlePlaceholder(this.props.titlePlaceholder);
151 153
           this.setContentPlaceholder(this.props.contentPlaceholder);
152
-          this.setTitleHTML(this.props.initialTitleHTML);
153
-          this.setContentHTML(this.props.initialContentHTML);
154
+          this.setTitleHTML(this.props.initialTitleHTML || '');
155
+          this.setContentHTML(this.props.initialContentHTML || '');
154 156
 
155 157
           this.props.hiddenTitle && this.hideTitle();
156 158
           this.props.enableOnChange && this.enableOnChange();
@@ -167,7 +169,7 @@ export default class RichTextEditor extends Component {
167 169
           console.log('FROM ZSS', message.data);
168 170
           break;
169 171
         case messages.SCROLL:
170
-          this.webviewBridge.setNativeProps({contentOffset: {y: message.data}});
172
+          this.webview.setNativeProps({contentOffset: {y: message.data}});
171 173
           break;
172 174
         case messages.TITLE_FOCUSED:
173 175
           this.titleFocusHandler && this.titleFocusHandler();
@@ -294,13 +296,13 @@ export default class RichTextEditor extends Component {
294 296
     const pageSource = PlatformIOS ? require('./editor.html') : { uri: 'file:///android_asset/editor.html' };
295 297
     return (
296 298
       <View style={{flex: 1}}>
297
-        <WebViewBridge
299
+        <WebView
298 300
           {...this.props}
299 301
           hideKeyboardAccessoryView={true}
300 302
           keyboardDisplayRequiresUserAction={false}
301
-          ref={(r) => {this.webviewBridge = r}}
302
-          onBridgeMessage={(message) => this.onBridgeMessage(message)}
303
-          injectedJavaScript={injectScript}
303
+          ref={(r) => {this.webview = r}}
304
+          onMessage={(message) => this.onMessage(message)}
305
+          // injectedJavaScript={injectScript}
304 306
           source={pageSource}
305 307
           onLoad={() => this.init()}
306 308
         />
@@ -323,9 +325,8 @@ export default class RichTextEditor extends Component {
323 325
   };
324 326
 
325 327
   _sendAction(action, data) {
326
-    let jsonString = JSON.stringify({type: action, data});
327
-    jsonString = this.escapeJSONString(jsonString);
328
-    this.webviewBridge.sendToBridge(jsonString);
328
+    let jsToBeExecutedOnPage = MessageConverter({ type: action, data });
329
+    this.webview.injectJavaScript(jsToBeExecutedOnPage + ';true;');
329 330
   }
330 331
 
331 332
   //-------------------------------------------------------------------------------
@@ -654,4 +655,4 @@ const styles = StyleSheet.create({
654 655
     marginRight: -20,
655 656
     marginTop: 20
656 657
   }
657
-});
658
+});

+ 115
- 173
src/WebviewMessageHandler.js Parādīt failu

@@ -1,176 +1,118 @@
1 1
 import {actions, messages} from './const';
2 2
 
3
-export const InjectedMessageHandler = `
4
-  if (WebViewBridge) {
5
-    WebViewBridge.onMessage = function (message) {
6
-
7
-      const action = JSON.parse(message);
8
-
9
-      switch(action.type) {
10
-        case '${actions.enableOnChange}':
11
-          zss_editor.enableOnChange();
12
-          break;
13
-        case '${actions.setTitleHtml}':
14
-          zss_editor.setTitleHTML(action.data);
15
-          break;
16
-        case '${actions.toggleTitle}':
17
-          zss_editor.toggleTitle(action.data);
18
-          break;
19
-        case '${actions.hideTitle}':
20
-          zss_editor.hideTitle(action.data);
21
-          break;
22
-        case '${actions.showTitle}':
23
-          zss_editor.showTitle(action.data);
24
-          break;
25
-        case '${actions.setContentHtml}':
26
-          zss_editor.setContentHTML(action.data);
27
-          break;
28
-        case '${actions.blurTitleEditor}':
29
-          zss_editor.blurTitleEditor();
30
-          break;
31
-        case '${actions.blurContentEditor}':
32
-          zss_editor.blurContentEditor();
33
-          break;
34
-        case '${actions.setBold}':
35
-          zss_editor.setBold();
36
-          break;
37
-        case '${actions.setItalic}':
38
-          zss_editor.setItalic();
39
-          break;
40
-        case '${actions.setUnderline}':
41
-          zss_editor.setUnderline();
42
-          break;
43
-        case '${actions.heading1}':
44
-          zss_editor.setHeading('h1');
45
-          break;
46
-        case '${actions.heading2}':
47
-          zss_editor.setHeading('h2');
48
-          break;
49
-        case '${actions.heading3}':
50
-          zss_editor.setHeading('h3');
51
-          break;
52
-        case '${actions.heading4}':
53
-          zss_editor.setHeading('h4');
54
-          break;
55
-        case '${actions.heading5}':
56
-          zss_editor.setHeading('h5');
57
-          break;
58
-        case '${actions.heading6}':
59
-          zss_editor.setHeading('h6');
60
-          break;
61
-        case '${actions.setParagraph}':
62
-          zss_editor.setParagraph();
63
-          break;
64
-        case '${actions.removeFormat}':
65
-          zss_editor.removeFormating();
66
-          break;
67
-        case '${actions.alignLeft}':
68
-          zss_editor.setJustifyLeft();
69
-          break;
70
-        case '${actions.alignCenter}':
71
-          zss_editor.setJustifyCenter();
72
-          break;
73
-        case '${actions.alignRight}':
74
-          zss_editor.setJustifyRight();
75
-          break;
76
-        case '${actions.alignFull}':
77
-          zss_editor.setJustifyFull();
78
-          break;
79
-        case '${actions.insertBulletsList}':
80
-          zss_editor.setUnorderedList();
81
-          break;
82
-        case '${actions.insertOrderedList}':
83
-          zss_editor.setOrderedList();
84
-          break;
85
-        case '${actions.insertLink}':
86
-          zss_editor.insertLink(action.data.url, action.data.title);
87
-          break;
88
-        case '${actions.updateLink}':
89
-          zss_editor.updateLink(action.data.url, action.data.title);
90
-          break;
91
-        case '${actions.insertImage}':
92
-          zss_editor.insertImage(action.data);
93
-          break;
94
-        case '${actions.setSubscript}':
95
-          zss_editor.setSubscript();
96
-          break;
97
-        case '${actions.setSuperscript}':
98
-          zss_editor.setSuperscript();
99
-          break;
100
-        case '${actions.setStrikethrough}':
101
-          zss_editor.setStrikeThrough();
102
-          break;
103
-        case '${actions.setHR}':
104
-          zss_editor.setHorizontalRule();
105
-          break;
106
-        case '${actions.setIndent}':
107
-          zss_editor.setIndent();
108
-          break;
109
-        case '${actions.setOutdent}':
110
-          zss_editor.setOutdent();
111
-          break;
112
-        case '${actions.setTitlePlaceholder}':
113
-          zss_editor.setTitlePlaceholder(action.data);
114
-          break;
115
-        case '${actions.setContentPlaceholder}':
116
-          zss_editor.setContentPlaceholder(action.data);
117
-          break;
118
-        case '${actions.getTitleHtml}':
119
-          var html = zss_editor.getTitleHTML();
120
-          WebViewBridge.send(JSON.stringify({type: '${messages.TITLE_HTML_RESPONSE}', data: html}));
121
-          break;
122
-        case '${actions.getTitleText}':
123
-          var html = zss_editor.getTitleText();
124
-          WebViewBridge.send(JSON.stringify({type: '${messages.TITLE_TEXT_RESPONSE}', data: html}));
125
-          break;
126
-        case '${actions.getContentHtml}':
127
-          var html = zss_editor.getContentHTML();
128
-          WebViewBridge.send(JSON.stringify({type: '${messages.CONTENT_HTML_RESPONSE}', data: html}));
129
-          break;
130
-        case '${actions.setTitleFocusHandler}':
131
-          zss_editor.setTitleFocusHandler();
132
-          break;
133
-        case '${actions.setContentFocusHandler}':
134
-          zss_editor.setContentFocusHandler();
135
-          break;
136
-        case '${actions.getSelectedText}':
137
-          var selectedText = getSelection().toString();
138
-          WebViewBridge.send(JSON.stringify({type: '${messages.SELECTED_TEXT_RESPONSE}', data: selectedText}));
139
-          break;
140
-        case '${actions.focusContent}':
141
-          zss_editor.focusContent();
142
-          break;
143
-        case '${actions.focusTitle}':
144
-          zss_editor.focusTitle();
145
-          break;
146
-        case '${actions.prepareInsert}':
147
-          zss_editor.prepareInsert();
148
-          break;
149
-        case '${actions.restoreSelection}':
150
-          zss_editor.restorerange();
151
-          break;
152
-        case '${actions.setCustomCSS}':
153
-          zss_editor.setCustomCSS(action.data);
154
-          break;
155
-        case '${actions.setTextColor}':
156
-          zss_editor.setTextColor(action.data);
157
-          break;
158
-        case '${actions.setBackgroundColor}':
159
-          zss_editor.setBackgroundColor(action.data);
160
-          break;
161
-        case '${actions.init}':
162
-          zss_editor.init();
163
-          break;
164
-        case '${actions.setEditorHeight}':
165
-          zss_editor.setEditorHeight(action.data);
166
-          break;
167
-        case '${actions.setFooterHeight}':
168
-          zss_editor.setFooterHeight(action.data);
169
-          break;
170
-        case '${actions.setPlatform}':
171
-          zss_editor.setPlatform(action.data);
172
-          break;
173
-      }
174
-    };
3
+export const MessageConverter = (action) => {
4
+  switch(action.type) {
5
+    case `${actions.init}`:
6
+      return 'zss_editor.init();';
7
+    case `${actions.setPlatform}`:
8
+      return `zss_editor.setPlatform('${action.data}');`;
9
+    case `${actions.enableOnChange}`:
10
+      return 'zss_editor.enableOnChange()';
11
+    case `${actions.setTitleHtml}`:
12
+      return `zss_editor.setTitleHTML('${action.data}');`;
13
+    case `${actions.toggleTitle}`:
14
+      return `zss_editor.toggleTitle('${action.data}');`;
15
+    case `${actions.hideTitle}`:
16
+      return `zss_editor.hideTitle('${action.data}');`;
17
+    case `${actions.showTitle}`:
18
+      return `zss_editor.showTitle('${action.data}');`;
19
+    case `${actions.setContentHtml}`:
20
+      return `zss_editor.setContentHTML('${action.data}');`;
21
+    case `${actions.blurTitleEditor}`:
22
+      return `zss_editor.blurTitleEditor();`;
23
+    case `${actions.blurContentEditor}`:
24
+      return `zss_editor.blurContentEditor();`;
25
+    case `${actions.setBold}`:
26
+      return `zss_editor.setBold();`;
27
+    case `${actions.setItalic}`:
28
+      return `zss_editor.setItalic();`;
29
+    case `${actions.setUnderline}`:
30
+      return `zss_editor.setUnderline();`;
31
+    case `${actions.heading1}`:
32
+      return `zss_editor.setHeading('h1');`;
33
+    case `${actions.heading2}`:
34
+      return `zss_editor.setHeading('h2');`;
35
+    case `${actions.heading3}`:
36
+      return `zss_editor.setHeading('h3');`;
37
+    case `${actions.heading4}`:
38
+      return `zss_editor.setHeading('h4');`;
39
+    case `${actions.heading5}`:
40
+      return `zss_editor.setHeading('h5');`;
41
+    case `${actions.heading6}`:
42
+      return `zss_editor.setHeading('h6');`;
43
+    case `${actions.setParagraph}`:
44
+      return `zss_editor.setParagraph();`;
45
+    case `${actions.removeFormat}`:
46
+      return `zss_editor.removeFormating();`;
47
+    case `${actions.alignLeft}`:
48
+      return `zss_editor.setJustifyLeft();`;
49
+    case `${actions.alignCenter}`:
50
+      return `zss_editor.setJustifyCenter();`;
51
+    case `${actions.alignRight}`:
52
+      return `zss_editor.setJustifyRight();`;
53
+    case `${actions.alignFull}`:
54
+      return `zss_editor.setJustifyFull();`;
55
+    case `${actions.insertBulletsList}`:
56
+      return `zss_editor.setUnorderedList();`;
57
+    case `${actions.insertOrderedList}`:
58
+      return `zss_editor.setOrderedList();`;
59
+    case `${actions.insertLink}`:
60
+      return `zss_editor.insertLink('${action.data.url}, ${action.data.title}');`;
61
+    case `${actions.updateLink}`:
62
+      return `zss_editor.updateLink('${action.data.url}, ${action.data.title}');`;
63
+    case `${actions.insertImage}`:
64
+      return `zss_editor.insertImage('${action.data}');`;
65
+    case `${actions.setSubscript}`:
66
+      return `zss_editor.setSubscript();`;
67
+    case `${actions.setSuperscript}`:
68
+      return `zss_editor.setSuperscript();`;
69
+    case `${actions.setStrikethrough}`:
70
+      return `zss_editor.setStrikeThrough();`;
71
+    case `${actions.setHR}`:
72
+      return `zss_editor.setHorizontalRule();`;
73
+    case `${actions.setIndent}`:
74
+      return `zss_editor.setIndent();`;
75
+    case `${actions.setOutdent}`:
76
+      return `zss_editor.setOutdent();`;
77
+    case `${actions.setTitlePlaceholder}`:
78
+      return `zss_editor.setTitlePlaceholder('${action.data}');`;
79
+    case `${actions.setContentPlaceholder}`:
80
+      return `zss_editor.setContentPlaceholder('${action.data}');`;
81
+    case `${actions.focusContent}`:
82
+      return `zss_editor.focusContent();`;
83
+    case `${actions.focusTitle}`:
84
+      return `zss_editor.focusTitle();`;
85
+    case `${actions.prepareInsert}`:
86
+      return `zss_editor.prepareInsert();`;
87
+    case `${actions.restoreSelection}`:
88
+      return `zss_editor.restorerange();`;
89
+    case `${actions.setCustomCSS}`:
90
+      return `zss_editor.setCustomCSS('${action.data}');`;
91
+    case `${actions.setTextColor}`:
92
+      return `zss_editor.setTextColor('${action.data}');`;
93
+    case `${actions.setFontSize}`:
94
+      return `zss_editor.setFontSize('${action.data}');`;
95
+    case `${actions.setBackgroundColor}`:
96
+      return `zss_editor.setBackgroundColor('${action.data}');`;
97
+    case `${actions.setEditorHeight}`:
98
+      return `zss_editor.setEditorHeight('${action.data}');`;
99
+    case `${actions.setFooterHeight}`:
100
+      return `zss_editor.setFooterHeight('${action.data}');`;
101
+    case `${actions.setTitleFocusHandler}`:
102
+      return `zss_editor.setTitleFocusHandler();`;
103
+    case `${actions.setContentFocusHandler}`:
104
+      return `zss_editor.setContentFocusHandler();`;
105
+    case `${actions.getTitleHtml}`:
106
+      return `var html = zss_editor.getTitleHTML();
107
+      ReactNativeWebView.postMessage(JSON.stringify({type: '${messages.TITLE_HTML_RESPONSE}', data: html}));`
108
+    case `${actions.getTitleText}`:
109
+      return `var html = zss_editor.getTitleText();
110
+      ReactNativeWebView.postMessage(JSON.stringify({type: '${messages.TITLE_TEXT_RESPONSE}', data: html}));`
111
+    case `${actions.getContentHtml}`:
112
+      return `var html = zss_editor.getContentHTML();
113
+      ReactNativeWebView.postMessage(JSON.stringify({type: '${messages.CONTENT_HTML_RESPONSE}', data: html}));`
114
+    case `${actions.getSelectedText}`:
115
+      return `var selectedText = getSelection().toString();
116
+      ReactNativeWebView.postMessage(JSON.stringify({type: '${messages.SELECTED_TEXT_RESPONSE}', data: selectedText}));`
175 117
   }
176
-`;
118
+};

+ 9
- 9
src/editor.html Parādīt failu

@@ -853,7 +853,7 @@
853 853
 						title: anchor.text(),
854 854
 						url: anchor.attr('href')
855 855
 					};
856
-					WebViewBridge.send(JSON.stringify({type: 'LINK_TOUCHED', data: link}))
856
+					ReactNativeWebView.postMessage(JSON.stringify({type: 'LINK_TOUCHED', data: link}))
857 857
 				});
858 858
 			}
859 859
 
@@ -920,7 +920,7 @@
920 920
 				// zss_editor.focusTitle();
921 921
 
922 922
 				setTimeout(function() {
923
-					WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
923
+					ReactNativeWebView.postMessage(JSON.stringify({type: 'ZSS_INITIALIZED'}))
924 924
 				}, 20);
925 925
 
926 926
 			}//end
@@ -932,7 +932,7 @@
932 932
 				});
933 933
 
934 934
 				if (selectedTextChangeMessage !== zss_editor.previousSelectedTextChangeMessage) {
935
-					WebViewBridge.send(selectedTextChangeMessage);
935
+					ReactNativeWebView.postMessage(selectedTextChangeMessage);
936 936
 					zss_editor.previousSelectedTextChangeMessage = selectedTextChangeMessage;
937 937
 				}
938 938
 			}
@@ -987,13 +987,13 @@
987 987
 
988 988
 			// This will show up in the XCode console as we are able to push this into an NSLog.
989 989
 			zss_editor.debug = function(msg) {
990
-				WebViewBridge.send(JSON.stringify({type: 'LOG', data: msg}));
990
+				ReactNativeWebView.postMessage(JSON.stringify({type: 'LOG', data: msg}));
991 991
 			}
992 992
 
993 993
 
994 994
 			zss_editor.setScrollPosition = function() {
995 995
 				var position = window.pageYOffset;
996
-				WebViewBridge.send(JSON.stringify({type: 'SCROLL', data: position}));
996
+				ReactNativeWebView.postMessage(JSON.stringify({type: 'SCROLL', data: position}));
997 997
 			}
998 998
 
999 999
 			function setPlaceholder(editorId, placeholder) {
@@ -1378,7 +1378,7 @@
1378 1378
 
1379 1379
 			zss_editor.enableOnChange = function() {
1380 1380
 				$('#zss_editor_content').on('input', function(){
1381
-					WebViewBridge.send(JSON.stringify({
1381
+					ReactNativeWebView.postMessage(JSON.stringify({
1382 1382
 						type: 'CONTENT_CHANGE',
1383 1383
 						data: {content: getHtml("zss_editor_content")}
1384 1384
 					}))
@@ -1566,7 +1566,7 @@
1566 1566
 				});
1567 1567
 
1568 1568
 				if (selectionChangeMessage !== zss_editor.previousSelectionChangeMessage) {
1569
-					WebViewBridge.send(selectionChangeMessage);
1569
+					ReactNativeWebView.postMessage(selectionChangeMessage);
1570 1570
 					zss_editor.previousSelectionChangeMessage = selectionChangeMessage;
1571 1571
 				}
1572 1572
 			}
@@ -1625,13 +1625,13 @@
1625 1625
 
1626 1626
 			zss_editor.setTitleFocusHandler = function() {
1627 1627
 				addFocusEvent('zss_editor_title', function() {
1628
-					WebViewBridge.send(JSON.stringify({type: 'TITLE_FOCUSED'}))
1628
+					ReactNativeWebView.postMessage(JSON.stringify({type: 'TITLE_FOCUSED'}))
1629 1629
 				});
1630 1630
 			}
1631 1631
 
1632 1632
 			zss_editor.setContentFocusHandler = function() {
1633 1633
 				addFocusEvent('zss_editor_content', function() {
1634
-					WebViewBridge.send(JSON.stringify({type: 'CONTENT_FOCUSED'}))
1634
+					ReactNativeWebView.postMessage(JSON.stringify({type: 'CONTENT_FOCUSED'}))
1635 1635
 				});
1636 1636
 			}
1637 1637