[FIX] web_editor: disable observer only when necessary

Before this commit, the linktool was disabling the editor observer
until it was destroyed. This messed up with the undo/redo and the
isDirty.

This commit only disable the observer when necessary.

task-2836351

Forward-port-of: #90064
X-original-commit: 57c98f314332bbb4c3225942e1d3748eb131d8b0
Part-of: odoo/odoo#91074
This commit is contained in:
Nicolas Bayet
2022-05-11 14:54:28 +02:00
parent 8d6c4e421e
commit 57d3211c99
4 changed files with 51 additions and 11 deletions
@@ -209,6 +209,7 @@ export class OdooEditor extends EventTarget {
return closestElement(selection.anchorNode, 'P, DIV');
}
},
preHistoryUndo: () => {},
isHintBlacklisted: () => false,
filterMutationRecords: (records) => records,
direction: 'ltr',
@@ -809,6 +810,7 @@ export class OdooEditor extends EventTarget {
* "consumed": The position has been undone and is considered consumed.
*/
historyUndo() {
this.options.preHistoryUndo();
// The last step is considered an uncommited draft so always revert it.
const lastStep = this._currentStep;
this.historyRevert(lastStep);
@@ -19,6 +19,7 @@ const Link = Widget.extend({
'input': '_onAnyChange',
'change': '_onAnyChange',
'input input[name="url"]': '_onURLInput',
'change input[name="url"]': '_onURLInputChange',
},
/**
@@ -142,6 +143,7 @@ const Link = Widget.extend({
var match = /mailto:(.+)/.exec(this.data.url);
this.$('input[name="url"]').val(match ? match[1] : this.data.url);
this._onURLInput();
this._savedURLInputOnDestroy = false;
}
this._updateOptionsUI();
@@ -152,6 +154,15 @@ const Link = Widget.extend({
return this._super.apply(this, arguments);
},
/**
* @override
*/
destroy () {
if (this._savedURLInputOnDestroy) {
this._adaptPreview();
}
this._super(...arguments);
},
//--------------------------------------------------------------------------
// Public
@@ -458,19 +469,26 @@ const Link = Widget.extend({
/**
* @private
*/
_onAnyChange: function () {
this._adaptPreview();
_onAnyChange: function (e) {
if (!e.target.closest('input[type="text"]')) {
this._adaptPreview();
}
},
/**
* @private
*/
_onURLInput: function () {
this._savedURLInputOnDestroy = true;
var $linkUrlInput = this.$('#o_link_dialog_url_input');
let value = $linkUrlInput.val();
let isLink = value.indexOf('@') < 0;
this.$('input[name="is_new_window"]').closest('.form-group').toggleClass('d-none', !isLink);
this.$('.o_strip_domain').toggleClass('d-none', value.indexOf(window.location.origin) !== 0);
},
_onURLInputChange: function () {
this._adaptPreview();
this._savedURLInputOnDestroy = false;
},
});
/**
@@ -45,31 +45,28 @@ const LinkTools = Link.extend({
* @override
*/
start: function () {
this.$link.addClass('oe_edited_link');
this.$button.addClass('active');
this._addHintClasses();
return this._super(...arguments);
},
destroy: function () {
if (!this.el) {
return this._super(...arguments);
}
$(this.options.wysiwyg.odooEditor.document).find('.oe_edited_link').removeClass('oe_edited_link');
const $contents = this.$link.contents();
if (!this.$link.attr('href') && !this.colorCombinationClass) {
$contents.unwrap();
}
this.$button.removeClass('active');
this.applyLinkToDom(this._getData());
if (!this.options.wysiwyg.odooEditor.isDestroyed) {
this.options.wysiwyg.odooEditor.historyStep();
}
this._observer.disconnect();
this._super(...arguments);
this._removeHintClasses();
},
applyLinkToDom() {
this._observer.disconnect();
this._removeHintClasses();
this._super(...arguments);
this.options.wysiwyg.odooEditor.historyStep();
this._addHintClasses();
this._observer.observe(this._link, {subtree: true, childList: true, characterData: true});
},
@@ -97,7 +94,6 @@ const LinkTools = Link.extend({
if (data === null) {
return;
}
data.classes += ' oe_edited_link';
this.applyLinkToDom(data);
},
/**
@@ -323,6 +319,24 @@ const LinkTools = Link.extend({
$colorPreview.css('background-image', isColorGradient(color) ? color : '');
}
},
/**
* Add hint to the classes of the link and button.
*/
_addHintClasses () {
this.options.wysiwyg.odooEditor.observerUnactive("hint_classes");
this.$link.addClass('oe_edited_link');
this.$button.addClass('active');
this.options.wysiwyg.odooEditor.observerActive("hint_classes");
},
/**
* Remove hint to the classes of the link and button.
*/
_removeHintClasses () {
this.options.wysiwyg.odooEditor.observerUnactive("hint_classes");
$(this.options.wysiwyg.odooEditor.document).find('.oe_edited_link').removeClass('oe_edited_link');
this.$button.removeClass('active');
this.options.wysiwyg.odooEditor.observerActive("hint_classes");
},
//--------------------------------------------------------------------------
// Handlers
@@ -163,6 +163,12 @@ const Wysiwyg = Widget.extend({
return !(record.target.classList && record.target.classList.contains('o_header_standard'));
});
},
preHistoryUndo: () => {
if (this.linkTools) {
this.linkTools.destroy();
this.linkTools = undefined;
}
},
commands: commands,
plugins: options.editorPlugins,
direction: localization.direction || 'ltr',