2018-10-02 02:46:09 +02:00
|
|
|
function getString(item) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
item.getAsString(resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-06 06:39:26 +02:00
|
|
|
export default function(state, emitter) {
|
2018-10-02 02:46:09 +02:00
|
|
|
window.addEventListener('paste', async event => {
|
2018-06-06 06:39:26 +02:00
|
|
|
if (state.route !== '/' || state.uploading) return;
|
|
|
|
|
2018-10-02 02:46:09 +02:00
|
|
|
const items = Array.from(event.clipboardData.items);
|
|
|
|
const transferFiles = items.filter(item => item.kind === 'file');
|
|
|
|
const strings = items.filter(item => item.kind === 'string');
|
|
|
|
if (transferFiles.length) {
|
|
|
|
const promises = transferFiles.map(async (f, i) => {
|
|
|
|
const blob = f.getAsFile();
|
|
|
|
if (!blob) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const name = await getString(strings[i]);
|
|
|
|
const file = new File([blob], name, { type: blob.type });
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
const files = (await Promise.all(promises)).filter(f => !!f);
|
|
|
|
if (files.length) {
|
|
|
|
emitter.emit('addFiles', { files });
|
|
|
|
}
|
|
|
|
} else if (strings.length) {
|
|
|
|
strings[0].getAsString(s => {
|
|
|
|
const file = new File([s], 'pasted.txt', { type: 'text/plain' });
|
|
|
|
emitter.emit('addFiles', { files: [file] });
|
|
|
|
});
|
2018-06-06 06:39:26 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|