[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
This commit is contained in:
David Beguin
2023-07-18 19:27:53 +02:00
parent 17ee75a84d
commit 111e20de9b
@@ -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
//------------------------------------------------------------------------------