Sfoglia il codice sorgente

#WOA-2373|Resolved|When pasting insert as plain text

Yevhen Pavliuk 8 anni fa
parent
commit
8d8453c4a2
1 ha cambiato i file con 29 aggiunte e 0 eliminazioni
  1. 29
    0
      src/editor.html

+ 29
- 0
src/editor.html Vedi File

@@ -879,12 +879,41 @@
879 879
 				setupTouchEndFocus('zss_editor_title');
880 880
 				setupTouchEndFocus('zss_editor_content');
881 881
 
882
+				whenPastingInsertAsPlainText('zss_editor_title');
883
+				whenPastingInsertAsPlainText('zss_editor_content');
884
+
882 885
 				setTimeout(function() {
883 886
 					WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
884 887
 				}, 20);
885 888
 
886 889
 			}//end
887 890
 
891
+			function whenPastingInsertAsPlainText(editorId) {
892
+				var editor = document.getElementById(editorId);
893
+				editor.addEventListener('paste', function(event) {
894
+					event.preventDefault();
895
+
896
+					var text = event.clipboardData.getData('text/plain');
897
+					var html = convertTextToHtmlWithLineBreaks(text);
898
+					zss_editor.insertHTML(html);
899
+				});
900
+			}
901
+
902
+			function convertTextToHtmlWithLineBreaks(text) {
903
+				var html = encodeHtmlEntities(text);
904
+				return replaceNewlinesWithLineBreaks(html);
905
+			}
906
+
907
+			function encodeHtmlEntities(text) {
908
+				var paragraph = document.createElement('p');
909
+				paragraph.textContent = text;
910
+				return paragraph.innerHTML;
911
+			}
912
+
913
+			function replaceNewlinesWithLineBreaks(html) {
914
+				return html.replace(/\r?\n/g, '<br>');
915
+			}
916
+
888 917
 			zss_editor.updateOffset = function() {
889 918
 
890 919
 				if (!zss_editor.updateScrollOffset)