Created
September 6, 2018 13:28
-
-
Save jrainlau/c434e16c8a691b3b73914a10f8ccc899 to your computer and use it in GitHub Desktop.
custom paste event
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
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