From 2d64bb58cd6ab45e4266e19f75f651e045479472 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Thu, 29 Aug 2019 13:10:20 +0000 Subject: [PATCH] [FIX] web_editor: prevent removing a main editable area When a snippet part is removed, like a column, we check that one of its ancestor has a snippet editor attached and we remove all the empty element up to and including that ancestor if possible. This way, if we remove the last column of most snippet, the whole snippet is removed. The problem is when that ancestor is a main editable area that should not be able to be removed. The case is currently probably never occuring but it could be backported if needed. This is made in preparation of the mega menu task, during whose implementation it was discovered. --- .../static/src/js/editor/snippets.editor.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/snippets.editor.js b/addons/web_editor/static/src/js/editor/snippets.editor.js index 258c6d46041..ccdfc8ddec5 100644 --- a/addons/web_editor/static/src/js/editor/snippets.editor.js +++ b/addons/web_editor/static/src/js/editor/snippets.editor.js @@ -206,17 +206,17 @@ var SnippetEditor = Widget.extend({ } if ($parent.closest(':data("snippet-editor")').length) { - while (!$parent.data('snippet-editor')) { + var editor = $parent.data('snippet-editor'); + while (!editor) { var $nextParent = $parent.parent(); - if ($parent.children().length === 0 && $parent.text().trim() === '' && !$parent.hasClass('oe_structure')) { + if (isEmptyAndRemovable($parent)) { $parent.remove(); } $parent = $nextParent; + editor = $parent.data('snippet-editor'); } - if ($parent.children().length === 0 && $parent.text().trim() === '' && !$parent.hasClass('oe_structure')) { - _.defer(function () { - $parent.data('snippet-editor').removeSnippet(); - }); + if (isEmptyAndRemovable($parent, editor)) { + setTimeout(() => editor.removeSnippet()); } } @@ -227,6 +227,12 @@ var SnippetEditor = Widget.extend({ this.trigger_up('snippet_removed'); this.destroy(); $parent.trigger('content_changed'); + + function isEmptyAndRemovable($el, editor) { + editor = editor || $el.data('snippet-editor'); + return $el.children().length === 0 && $el.text().trim() === '' + && !$el.hasClass('oe_structure') && (!editor || editor.isTargetParentEditable); + } }, /** * Displays/Hides the editor overlay and notifies the associated snippet