Skip to content

Commit cbd38e7

Browse files
committed
[FIX] website: convert images added to Image Wall to webp
Before this commit, when images are added to an Image Wall or to an Image Gallery, they remained in their original format. After this commit, images added to an Image Wall or an Image Gallery are converted to webp. Thanks to [1], the Image Gallery dataset entries related to the conversion are preserved. [1]: odoo@e73a1a9 task-3666894 X-original-commit: 6aaddd3 Part-of: odoo#170037 Signed-off-by: Benjamin Vray (bvr) <[email protected]>
1 parent 6fd2f89 commit cbd38e7

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

addons/website/static/src/snippets/s_image_gallery/options.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import options from "@web_editor/js/editor/snippets.options";
55
import wUtils from '@website/js/utils';
66
import { _t } from "@web/core/l10n/translation";
77
import { renderToElement } from "@web/core/utils/render";
8+
import {
9+
loadImageInfo,
10+
applyModifications,
11+
} from "@web_editor/js/editor/image_processing";
812

913
/**
1014
* This class provides layout methods for interacting with the ImageGallery
@@ -420,6 +424,13 @@ options.registry.gallery = options.registry.GalleryLayout.extend({
420424
});
421425

422426
options.registry.GalleryImageList = options.registry.GalleryLayout.extend({
427+
/**
428+
* @override
429+
*/
430+
init() {
431+
this.rpc = this.bindService("rpc");
432+
return this._super.apply(this, arguments);
433+
},
423434
/**
424435
* @override
425436
*/
@@ -497,18 +508,44 @@ options.registry.GalleryImageList = options.registry.GalleryLayout.extend({
497508
multiImages: true,
498509
onlyImages: true,
499510
save: images => {
511+
const imagePromises = [];
500512
for (const image of images) {
501-
$('<img/>', {
513+
const $img = $('<img/>', {
502514
class: $images.length > 0 ? $images[0].className : 'img img-fluid d-block ',
503515
src: image.src,
504516
'data-index': ++index,
505517
alt: image.alt || '',
506518
'data-name': _t('Image'),
507519
style: $images.length > 0 ? $images[0].style.cssText : '',
508520
}).appendTo($container);
521+
const imgEl = $img[0];
522+
imagePromises.push(new Promise(resolve => {
523+
loadImageInfo(imgEl, this.rpc).then(() => {
524+
if (imgEl.dataset.mimetype && ![
525+
"image/gif",
526+
"image/svg+xml",
527+
"image/webp",
528+
].includes(imgEl.dataset.mimetype)) {
529+
// Convert to webp but keep original width.
530+
imgEl.dataset.mimetype = "image/webp";
531+
applyModifications(imgEl, {
532+
mimetype: "image/webp",
533+
}).then(src => {
534+
imgEl.src = src;
535+
imgEl.classList.add("o_modified_image_to_save");
536+
resolve();
537+
});
538+
} else {
539+
resolve();
540+
}
541+
});
542+
}));
509543
}
544+
savedPromise = Promise.all(imagePromises);
510545
if (images.length > 0) {
511-
savedPromise = this._relayout();
546+
savedPromise = savedPromise.then(async () => {
547+
await this._relayout();
548+
});
512549
this.trigger_up('cover_update');
513550
}
514551
},

0 commit comments

Comments
 (0)