Skip to content

Commit e9cc1a5

Browse files
committed
[FIX] web_editor: select optimized width format on crop reset
When cropping an image, the selected format is lost. Ideally, resetting the crop would restore the format selected before cropping. However, before this PR, resetting the crop loses the format size but keeps the right mimetype. This commit selects the "Suggested" (optimized width) format when resetting the crop. This behavior is expected to be changed in a future task to instead go back to the format that was selected before cropping. This commit also fixes the way the original size is computed. If the current image has a data-width attribute, use its value instead of the naturalWidth property, as the latter actually corresponds in some cases to the "optimized width". task-3847470
1 parent 60e25f7 commit e9cc1a5

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6567,7 +6567,7 @@ const ImageHandlerOption = SnippetOptionWidget.extend({
65676567
async _getOriginalSize() {
65686568
const image = this._getImg();
65696569
const originalImage = await loadImage(this.originalSrc);
6570-
return image.dataset.width ? image.naturalWidth : originalImage.naturalWidth;
6570+
return Math.round(image.dataset.width) || originalImage.naturalWidth;
65716571
},
65726572
/**
65736573
* @param {HTMLImageElement} img
@@ -6762,7 +6762,6 @@ registry.ImageTools = ImageHandlerOption.extend({
67626762
// temporarily into the body.
67636763
const imageCropWrapperElement = document.createElement('div');
67646764
document.body.append(imageCropWrapperElement);
6765-
img.dataset.targetMimetype = img.dataset.mimetypeBeforeConversion;
67666765
const imageCropWrapper = await attachComponent(this, imageCropWrapperElement, ImageCrop, {
67676766
rpc: this.rpc,
67686767
activeOnStart: true,
@@ -6776,6 +6775,11 @@ registry.ImageTools = ImageHandlerOption.extend({
67766775
imageCropWrapperElement.remove();
67776776

67786777
await this._reapplyCurrentShape();
6778+
6779+
// Reset to "Suggested" format
6780+
await this._computeAvailableFormats(); // Update this.optimizedWidth
6781+
const optimizedMimetype = canExportCanvasAsWebp() ? "image/webp" : "image/jpeg";
6782+
await this.selectFormat(false, `${this.optimizedWidth} ${optimizedMimetype}`);
67796783
},
67806784
/**
67816785
* Resets the image rotation and translation

addons/website/static/tests/tours/snippet_image_mimetype.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,12 @@ function testImageSnippet(imageFormat, originalMimetype, formatMimetype) {
354354
...testStepOnOff(setImageShape, removeImageShape, true),
355355

356356
// Image crop
357-
...testStepOnOff(cropImage, removeImageCrop, false, false, "image/jpeg"),
357+
...testStepOnOff(cropImage, removeImageCrop, false, false, formatMimetype),
358358

359359
// Image crop while shape on
360360
...setImageShape(),
361-
...testStepOnOff(cropImage, removeImageCrop, true, true, "image/jpeg"),
362-
...removeImageShape("image/jpeg"),
361+
...testStepOnOff(cropImage, removeImageCrop, true, true, formatMimetype),
362+
...removeImageShape(formatMimetype),
363363
];
364364
}
365365

@@ -545,16 +545,16 @@ wTourUtils.registerWebsitePreviewTour(
545545
mockCanvasToDataURLStep,
546546
...removeImageCrop("image/jpeg"), // !isChanged && Original format is smaller
547547
...testImageMimetypeIs("image/jpeg"),
548-
testFormatSnippetOption("800px (Original) jpeg"),
548+
testFormatSnippetOption("690px (Suggested) jpeg"),
549549
...setImageFormat("512 image/jpeg"),
550550

551551
// No webp -> webp
552552
...cropImage("image/jpeg"),
553553
...testImageMimetypeIs("image/jpeg"),
554554
unmockCanvasToDataURLStep,
555-
...removeImageCrop("image/jpeg"),
556-
...testImageMimetypeIs("image/jpeg"),
557-
testFormatSnippetOption("800px (Original) jpeg"),
555+
...removeImageCrop("image/webp"),
556+
...testImageMimetypeIs("image/webp"),
557+
testFormatSnippetOption("690px (Suggested) webp"),
558558
]
559559
);
560560

0 commit comments

Comments
 (0)