Browse Source

Add support for selected text change notifications

Yevhen Pavliuk 8 years ago
parent
commit
512e6dc859
3 changed files with 33 additions and 8 deletions
  1. 18
    7
      src/RichTextEditor.js
  2. 2
    1
      src/const.js
  3. 13
    0
      src/editor.html

+ 18
- 7
src/RichTextEditor.js View File

40
       linkUrl: '',
40
       linkUrl: '',
41
       keyboardHeight: 0
41
       keyboardHeight: 0
42
     };
42
     };
43
+    this._selectedTextChangeListeners = [];
43
   }
44
   }
44
 
45
 
45
   componentWillMount() {
46
   componentWillMount() {
154
         case messages.CONTENT_FOCUSED:
155
         case messages.CONTENT_FOCUSED:
155
           this.contentFocusHandler && this.contentFocusHandler();
156
           this.contentFocusHandler && this.contentFocusHandler();
156
           break;
157
           break;
157
-        case messages.SELECTION_CHANGE:
158
+        case messages.SELECTION_CHANGE: {
158
           const items = message.data.items;
159
           const items = message.data.items;
159
-          this.state.listeners.map((listener) => listener(items));
160
-          break
161
-        case messages.CONTENT_CHANGE:
162
-          const content = message.data.content;
163
-          this.state.onChange.map((listener) => listener(content));
164
-          break
160
+          this.state.listeners.map((listener) => {
161
+            listener(items);
162
+          });
163
+          break;
164
+        }
165
+        case messages.SELECTED_TEXT_CHANGED: {
166
+          const selectedText = message.data;
167
+          this._selectedTextChangeListeners.forEach((listener) => {
168
+            listener(selectedText);
169
+          });
170
+          break;
171
+        }
165
       }
172
       }
166
     } catch(e) {
173
     } catch(e) {
167
       //alert('NON JSON MESSAGE');
174
       //alert('NON JSON MESSAGE');
559
     this.contentFocusHandler = callbackHandler;
566
     this.contentFocusHandler = callbackHandler;
560
     this._sendAction(actions.setContentFocusHandler);
567
     this._sendAction(actions.setContentFocusHandler);
561
   }
568
   }
569
+
570
+  addSelectedTextChangeListener(listener) {
571
+    this._selectedTextChangeListeners.push(listener);
572
+  }
562
 }
573
 }
563
 
574
 
564
 const styles = StyleSheet.create({
575
 const styles = StyleSheet.create({

+ 2
- 1
src/const.js View File

65
   SELECTION_CHANGE: 'SELECTION_CHANGE',
65
   SELECTION_CHANGE: 'SELECTION_CHANGE',
66
   CONTENT_CHANGE: 'CONTENT_CHANGE',
66
   CONTENT_CHANGE: 'CONTENT_CHANGE',
67
   SELECTED_TEXT_RESPONSE: 'SELECTED_TEXT_RESPONSE',
67
   SELECTED_TEXT_RESPONSE: 'SELECTED_TEXT_RESPONSE',
68
-  LINK_TOUCHED: 'LINK_TOUCHED'
68
+  LINK_TOUCHED: 'LINK_TOUCHED',
69
+  SELECTED_TEXT_CHANGED: 'SELECTED_TEXT_CHANGED'
69
 };
70
 };

+ 13
- 0
src/editor.html View File

889
 					zss_editor.calculateEditorHeightWithCaretPosition();
889
 					zss_editor.calculateEditorHeightWithCaretPosition();
890
 					zss_editor.setScrollPosition();
890
 					zss_editor.setScrollPosition();
891
 					zss_editor.enabledEditingItems(e);
891
 					zss_editor.enabledEditingItems(e);
892
+					notifyIfSelectedTextHasChanged();
892
 				});
893
 				});
893
 
894
 
894
 				// Make sure that when we tap anywhere in the document we focus on the editor
895
 				// Make sure that when we tap anywhere in the document we focus on the editor
919
 
920
 
920
 			}//end
921
 			}//end
921
 
922
 
923
+			function notifyIfSelectedTextHasChanged() {
924
+				var selectedTextChangeMessage = JSON.stringify({
925
+					data: String(getSelection()),
926
+					type: 'SELECTED_TEXT_CHANGED'
927
+				});
928
+
929
+				if (selectedTextChangeMessage !== zss_editor.previousSelectedTextChangeMessage) {
930
+					WebViewBridge.send(selectedTextChangeMessage);
931
+					zss_editor.previousSelectedTextChangeMessage = selectedTextChangeMessage;
932
+				}
933
+			}
934
+
922
 			function whenPastingInsertAsPlainText(editorId) {
935
 			function whenPastingInsertAsPlainText(editorId) {
923
 				var editor = document.getElementById(editorId);
936
 				var editor = document.getElementById(editorId);
924
 				editor.addEventListener('paste', function(event) {
937
 				editor.addEventListener('paste', function(event) {