From 33cbbed774e7f07813db2b186810cf890efe8e47 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 12 May 2016 11:05:40 +0200 Subject: [PATCH] Prevent pasting images from word/excel/numbers which is normally text or html. Just past images if first item is really an image. --- .../app/lib/base/jquery.contenteditable.js | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js index a39b741ab..1eab9a987 100644 --- a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js +++ b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js @@ -200,36 +200,35 @@ } var clipboardData = e.clipboardData || e.originalEvent.clipboardData - if (clipboardData && clipboardData.items) { + if (clipboardData && clipboardData.items && clipboardData.items[0]) { var imageInserted = false - jQuery.each(clipboardData.items, function(index, item){ - console.log(index, item) - if (item.kind == 'file' && item.type == 'image/png') { - _this.log('paste image', item) + var item = clipboardData.items[0] + if (item.kind == 'file' && (item.type == 'image/png' || item.type == 'image/jpeg')) { + _this.log('paste image', item) + console.log(item) - var imageFile = item.getAsFile() - var reader = new FileReader() + var imageFile = item.getAsFile() + var reader = new FileReader() - reader.onload = function (e) { - var result = e.target.result - var img = document.createElement('img') - img.src = result + reader.onload = function (e) { + var result = e.target.result + var img = document.createElement('img') + img.src = result - insert = function(dataUrl, width, height) { - //console.log('dataUrl', dataUrl) - _this.log('image inserted') - result = dataUrl - img = "" - document.execCommand('insertHTML', false, img) - } - - // resize if to big - App.ImageService.resize(img.src, 460, 'auto', 2, 'image/jpeg', 'auto', insert) + insert = function(dataUrl, width, height) { + //console.log('dataUrl', dataUrl) + _this.log('image inserted') + result = dataUrl + img = "" + document.execCommand('insertHTML', false, img) } - reader.readAsDataURL(imageFile) - imageInserted = true + + // resize if to big + App.ImageService.resize(img.src, 460, 'auto', 2, 'image/jpeg', 'auto', insert) } - }) + reader.readAsDataURL(imageFile) + imageInserted = true + } } if (imageInserted) { return