Skip to content

Commit aa81cf5

Browse files
committed
[PERF] web_editor: save original image size in element attributes
When computing the original size of the image, the original image is loaded to make sure we get the right value. This commit caches it into the `data-width` attribute of the image to avoid loading the original image every time. This fixes the performance penalty introduced in commit [1]. Because the attachment/original image could change between page reloads, the `data-width` attribute is removed in `cleanForSave`. [1]: 60e25f7 task-3847470
1 parent 1da99a2 commit aa81cf5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

addons/web_editor/static/src/js/editor/snippets.options.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6177,6 +6177,13 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
61776177
// This does not update the target.
61786178
await this._applyOptions(false);
61796179
},
6180+
/**
6181+
* @override
6182+
*/
6183+
async cleanForSave() {
6184+
const img = this._getImg();
6185+
delete img.dataset.width;
6186+
},
61806187

61816188
//--------------------------------------------------------------------------
61826189
// Public
@@ -6413,6 +6420,10 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
64136420

64146421
if (update) {
64156422
img.classList.add('o_modified_image_to_save');
6423+
if (!img.dataset.width) {
6424+
// Save image original width.
6425+
img.dataset.width = await this._getOriginalSize();
6426+
}
64166427
this._setImageMimetype(img, mimetype);
64176428
const loadedImg = await loadImage(dataURL, img);
64186429
this._applyImage(loadedImg);
@@ -6556,8 +6567,13 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
65566567
*/
65576568
async _getOriginalSize() {
65586569
const image = this._getImg();
6559-
const originalImage = await loadImage(this.originalSrc);
6560-
return Math.round(image.dataset.width) || originalImage.naturalWidth;
6570+
const storedOriginalWidth = Math.round(image.dataset.width);
6571+
if (storedOriginalWidth) {
6572+
return storedOriginalWidth;
6573+
} else {
6574+
const originalImage = await loadImage(this.originalSrc);
6575+
return originalImage.naturalWidth;
6576+
}
65616577
},
65626578
/**
65636579
* @param {HTMLImageElement} img

0 commit comments

Comments
 (0)