From 402bf48fb52fa2ae6273cd064d12fa4ef7af3b0f Mon Sep 17 00:00:00 2001 From: can-odoo Date: Thu, 12 Jan 2023 16:16:30 +0530 Subject: [PATCH] [FIX] website: remove ToC snippet on removing last content The user has the option to remove the last block from the Table of Content snippet prior to this commit, but doing so breaks the Add Item feature because there is no reference to adding a new item in the snippet area, and doing so is also not a good idea. If the block is the final one in the Table of Content after this commit, removing the last Content will remove the ToC snippet. task-3081442 closes odoo/odoo#135733 X-original-commit: 164db4013439042725c94f1690ae0a311be9d554 Signed-off-by: Guillaume Dieleman (gdi) --- .../static/src/snippets/s_table_of_content/options.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/addons/website/static/src/snippets/s_table_of_content/options.js b/addons/website/static/src/snippets/s_table_of_content/options.js index 727d7bd2306..b75a41145e7 100644 --- a/addons/website/static/src/snippets/s_table_of_content/options.js +++ b/addons/website/static/src/snippets/s_table_of_content/options.js @@ -78,6 +78,16 @@ options.registry.TableOfContent = options.Class.extend({ * @private */ _generateNav: function (ev) { + const blockTextContent = this.$target[0].textContent.replaceAll('\n', '').trim(); + if (blockTextContent === '') { + // destroy public widget and remove the ToC since there are no more + // child elements, before doing so the observer needs to be + // disconnected else observer observe mutation and _generateNav + // gets called even after there's no more ToC. + this.observer.disconnect(); + this.trigger_up('remove_snippet', {$snippet: this.$target}); + return; + } this.options.wysiwyg && this.options.wysiwyg.odooEditor.unbreakableStepUnactive(); const headingsEls = this.$target.find(this.targetedElements).toArray() .filter(el => !el.closest('.o_snippet_desktop_invisible'));