付潭

淡泊高远,方圆之道。 助人扶危,处世之本。 修身养性,长乐之方。 好学勤勉,成就之功。

导航

CKEditor可视化在线编辑器使用教程与CKFinder配置

CKEditor是一款功能非常强大的在线HTML编辑器,后台编辑新闻列表都要用到它。今天做一份有关于将这款插件插入到网站cms后台的方法:

安装:

  下载CKEDITOR的文件,最新的CKEditor 4.0版本下载地址,解压后复制到你网站的根目录下就行,so easy!

引用CKEDITOR的JS文件:

  新建php页面,添加其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.appendTo(elementOrId, config) 它可以在特定的dom对象中创建CKEDITOR

 

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

CKEDITOR.appendTo( 'editorSpace' );
CKFinder配置

CK默认不带上传达图片功能,为是这了安全起见,如果你需要上传图片,需要去下载一个ckfinder ,基本配置方法如下:

1、下载CKFinder。
下载CKFinder,解压跟CKEditor放同一目录,下载地址:http://ckfinder.com/

2、开启上传功能。
打开ckfinder\config.php文件,找到函数CheckAuthentication(),大约在21行,将最后一行return false;改为return true;

3、自定义上传文件目录。
打开ckfinder\config.php文件,设置变量$baseUrl = '/upload/';,大约在63行。

4、设置按日期生成上传子目录。
如果您的网站上传文件比较多,文件都在一个目录下的话,对文件性能可能不是很思想,这时我们按/upload/yyyy/mm/格式生成目录。打开 ckfinder\config.php文件,找到$config['ResourceType'][],大约在189行,在 'url' => $baseUrl . 'files' 和 'directory' => $baseDir . 'files' 都加上  . '/' . date("Y") . '/' . date("m"),其它 Images,Flash项也是如此。这里的 url 表示上传后返回到输入框的地址,directory表示上传文件的目录。

5、按日期+随机字符重命名上传文件。
这样可以支持中文名字的文件上传。打开ckfinder\core\connector\php\php5\CommandHandler\FileUpload.php文件,在文件最后加个生成文件名函数

 代码如下 复制代码

function mySetFileName() {
    $gettime = explode(' ', microtime());
    $string = 'abcdefghijklmnopgrstuvwxyz0123456789';
    $rand = '';

    for ($x = 0; $x < 5; $x++) {
        $rand .= substr($string, mt_rand(0, strlen($string) - 1), 1);
    }

    return date("ymdHis") . substr($gettime[0], 2, 6) . $rand;
}

查找下面代码,大约在63行,

 代码如下 复制代码
if ($sFileName != $sUnsafeFileName) {
    $iErrorNumber = CKFINDER_CONNECTOR_ERROR_UPLOADED_INVALID_NAME_RENAMED;
}


在下方添加下面代码,

 代码如下 复制代码
$sExtension = CKFinder_Connector_Utils_FileSystem::getExtension($sFileName);
$sFileName = mySetFileName() . '.' . $sExtension;

 还有一个问题要注意:ckeditor无法在提交时判断是否为空,是因为ckeditor提交时不能及时抓取到相应的值。ckeditor会在点击提交后才会把隐藏内容设置到ckeditor值中,

 因此,我们要用下面的语句作为判断:

var oEditor = CKEDITOR.instances.文本域的id值;

if(oEditor.getData()==""){alert("请填写您的回答内容!");return false;}

解决办法:

整合ckeditor和ckfinder

   1、新建一个php网页文件,在头部加载ckeditor和ckfinder,如:<script type="text/javascript" src="ckeditor/ckeditor.js"></script>//加载ckeditor,<script type="text/javascript"

src="ckeditor/ckfinder/ckfinder.js"></script>//加载ckfinder。
   2、在php网页文件里新建一个表单,然后再在表单里建一个textarea去,并命名name属性,如:<textarea name="editor1"></textarea>。
   3、在表单之后加入一段js代码,如下:<script type="text/javascript">
  CKEDITOR.replace( 'editor1',
{
filebrowserBrowseUrl : 'ckeditor/ckfinder/ckfinder.html',
filebrowserImageBrowseUrl : 'ckeditor/ckfinder/ckfinder.html?Type=Images',
filebrowserFlashBrowseUrl : 'ckeditor/ckfinder/ckfinder.html?Type=Flash',
filebrowserUploadUrl : 'ckeditor/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
filebrowserImageUploadUrl : 'ckeditor/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
filebrowserFlashUploadUrl : 'ckeditor/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
});
</script>  测试下看能不能上传文件。

   4、关于ckeditor的设置,由于ckeditor的菜单选项繁多,我们日常好多都用不上,所以我对ekeditor菜单进行了一些删减,在ekeditor的文件夹中找到config.js文件,在’CKEDITOR.editorConfig = function( config ){‘之后添加如下代码:

config.height=500;
config.width=700;//config.height和config.width是设置eckeditor的宽和高的,这里可以自行定义。

config.toolbar_Full = [
['Source','-','Save','Preview'],
['Cut','Copy','Paste','PasteText','PasteFromWord',],
['Undo','Redo','-','Find','Replace']
['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Link','Unlink','Anchor'],'/',
['Styles','Format','Font','FontSize'],
['TextColor','BGColor'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak']
];

   这里贴出php中整合ckeditor和ckfinder的php的测试源码

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckeditor/ckfinder/ckfinder.js"></script>
</head>

<body>
<form>
  <textarea name="editor1"></textarea>
</form>
<script type="text/javascript">
  CKEDITOR.replace( 'editor1',
{
filebrowserBrowseUrl : 'ckeditor/ckfinder/ckfinder.html',
filebrowserImageBrowseUrl : 'ckeditor/ckfinder/ckfinder.html?

Type=Images',
filebrowserFlashBrowseUrl : 'ckeditor/ckfinder/ckfinder.html?

Type=Flash',
filebrowserUploadUrl :

'ckeditor/ckfinder/core/connector/php/connector.php?

command=QuickUpload&type=Files',
filebrowserImageUploadUrl :

'ckeditor/ckfinder/core/connector/php/connector.php?

command=QuickUpload&type=Images',
filebrowserFlashUploadUrl :

'ckeditor/ckfinder/core/connector/php/connector.php?

command=QuickUpload&type=Flash'
});
</script>

</body>
</html>

最后分享一个 功能完善,被汉化过的CKEditor版本下载地址>>

 

属性配置:

所有的配置,都可以查阅官方的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 the customConfig 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。

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

Code

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的方式定义工具栏参数。

Code

 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数组。通过样式的名称我可以让页面的多个编辑器应用该样式。

Code

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';

OR

config.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:

Code

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.htmlWriter class ("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:

Code

 

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:

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

Code

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

posted on 2012-12-19 23:40  付潭  阅读(3249)  评论(0编辑  收藏  举报