Created
August 7, 2019 13:55
-
-
Save postpostscript/0085d72840da1e870d336e8dae4b15a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sanitizeHTML from 'sanitize-html' | |
const wysiwygSel = '.wysiwyg' | |
const allowedTags = [ | |
'b', | |
'i', | |
'u', | |
'br', | |
] | |
function pasteEventHTML(e) { | |
const html = e.clipboardData.getData("text/html") | |
const sanitizedHTML = html && sanitizeHTML(html, { | |
allowedTags, | |
}) | |
return sanitizedHTML || e.clipboardData.getData("text/plain") | |
} | |
document.addEventListener('paste', function (e) { | |
if (!(e.target && e.target.closest(wysiwygSel))) { | |
// only overwrite pastes when pasting into a wysiwyg editor | |
return | |
} | |
e.preventDefault() | |
document.execCommand("insertHTML", false, pasteEventHTML(e)) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment