Skip to content

Instantly share code, notes, and snippets.

@jrainlau
Created September 6, 2018 13:28
Show Gist options
  • Save jrainlau/c434e16c8a691b3b73914a10f8ccc899 to your computer and use it in GitHub Desktop.
Save jrainlau/c434e16c8a691b3b73914a10f8ccc899 to your computer and use it in GitHub Desktop.
custom paste event
import chooseImg from 'chooseImg'
const onPaste = (e) => {
if (!(e.clipboardData && e.clipboardData.items)) {
return
}
return new Promise((resolve, reject) => {
for (let i = 0, len = e.clipboardData.items.length; i < len; i++) {
const item = e.clipboardData.items[i]
if (item.kind === 'string') {
item.getAsString((str) => {
resolve(str)
})
} else if (item.kind === 'file') {
const pasteFile = item.getAsFile()
const imgEvent = {
target: {
files: [pasteFile]
}
}
chooseImg(imgEvent, (url) => {
resolve(url)
})
} else {
reject(new Error('Not allow to paste this type!'))
}
}
})
}
export default onPaste
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment