Tinymce : Don't replicate class when adding a new paragraph on enter

冷暖自知 提交于 2019-12-11 04:35: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;
            }
        }
    });
},
...


来源:https://stackoverflow.com/questions/45392375/tinymce-dont-replicate-class-when-adding-a-new-paragraph-on-enter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!