Skip to content

Commit f3e4e94

Browse files
committed
[FIX] web_editor: image format set to None when applying a shape
When computing the current image format, don't guess and instead use the `_getImageWidth` method which normalizes the dataset attribute that takes priority. Make sure to only use the `naturalWidth` attribute only as a last resort, and only for the original size. task-3847470
1 parent e9cc1a5 commit f3e4e94

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6268,19 +6268,9 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
62686268
const img = this._getImg();
62696269
const _super = this._super.bind(this);
62706270

6271-
// Make sure image is loaded because we need its naturalWidth
6272-
await new Promise((resolve, reject) => {
6273-
if (img.complete) {
6274-
resolve();
6275-
return;
6276-
}
6277-
img.addEventListener('load', resolve, {once: true});
6278-
img.addEventListener('error', resolve, {once: true});
6279-
});
6280-
62816271
switch (methodName) {
62826272
case 'selectFormat':
6283-
return this._getCurrentFormat(img);
6273+
return await this._getCurrentFormat(img);
62846274
case 'setFilter':
62856275
return img.dataset.filter;
62866276
case 'glFilter':
@@ -6373,7 +6363,7 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
63736363
// - Changing the specs of the available formats, where some previously
63746364
// used format cannot be selected anymore
63756365
// - External modification of the format
6376-
const currentFormat = this._getCurrentFormat(img);
6366+
const currentFormat = await this._getCurrentFormat(img);
63776367
const [selectedSize, selectedMimetype] = currentFormat?.split(" ") || [];
63786368
if (
63796369
selectedSize &&
@@ -6552,12 +6542,12 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
65526542
},
65536543
/**
65546544
* @param {HTMLImageElement} [image] Override image element.
6555-
* @return {string} The image's current width.
6545+
* @return {Promise<string>} The image's current width (selected format).
65566546
* @private
65576547
*/
6558-
_getImageWidth(image = this._getImg()) {
6548+
async _getImageWidth(image = this._getImg()) {
65596549
return Math.trunc(
6560-
image.dataset.resizeWidth || image.dataset.width || image.naturalWidth
6550+
image.dataset.resizeWidth || image.dataset.width || (await this._getOriginalSize())
65616551
).toString();
65626552
},
65636553
/**
@@ -6571,11 +6561,11 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
65716561
},
65726562
/**
65736563
* @param {HTMLImageElement} img
6574-
* @return {string} The image's current format (widget value).
6564+
* @return {Promise<string>} The image's current format (widget value).
65756565
* @private
65766566
*/
6577-
_getCurrentFormat(img) {
6578-
return `${img.naturalWidth} ${this._getImageMimetype(img)}`;
6567+
async _getCurrentFormat(img) {
6568+
return `${await this._getImageWidth(img)} ${this._getImageMimetype(img)}`;
65796569
},
65806570
/**
65816571
* Returns whether the format is the image's original format.
@@ -6604,7 +6594,7 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
66046594
* @private
66056595
*/
66066596
async _getImageTargetMimetype(image = this._getImg()) {
6607-
const currentSize = this._getImageWidth(image);
6597+
const currentSize = await this._getImageWidth(image);
66086598
const currentMimetype = this._getImageMimetype(image);
66096599

66106600
const isOriginalFormat = await this._isOriginalFormat(currentSize, currentMimetype);

0 commit comments

Comments
 (0)