Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative/substitute for queryCommandState('bold')

I created a rich content editor based on execCommand and queryCommandState whose are now obsolete. I'm looking for a substitute to these commands, especially for

document.queryCommandState('bold')

I think the following is a good start:

window.getSelection().getRangeAt(0);

I get the current selection, but I can't figure out if the selection is in bold <b> or not.

like image 398
Fifi Avatar asked Mar 23 '20 09:03

Fifi


2 Answers

It can be done with window.getSelection()

JSFiddle: https://jsfiddle.net/Imabot/s54zoxk2/

Explanations: https://lucidar.me/en/rich-content-editor/lightweight-rich-content-editor-part-2-check-if-bold/

Hope this will help others...

like image 77
Fifi Avatar answered Sep 18 '22 12:09

Fifi


You could get all computed styles of the node and then check the value of your propertie.

function getComputedStyles(currentNode) {
    if(currentNode.id != maxTreeNodeId) {
        try {
            var styles = window.getComputedStyle(currentNode);
            console.log(styles.fontWeight); // Print font weight, 700 = bold
        } catch(err) {
            this.getComputedStyles(currentNode.parentNode);
        }
    }
}
like image 21
Leandro Pozas Avatar answered Sep 20 '22 12:09

Leandro Pozas