Sfoglia il codice sorgente

Merge pull request #9 from wix/woa-2914

Add support for adding listeners to selected text change
d4vidi 8 anni fa
parent
commit
7788ddb95a
3 ha cambiato i file con 51 aggiunte e 9 eliminazioni
  1. 23
    7
      src/RichTextEditor.js
  2. 2
    1
      src/const.js
  3. 26
    1
      src/editor.html

+ 23
- 7
src/RichTextEditor.js Vedi File

@@ -32,7 +32,7 @@ export default class RichTextEditor extends Component {
32 32
     this._onKeyboardWillShow = this._onKeyboardWillShow.bind(this);
33 33
     this._onKeyboardWillHide = this._onKeyboardWillHide.bind(this);
34 34
     this.state = {
35
-      listeners: [],
35
+      selectionChangeListeners: [],
36 36
       onChange: [],
37 37
       showLinkDialog: false,
38 38
       linkInitialUrl: '',
@@ -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,25 @@ 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:
160
+          this.state.selectionChangeListeners.map((listener) => {
161
+            listener(items);
162
+          });
163
+          break;
164
+        }
165
+        case messages.CONTENT_CHANGE: {
162 166
           const content = message.data.content;
163 167
           this.state.onChange.map((listener) => listener(content));
164
-          break
168
+          break;
169
+        }
170
+        case messages.SELECTED_TEXT_CHANGED: {
171
+          const selectedText = message.data;
172
+          this._selectedTextChangeListeners.forEach((listener) => {
173
+            listener(selectedText);
174
+          });
175
+          break;
176
+        }
165 177
       }
166 178
     } catch(e) {
167 179
       //alert('NON JSON MESSAGE');
@@ -319,7 +331,7 @@ export default class RichTextEditor extends Component {
319 331
 
320 332
   registerToolbar(listener) {
321 333
     this.setState({
322
-      listeners: [...this.state.listeners, listener]
334
+      selectionChangeListeners: [...this.state.selectionChangeListeners, listener]
323 335
     });
324 336
   }
325 337
   
@@ -559,6 +571,10 @@ export default class RichTextEditor extends Component {
559 571
     this.contentFocusHandler = callbackHandler;
560 572
     this._sendAction(actions.setContentFocusHandler);
561 573
   }
574
+
575
+  addSelectedTextChangeListener(listener) {
576
+    this._selectedTextChangeListeners.push(listener);
577
+  }
562 578
 }
563 579
 
564 580
 const styles = StyleSheet.create({

+ 2
- 1
src/const.js Vedi File

@@ -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
 };

+ 26
- 1
src/editor.html Vedi File

@@ -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) {
@@ -1537,7 +1550,19 @@
1537 1550
 
1538 1551
 				}
1539 1552
 
1540
-				WebViewBridge.send(JSON.stringify({type: 'SELECTION_CHANGE', data: {items: items}}))
1553
+				notifyIfSelectionHasChanged(items);
1554
+			}
1555
+
1556
+			function notifyIfSelectionHasChanged(items) {
1557
+				var selectionChangeMessage = JSON.stringify({
1558
+					data: {items: items},
1559
+					type: 'SELECTION_CHANGE'
1560
+				});
1561
+
1562
+				if (selectionChangeMessage !== zss_editor.previousSelectionChangeMessage) {
1563
+					WebViewBridge.send(selectionChangeMessage);
1564
+					zss_editor.previousSelectionChangeMessage = selectionChangeMessage;
1565
+				}
1541 1566
 			}
1542 1567
 
1543 1568
 			zss_editor.focusContent = function() {