代码改变世界

一些CKEditor定制问题

  Justany_WhiteSnow  阅读(6819)  评论(0编辑  收藏  举报

 

 

调整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就仅仅是富文本编辑器了。

 

 

 

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示