Przeglądaj źródła

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

Yevhen Pavliuk 8 lat temu
rodzic
commit
8d8453c4a2
1 zmienionych plików z 29 dodań i 0 usunięć
  1. 29
    0
      src/editor.html

+ 29
- 0
src/editor.html Wyświetl plik

879
 				setupTouchEndFocus('zss_editor_title');
879
 				setupTouchEndFocus('zss_editor_title');
880
 				setupTouchEndFocus('zss_editor_content');
880
 				setupTouchEndFocus('zss_editor_content');
881
 
881
 
882
+				whenPastingInsertAsPlainText('zss_editor_title');
883
+				whenPastingInsertAsPlainText('zss_editor_content');
884
+
882
 				setTimeout(function() {
885
 				setTimeout(function() {
883
 					WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
886
 					WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
884
 				}, 20);
887
 				}, 20);
885
 
888
 
886
 			}//end
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
 			zss_editor.updateOffset = function() {
917
 			zss_editor.updateOffset = function() {
889
 
918
 
890
 				if (!zss_editor.updateScrollOffset)
919
 				if (!zss_editor.updateScrollOffset)