CKEDITOR的使用与配置

2024-02-17 21:08
文章标签 配置 使用 ckeditor

本文主要是介绍CKEDITOR的使用与配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

安装:

  下载CKEDITOR的文件,解压后复制到工程的WEBROOT目录下就OK!

引用CKEDITOR的JS文件:

  新建JSP页面,添加其JS文件<script type="text/javascript" src="ckeditor/ckeditor.js"></script>

  注意:1.src的路径。

     2.不要写成<script type="text/javascript" src="ckeditor/ckeditor.js />样式,在现有的3.0.1版本中会出现CKEDITOR未定义的脚本错误提示,致使不能生成编辑器。

替换TEXTAREA标签:

 

< textarea  rows ="30"  cols ="50"  name ="editor01" > 请输入. </ textarea >
< script  type ="text/javascript" > CKEDITOR.replace( ' editor01 ' ); </ script >

 

注意:要在textarea后面加入javascript.如果要在HEAD区写javasript,那么采用如下代码:

 

< script  type ="text/javascript" >
    window.onload 
=   function ()
    {
        CKEDITOR.replace( 
' editor01 '  );
    };
</ script >

 

好了到此一个默认的CKEDITOR就配置完毕了,可以去页面看看它的模样了。

当然还有一个方法CKEDITOR.appendTo(elementOrId, config) 它可以在特定的dom对象中创建CKEDITOR

 

< div  id ="editorSpace" >< : /div >

CKEDITOR.appendTo( 'editorSpace' );

 

属性配置:

所有的配置,都可以查阅官方的API:http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html

IN-PAGE配置方式:

The best way to set your configurations is in-page, when creating editor instances. In this way you avoid touching the original distribution files in the CKEditor installation folder, making the upgrade task easier.

最好在创建编辑器的页面中配置你的编辑器属性,使用这种方式,你无需理会在CKEDITOR安装目录中的配置文件(ps:in-page的优先级最高).

 

CKEDITOR.replace( 'editor1',
    {
        toolbar : 'Basic',
        uiColor : '#9AB8F3'
    });

注意:合法的属性是以“{”开始,“}”"结束,属性名和属性值用“:”隔离.

 

默认属性文件配置方式:

You can also place your settings inside the config.js file also. You will note that the file is mostly empty by default. You simply need to add the configurations that you want to change. For example:

你也可以在config.js 中加入配置信息,当你打开该文件时你会发觉它几乎为空(默认)。你要做的是把配置信息加入其中。

 

CKEDITOR.editorConfig = function( config )
{
    config.language = 'fr';
    config.uiColor = '#AADC6E';
};

自定义属性文件配置方式:

Suppose you have copied config,js inside a folder named "custom" in the root of your web site. You have also renamed the file to "ckeditor_config.js". At that point, you must only set thecustomConfig setting when creating the editor instances. For example:  

 

CKEDITOR.replace( 'editor1',
    {
        customConfig : '/custom/ckeditor_config.js'
    });

假设你在custom目录中有一自定义的配置文件ckeditor_config.js,那么就必须在创建ckeditor实例时制定它的路径(用customConfig属性)。

 

Configurations Overload Order 配置的优先级

You're not required to use only one of the above configuration options. You can mix all of them, and the configurations will be overloaded properly. The following is configurations loading order when creating an editor instance:

你不必仅使用上面的一种方式进行配置,而是可以混合使用它们,这些配置会以特定的优先级顺序进行复写。

  1. The editor instance is created. At this point all its default configurations are set. 
    编辑器创建的那一瞬间,会加载默认的配置信息。
  2. If the customConfig setting has been set "in-page", that file is loaded, otherwise the default config.js file is loaded. All settings in this file override the current instance settings.
    这时,如果系统发现我们制定了一个自定义的配置文件,那么就会加载它,否则就会加载默认的配置文件。加载的该文件的属性将会复写当前实例的属性。
  3. If the settings loaded in point "2" also define a new customConfig value, this new file is loaded and its settings overload the current instance settings. This happens recursively for all files until no customConfig is defined. 
    如果在自定义的文件中有加入新的其它自定义文件,那么新的文件会复写当前实例的属性。这样一直循环递归,知道没有新为自定义文件为止。
  4. Finally the settings defined "in-page" override the current instance settings (except customConfig, which has been used at point "1"). 
    最后行内的属性(除了customConfig)将复习当前实例的属性。

 

Avoiding Loading External Settings Files

It is also possible to completely avoid loading an external configuration file, reducing the number of files loaded. To do that, it's enough to set the customConfig setting to an empty string. For example:

 
CKEDITOR.replace( 'editor1',{customConfig : ''});

This setting is definitely recommended if you are not setting configurations in the config.js file nor a custom configuration file.

这段就不翻译了,so,easy!

 

配置个性化工具栏:

 

A toolbar definition is a JavaScript array that contains the elements to be displayed in all "toolbar rows" available in the editor. There are two ways to set the desired toolbar definition in the editor. It can be set directly into the "toolbar" setting, or it can instead be set to a configuration named "toolbar_<name>", where "<name>" is a name that can be used to identify the toolbar in the "toolbar" setting. The following is the default setting we have in the editor.

上面的英文太罗嗦了说重点算了:工具栏是通过数组来设定的,工具栏的名字以toolbar_<name>的方式命名,其中的<name>是用来赋值给config.toolbar这一变量的。

那么以下代码定义了toolbar_Full和toolbar_Basic的两种工具栏配置,并使用了config.toolbar = 'Full';定义当前的编辑器的工具栏为Full。

其中("-") 为空间栏的水平分割,("/") 为换行。


config.toolbar = 'Full';

config.toolbar_Full =
[
    ['Source','-','Save','NewPage','Preview','-','Templates'],
    ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
    ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
    ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
    '/',
    ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
    ['Link','Unlink','Anchor'],
    ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
    '/',
    ['Styles','Format','Font','FontSize'],
    ['TextColor','BGColor'],
    ['Maximize', 'ShowBlocks','-','About']
];

config.toolbar_Basic =
[
    ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']
];

Note that two toolbar definitions have been defined, one named "Full" and the other "Basic". The "Full" definition has been set to be used in the toolbar setting.

 

You can create as many toolbar definitions as you want inside the configuration file. Later, based on some criteria, you can decide the toolbar to use for each editor instance. For example, with the following code, two editors are created in the page, each one using a different toolbar:

你也可以根据您的需要在配置文件中建立多个工具栏的定义,然后,可以根据特定条件,决定使用哪个定义。

 
复制代码
CKEDITOR.replace( 'editor1',{toolbar : 'MyToolbar'});CKEDITOR.replace( 'editor2',{toolbar : 'Basic'});
复制代码

It's also possible to set the toolbar definition in-page, when creating the editor instance directly. In that case, just assign it to the toolbar setting directly, for example:

也可以通过IN-PAGE的方式定义工具栏参数。

 
CKEDITOR.replace( 'editor1',{toolbar :[['Styles', 'Format'],['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', '-', 'About']]});

 The Styles Definition

The styles definition is a JavaScript array which is registered by calling the CKEDITOR.addStylesSet() function. A unique name must be assigned to your style definition, so you can later set each editor instance to load it. It means that you can have a single style definition which is shared by several editor instances present on the page.

The following is a sample style definition registration:

