diff --git a/application/Espo/Resources/i18n/en_US/Email.json b/application/Espo/Resources/i18n/en_US/Email.json index 9243bbb7b2..27d74da782 100644 --- a/application/Espo/Resources/i18n/en_US/Email.json +++ b/application/Espo/Resources/i18n/en_US/Email.json @@ -102,7 +102,8 @@ "noSmtpSetup": "No SMTP setup. {link}.", "testEmailSent": "Test email has been sent", "emailSent": "Email has been sent", - "savedAsDraft": "Saved as draft" + "savedAsDraft": "Saved as draft", + "confirmInsertTemplate": "The email body will be lost. Are you sure you want to insert the template?" }, "presetFilters": { "sent": "Sent", diff --git a/client/src/views/email/record/compose.js b/client/src/views/email/record/compose.js index 90e25ceae8..31349dacb7 100644 --- a/client/src/views/email/record/compose.js +++ b/client/src/views/email/record/compose.js @@ -54,30 +54,27 @@ Espo.define('views/email/record/compose', ['views/record/edit', 'views/email/rec } this.listenTo(this.model, 'insert-template', function (data) { - var body = data.body; - if (this.hasSignature()) { - body = this.appendSignature(body || '', data.isHtml); - } - this.model.set('isHtml', data.isHtml); - this.model.set('name', data.subject); - this.model.set('body', ''); - this.model.set('body', body); + var body = this.model.get('body'); - if (this.options.keepAttachmentsOnSelectTemplate) { - this.initialAttachmentsIds.forEach(function (id) { - if (data.attachmentsIds) { - data.attachmentsIds.push(id); - } - if (data.attachmentsNames) { - data.attachmentsNames[id] = this.initialAttachmentsNames[id] || id; - } + var bodyPlain = body.replace(//mg, ''); + bodyPlain = bodyPlain.replace(/<\/p\s*\/?>/mg, ''); + bodyPlain = bodyPlain.replace(/ /g, ''); + bodyPlain = bodyPlain.replace(/\n/g, ''); + + var $div = $('
').html(bodyPlain); + bodyPlain = $div.text(); + + if (bodyPlain !== '' && this.model.get('body') !== this.attributes.body) { + this.confirm({ + message: this.translate('confirmInsertTemplate', 'messages', 'Email'), + confirmText: this.translate('Yes') + }, function () { + this.insertTemplate(data); }, this); + } else { + this.insertTemplate(data); } - this.model.set({ - attachmentsIds: data.attachmentsIds, - attachmentsNames: data.attachmentsNames - }); }, this); if (this.options.selectTemplateDisabled) { @@ -85,6 +82,33 @@ Espo.define('views/email/record/compose', ['views/record/edit', 'views/email/rec } }, + insertTemplate: function (data) { + var body = data.body; + if (this.hasSignature()) { + body = this.appendSignature(body || '', data.isHtml); + } + this.model.set('isHtml', data.isHtml); + this.model.set('name', data.subject); + this.model.set('body', ''); + this.model.set('body', body); + + if (this.options.keepAttachmentsOnSelectTemplate) { + this.initialAttachmentsIds.forEach(function (id) { + if (data.attachmentsIds) { + data.attachmentsIds.push(id); + } + if (data.attachmentsNames) { + data.attachmentsNames[id] = this.initialAttachmentsNames[id] || id; + } + }, this); + } + + this.model.set({ + attachmentsIds: data.attachmentsIds, + attachmentsNames: data.attachmentsNames + }); + }, + prependSignature: function (body, isHtml) { if (isHtml) { var signature = this.getSignature();