Tinymce : Don't replicate class when adding a

2019-07-27 07:00发布

问题:

Using a custom button, I add a paragraphe with a class and some content, like so :

<p class="mce-new-class">my custom content</p>

When I press enter after such a paragraph, TinyMCE will automatically add a new paragraph using the exact same class :

<p class="mce-new-class">my custom content</p>
<p class="mce-new-class">&nbsp;</p>

I'd like to only have a new paragraph but without the class :

<p class="mce-new-class">my custom content</p>
<p>&nbsp;</p>

I've tried this :

tinymce.init({
    ...
    setup: function (ed) {
        ed.on('keydown',function(e) {
            if(e.keyCode == 13){
                ed.selection.setContent('<p>&nbsp;</p>'); 
                return false;
            }
        });
    }
});

But this applies to all situations and will brake other usefull situations such as replicating a list element on "enter press"

Any help would be much appreciated

回答1:

Found out a solution :

...
setup: function (ed) {
    ed.on('keydown',function(e) {
        if(e.keyCode == 13){
            if(ed.dom.hasClass(ed.selection.getNode(), 'mce-new-class')){               
                ed.selection.setContent('<p>&nbsp;</p>'); 
                return false;                   
            } else {                
                return true;
            }
        }
    });
},
...