diff --git a/addons/web_editor/static/src/components/media_dialog/image_selector.js b/addons/web_editor/static/src/components/media_dialog/image_selector.js index 4031a5e1eec..3e686294a94 100644 --- a/addons/web_editor/static/src/components/media_dialog/image_selector.js +++ b/addons/web_editor/static/src/components/media_dialog/image_selector.js @@ -1,7 +1,7 @@ /** @odoo-module */ import { useService } from '@web/core/utils/hooks'; -import { getCSSVariableValue } from 'web_editor.utils'; +import { getCSSVariableValue, DEFAULT_PALETTE } from 'web_editor.utils'; import { Attachment, FileSelector, IMAGE_MIMETYPES, IMAGE_EXTENSIONS } from './file_selector'; const { useRef, useState, useEffect } = owl; @@ -23,7 +23,10 @@ export class AutoResizeImage extends Attachment { }, () => []); } - onImageLoaded() { + async onImageLoaded() { + if (this.props.onLoaded) { + await this.props.onLoaded(this.image.el); + } const aspectRatio = this.image.el.offsetWidth / this.image.el.offsetHeight; const width = aspectRatio * this.props.minRowHeight; this.container.el.style.flexGrow = width; @@ -254,6 +257,37 @@ export class ImageSelector extends FileSelector { return imageEl; })); } + + /** + * This converts the colors of an svg coming from the media library to + * the palette's ones, and make them dynamic. + * + * @param {HTMLElement} imgEl + * @param {Object} media + * @returns + */ + async onLibraryImageLoaded(imgEl, media) { + const mediaUrl = imgEl.src; + try { + const response = await fetch(mediaUrl); + if (response.headers.get('content-type') === 'image/svg+xml') { + const svg = await response.text(); + const dynamicColors = {}; + const combinedColorsRegex = new RegExp(Object.values(DEFAULT_PALETTE).join('|'), 'gi'); + svg.replace(combinedColorsRegex, match => { + const colorId = Object.keys(DEFAULT_PALETTE).find(key => DEFAULT_PALETTE[key] === match.toUpperCase()); + const colorKey = 'c' + colorId + dynamicColors[colorKey] = getCSSVariableValue('o-color-' + colorId); + }); + if (Object.keys(dynamicColors).length) { + media.isDynamicSVG = true; + media.dynamicColors = dynamicColors; + } + } + } catch (_e) { + console.error('CORS is misconfigured on the API server, image will be treated as non-dynamic.'); + } + } } ImageSelector.mediaSpecificClasses = ['img', 'img-fluid', 'o_we_custom_image']; diff --git a/addons/web_editor/static/src/components/media_dialog/image_selector.xml b/addons/web_editor/static/src/components/media_dialog/image_selector.xml index c0ba983f826..7b119e20780 100644 --- a/addons/web_editor/static/src/components/media_dialog/image_selector.xml +++ b/addons/web_editor/static/src/components/media_dialog/image_selector.xml @@ -49,7 +49,8 @@ altDescription="media.tooltip" minRowHeight="MIN_ROW_HEIGHT" selected="this.selectedMediaIds.includes(media.id)" - onImageClick="() => this.onClickMedia(media)"/> + onImageClick="() => this.onClickMedia(media)" + onLoaded="(imgEl) => this.onLibraryImageLoaded(imgEl, media)"/> diff --git a/addons/web_editor/static/src/components/media_dialog/media_dialog.scss b/addons/web_editor/static/src/components/media_dialog/media_dialog.scss index 76f9a650106..4fdcd90321d 100644 --- a/addons/web_editor/static/src/components/media_dialog/media_dialog.scss +++ b/addons/web_editor/static/src/components/media_dialog/media_dialog.scss @@ -38,8 +38,12 @@ $min-row-height: 128; opacity: 1; } - img { - width: 100%; + .o_we_media_dialog_img_wrapper { + @extend %o-preview-alpha-background; + + img { + width: 100%; + } } }