简体   繁体   English

Tinymce:在回车上添加新段落时不要复制类

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

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 : 在此类段落之后按Enter键时,TinyMCE将使用完全相同的类自动添加新段落:

<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" 但这适用于所有情况,并且会阻止其他有用的情况,例如在“ enter press”上复制列表元素

Any help would be much appreciated 任何帮助将非常感激

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;
            }
        }
    });
},
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM