pasteManager.js 730 B

12345678910111213141516171819202122232425
  1. /* global MAXFILESIZE */
  2. import { bytes } from './utils';
  3. export default function(state, emitter) {
  4. window.addEventListener('paste', event => {
  5. if (state.route !== '/' || state.uploading) return;
  6. for (const item of event.clipboardData.items) {
  7. if (!item.type.includes('image')) continue;
  8. const file = item.getAsFile();
  9. if (!file) continue; // Sometimes null
  10. if (file.size > MAXFILESIZE) {
  11. // eslint-disable-next-line no-alert
  12. alert(state.translate('fileTooBig', { size: bytes(MAXFILESIZE) }));
  13. continue;
  14. }
  15. emitter.emit('upload', { file, type: 'paste' });
  16. return; // return here since only one file is allowed to be uploaded at a time
  17. }
  18. });
  19. }