Skip to content

Instantly share code, notes, and snippets.

@tanthammar
Created May 2, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanthammar/cd0b9213e6639deae5e19dc42bf3e547 to your computer and use it in GitHub Desktop.
Save tanthammar/cd0b9213e6639deae5e19dc42bf3e547 to your computer and use it in GitHub Desktop.
Capture plain-text on paste JS
/**
* Capture the <CTL-V> paste event, only allow plain-text, no images.
*
* see: https://stackoverflow.com/a/28213320
*
* @param {object} evt - array of files
* @author Daniel Thompson-Yvetot
* @license MIT
*/
pasteCapture(evt, ref) {
let text, onPasteStripFormattingIEPaste
evt.preventDefault()
if (evt.originalEvent && evt.originalEvent.clipboardData.getData) {
text = evt.originalEvent.clipboardData.getData('text/plain')
this.$refs[ref].runCmd('insertText', text)
} else if (evt.clipboardData && evt.clipboardData.getData) {
text = evt.clipboardData.getData('text/plain')
this.$refs[ref].runCmd('insertText', text)
} else if (window.clipboardData && window.clipboardData.getData) {
if (!onPasteStripFormattingIEPaste) {
onPasteStripFormattingIEPaste = true
this.$refs[ref].runCmd('ms-pasteTextOnly', text)
}
onPasteStripFormattingIEPaste = false
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment