diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index 1dfdc5be860..fca6f65f7b6 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -77,7 +77,14 @@ function _buildTitleElement(title) { * @param {string} src * @returns {HTMLElement} */ -function _buildImgElement(src) { +async function _buildImgElement(src) { + if (src.split('.').pop() === 'svg') { + const response = await window.fetch(src); + const text = await response.text(); + const parser = new window.DOMParser(); + const xmlDoc = parser.parseFromString(text, 'text/xml'); + return xmlDoc.getElementsByTagName('svg')[0]; + } const imgEl = document.createElement('img'); imgEl.src = src; return imgEl; @@ -153,6 +160,15 @@ const UserValueWidget = Widget.extend({ this._value = ''; this.$target = $target; }, + /** + * @override + */ + async willStart() { + await this._super(...arguments); + if (this.options.dataAttributes.img) { + this.imgEl = await _buildImgElement(this.options.dataAttributes.img); + } + }, /** * @override */ @@ -162,9 +178,8 @@ const UserValueWidget = Widget.extend({ _addTitleAndAllowedAttributes(el, this.title, this.options); this.containerEl = document.createElement('div'); - if (el.dataset.img) { - const imgEl = _buildImgElement(el.dataset.img); - this.containerEl.appendChild(imgEl); + if (this.imgEl) { + this.containerEl.appendChild(this.imgEl); } el.appendChild(this.containerEl); @@ -192,7 +207,13 @@ const UserValueWidget = Widget.extend({ * @override */ destroy() { - this.$el.off('.img_animate'); + // Check if $el exists in case the widget is destroyed before it has + // been fully initialized. + // TODO there is probably better to do. This case was found only in + // tours, where the editor is left before the widget icon is loaded. + if (this.$el) { + this.$el.off('.img_animate'); + } this._super(...arguments); }, @@ -2280,6 +2301,13 @@ const SnippetOptionWidget = Widget.extend({ await this._renderXMLWidgets(uiFragment); await this._renderCustomWidgets(uiFragment); + if (this.isDestroyed()) { + // TODO there is probably better to do. This case was found only in + // tours, where the editor is left before the widget are fully + // loaded (loadMethodsData doesn't work if the widget is destroyed). + return uiFragment; + } + const validMethodNames = []; for (const key in this) { validMethodNames.push(key); diff --git a/addons/web_editor/static/src/scss/web_editor.variables.scss b/addons/web_editor/static/src/scss/web_editor.variables.scss index cf4b8b3b7eb..f71d4d36184 100644 --- a/addons/web_editor/static/src/scss/web_editor.variables.scss +++ b/addons/web_editor/static/src/scss/web_editor.variables.scss @@ -54,7 +54,7 @@ $o-we-item-border-color: $o-we-bg-darkest !default; $o-we-item-border-radius: 1px !default; $o-we-item-clickable-bg: linear-gradient($o-we-bg-light, $o-we-bg-darker) !default; $o-we-item-pressed-bg: linear-gradient($o-we-bg-darkest, $o-we-bg-darker) !default; -$o-we-item-pressed-color: $o-we-fg-light !default; +$o-we-item-pressed-color: $o-we-fg-lighter !default; $o-we-dropdown-spacing: 5px !default; $o-we-dropdown-bg: $o-we-bg-darker !default; diff --git a/addons/web_editor/static/src/scss/wysiwyg_snippets.scss b/addons/web_editor/static/src/scss/wysiwyg_snippets.scss index 4250f8c97e5..a65d73f87c4 100644 --- a/addons/web_editor/static/src/scss/wysiwyg_snippets.scss +++ b/addons/web_editor/static/src/scss/wysiwyg_snippets.scss @@ -254,6 +254,20 @@ body.editor_enable.editor_has_snippets { width: $o-we-sidebar-width / 2; // TODO maybe there is a better way to implement this } + @mixin we-svg-icon( + $graphic: $o-we-sidebar-content-field-color, + $subdle: $o-we-sidebar-content-field-color, + $subdle-opacity: 0.5) { + svg { + .o_graphic { + fill: $graphic; + } + .o_subdle { + fill: rgba($subdle, $subdle-opacity); + } + } + } + we-button, we-toggler { cursor: pointer; @include o-text-overflow(block); @@ -264,10 +278,14 @@ body.editor_enable.editor_has_snippets { color: $o-we-sidebar-content-field-color; line-height: $o-we-sidebar-content-field-height - 2 * $o-we-sidebar-content-field-border-width; text-align: left; + @include we-svg-icon(); + &.active, &:hover { + color: $o-we-sidebar-content-field-pressed-color; + @include we-svg-icon($o-we-sidebar-content-field-pressed-color, $subdle-opacity: .75); + } &.active { background: $o-we-sidebar-content-field-pressed-bg; - color: $o-we-sidebar-content-field-pressed-color; } &.fa {