Pārlūkot izejas kodu

Add support for selected text change notifications

Yevhen Pavliuk 8 gadus atpakaļ
vecāks
revīzija
512e6dc859
3 mainītis faili ar 33 papildinājumiem un 8 dzēšanām
  1. 18
    7
      src/RichTextEditor.js
  2. 2
    1
      src/const.js
  3. 13
    0
      src/editor.html

+ 18
- 7
src/RichTextEditor.js Parādīt failu

@@ -40,6 +40,7 @@ export default class RichTextEditor extends Component {
40 40
       linkUrl: '',
41 41
       keyboardHeight: 0
42 42
     };
43
+    this._selectedTextChangeListeners = [];
43 44
   }
44 45
 
45 46
   componentWillMount() {
@@ -154,14 +155,20 @@ export default class RichTextEditor extends Component {
154 155
         case messages.CONTENT_FOCUSED:
155 156
           this.contentFocusHandler && this.contentFocusHandler();
156 157
           break;
157
-        case messages.SELECTION_CHANGE:
158
+        case messages.SELECTION_CHANGE: {
158 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 173
     } catch(e) {
167 174
       //alert('NON JSON MESSAGE');
@@ -559,6 +566,10 @@ export default class RichTextEditor extends Component {
559 566
     this.contentFocusHandler = callbackHandler;
560 567
     this._sendAction(actions.setContentFocusHandler);
561 568
   }
569
+
570
+  addSelectedTextChangeListener(listener) {
571
+    this._selectedTextChangeListeners.push(listener);
572
+  }
562 573
 }
563 574
 
564 575
 const styles = StyleSheet.create({

+ 2
- 1
src/const.js Parādīt failu

@@ -65,5 +65,6 @@ export const messages = {
65 65
   SELECTION_CHANGE: 'SELECTION_CHANGE',
66 66
   CONTENT_CHANGE: 'CONTENT_CHANGE',
67 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 Parādīt failu

@@ -889,6 +889,7 @@
889 889
 					zss_editor.calculateEditorHeightWithCaretPosition();
890 890
 					zss_editor.setScrollPosition();
891 891
 					zss_editor.enabledEditingItems(e);
892
+					notifyIfSelectedTextHasChanged();
892 893
 				});
893 894
 
894 895
 				// Make sure that when we tap anywhere in the document we focus on the editor
@@ -919,6 +920,18 @@
919 920
 
920 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 935
 			function whenPastingInsertAsPlainText(editorId) {
923 936
 				var editor = document.getElementById(editorId);
924 937
 				editor.addEventListener('paste', function(event) {