Calling async invokeOperation on sync isEnable
Posted: Thu Feb 16, 2023 11:16 pm
Good afternoon!
We are developing a button that inserts the table element inside a custom xml.
We want this button to be active only when the table element is allowed by the schema in that context where the cursor is located.
We use Abstraction isEnable() method calling invokeOperation() "GetElementsAtCaretOperation", however, this method is asynchronous and isEnable() is synchronous.
When the method returns the result, the button is not enabled/disabled.
We considered using a workaround by updating the DOM directly, however, we would like to know if there is a better way to solve this problem.
Follow the code for better understanding.
We are developing a button that inserts the table element inside a custom xml.
We want this button to be active only when the table element is allowed by the schema in that context where the cursor is located.
We use Abstraction isEnable() method calling invokeOperation() "GetElementsAtCaretOperation", however, this method is asynchronous and isEnable() is synchronous.
When the method returns the result, the button is not enabled/disabled.
We considered using a workaround by updating the DOM directly, however, we would like to know if there is a better way to solve this problem.
Follow the code for better understanding.
Code: Select all
// The action is enabled only if there is some content selected.
isEnabled() {
var elementCaret = new elementAtCaret(this.editor);
elementCaret.isElementValidAtCaretPosition('table')
.then( result => {
this.enableActionByDom(result);
});
return false;
}
enableActionByDom(enabled){
if(enabled){
console.log('ENABLE button using DOM TO DO...');
}else{
console.log('DISABLE button using DOM TO DO...');
}
}
//*********************************
async isElementValidAtCaretPosition(element){
var elementsAtCaret = await this.getElementsAtCaretOperation();
var isValid = (elementsAtCaret).split(",").includes(element);
return(isValid);
}
async getElementsAtCaretOperation(){
var elementsAtCaret = '';
await this.editor.getActionsManager().invokeOperation('com.oxygen.element.GetElementsAtCaretOperation', {})
.then((str) => {
elementsAtCaret = str;
console.log('getElementsAtCaretOperation:', elementsAtCaret);
});
return elementsAtCaret;
}