From fe15eeeac71d81ec100b52326e52cfd4251f0271 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 24 May 2022 16:08:48 +0300 Subject: [PATCH] wysiwyg fix paste upload duplicate --- client/src/views/fields/wysiwyg.js | 100 +++++++++-------------------- 1 file changed, 29 insertions(+), 71 deletions(-) diff --git a/client/src/views/fields/wysiwyg.js b/client/src/views/fields/wysiwyg.js index b417c2b76b..4a06f4212b 100644 --- a/client/src/views/fields/wysiwyg.js +++ b/client/src/views/fields/wysiwyg.js @@ -446,40 +446,17 @@ define('views/fields/wysiwyg', ['views/fields/text', 'lib!Summernote'], function keyMap: keyMap, callbacks: { onImageUpload: (files) => { - var file = files[0]; + let file = files[0]; - this.notify('Uploading...'); + Espo.Ui.notify(this.translate('Uploading...')); - this.getModelFactory().create('Attachment', (attachment) => { - var fileReader = new FileReader(); + this.uploadInlineAttachment(file) + .then(attachment => { + let url = '?entryPoint=attachment&id=' + attachment.id; + this.$summernote.summernote('insertImage', url); - fileReader.onload = (e) => { - attachment.set('name', file.name); - attachment.set('type', file.type); - attachment.set('role', 'Inline Attachment'); - attachment.set('global', true); - attachment.set('size', file.size); - - if (this.model.id) { - attachment.set('relatedId', this.model.id); - } - - attachment.set('relatedType', this.model.name); - attachment.set('file', e.target.result); - attachment.set('field', this.name); - - attachment - .save() - .then(() => { - let url = '?entryPoint=attachment&id=' + attachment.id; - this.$summernote.summernote('insertImage', url); - - Espo.Ui.notify(false); - }); - }; - - fileReader.readAsDataURL(file); - }); + Espo.Ui.notify(false); + }); }, onBlur: () => { this.trigger('change'); @@ -523,55 +500,36 @@ define('views/fields/wysiwyg', ['views/fields/text', 'lib!Summernote'], function this.$toolbar = this.$el.find('.note-toolbar'); this.$area = this.$el.find('.note-editing-area'); + }, - this.$area.on('paste', (e) => { - var items = e.originalEvent.clipboardData.items; - - if (!items) { - return; - } - - for (var i = 0; i < items.length; i++) { - if (!~items[i].type.indexOf('image')) { - continue; - } - - let file = items[i].getAsFile(); - - e.preventDefault(); - e.stopPropagation(); - + uploadInlineAttachment: function (file) { + return new Promise((resolve, reject) => { + this.getModelFactory().create('Attachment', attachment => { let fileReader = new FileReader(); - fileReader.onload = e => { - this.getModelFactory().create('Attachment', attachment => { - attachment.set('name', file.name); - attachment.set('type', file.type); - attachment.set('role', 'Inline Attachment'); - attachment.set('global', true); - attachment.set('size', file.size); + fileReader.onload = (e) => { + attachment.set('name', file.name); + attachment.set('type', file.type); + attachment.set('role', 'Inline Attachment'); + attachment.set('global', true); + attachment.set('size', file.size); - if (this.model.id) { - attachment.set('relatedId', this.model.id); - } + if (this.model.id) { + attachment.set('relatedId', this.model.id); + } - attachment.set('relatedType', this.model.name); - attachment.set('file', e.target.result); - attachment.set('field', this.name); + attachment.set('relatedType', this.model.name); + attachment.set('file', e.target.result); + attachment.set('field', this.name); - attachment - .save() - .then(() => { - let url = '?entryPoint=attachment&id=' + attachment.id; - this.$summernote.summernote('insertImage', url); - - Espo.Ui.notify(false); - }); - }); + attachment + .save() + .then(() => resolve(attachment)) + .catch(() => reject()); }; fileReader.readAsDataURL(file); - } + }); }); },