通过CKEDITOR.addStylesSet() 函数,我们可以定义一个样式,结合以下的例子,my_styles为样式的名称,具体的样式为一个JAVASCRIPT数组。通过样式的名称我可以让页面的多个编辑器应用该样式。

 
CKEDITOR.addStylesSet( 'my_styles', [// Block Styles{ name : 'Blue Title', element : 'h2', styles : { 'color' : 'Blue' } },{ name : 'Red Title' , element : 'h3', styles : { 'color' : 'Red' } },// Inline Styles{ name : 'CSS Style', element : 'span', attributes : { 'class' : 'my_style' } },{ name : 'Marker: Yellow', element : 'span', styles : { 'background-color' : 'Yellow' } } ]);

The above definition registration can be placed inline in the page using the editor, or can even live in an external file, which is loaded "on demand", when needed only (see below).

样式可以通过IN-PAGE或配置文件中进行注册。

After that, you must instruct the editor to use your newly registered style definition by using the stylesCombo_stylesSet setting. This may be set into the config.js file, for example:

你可以通过在config.js 加入以下代码使编辑器应用该样式:

 
config.stylesCombo_stylesSet = 'my_styles';

 

Using an External Styles Definition File 使用自定义的样式文件

You can include the above styles definition registration call into an external JavaScript file. This is the preferred way for it because it will be loaded only when opening the Styles selection box, enhancing the page loading performance. Users may feel a small loading gap because of it though.

By default, the editor uses the "plugins/stylescombo/styles/default.js" file, which is a "minified" JavaScript file. You can find the uncompressed version of it at "_source/plugins/stylescombo/styles/default.js". You can see it online at our SVN also:http://svn.fckeditor.net/CKEditor/trunk/_source/plugins/stylescombo/styles/default.js. It can be used as a template for your custom file.

Your style definition file can be saved anywhere at your web site (or the web). You must just know the URL to reach it. For example, you can save at it at the root of your web site, so you can reach it with "/styles.js", or even place it in a central web site, so you can locate it with "http://www.example.com/styles.js". At that point, simply change the stylesCombo_stylesSet setting to point the editor to your file:

默认的样式文件在"_source/plugins/stylescombo/styles/default.js"目录中,我们也可以自己定义一个样式文件,如在站点的跟目录中有styles.js这一个样式文件,这个文件通过以下代码指定其路径:

 
config.stylesCombo_stylesSet = 'my_styles:/styles.js';ORconfig.stylesCombo_stylesSet = 'my_styles:http://www.example.com/styles.js';

Style Rules

The entries inside a style definition are called "style rules". Each rule defines the display name for a single style as well as the element, attributes and css styles to be used for it. The following is the generic representation for it:

 
{name : 'Display name',element : 'tag name (for example "span")',styles :{'css-style1' : 'desired value','css-style2' : 'desired value',}attributes :{'attribute-name1' : 'desired value','attribute-name2' : 'desired value',} }

The "name" and "element" values are required, while other values are optional.

 

Style Types

There are three types of style types, each one related to the element used in the style rule:

  • Block styles: applied to the text blocks (paragraphs) as a whole, not limited to the text selection. The elements values for that are: address, div, h1, h2, h3, h4, h5, h6, p and pre.
  • Object styles: applied to special selectable objects (not textual), whenever such selection is supported by the browser. The elements values for that are: a, embed, hr, img, li, object, ol, table, td, tr and ul.
  • Inline styles: applied to text selections for style rules using elements not defined in the other style types.

Output Formatting

 The HTML Writer

这个是用来对各种标签的排版进行设定的

Technically speaking, writing the final output is a task executed by the CKEDITOR.htmlWriterclass ("writer"), used by the CKEDITOR.htmlDataProcessor class. Therefore, the current writer instance for a specific editor instance can be retrieved by the <editorInstance>.dataProcessor.writer property.

By setting the writer properties, it's possible to configure several output formatting options. The following example is the best way to summarize the most used of them, with their default values:

 
var writer = editor.dataProcessor.write;// The character sequence to use for every indentation step. writer.indentationChars = '\t';// The way to close self closing tags, like <br />. writer.selfClosingEnd = ' />';// The character sequence to be used for line breaks. writer.lineBreakChars = '\n';// Set writing rules for the <p> tag. writer.setRules( 'p',{// Indicates that this tag causes indentation on line breaks inside of it.indent : true,// Insert a line break before the <p> tag.breakBeforeOpen : true,// Insert a line break after the <p> tag.breakAfterOpen : true,// Insert a line break before the </p> closing tag.breakBeforeClose : false,// Insert a line break after the </p> closing tag.breakAfterClose : true});

 

Setting Writer Rules

Because the writer is a property of each editor instance, and also because it's dependency on the writer plugin to be loaded, the best way to make changes to it is by listening to the "instanceReady" event, so it's safe to assume that the dataProcessor property will be loaded and ready to changes. The following is an example of it, when creating an editor instance:

可以为单一得编辑器指定标签的格式。

 
CKEDITOR.replace( 'editor1',{on :{instanceReady : function( ev ){// Output paragraphs as <p>Text</p>.this.dataProcessor.writer.setRules( 'p',{indent : false,breakBeforeOpen : true,breakAfterOpen : false,breakBeforeClose : false,breakAfterClose : true});}}});

Another way for it is by using the CKEDITOR object, so all editor instances will be changed:

也可以对所有的编辑器进行设定。

 
CKEDITOR.on( 'instanceReady', function( ev ){// Out self closing tags the HTML4 way, like <br>.ev.editor.dataProcessor.writer.selfClosingEnd = '>';});

 

这篇关于CKEDITOR的使用与配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/718987

相关文章

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa