Browse Source

added focus handlers + editor initialized callback

Artal Druk 8 years ago
parent
commit
d3379d3928
5 changed files with 59 additions and 12 deletions
  1. 13
    5
      example/app.js
  2. 15
    3
      src/RichTextEditor.js
  3. 6
    0
      src/WebviewMessageHandler.js
  4. 17
    0
      src/ZSSRichTextEditor/ZSSRichTextEditor.js
  5. 8
    4
      src/const.js

+ 13
- 5
example/app.js View File

@@ -22,6 +22,7 @@ export default class RichTextExample extends Component {
22 22
               style={styles.richText}
23 23
               initialTitleHTML={'Title!!'}
24 24
               initialContentHTML={'Hello <b>World</b> <p>this is a new paragraph</p> <p>this is another new paragraph</p>'}
25
+              editorInitializedCallback={() => this.onEditorInitialized()}
25 26
           />
26 27
           <RichTextToolbar
27 28
             getEditor={() => this.richtext}
@@ -31,17 +32,24 @@ export default class RichTextExample extends Component {
31 32
     );
32 33
   }
33 34
 
35
+  onEditorInitialized() {
36
+    this.setFocusHandlers();
37
+    this.getHTML();
38
+  }
39
+
34 40
   async getHTML() {
35 41
     const titleHtml = await this.richtext.getTitleHtml();
36 42
     const contentHtml = await this.richtext.getContentHtml();
37 43
     //alert(titleHtml + ' ' + contentHtml)
38 44
   }
39 45
 
40
-  componentDidMount() {
41
-
42
-    setTimeout(()=>{
43
-      this.getHTML();
44
-    }, 3000);
46
+  setFocusHandlers() {
47
+    this.richtext.setTitleFocusHandler(() => {
48
+      //alert('title focus');
49
+    });
50
+    this.richtext.setContentFocusHandler(() => {
51
+      //alert('content focus');
52
+    });
45 53
   }
46 54
 }
47 55
 

+ 15
- 3
src/RichTextEditor.js View File

@@ -13,6 +13,7 @@ export default class RichTextEditor extends Component {
13 13
   static propTypes = {
14 14
     initialTitleHTML: PropTypes.string,
15 15
     initialContentHTML: PropTypes.string,
16
+    editorInitializedCallback: PropTypes.func
16 17
   };
17 18
 
18 19
   constructor(props) {
@@ -50,6 +51,7 @@ export default class RichTextEditor extends Component {
50 51
         case messages.ZSS_INITIALIZED:
51 52
           this.setTitleHTML(this.props.initialTitleHTML);
52 53
           this.setContentHTML(this.props.initialContentHTML);
54
+          this.props.editorInitializedCallback && this.props.editorInitializedCallback();
53 55
           break;
54 56
         case messages.LOG:
55 57
           console.log('FROM ZSS', message.data);
@@ -57,8 +59,13 @@ export default class RichTextEditor extends Component {
57 59
         case messages.SCROLL:
58 60
           this.webviewBridge.setNativeProps({contentOffset: {y: message.data}});
59 61
           break;
62
+        case messages.TITLE_FOCUSED:
63
+          this.titleFocusHandler && this.titleFocusHandler();
64
+          break;
65
+        case messages.CONTENT_FOCUSED:
66
+          this.contentFocusHandler && this.contentFocusHandler();
67
+          break;
60 68
       }
61
-
62 69
     } catch(e) {
63 70
       //alert('NON JSON MESSAGE');
64 71
     }
@@ -228,8 +235,13 @@ export default class RichTextEditor extends Component {
228 235
     });
229 236
   }
230 237
 
231
-  async getHtml() {
232
-
238
+  setTitleFocusHandler(callbackHandler) {
239
+    this.titleFocusHandler = callbackHandler;
240
+    this._sendAction(actions.setTitleFocusHandler);
233 241
   }
234 242
 
243
+  setContentFocusHandler(callbackHandler) {
244
+    this.contentFocusHandler = callbackHandler;
245
+    this._sendAction(actions.setContentFocusHandler);
246
+  }
235 247
 }

+ 6
- 0
src/WebviewMessageHandler.js View File

@@ -100,6 +100,12 @@ export const InjectedMessageHandler = `
100 100
           var html = zss_editor.getContentHTML();
101 101
           WebViewBridge.send(JSON.stringify({type: '${messages.CONTENT_HTML_RESPONSE}', data: html}));
102 102
           break;
103
+        case '${actions.setTitleFocusHandler}':
104
+          zss_editor.setTitleFocusHandler();
105
+          break;
106
+        case '${actions.setContentFocusHandler}':
107
+          zss_editor.setContentFocusHandler();
108
+          break;
103 109
       }
104 110
     };
105 111
   }

+ 17
- 0
src/ZSSRichTextEditor/ZSSRichTextEditor.js View File

@@ -745,4 +745,21 @@ zss_editor.setCustomCSS = function(customCSS) {
745 745
     
746 746
 }
747 747
 
748
+function addFocusEvent(editorId, callbackHandler) {
749
+    var editor = $(`#${editorId}`);
750
+    editor.focus(callbackHandler);
751
+}
752
+
753
+zss_editor.setTitleFocusHandler = function() {
754
+    addFocusEvent('zss_editor_title', function() {
755
+        WebViewBridge.send(JSON.stringify({type: 'TITLE_FOCUSED'}))
756
+    });
757
+}
758
+
759
+zss_editor.setContentFocusHandler = function() {
760
+    addFocusEvent('zss_editor_content', function() {
761
+        WebViewBridge.send(JSON.stringify({type: 'CONTENT_FOCUSED'}))
762
+    });
763
+}
764
+
748 765
 //end

+ 8
- 4
src/const.js View File

@@ -29,13 +29,17 @@ export const actions = {
29 29
   setIndent: 'SET_INDENT',
30 30
   setOutdent: 'SET_OUTDENT',
31 31
   setTitlePlaceholder: 'SET_TITLE_PLACEHOLDER',
32
-  setContentPlaceholder: 'SET_TITLE_PLACEHOLDER'
32
+  setContentPlaceholder: 'SET_TITLE_PLACEHOLDER',
33
+  setTitleFocusHandler: 'SET_TITLE_FOCUS_HANDLER',
34
+  setContentFocusHandler: 'SET_CONTENT_FOCUS_HANDLER'
33 35
 };
34 36
 
35 37
 export const messages = {
36
-  TITLE_HTML_RESPONSE : 'TITLE_HTML_RESPONSE',
37
-  CONTENT_HTML_RESPONSE : 'CONTENT_HTML_RESPONSE',
38
+  TITLE_HTML_RESPONSE: 'TITLE_HTML_RESPONSE',
39
+  CONTENT_HTML_RESPONSE: 'CONTENT_HTML_RESPONSE',
38 40
   ZSS_INITIALIZED: 'ZSS_INITIALIZED',
39 41
   SCROLL: 'SCROLL',
40
-  LOG: 'LOG'
42
+  LOG: 'LOG',
43
+  TITLE_FOCUSED: 'TITLE_FOCUSED',
44
+  CONTENT_FOCUSED: 'CONTENT_FOCUSED'
41 45
 }