CKEditor:在paste上应用removeFormat [英] CKEditor: Apply removeFormat on paste

查看:597
本文介绍了CKEditor:在paste上应用removeFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功设置为粘贴事件,以便在粘贴到文本区域时捕获粘贴的HTML。

I have successfully managed to set up an on paste event to capture the HTML pasted into the text area as it is pasted.

我需要自动应用removeFormat命令到该HTML之前或在它被粘贴到文本区域,以便我可以剥离它的类,各种标签和其他属性。

I need to automatically apply the removeFormat command to that HTML before or at the time it is pasted into the text area, so that I can strip it of classes, various tags, and other attributes. Could somebody point me in the right direction to apply the removeFormat command correctly?

这里是我的代码到目前为止:

Here's my code so far:

$(function(){
        $('textarea').ckeditor(
            function( textarea ){
                var editor = this;
                editor.on('paste', function( e ) { 
                    //alert(e.data.html); // This shows the HTML
                    editor.execCommand( 'removeFormat', e.data.html ); // Doesn't seem to do anything, HTML is pasted with the attributes intact
                    });              
            }
        )
    });

谢谢!

强制纯文本选项不可行,因为有一些HTML元素我希望保留(p,表和其他)。

P.S. Force plain text option is not viable as there are some HTML elements I wish to keep (p,table and others).

推荐答案

您需要先选择内容,然后才能将removeFormat应用于其中。

You need to select the content before you can apply removeFormat to it.

您可以尝试抓取该范围(即使它只是光标位于插入点)

You could try grabbing the range ( even if it's just the cursor sitting at the insertion point ) and saving a bookmark before you paste.

粘贴后,使用书签重新选择该范围。

After you paste, use the bookmark to select that range again.

应选择您在范围开始和结束之间粘贴的所有内容。

That should select everything that you pasted between the start and end of the range.

然后您可以使用removeFormat:

Then you can use removeFormat:

editor.execCommand( 'removeFormat', editor.selection );

以下是范围和选择API页面的链接:

Here are the links to the range and selection API pages:

我发现使用范围更容易,createBookmark方法很好,因为它设置标记,并且即使DOM更改也可以抓取正确的开始和结束点(因为它会在您粘贴新内容)。您可以在粘贴后使用moveToBookmark()选择范围。

I've found it easier to work with ranges, the createBookmark method is good because it sets markers and you can grab the correct start and end points even if the DOM changes ( as it will when you paste in the new content ). You can use moveToBookmark() after the paste to select the range.

因为文档很稀疏,我发现在源代码中调用方法的地方很有帮助。看看它们的使用方式让我更好地了解应用这些方法需要什么样的对象。

Because the documentation is sparse, I've found it helpful to search the source code for places where the methods are called. Looking at how they're used gives me a better idea of what kind of object I need to apply the methods to.

好吧,
Joe

Be Well, Joe

这篇关于CKEditor:在paste上应用removeFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