From 111e20de9ba8a0c6b04c40b93f35864b5591eaae Mon Sep 17 00:00:00 2001 From: David Beguin Date: Thu, 29 Jun 2023 08:10:03 +0000 Subject: [PATCH] [IMP] web_editor: add isSelectionInSelectors utility method This commit add a new utility method to check if the selection (current cursor position) starts inside one of the given selectors. This method will be used, notably, for filtering some commands inside some HTML Elements of the web editor. Some commands should not be available within certain elements like table, custom behaviours (knowledge app) or banners (that will be introduced in the next commit). Task-2782426 Part-of: odoo/odoo#120690 --- .../js/editor/odoo-editor/src/utils/utils.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js index 00b6b4f6679..78799ada199 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js @@ -871,6 +871,25 @@ export function preserveCursor(document) { }; } +/** + * Check if the selection starts inside given selector. This function can be + * used as the `isDisabled` property of a command of the PowerBox to disable + * a command in the given selectors. + * @param {string}: comma separated string with all the desired selectors + * @returns {boolean} true selector is within one of the selector + * (if the command should be filtered) + */ +export function isSelectionInSelectors(selector) { + let anchor = document.getSelection().anchorNode; + if (anchor && anchor.nodeType && anchor.nodeType !== Node.ELEMENT_NODE) { + anchor = anchor.parentElement; + } + if (anchor && closestElement(anchor, selector)) { + return true; + } + return false; +} + //------------------------------------------------------------------------------ // Format utils //------------------------------------------------------------------------------