Last active
July 25, 2024 03:28
-
-
Save juji/2411d2e1b8ee2580933039e6e0e81da3 to your computer and use it in GitHub Desktop.
clean clipboard text
This file contains hidden or 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
export function cleanClipboardText(e: ClipboardEvent){ | |
const pastedText = e.clipboardData?.getData('text/plain'); | |
return pastedText ? stripBom(pastedText) : '' | |
} | |
export function onPaste(e: ClipboardEvent){ | |
e.preventDefault() | |
const sel = window.getSelection() | |
if(!sel) return; | |
if (!sel.rangeCount) return; | |
const data = cleanClipboardText(e) | |
sel.getRangeAt(0).insertNode(document.createTextNode(data)); | |
sel.collapseToEnd(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment