From 608112d56c643fd7a50e11ba5071cc5870b68a73 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Wed, 6 Jun 2018 12:39:26 +0800 Subject: [PATCH] Fix #843: Upload image on paste --- app/main.js | 2 ++ app/pasteManager.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 app/pasteManager.js diff --git a/app/main.js b/app/main.js index 353afa1a..d6ee9125 100644 --- a/app/main.js +++ b/app/main.js @@ -4,6 +4,7 @@ import app from './routes'; import locale from '../common/locales'; import fileManager from './fileManager'; import dragManager from './dragManager'; +import pasteManager from './pasteManager'; import { canHasSend } from './utils'; import storage from './storage'; import metrics from './metrics'; @@ -48,5 +49,6 @@ app.use(metrics); app.use(fileManager); app.use(dragManager); app.use(experiments); +app.use(pasteManager); app.mount('body'); diff --git a/app/pasteManager.js b/app/pasteManager.js new file mode 100644 index 00000000..ecae2476 --- /dev/null +++ b/app/pasteManager.js @@ -0,0 +1,24 @@ +/* global MAXFILESIZE */ +import { bytes } from './utils'; + +export default function(state, emitter) { + window.addEventListener('paste', event => { + if (state.route !== '/' || state.uploading) return; + + for (const item of event.clipboardData.items) { + if (!item.type.includes('image')) continue; + + const file = item.getAsFile(); + + if (!file) continue; // Sometimes null + + if (file.size > MAXFILESIZE) { + // eslint-disable-next-line no-alert + alert(state.translate('fileTooBig', { size: bytes(MAXFILESIZE) })); + continue; + } + + emitter.emit('upload', { file, type: 'paste' }); + } + }); +}