Skip to content

Instantly share code, notes, and snippets.

@postpostscript
Created August 7, 2019 13:55
Show Gist options
  • Save postpostscript/0085d72840da1e870d336e8dae4b15a1 to your computer and use it in GitHub Desktop.
Save postpostscript/0085d72840da1e870d336e8dae4b15a1 to your computer and use it in GitHub Desktop.
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