Explorar el Código

support title in editor

Artal Druk hace 8 años
padre
commit
e36394fc0d
Se han modificado 6 ficheros con 206 adiciones y 100 borrados
  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 Ver fichero

20
           <RichTextEditor
20
           <RichTextEditor
21
               ref={(r)=>this.richtext = r}
21
               ref={(r)=>this.richtext = r}
22
               style={styles.richText}
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
           <RichTextToolbar
26
           <RichTextToolbar
26
             getEditor={() => this.richtext}
27
             getEditor={() => this.richtext}
31
   }
32
   }
32
 
33
 
33
   async getHTML() {
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
   componentDidMount() {
40
   componentDidMount() {

+ 66
- 23
src/RichTextEditor.js Ver fichero

11
 
11
 
12
 export default class RichTextEditor extends Component {
12
 export default class RichTextEditor extends Component {
13
   static propTypes = {
13
   static propTypes = {
14
-    initialHTML: PropTypes.string
14
+    initialTitleHTML: PropTypes.string,
15
+    initialContentHTML: PropTypes.string,
15
   };
16
   };
16
 
17
 
17
   constructor(props) {
18
   constructor(props) {
24
       const message = JSON.parse(str);
25
       const message = JSON.parse(str);
25
 
26
 
26
       switch (message.type) {
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
           break;
49
           break;
38
         case messages.ZSS_INITIALIZED:
50
         case messages.ZSS_INITIALIZED:
39
-          this.setHTML(this.props.initialHTML);
51
+          this.setTitleHTML(this.props.initialTitleHTML);
52
+          this.setContentHTML(this.props.initialContentHTML);
40
           break;
53
           break;
41
         case messages.LOG:
54
         case messages.LOG:
42
           console.log('FROM ZSS', message.data);
55
           console.log('FROM ZSS', message.data);
71
   //-------------------------------------------------------------------------------
84
   //-------------------------------------------------------------------------------
72
   //--------------- Public API
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
   setBold() {
103
   setBold() {
171
     this._sendAction(actions.setOutdent);
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
     return new Promise((resolve, reject) => {
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
       }, 5000);
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 Ver fichero

5
     WebViewBridge.onMessage = function (message) {
5
     WebViewBridge.onMessage = function (message) {
6
       const action = JSON.parse(message);
6
       const action = JSON.parse(message);
7
       switch(action.type) {
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
           break;
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
           break;
19
           break;
14
         case '${actions.setBold}':
20
         case '${actions.setBold}':
15
           zss_editor.setBold();
21
           zss_editor.setBold();
69
           zss_editor.setSuperscript();
75
           zss_editor.setSuperscript();
70
           break;
76
           break;
71
         case '${actions.setStrikethrough}':
77
         case '${actions.setStrikethrough}':
72
-          zss_editor.setStrikethrough();
78
+          zss_editor.setStrikeThrough();
73
           break;
79
           break;
74
         case '${actions.setHR}':
80
         case '${actions.setHR}':
75
           zss_editor.setHorizontalRule();
81
           zss_editor.setHorizontalRule();
80
         case '${actions.setOutdent}':
86
         case '${actions.setOutdent}':
81
           zss_editor.setOutdent();
87
           zss_editor.setOutdent();
82
           break;
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
           break;
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
           break;
102
           break;
90
       }
103
       }
91
     };
104
     };

+ 100
- 58
src/ZSSRichTextEditor/ZSSRichTextEditor.js Ver fichero

36
 /**
36
 /**
37
  * The initializer function that must be called onLoad
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
     $(document).on('selectionchange',function(e){
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
     $(window).on('scroll', function(e) {
74
     $(window).on('scroll', function(e) {
57
                  zss_editor.updateOffset();
75
                  zss_editor.updateOffset();
58
                  });
76
                  });
67
     $(window).on('touchstart', function(e) {
85
     $(window).on('touchstart', function(e) {
68
                  zss_editor.isDragging = false;
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
     setTimeout(function() {
92
     setTimeout(function() {
78
         WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
93
         WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
113
     WebViewBridge.send(JSON.stringify({type: 'SCROLL', data: position}));
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
     //set placeHolder
134
     //set placeHolder
122
-	editor.attr("placeholder",placeholder);
135
+    editor.attr("placeholder",placeholder);
123
 	
136
 	
124
     //set focus			 
137
     //set focus			 
125
-	editor.focusout(function(){
126
-        var element = $(this);        
138
+    editor.focusout(function(){
139
+        var element = $(this);
127
         if (!element.text().trim().length) {
140
         if (!element.text().trim().length) {
128
             element.empty();
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
 zss_editor.setFooterHeight = function(footerHeight) {
154
 zss_editor.setFooterHeight = function(footerHeight) {
151
     return topPosition;
169
     return topPosition;
152
 }
170
 }
153
 
171
 
154
-zss_editor.calculateEditorHeightWithCaretPosition = function() {
172
+zss_editor.calculateEditorHeightWithCaretPosition = function(editorId) {
155
     
173
     
156
     var padding = 50;
174
     var padding = 50;
157
     var c = zss_editor.getCaretYPosition();
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
     var offsetY = window.document.body.scrollTop;
180
     var offsetY = window.document.body.scrollTop;
163
     var height = zss_editor.contentHeight;
181
     var height = zss_editor.contentHeight;
488
     zss_editor.enabledEditingItems();
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
     editor.html(html);
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
 zss_editor.insertHTML = function(html) {
522
 zss_editor.insertHTML = function(html) {
497
     document.execCommand('insertHTML', false, html);
523
     document.execCommand('insertHTML', false, html);
498
     zss_editor.enabledEditingItems();
524
     zss_editor.enabledEditingItems();
499
 }
525
 }
500
 
526
 
501
-zss_editor.getHTML = function() {
502
-    
527
+function getHtml(editorId) {
503
     // Images
528
     // Images
504
     var img = $('img');
529
     var img = $('img');
505
     if (img.length != 0) {
530
     if (img.length != 0) {
506
         $('img').removeClass('zs_active');
531
         $('img').removeClass('zs_active');
507
         $('img').each(function(index, e) {
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
     // Blockquote
543
     // Blockquote
519
     var bq = $('blockquote');
544
     var bq = $('blockquote');
520
     if (bq.length != 0) {
545
     if (bq.length != 0) {
521
         bq.each(function() {
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
                 b.css({'border': ''});
549
                 b.css({'border': ''});
525
-                }
526
-                if (b.css('padding').indexOf('0px') != -1) {
550
+            }
551
+            if (b.css('padding').indexOf('0px') != -1) {
527
                 b.css({'padding': ''});
552
                 b.css({'padding': ''});
528
-                }
529
-                });
553
+            }
554
+        });
530
     }
555
     }
531
-    
556
+
532
     // Get the contents
557
     // Get the contents
533
-    var h = document.getElementById("zss_editor_content").innerHTML;
534
-    
558
+    var h = document.getElementById(editorId).innerHTML;
559
+
535
     return h;
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
     return $('#zss_editor_content').text();
576
     return $('#zss_editor_content').text();
540
 }
577
 }
541
 
578
 
658
     }
695
     }
659
 }
696
 }
660
 
697
 
661
-zss_editor.focusEditor = function() {
698
+zss_editor.focusEditor = function(editorId) {
662
     
699
     
663
     // the following was taken from http://stackoverflow.com/questions/1125292/how-to-move-cursor-to-end-of-contenteditable-entity/3866442#3866442
700
     // the following was taken from http://stackoverflow.com/questions/1125292/how-to-move-cursor-to-end-of-contenteditable-entity/3866442#3866442
664
     // and ensures we move the cursor to the end of the editor
701
     // and ensures we move the cursor to the end of the editor
665
-    var editor = $('#zss_editor_content');
702
+    var editor = $(`#${editorId}`);
666
     var range = document.createRange();
703
     var range = document.createRange();
667
     range.selectNodeContents(editor.get(0));
704
     range.selectNodeContents(editor.get(0));
668
     range.collapse(false);
705
     range.collapse(false);
672
     editor.focus();
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
     $('#zss_editor_content').blur();
718
     $('#zss_editor_content').blur();
677
 }
719
 }
678
 
720
 

+ 10
- 5
src/const.js Ver fichero

1
 export const actions = {
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
   setBold: 'SET_BOLD',
8
   setBold: 'SET_BOLD',
6
   setItalic: 'SET_ITALIC',
9
   setItalic: 'SET_ITALIC',
7
   setUnderline: 'SET_UNDERLINE',
10
   setUnderline: 'SET_UNDERLINE',
25
   setHR: 'SET_HR',
28
   setHR: 'SET_HR',
26
   setIndent: 'SET_INDENT',
29
   setIndent: 'SET_INDENT',
27
   setOutdent: 'SET_OUTDENT',
30
   setOutdent: 'SET_OUTDENT',
28
-  setPlaceholder: 'SET_PLACEHOLDER'
31
+  setTitlePlaceholder: 'SET_TITLE_PLACEHOLDER',
32
+  setContentPlaceholder: 'SET_TITLE_PLACEHOLDER'
29
 };
33
 };
30
 
34
 
31
 export const messages = {
35
 export const messages = {
32
-  HTML_RESPONSE : 'HTML_RESPONSE',
36
+  TITLE_HTML_RESPONSE : 'TITLE_HTML_RESPONSE',
37
+  CONTENT_HTML_RESPONSE : 'CONTENT_HTML_RESPONSE',
33
   ZSS_INITIALIZED: 'ZSS_INITIALIZED',
38
   ZSS_INITIALIZED: 'ZSS_INITIALIZED',
34
   SCROLL: 'SCROLL',
39
   SCROLL: 'SCROLL',
35
   LOG: 'LOG'
40
   LOG: 'LOG'

+ 2
- 2
src/editor.html Ver fichero

15
 
15
 
16
 	<body onLoad="zss_editor.init();">
16
 	<body onLoad="zss_editor.init();">
17
 		<!-- ZSSRichTextEditor Editable Content -->
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
 		<div><hr></div>
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
 	</body>
21
 	</body>
22
 </html>
22
 </html>