一些CKEditor定制问题

调整CKEditor的basePath

使用base标签,或者项目文件结构的原因,我们可能需要调整CKEditor的basePath。

basePath: (function() {
                // ATTENTION: fixes to this code must be ported to
                // var basePath in "core/loader.js".

                // Find out the editor directory path, based on its <script> tag.
                var path = window.CKEDITOR_BASEPATH || '';

                if ( !path ) {
                    var scripts = document.getElementsByTagName( 'script' );

                    for ( var i = 0; i < scripts.length; i++ ) {
                        var match = scripts[ i ].src.match( /(^|.*[\\\/])ckeditor(?:_basic)?(?:_source)?.js(?:\?.*)?$/i );

                        if ( match ) {
                            path = match[ 1 ];
                            break;
                        }
                    }
                }

                // In IE (only) the script.src string is the raw value entered in the
                // HTML source. Other browsers return the full resolved URL instead.
                if ( path.indexOf( ':/' ) == -1 ) {
                    // Absolute path.
                    if ( path.indexOf( '/' ) === 0 )
                        path = location.href.match( /^.*?:\/\/[^\/]*/ )[ 0 ] + path;
                    // Relative path.
                    else
                        path = location.href.match( /^[^\?]*\/(?:)/ )[ 0 ] + path;
                }

                if ( !path )
                    throw 'The CKEditor installation path could not be automatically detected. Please set the global variable "CKEDITOR_BASEPATH" before creating editor instances.';

                return path;
            })()

可见,我们可以通过设置window.CKEDITOR_BASEPATH来调整CKEditor的basePath。

自定义CKEditor的语言

CKEditor可以根据浏览器来自动选择语言,但可能我们需要其和UI统一语言,而不是自动选择,这时候我们就需要自定义其语言。

我们需要给Config的language属性定义成相应的语言。

  • 在repalce时传入config
var idSelector = "editor1"; 

CKEDITOR.replace(idSelector, {language: 'zh-cn'}); //设置成简体中文
  • 在editor实例生成时自动设置
CKEDITOR.on('instanceCreated', function(event){ // 当editor实例创建时
  var editor = event.editor;

  editor.on('configLoaded', function(){ // 当实例载入config时
    editor.config.language = "zh-cn"; // 修改成简体中文
  });

};

Advanced Content Filter

CKEditor会自动开启内容过滤器,其会对节点的属性进行过滤,可是我们可能不希望其过滤,可以通过设置config.allowedContent为true,来关闭该功能。

这样CKEditor就仅仅是富文本编辑器了。

原文地址:https://www.cnblogs.com/justany/p/3063934.html