Parcourir la source

support title in editor

Artal Druk il y a 8 ans
Parent
révision
e36394fc0d
6 fichiers modifiés avec 206 ajouts et 100 suppressions
  1. 5
    2
      example/app.js
  2. 66
    23
      src/RichTextEditor.js
  3. 23
    10
      src/WebviewMessageHandler.js
  4. 100
    58
      src/ZSSRichTextEditor/ZSSRichTextEditor.js
  5. 10
    5
      src/const.js
  6. 2
    2
      src/editor.html

+ 5
- 2
example/app.js Voir le fichier

@@ -20,7 +20,8 @@ export default class RichTextExample extends Component {
20 20
           <RichTextEditor
21 21
               ref={(r)=>this.richtext = r}
22 22
               style={styles.richText}
23
-              initialHTML={'Hello <b>World</b> <p>this is a new paragraph</p> <p>this is another new paragraph</p>'}
23
+              initialTitleHTML={'Title!!'}
24
+              initialContentHTML={'Hello <b>World</b> <p>this is a new paragraph</p> <p>this is another new paragraph</p>'}
24 25
           />
25 26
           <RichTextToolbar
26 27
             getEditor={() => this.richtext}
@@ -31,7 +32,9 @@ export default class RichTextExample extends Component {
31 32
   }
32 33
 
33 34
   async getHTML() {
34
-    const html = await this.richtext.getHtml();
35
+    const titleHtml = await this.richtext.getTitleHtml();
36
+    const contentHtml = await this.richtext.getContentHtml();
37
+    //alert(titleHtml + ' ' + contentHtml)
35 38
   }
36 39
 
37 40
   componentDidMount() {

+ 66
- 23
src/RichTextEditor.js Voir le fichier

@@ -11,7 +11,8 @@ const injectScript = `
11 11
 
12 12
 export default class RichTextEditor extends Component {
13 13
   static propTypes = {
14
-    initialHTML: PropTypes.string
14
+    initialTitleHTML: PropTypes.string,
15
+    initialContentHTML: PropTypes.string,
15 16
   };
16 17
 
17 18
   constructor(props) {
@@ -24,19 +25,31 @@ export default class RichTextEditor extends Component {
24 25
       const message = JSON.parse(str);
25 26
 
26 27
       switch (message.type) {
27
-        case messages.HTML_RESPONSE:
28
-          if (this.resolve) {
29
-            this.resolve(message.data);
30
-            this.resolve = undefined;
31
-            this.reject = undefined;
32
-            if (this.pendingHtml) {
33
-              clearTimeout(this.pendingHtml);
34
-              this.pendingHtml = undefined;
28
+        case messages.TITLE_HTML_RESPONSE:
29
+          if (this.titleResolve) {
30
+            this.titleResolve(message.data);
31
+            this.titleResolve = undefined;
32
+            this.titleReject = undefined;
33
+            if (this.pendingTitleHtml) {
34
+              clearTimeout(this.pendingTitleHtml);
35
+              this.pendingTitleHtml = undefined;
36
+            }
37
+          }
38
+          break;
39
+        case messages.CONTENT_HTML_RESPONSE:
40
+          if (this.contentResolve) {
41
+            this.contentResolve(message.data);
42
+            this.contentResolve = undefined;
43
+            this.contentReject = undefined;
44
+            if (this.pendingContentHtml) {
45
+              clearTimeout(this.pendingContentHtml);
46
+              this.pendingContentHtml = undefined;
35 47
             }
36 48
           }
37 49
           break;
38 50
         case messages.ZSS_INITIALIZED:
39
-          this.setHTML(this.props.initialHTML);
51
+          this.setTitleHTML(this.props.initialTitleHTML);
52
+          this.setContentHTML(this.props.initialContentHTML);
40 53
           break;
41 54
         case messages.LOG:
42 55
           console.log('FROM ZSS', message.data);
@@ -71,12 +84,20 @@ export default class RichTextEditor extends Component {
71 84
   //-------------------------------------------------------------------------------
72 85
   //--------------- Public API
73 86
 
74
-  setHTML(html) {
75
-    this._sendAction(actions.setHtml, html);
87
+  setTitleHTML(html) {
88
+    this._sendAction(actions.setTitleHtml, html);
76 89
   }
77 90
 
78
-  blurEditor() {
79
-    this._sendAction(actions.blurEditor);
91
+  setContentHTML(html) {
92
+    this._sendAction(actions.setContentHtml, html);
93
+  }
94
+
95
+  blurTitleEditor() {
96
+    this._sendAction(actions.blurTitleEditor);
97
+  }
98
+
99
+  blurContentEditor() {
100
+    this._sendAction(actions.blurContentEditor);
80 101
   }
81 102
 
82 103
   setBold() {
@@ -171,22 +192,44 @@ export default class RichTextEditor extends Component {
171 192
     this._sendAction(actions.setOutdent);
172 193
   }
173 194
 
174
-  setPlaceholder() {
175
-    this._sendAction(actions.setPlaceholder);
195
+  setTitlePlaceholder() {
196
+    this._sendAction(actions.setTitlePlaceholder);
176 197
   }
177 198
 
178
-  async getHtml() {
199
+  setContentPlaceholder() {
200
+    this._sendAction(actions.setContentPlaceholder);
201
+  }
202
+
203
+  async getTitleHtml() {
179 204
     return new Promise((resolve, reject) => {
180
-      this.resolve = resolve;
181
-      this.reject = reject;
182
-      this._sendAction(actions.getHtml);
205
+      this.titleResolve = resolve;
206
+      this.titleReject = reject;
207
+      this._sendAction(actions.getTitleHtml);
183 208
 
184
-      this.pendingHtml = setTimeout(() => {
185
-        if (this.reject) {
186
-          this.reject('timeout')
209
+      this.pendingTitleHtml = setTimeout(() => {
210
+        if (this.titleReject) {
211
+          this.titleReject('timeout')
187 212
         }
188 213
       }, 5000);
189 214
     });
190 215
   }
191 216
 
217
+  async getContentHtml() {
218
+    return new Promise((resolve, reject) => {
219
+      this.contentResolve = resolve;
220
+      this.contentReject = reject;
221
+      this._sendAction(actions.getContentHtml);
222
+
223
+      this.pendingContentHtml = setTimeout(() => {
224
+        if (this.contentReject) {
225
+          this.contentReject('timeout')
226
+        }
227
+      }, 5000);
228
+    });
229
+  }
230
+
231
+  async getHtml() {
232
+
233
+  }
234
+
192 235
 }

+ 23
- 10
src/WebviewMessageHandler.js Voir le fichier

@@ -5,11 +5,17 @@ export const InjectedMessageHandler = `
5 5
     WebViewBridge.onMessage = function (message) {
6 6
       const action = JSON.parse(message);
7 7
       switch(action.type) {
8
-        case '${actions.setHtml}':
9
-          zss_editor.setHTML(action.data);
8
+        case '${actions.setTitleHtml}':
9
+          zss_editor.setTitleHTML(action.data);
10 10
           break;
11
-        case '${actions.blurEditor}':
12
-          zss_editor.blurEditor();
11
+        case '${actions.setContentHtml}':
12
+          zss_editor.setContentHTML(action.data);
13
+          break;
14
+        case '${actions.blurTitleEditor}':
15
+          zss_editor.blurTitleEditor();
16
+          break;
17
+        case '${actions.blurContentEditor}':
18
+          zss_editor.blurContentEditor();
13 19
           break;
14 20
         case '${actions.setBold}':
15 21
           zss_editor.setBold();
@@ -69,7 +75,7 @@ export const InjectedMessageHandler = `
69 75
           zss_editor.setSuperscript();
70 76
           break;
71 77
         case '${actions.setStrikethrough}':
72
-          zss_editor.setStrikethrough();
78
+          zss_editor.setStrikeThrough();
73 79
           break;
74 80
         case '${actions.setHR}':
75 81
           zss_editor.setHorizontalRule();
@@ -80,12 +86,19 @@ export const InjectedMessageHandler = `
80 86
         case '${actions.setOutdent}':
81 87
           zss_editor.setOutdent();
82 88
           break;
83
-        case '${actions.setPlaceholder}':
84
-          zss_editor.setPlaceholder();
89
+        case '${actions.setTitlePlaceholder}':
90
+          zss_editor.setTitlePlaceholder();
91
+          break;
92
+        case '${actions.setContentPlaceholder}':
93
+          zss_editor.setContentPlaceholder();
94
+          break;
95
+        case '${actions.getTitleHtml}':
96
+          var html = zss_editor.getTitleHTML();
97
+          WebViewBridge.send(JSON.stringify({type: '${messages.TITLE_HTML_RESPONSE}', data: html}));
85 98
           break;
86
-        case '${actions.getHtml}':
87
-          const html = zss_editor.getHTML();
88
-          WebViewBridge.send(JSON.stringify({type: '${messages.HTML_RESPONSE}', data: html}));
99
+        case '${actions.getContentHtml}':
100
+          var html = zss_editor.getContentHTML();
101
+          WebViewBridge.send(JSON.stringify({type: '${messages.CONTENT_HTML_RESPONSE}', data: html}));
89 102
           break;
90 103
       }
91 104
     };

+ 100
- 58
src/ZSSRichTextEditor/ZSSRichTextEditor.js Voir le fichier

@@ -36,23 +36,41 @@ zss_editor.updateScrollOffset = false;
36 36
 /**
37 37
  * The initializer function that must be called onLoad
38 38
  */
39
-zss_editor.init = function() {
40 39
 
40
+function setupTouchEndEnableEditing(editorId) {
41
+    $(`#${editorId}`).on('touchend', function(e) {
42
+        zss_editor.enabledEditingItems(e);
43
+        var clicked = $(e.target);
44
+        if (!clicked.hasClass('zs_active')) {
45
+            $('img').removeClass('zs_active');
46
+        }
47
+    });
48
+}
41 49
 
42
-    $('#zss_editor_content').on('touchend', function(e) {
43
-                                zss_editor.enabledEditingItems(e);
44
-                                var clicked = $(e.target);
45
-                                if (!clicked.hasClass('zs_active')) {
46
-                                $('img').removeClass('zs_active');
47
-                                }
48
-                                });
49
-    
50
+function setupSelectionChange(editorId) {
50 51
     $(document).on('selectionchange',function(e){
51
-                   zss_editor.calculateEditorHeightWithCaretPosition();
52
-                   zss_editor.setScrollPosition();
53
-                   zss_editor.enabledEditingItems(e);
54
-                   });
55
-    
52
+        zss_editor.calculateEditorHeightWithCaretPosition(editorId);
53
+        zss_editor.setScrollPosition();
54
+        zss_editor.enabledEditingItems(e);
55
+    });
56
+}
57
+
58
+function setupTouchEndFocus(editorId) {
59
+    $(window).on('touchend', function(e) {
60
+        if (!zss_editor.isDragging && (e.target.id == "zss_editor_footer"||e.target.nodeName.toLowerCase() == "html")) {
61
+            zss_editor.focusEditor(editorId);
62
+        }
63
+    });
64
+}
65
+
66
+zss_editor.init = function() {
67
+
68
+    setupTouchEndEnableEditing('zss_editor_title');
69
+    setupTouchEndEnableEditing('zss_editor_content');
70
+
71
+    setupSelectionChange('zss_editor_title');
72
+    setupSelectionChange('zss_editor_content');
73
+
56 74
     $(window).on('scroll', function(e) {
57 75
                  zss_editor.updateOffset();
58 76
                  });
@@ -67,12 +85,9 @@ zss_editor.init = function() {
67 85
     $(window).on('touchstart', function(e) {
68 86
                  zss_editor.isDragging = false;
69 87
                  });
70
-    $(window).on('touchend', function(e) {
71
-                 if (!zss_editor.isDragging && (e.target.id == "zss_editor_footer"||e.target.nodeName.toLowerCase() == "html")) {
72
-                 zss_editor.focusEditor();
73
-                 }
74
-                 });
75 88
 
89
+    setupTouchEndFocus('zss_editor_title');
90
+    setupTouchEndFocus('zss_editor_content');
76 91
 
77 92
     setTimeout(function() {
78 93
         WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
@@ -113,24 +128,27 @@ zss_editor.setScrollPosition = function() {
113 128
     WebViewBridge.send(JSON.stringify({type: 'SCROLL', data: position}));
114 129
 }
115 130
 
116
-
117
-zss_editor.setPlaceholder = function(placeholder) {
118
-    
119
-    var editor = $('#zss_editor_content');
131
+function setPlaceholder(editorId, placeholder) {
132
+    var editor = $(`#${editorId}`);
120 133
     
121 134
     //set placeHolder
122
-	editor.attr("placeholder",placeholder);
135
+    editor.attr("placeholder",placeholder);
123 136
 	
124 137
     //set focus			 
125
-	editor.focusout(function(){
126
-        var element = $(this);        
138
+    editor.focusout(function(){
139
+        var element = $(this);
127 140
         if (!element.text().trim().length) {
128 141
             element.empty();
129 142
         }
130 143
     });
131
-	
132
-	
133
-    
144
+}
145
+
146
+zss_editor.setTitlePlaceholder = function(placeholder) {
147
+    setPlaceholder('zss_editor_title', placeholder);
148
+}
149
+
150
+zss_editor.setContentPlaceholder = function(placeholder) {
151
+    setPlaceholder('zss_editor_content', placeholder);
134 152
 }
135 153
 
136 154
 zss_editor.setFooterHeight = function(footerHeight) {
@@ -151,13 +169,13 @@ zss_editor.getCaretYPosition = function() {
151 169
     return topPosition;
152 170
 }
153 171
 
154
-zss_editor.calculateEditorHeightWithCaretPosition = function() {
172
+zss_editor.calculateEditorHeightWithCaretPosition = function(editorId) {
155 173
     
156 174
     var padding = 50;
157 175
     var c = zss_editor.getCaretYPosition();
158
-    var e = document.getElementById('zss_editor_content');
176
+    var e = document.getElementById(editorId);
159 177
     
160
-    var editor = $('#zss_editor_content');
178
+    var editor = $(`#${editorId}`);
161 179
     
162 180
     var offsetY = window.document.body.scrollTop;
163 181
     var height = zss_editor.contentHeight;
@@ -488,54 +506,73 @@ zss_editor.insertImageBase64String = function(imageBase64String, alt) {
488 506
     zss_editor.enabledEditingItems();
489 507
 }
490 508
 
491
-zss_editor.setHTML = function(html) {
492
-    var editor = $('#zss_editor_content');
509
+function setHTML(editorId, html) {
510
+    var editor = $(`#${editorId}`);
493 511
     editor.html(html);
494 512
 }
495 513
 
514
+zss_editor.setTitleHTML = function(html) {
515
+    setHTML('zss_editor_title', html);
516
+}
517
+
518
+zss_editor.setContentHTML = function(html) {
519
+    setHTML('zss_editor_content', html);
520
+}
521
+
496 522
 zss_editor.insertHTML = function(html) {
497 523
     document.execCommand('insertHTML', false, html);
498 524
     zss_editor.enabledEditingItems();
499 525
 }
500 526
 
501
-zss_editor.getHTML = function() {
502
-    
527
+function getHtml(editorId) {
503 528
     // Images
504 529
     var img = $('img');
505 530
     if (img.length != 0) {
506 531
         $('img').removeClass('zs_active');
507 532
         $('img').each(function(index, e) {
508
-                      var image = $(this);
509
-                      var zs_class = image.attr('class');
510
-                      if (typeof(zs_class) != "undefined") {
511
-                      if (zs_class == '') {
512
-                      image.removeAttr('class');
513
-                      }
514
-                      }
515
-                      });
533
+            var image = $(this);
534
+            var zs_class = image.attr('class');
535
+            if (typeof(zs_class) != "undefined") {
536
+                if (zs_class == '') {
537
+                    image.removeAttr('class');
538
+                }
539
+            }
540
+        });
516 541
     }
517
-    
542
+
518 543
     // Blockquote
519 544
     var bq = $('blockquote');
520 545
     if (bq.length != 0) {
521 546
         bq.each(function() {
522
-                var b = $(this);
523
-                if (b.css('border').indexOf('none') != -1) {
547
+            var b = $(this);
548
+            if (b.css('border').indexOf('none') != -1) {
524 549
                 b.css({'border': ''});
525
-                }
526
-                if (b.css('padding').indexOf('0px') != -1) {
550
+            }
551
+            if (b.css('padding').indexOf('0px') != -1) {
527 552
                 b.css({'padding': ''});
528
-                }
529
-                });
553
+            }
554
+        });
530 555
     }
531
-    
556
+
532 557
     // Get the contents
533
-    var h = document.getElementById("zss_editor_content").innerHTML;
534
-    
558
+    var h = document.getElementById(editorId).innerHTML;
559
+
535 560
     return h;
536 561
 }
537 562
 
538
-zss_editor.getText = function() {
563
+zss_editor.getTitleHTML = function() {
564
+    return getHtml("zss_editor_title");
565
+}
566
+
567
+zss_editor.getContentHTML = function() {
568
+    return getHtml("zss_editor_content");
569
+}
570
+
571
+zss_editor.getTitleText = function() {
572
+    return $('#zss_editor_title').text();
573
+}
574
+
575
+zss_editor.getContentText = function() {
539 576
     return $('#zss_editor_content').text();
540 577
 }
541 578
 
@@ -658,11 +695,11 @@ zss_editor.enabledEditingItems = function(e) {
658 695
     }
659 696
 }
660 697
 
661
-zss_editor.focusEditor = function() {
698
+zss_editor.focusEditor = function(editorId) {
662 699
     
663 700
     // the following was taken from http://stackoverflow.com/questions/1125292/how-to-move-cursor-to-end-of-contenteditable-entity/3866442#3866442
664 701
     // and ensures we move the cursor to the end of the editor
665
-    var editor = $('#zss_editor_content');
702
+    var editor = $(`#${editorId}`);
666 703
     var range = document.createRange();
667 704
     range.selectNodeContents(editor.get(0));
668 705
     range.collapse(false);
@@ -672,7 +709,12 @@ zss_editor.focusEditor = function() {
672 709
     editor.focus();
673 710
 }
674 711
 
675
-zss_editor.blurEditor = function() {
712
+
713
+zss_editor.blurTitleEditor = function() {
714
+    $('#zss_editor_title').blur();
715
+}
716
+
717
+zss_editor.blurContentEditor = function() {
676 718
     $('#zss_editor_content').blur();
677 719
 }
678 720
 

+ 10
- 5
src/const.js Voir le fichier

@@ -1,7 +1,10 @@
1 1
 export const actions = {
2
-  setHtml: 'SET_HTML',
3
-  getHtml: 'GET_HTML',
4
-  blurEditor: 'BLUR_EDITOR',
2
+  setTitleHtml: 'SET_TITLE_HTML',
3
+  setContentHtml: 'SET_CONTENT_HTML',
4
+  getTitleHtml: 'GET_TITLE_HTML',
5
+  getContentHtml: 'GET_CONTENT_HTML',
6
+  blurTitleEditor: 'BLUR_TITLE_EDITOR',
7
+  blurContentEditor: 'BLUR_CONTENT_EDITOR',
5 8
   setBold: 'SET_BOLD',
6 9
   setItalic: 'SET_ITALIC',
7 10
   setUnderline: 'SET_UNDERLINE',
@@ -25,11 +28,13 @@ export const actions = {
25 28
   setHR: 'SET_HR',
26 29
   setIndent: 'SET_INDENT',
27 30
   setOutdent: 'SET_OUTDENT',
28
-  setPlaceholder: 'SET_PLACEHOLDER'
31
+  setTitlePlaceholder: 'SET_TITLE_PLACEHOLDER',
32
+  setContentPlaceholder: 'SET_TITLE_PLACEHOLDER'
29 33
 };
30 34
 
31 35
 export const messages = {
32
-  HTML_RESPONSE : 'HTML_RESPONSE',
36
+  TITLE_HTML_RESPONSE : 'TITLE_HTML_RESPONSE',
37
+  CONTENT_HTML_RESPONSE : 'CONTENT_HTML_RESPONSE',
33 38
   ZSS_INITIALIZED: 'ZSS_INITIALIZED',
34 39
   SCROLL: 'SCROLL',
35 40
   LOG: 'LOG'

+ 2
- 2
src/editor.html Voir le fichier

@@ -15,8 +15,8 @@
15 15
 
16 16
 	<body onLoad="zss_editor.init();">
17 17
 		<!-- ZSSRichTextEditor Editable Content -->
18
-		<div id="zss_editor_title" class="zs_editor_content" contenteditable="true" placeholder="title"></div>
18
+		<div id="zss_editor_title" class="zs_editor_content" contenteditable="true" placeholder=""></div>
19 19
 		<div><hr></div>
20
-		<div id="zss_editor_content" class="zs_editor_content" contenteditable="true" placeholder="content"></div>
20
+		<div id="zss_editor_content" class="zs_editor_content" contenteditable="true" placeholder=""></div>
21 21
 	</body>
22 22
 </html>