CKEditor:在粘贴上应用removeFormat

发布于 2024-12-01 21:41:07 字数 718 浏览 1 评论 0 原文

我已经成功地设置了一个粘贴事件来捕获粘贴到文本区域的 HTML。

我需要在将 HTML 粘贴到文本区域之前或之时自动将removeFormat 命令应用于该 HTML,以便我可以去除其中的类、各种标签和其他属性。有人可以指出我正确的方向来正确应用removeFormat命令吗?

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

$(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
                    });              
            }
        )
    });

谢谢!

PS Force 纯文本选项不可行,因为我希望保留一些 HTML 元素(p、table 等)。

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

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

Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光无声 2024-12-08 21:41:07

您可以使用

config.forcePasteAsPlainText = true;

cf http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR .config.html

You can use

config.forcePasteAsPlainText = true;

cf http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html

寻找一个思念的角度 2024-12-08 21:41:07

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

您可以尝试抓取范围(即使只是光标位于插入点)并在粘贴之前保存书签。

粘贴后,使用书签再次选择该范围。

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

然后您可以使用removeFormat:

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

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

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.range.html

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom。 Selection.html

我发现使用范围更容易,createBookmark 方法很好,因为它设置标记,即使 DOM 发生变化,您也可以获取正确的起点和终点(如当您粘贴新内容时就会如此)。您可以在粘贴后使用 moveToBookmark() 来选择范围。

http://docs.cksource.com/ckeditor_api/symbols/ CKEDITOR.dom.range.html#createBookmark

由于文档很少,我发现在源代码中搜索调用方法的位置很有帮助。了解它们的使用方式可以让我更好地了解需要将这些方法应用到哪种类型的对象。

祝你好运,

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.

Then you can use removeFormat:

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

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

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.range.html

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.selection.html

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.

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.range.html#createBookmark

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.

Be Well,
Joe

故乡的云 2024-12-08 21:41:07

从 CKEditor 4.1 开始,无需进行自定义编码来定义将数据粘贴到 CKEditor 时应保留的元素列表,高级内容过滤器应该可以解决问题。

要么使用默认配置启用 ACF - CKEditor 将接受可以用它创建的所有标签,要么使用或多或少严格的允许标签/属性/样式集定义您自己的规则集。请参阅文档

Starting from CKEditor 4.1 there is no need to do custom coding to define the list of elements that should be kept when pasting data into CKEditor, Advanced Content Filter should do the trick.

Either leave ACF enabled with the default configuration - CKEditor will accept all tags that can be created with it, or define your own set of rules with more or less strict set of allowed tags/attributes/styles. See documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文