From 93f69ec9d7241c71790881ec1dba5010ebb225d2 Mon Sep 17 00:00:00 2001 From: "Louis (loco)" Date: Tue, 25 Apr 2023 08:05:19 +0000 Subject: [PATCH] [FIX] web_editor: modify background element at option change For background images, modifying options such as "Filter" does not directly lead to the modification of their corresponding data attributes in the DOM as those modifications are done at the save request. This should not be the case. The goal of this commit is to, as for regular images, modify their corresponding DOM data attributes at the option update. Steps to reproduce the bug: - Add a Cover snippet on the website. - Replace the background image by one of your own. - Add a filter to the background image. - Inspect the Cover snippet. => The `data-gl-filter` attribute has not been added in the DOM and you have to click on "Save" for it to be actually added. task-3287330 X-original-commit: ed19a1e91a2706b157532778b15d4a581a4da0e2 Part-of: odoo/odoo#123873 --- .../static/src/js/editor/snippets.options.js | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index b9b64579361..202f6385546 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -6539,22 +6539,6 @@ registry.BackgroundOptimize = ImageHandlerOption.extend({ this.$target.off('.BackgroundOptimize'); return this._super(...arguments); }, - /** - * Marks the target for creation of an attachment and copies data attributes - * to the target so that they can be restored on this.img in later editions. - * - * @override - */ - async cleanForSave() { - const img = this._getImg(); - if (img.matches('.o_modified_image_to_save')) { - this.$target.addClass('o_modified_image_to_save'); - Object.entries(img.dataset).forEach(([key, value]) => { - this.$target[0].dataset[key] = value; - }); - this.$target[0].dataset.bgSrc = img.getAttribute('src'); - } - }, //-------------------------------------------------------------------------- // Private @@ -6611,6 +6595,21 @@ registry.BackgroundOptimize = ImageHandlerOption.extend({ parts.url = `url('${img.getAttribute('src')}')`; const combined = backgroundImagePartsToCss(parts); this.$target.css('background-image', combined); + // Apply modification on the DOM HTML element that is currently being + // modified. + this.$target[0].classList.add("o_modified_image_to_save"); + // First delete the data attributes relative to the image background + // from the target as a data attribute could have been be removed (ex: + // glFilter). + for (const attribute in this.$target[0].dataset) { + if (isBackgroundImageAttribute(attribute)) { + delete this.$target[0].dataset[attribute]; + } + } + Object.entries(img.dataset).forEach(([key, value]) => { + this.$target[0].dataset[key] = value; + }); + this.$target[0].dataset.bgSrc = img.getAttribute("src"); }, //--------------------------------------------------------------------------