javascript - 在 CKEditor 3 中对所有粘贴的内容使用 "pastefromword"过滤

标签 javascript ckeditor paste

CKEditor 是一个很棒的编辑器,pastefromword 插件也非常好。我希望将插件提供的过滤应用于所有粘贴的文本。例如,从 word 粘贴时,所有字体和大小都会被剥离。从电子邮件粘贴时不会发生这种情况。

也就是说,我提出了以下解决方案并将其发布在此处以获得一些反馈。我想知道我是不是把它弄得太复杂了,或者是否有更简单的方法。我只是从 pastefromword/plugin.js 复制了代码。

通过我的自定义 config.js

...
CKEDITOR.config.pasteFromWordCleanupFile = '/pastefromword.js';
...
CKEDITOR.on( 'instanceReady', function( ev ) {
    /**
     * Paste event to apply Paste From Word filtering on all text.
     *
     * The pastefromword plugin will only process text that has tell-tale signs
     * it is from Word. Use this hook to treat all pasted text as if
     * it is coming from Word.
     *
     * This method is a slightly modified version of code found in
     * plugins/pastefromword/plugin.js
     */
    ev.editor.on( 'paste', function( evt ) {    
        var data = evt.data,
            editor = evt.editor,
            content;

        /**
         * "pasteFromWordHappened" is a custom property set in custom
         * pastefromword.js, so that filtering does not happen twice for content
         * actually coming from Word. It's a dirty hack I know.
         */
        if( editor.pasteFromWordHappened ) {
            // Reset property and exit paste event
            editor.pasteFromWordHappened = 0;
            return;
        }

        var loadRules = function( callback ) {
            var isLoaded = CKEDITOR.cleanWord;

            if( isLoaded ) {
                callback();
            }
            else {
                CKEDITOR.scriptLoader.load( CKEDITOR.config.pasteFromWordCleanupFile, callback, null, false, true );
            }

            return !isLoaded;
        };

        content = data['html'];

        // No need to filter text if html tags are not presence, so perform a regex
        // to test for html tags.
        if( content && (/<[^<]+?>/).test(content) ) {
            var isLazyLoad = loadRules( function(){
                if( isLazyLoad ) {
                    editor.fire('paste', data);
                }
                else {
                    data[ 'html' ] = CKEDITOR.cleanWord( content, editor );
                    // Reset property or if user tries to paste again, it won't work
                    editor.pasteFromWordHappened = 0;
                }
            });

            isLazyLoad && evt.cancel();
        }

    });
});

最佳答案

我的解决方案现在在此博客条目中:http://www.keyvan.net/2012/11/clean-up-html-on-paste-in-ckeditor/

关于javascript - 在 CKEditor 3 中对所有粘贴的内容使用 "pastefromword"过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5227140/

相关文章:

javascript - Jquery 选择器做 text() 但没有类或 id?

javascript - 使用 jQuery 进行 RSS 时多个帖子不可见

angularjs - 使用 AngularJS 实现 CKEditor(pasteState 事件)

ruby-on-rails-3 - 有没有办法将 html/css 选项传递给rails_admin 输入?

Android - 为粘贴文本中的每个字符调用一次 TextWatcher

linux - 以 CSV 格式打印 JSON 中的所有键和值

Vim - 用可视模式交换列

javascript - 在 asp.net 中插入文本框的编程指南

javascript - 使用 lodash 对 Json/XML 集合进行分组

CKEditor工具栏关闭按钮右对齐