diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js index 5bb6c8b4b7e..bfdea46b331 100644 --- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js +++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js @@ -1356,6 +1356,11 @@ const Wysiwyg = Widget.extend({ const field = $editable.data('oe-field'); const type = $editable.data('oe-type'); + // The html_field value should not be updated while the mediaDialog is + // in use because if its value change, restoreSelection may fail since + // it has a reference to HTMLElements which are not in the DOM anymore. + this._shouldDelayBlur = true; + this.mediaDialogWrapper = new ComponentWrapper(this, MediaDialogWrapper, { resModel: model, resId: $editable.data('oe-id'), @@ -2445,8 +2450,9 @@ const Wysiwyg = Widget.extend({ if (!e.target.classList.contains('o_editable_date_field_linked')) { this.$editable.find('.o_editable_date_field_linked').removeClass('o_editable_date_field_linked'); } - if (e.target.closest('.oe-toolbar')) { - this._onToolbar = true; + const closestDialog = e.target.closest('.o_dialog, .o_web_editor_dialog'); + if (e.target.closest('.oe-toolbar') || (closestDialog && closestDialog.querySelector('.o_select_media_dialog, .o_link_dialog'))) { + this._shouldDelayBlur = true; } else { if (this._pendingBlur && !e.target.closest('.o_wysiwyg_wrapper')) { // todo: to remove when removing the legacy field_html @@ -2454,11 +2460,11 @@ const Wysiwyg = Widget.extend({ this.options.onWysiwygBlur && this.options.onWysiwygBlur(); this._pendingBlur = false; } - this._onToolbar = false; + this._shouldDelayBlur = false; } }, _onBlur: function () { - if (this._onToolbar) { + if (this._shouldDelayBlur) { this._pendingBlur = true; } else { // todo: to remove when removing the legacy field_html