CKEDITOR - <p> instead of <br/> causing copy-paste issues

橙三吉。 提交于 2021-02-11 18:08:13

问题


Some time ago I changed the CKEDITOR to not use <br> tag and use <p> instead. This makes things alot easier for me.

But today I spotted a problem here... When I paste into ckeditor this text:

Text

More text

CKEDITOR makes this: <p>Text</p><p>More text</p>. How can I configure ckeditor so that it will put put only single <p> tag over whole text, and inside it will put <br/>'s?


回答1:


There's no configuration option for pasting itself. You can, however, change behaviour of entire CKEditor if you set config.enterMode to CKEDITOR.ENTER_BR. Then CKEditor will not use paragraphs at all. On the other hand, it's not recommended to use other enter modes, because the default (CKEDITOR.ENTER_P) is the most correct, semantic and best supported.

Although, if you must change the paste behaviour, there's one more way. You can listen to editor#paste event and transform content in your preferred way. Very rough implementation would look like this:

editor.on( 'paste', function( evt ) {
    var data = evt.data.dataValue;
    data = data
        .replace( /^<p>/, '' )
        .replace( /<\/p>$/, '' )
        .replace( /<\/p><p>/g, '<br />' );
    evt.data.dataValue = data;
} );


来源:https://stackoverflow.com/questions/20436417/ckeditor-p-instead-of-br-causing-copy-paste-issues

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