diff --git a/addons/web_editor/static/src/js/backend/field_html.js b/addons/web_editor/static/src/js/backend/field_html.js index 1905c350f7b..82169c53fba 100644 --- a/addons/web_editor/static/src/js/backend/field_html.js +++ b/addons/web_editor/static/src/js/backend/field_html.js @@ -107,9 +107,11 @@ var FieldHtml = basic_fields.DebouncedField.extend(TranslatableFieldMixin, { return this._super(); } var _super = this._super.bind(this); - return this.wysiwyg.save().then(function (result) { - self._isDirty = result.isDirty; - _super(); + return this.wysiwyg.saveCroppedImages(this.$content).then(function () { + return self.wysiwyg.save().then(function (result) { + self._isDirty = result.isDirty; + _super(); + }); }); }, /** diff --git a/addons/web_editor/static/src/js/editor/editor.js b/addons/web_editor/static/src/js/editor/editor.js index 967b9dbb226..61cf26a5492 100644 --- a/addons/web_editor/static/src/js/editor/editor.js +++ b/addons/web_editor/static/src/js/editor/editor.js @@ -229,50 +229,7 @@ var EditorMenuBar = Widget.extend({ * @private */ _saveCroppedImages: function () { - var self = this; - var defs = _.map(this.rte.editable().find('.o_cropped_img_to_save'), function (croppedImg) { - var $croppedImg = $(croppedImg); - $croppedImg.removeClass('o_cropped_img_to_save'); - - var resModel = $croppedImg.data('crop:resModel'); - var resID = $croppedImg.data('crop:resID'); - var cropID = $croppedImg.data('crop:id'); - var mimetype = $croppedImg.data('crop:mimetype'); - var originalSrc = $croppedImg.data('crop:originalSrc'); - - var datas = $croppedImg.attr('src').split(',')[1]; - - if (!cropID) { - var name = originalSrc + '.crop'; - return self._rpc({ - model: 'ir.attachment', - method: 'create', - args: [{ - res_model: resModel, - res_id: resID, - name: name, - datas: datas, - mimetype: mimetype, - url: originalSrc, // To save the original image that was cropped - }], - }).then(function (attachmentID) { - return self._rpc({ - model: 'ir.attachment', - method: 'generate_access_token', - args: [[attachmentID]], - }).then(function (access_token) { - $croppedImg.attr('src', '/web/image/' + attachmentID + '?access_token=' + access_token[0]); - }); - }); - } else { - return self._rpc({ - model: 'ir.attachment', - method: 'write', - args: [[cropID], {datas: datas}], - }); - } - }); - return Promise.all(defs); + return this.rte.saveCroppedImages(this.rte.editable()); }, //-------------------------------------------------------------------------- diff --git a/addons/web_editor/static/src/js/editor/rte.summernote.js b/addons/web_editor/static/src/js/editor/rte.summernote.js index 2be70626ab4..8a848389c51 100644 --- a/addons/web_editor/static/src/js/editor/rte.summernote.js +++ b/addons/web_editor/static/src/js/editor/rte.summernote.js @@ -9,6 +9,7 @@ var ColorPaletteWidget = require('web_editor.ColorPalette').ColorPaletteWidget; var mixins = require('web.mixins'); var fonts = require('wysiwyg.fonts'); var rte = require('web_editor.rte'); +var ServicesMixin = require('web.ServicesMixin'); var weWidgets = require('wysiwyg.widgets'); var _t = core._t; @@ -1051,7 +1052,7 @@ $.summernote.lang.odoo = { * instantiate media, link and alt dialogs outside the main editor: in the * simple HTML fields and forum textarea. */ -var SummernoteManager = Class.extend(mixins.EventDispatcherMixin, { +var SummernoteManager = Class.extend(mixins.EventDispatcherMixin, ServicesMixin, { /** * @constructor */ @@ -1076,6 +1077,58 @@ var SummernoteManager = Class.extend(mixins.EventDispatcherMixin, { core.bus.off('media_dialog_demand', this, this._onMediaDialogDemand); }, + /** + * Create/Update cropped attachments. + * + * @param {jQuery} $editable + * @returns {Promise} + */ + saveCroppedImages: function ($editable) { + var defs = _.map($editable.find('.o_cropped_img_to_save'), croppedImg => { + var $croppedImg = $(croppedImg); + $croppedImg.removeClass('o_cropped_img_to_save'); + + var resModel = $croppedImg.data('crop:resModel'); + var resID = $croppedImg.data('crop:resID'); + var cropID = $croppedImg.data('crop:id'); + var mimetype = $croppedImg.data('crop:mimetype'); + var originalSrc = $croppedImg.data('crop:originalSrc'); + + var datas = $croppedImg.attr('src').split(',')[1]; + + if (!cropID) { + var name = originalSrc + '.crop'; + return this._rpc({ + model: 'ir.attachment', + method: 'create', + args: [{ + res_model: resModel, + res_id: resID, + name: name, + datas: datas, + mimetype: mimetype, + url: originalSrc, // To save the original image that was cropped + }], + }).then(attachmentID => { + return this._rpc({ + model: 'ir.attachment', + method: 'generate_access_token', + args: [[attachmentID]], + }).then(access_token => { + $croppedImg.attr('src', '/web/image/' + attachmentID + '?access_token=' + access_token[0]); + }); + }); + } else { + return this._rpc({ + model: 'ir.attachment', + method: 'write', + args: [[cropID], {datas: datas}], + }); + } + }); + return Promise.all(defs); + }, + //-------------------------------------------------------------------------- // Handlers //-------------------------------------------------------------------------- @@ -1201,6 +1254,20 @@ rte.Class.include({ this._super.apply(this, arguments); this._summernoteManager.destroy(); }, + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * Create/Update cropped attachments. + * + * @param {jQuery} $editable + * @returns {Deferred} + */ + saveCroppedImages: function ($editable) { + return this._summernoteManager.saveCroppedImages($editable); + }, }); return SummernoteManager; }); diff --git a/addons/web_editor/static/src/js/wysiwyg/root.js b/addons/web_editor/static/src/js/wysiwyg/root.js index e5c225e646d..f6f33540e35 100644 --- a/addons/web_editor/static/src/js/wysiwyg/root.js +++ b/addons/web_editor/static/src/js/wysiwyg/root.js @@ -8,7 +8,7 @@ var assetsLoaded = false; var WysiwygRoot = Widget.extend({ assetLibs: ['web_editor.compiled_assets_wysiwyg'], - publicMethods: ['isDirty', 'save', 'getValue', 'setValue', 'getEditable', 'on', 'trigger', 'focus'], + publicMethods: ['isDirty', 'save', 'getValue', 'setValue', 'getEditable', 'on', 'trigger', 'focus', 'saveCroppedImages'], /** * @see 'web_editor.wysiwyg' module diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js index 361355c4cab..fe7399fdebe 100644 --- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js +++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js @@ -59,7 +59,7 @@ var Wysiwyg = Widget.extend({ * @override **/ willStart: function () { - new SummernoteManager(this); + this._summernoteManager = new SummernoteManager(this); this.$target = this.$el; return this._super(); }, @@ -152,6 +152,15 @@ var Wysiwyg = Widget.extend({ } return Promise.resolve({isDirty:isDirty, html:html}); }, + /** + * Create/Update cropped attachments. + * + * @param {jQuery} $editable + * @returns {Promise} + */ + saveCroppedImages: function ($editable) { + return this._summernoteManager.saveCroppedImages($editable); + }, /** * @param {String} value * @param {Object} options