Browse Source

Added handleEditablePaste thanks to @ryanatkn https://gitlab.com/medlabboulder/communityrule/-/merge_requests/2

Nathan Schneider 4 years ago
parent
commit
95b3ce8ef4
1 changed files with 40 additions and 1 deletions
  1. 40 1
      _layouts/rule.html

+ 40 - 1
_layouts/rule.html

@@ -216,6 +216,38 @@ https://github.com/Bernardo-Castilho/dragdroptouch -->
       return input;
   }
   
+  // Intercepts the paste event for editable fields and
+  // converts the pasted content to plain text,
+  // stripping styles and unwanted markup added by programs like Word.
+  function handleEditablePaste(event) {
+      try {
+          var pastedText = event.clipboardData
+              ? event.clipboardData.getData("text/plain")
+              : window.clipboardData.getData("Text"); // support IE
+          var cleanedText = cleanPastedText(pastedText);
+          // Pastes the cleaned up text.
+          if (document.queryCommandSupported('insertText')) {
+              document.execCommand('insertText', false, cleanedText);
+          } else { // support IE
+              document.execCommand('paste', false, cleanedText);
+          }
+          event.stopPropagation();
+          event.preventDefault();
+          return false;
+      } catch (err) {
+          // If anything goes wrong with browser compatibility,
+          // pass the event through without modification.
+          return true;
+      }
+  }
+
+  // Removes junk that comes with pasting from text editors like Word.
+  // taken from https://stackoverflow.com/questions/2875027/clean-microsoft-word-pasted-text-using-javascript
+  function cleanPastedText(text) {
+      return text.replace(/.*<!--.*-->/g, "");
+  }
+
+
   // toggleVisible(id)
   // Toggles the visibility of a given element by given ID
   function toggleVisible(id) {
@@ -553,7 +585,14 @@ https://github.com/Bernardo-Castilho/dragdroptouch -->
               toggleEditMode();
           }
       }
-  }  
+  }
+  // eqip editable fields to remove formatting from pasted content    
+  window.onload = function() {
+      var editableElements = document.getElementsByClassName("editable");
+      for (var i = 0; i < editableElements.length; i++ ) {
+          editableElements[i].addEventListener("paste", handleEditablePaste);
+      } 
+  }
   
 </script>