From 67d23ff8ae691397ae9a5c12699397e01e34408e Mon Sep 17 00:00:00 2001 From: "Sam X. Chen" Date: Sat, 10 Aug 2019 21:21:42 -0400 Subject: Add Feature: Richer Editor insert picture as attachment instead of b64 string --- client/components/main/editor.js | 65 ++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 15 deletions(-) (limited to 'client/components/main/editor.js') diff --git a/client/components/main/editor.js b/client/components/main/editor.js index e217e113..248f4588 100755 --- a/client/components/main/editor.js +++ b/client/components/main/editor.js @@ -176,36 +176,71 @@ Template.editor.onRendered(() => { const $summernote = getSummernote(this); if (files && files.length > 0) { const image = files[0]; - const reader = new FileReader(); + const currentCard = Cards.findOne(Session.get('currentCard')); const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL; const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO; - const processData = function(dataURL) { + const insertImage = src => { const img = document.createElement('img'); - img.src = dataURL; + img.src = src; img.setAttribute('width', '100%'); $summernote.summernote('insertNode', img); }; - reader.onload = function(e) { - const dataurl = e && e.target && e.target.result; - if (dataurl !== undefined) { - if (MAX_IMAGE_PIXEL) { + const processData = function(fileObj) { + Utils.processUploadedAttachment( + currentCard, + fileObj, + attachment => { + if ( + attachment && + attachment._id && + attachment.isImage() + ) { + attachment.one('uploaded', function() { + const maxTry = 3; + const checkItvl = 500; + let retry = 0; + const checkUrl = function() { + // even though uploaded event fired, attachment.url() is still null somehow //TODO + const url = attachment.url(); + if (url) { + insertImage(url); + } else { + retry++; + if (retry < maxTry) { + setTimeout(checkUrl, checkItvl); + } + } + }; + checkUrl(); + }); + } + }, + ); + }; + if (MAX_IMAGE_PIXEL) { + const reader = new FileReader(); + reader.onload = function(e) { + const dataurl = e && e.target && e.target.result; + if (dataurl !== undefined) { // need to shrink image Utils.shrinkImage({ dataurl, maxSize: MAX_IMAGE_PIXEL, ratio: COMPRESS_RATIO, - callback(changed) { - if (changed !== false && !!changed) { - processData(changed); + toBlob: true, + callback(blob) { + if (blob !== false) { + blob.name = image.name; + processData(blob); } }, }); - } else { - processData(dataurl); } - } - }; - reader.readAsDataURL(image); + }; + reader.readAsDataURL(image); + } else { + processData(image); + } } }, onPaste() { -- cgit v1.2.3-1-g7c22