分享

FCKeditor使用方法技术详解

 邵飞翔 2016-03-27

FCKeditor使用方法技术详解

1、概述

FCKeditor是目前最优秀的可见即可得网页编辑器之一,它采用JavaScript编写。具备功能强大、配置容易、跨浏览器、支持多种编程语言、开源等特点。它非常流行,互联网上很容易找到相关技术文档,国内许多WEB项目和大型网站均采用了FCKeditor(如百度,阿里巴巴)。本文将通过与PHP相结合,从基本安装到高级的配置循序渐进介绍给广大PHPer

FCKeditor官方网站:http://www./

FCKeditor Wikihttp://wiki./

2、下载FCKeditor

登录FCKeditor官方站(http://www.),点击网站右上角“Download”链接。

笔者编写本文时,FCKeditor当前最新的稳定版本是2.6.6,因此我们下载此版本的zip压缩格式文档。

       FCKeditor 2.6.6版下载地址:

http://nchc.dl./project/fckeditor/FCKeditor/2.6.6/FCKeditor_2.6.6.zip

 

 

 

3、安装FCKeditor

       解压“FCKeditor_2.6.6.zip”文档到您的网站目录下,我们先假定您存放FCKeditor和调用脚本存于同一个目录下。fckeditor目录包含FCKeditor2.4.3程序文件。check.php用于处理表单数据。add_article.phpadd_article_js.html分别是PHP调用FCKeditorJavaScript调用FCKeditor实例脚本文件。

3.1、用PHP调用FCKeditor

       调用FCKeditor必须先载入FCKeditor类文件。具体代码如下。

<?php

include("fckeditor/fckeditor.php") ; // 用于载入FCKeditor类文件

?>

接下来,我们需要创建FCKeditor实例、指定FCKeditor存放路径和创建(显示)编辑器等。具体代码如下所示(代码一般放在表单内)。

<?php

$oFCKeditor = new FCKeditor('FCKeditor1') ;  // 创建FCKeditor实例

$oFCKeditor->BasePath = './fckeditor/';        // 设置FCKeditor目录地址

$FCKeditor->Width='100%';                //设置显示宽度

$FCKeditor->Height='300px';               //设置显示高度的高度

$oFCKeditor->Create() ;                    // 创建编辑器

?>

下面是笔者创建好的实例代码,您可将代码保存为add_article.php

<?php

include("fckeditor/fckeditor.php") ; // 用于载入FCKeditor类文件

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>PHP调用FCKeditor</title>

</head>

<body>

<form action="check.php" method="post" name="exapmle">

<?php

$oFCKeditor = new FCKeditor('FCKeditor1') ;  // 创建FCKeditor实例,可创建多个实例

$oFCKeditor->BasePath = './fckeditor/';      // 设置FCKeditor目录地址

$oFCKeditor->Create() ;                      // 创建编辑器

?>

<input name="ok" type="submit" value="提交" >

</form>

</body>

</html>

通过浏览里打开http://you-address/add_article.php 查看FCKeditor安装效果。如图3所示。

 

3FCKeditor安装成功

 

注意:如果您想将FCKeditor创建为HTML结果代码,以便于在模板引擎里面调用(如Smarty)可使用如下代码。

$output = $oFCKeditor->CreateHtml() ;

 

现在,您可通过POST方式获得编辑器的变量值。本例将表单的action设置为check.php,您可在check.php里使用代码

$fckeditorValue = $_POST['FCKeditor1'];

获得编辑器的变量值了。

 

    FCKeditor安装成功了。但是,我们还可以通过更多设置来使FCKeditor更加灵活人性化。具体方法文本后面介绍。

3.2、用JavaScript调用FCKeditor

       调用FCKeditor必须先载入FCKeditor类文件,但与PHP调用方法不同,应用下面的代码。

<script type="text/javascript" src="./fckeditor/fckeditor.js"></script> <!--载入fckeditor-->

载入FCKeditor类成功后,有三种方法创建(显示)编辑器。

一:内嵌方法(推荐)

在您想要显示FCKeditor的地方创建如下代码(通常在表单里):

<script type="text/javascript">

  var oFCKeditor = new FCKeditor('FCKeditor1');

  oFCKeditor.BasePath = "./fckeditor /";

  oFCKeditor.Create();

</script>

下面是笔者创建好的实例代码,您可将代码保存为add_article_js.html

<html>

<head>

<script type="text/javascript" src="./fckeditor/fckeditor.js"></script> <!--载入fckeditor-->

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>JavaScript调用FCKeditor</title>

</head>

<body>

<form action="check.php" method="post" name="example">

<script type="text/javascript">

  var oFCKeditor = new FCKeditor('FCKeditor1');

  oFCKeditor.BasePath = "./fckeditor/";

  oFCKeditor.Create();

</script>

<input name="ok" type="submit" value="提交" >

</form>

</body>

</html>

通过浏览里打开http://you-address/add_article_js.html 查看FCKeditor安装效果。效果和图3完全一样。

       同样,如果您可以使用和前面一样的方法取得编辑器的POST变量值。

$fckeditorValue = $_POST['FCKeditor1'];

二:文本区域(TEXTAREA)方法

同内嵌方法一样,也必须先载入fckeditor类。但创建(显示)编辑器同内嵌方法不同,我们需要为window.onload定义一个函数。这样,函数便可以在页面加载时执行了。函数的定义代码如下所示。

<script type="text/javascript">

   window.onload = function()

   {

      var oFCKeditor = new FCKeditor( 'MyTextarea' ) ;

      oFCKeditor.BasePath = "./FCKeditor/" ;

      oFCKeditor.ReplaceTextarea() ;

}

</script>

接着,您就可以在页面中(通常在表单里)定义idMyTextarea的文本区域(TEXTAREA)。代码如下所示:

<textarea id ="MyTextarea" name="MyTextarea" ></textarea>

下面是笔者创建好的实例代码,显示效果当然也是一样的。笔者这里就不哆嗦了。

<html>

<head>

<script type="text/javascript" src="./fckeditor/fckeditor.js"></script> <!--载入fckeditor-->

<script type="text/javascript">

      window.onload = function()

      {

        var oFCKeditor = new FCKeditor( 'MyTextarea' ) ;

        oFCKeditor.BasePath = "./fckeditor/" ;

        oFCKeditor.ReplaceTextarea() ;

      }

</script>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>JavaScript调用FCKeditor</title>

</head>

<body>

<form action="check.php" method="post" name="example">

<textarea id ="MyTextarea" name="MyTextarea" ></textarea>

<input name="ok" type="submit" value="提交">

</form>

</body>

</html>

三:适合于Ajax的调用方法

同理,您同样需要加载类文件。然后使用下面的代码对div元素创建(显示)编辑器。

var div = document.getElementById("myFCKeditor"); //使用getElementById方法取得myFCKeditor ID元素

var fck = new FCKeditor("myFCKeditor"); //创建fckeditor实例

div.innerHTML = fck.CreateHtml();//使用innerHTML方法,在myFCKeditor div元素里创建编辑器

 

    和使用PHP调用fckeditor实例一样,用javascript方法调用fckeditor实例也可以设置编辑器宽度和高度等。
oFCKeditor.Height = 400 ;    // 400 像素
oFCKeditor.Height = "250" ;  // 250 像素
oFCKeditor.Width  = "100%" ; // 百分比

4、FCKeditor常用设置

FCKeditor已经安装成功了,也可以使用了。但是我们可以通过一些简单的设置使FCKeditor更加符合您的项目需求。

设置工具栏很简单,只需打开fckeditor目录下面的fckconfig.js文件,按CTRL+F搜索FCKConfig.ToolbarSets["Default"]代码,找到如下代码。

       FCKConfig.ToolbarSets["Default"] = [

       ['Source','DocProps','-','Save','NewPage','Preview','-','Templates'],

       ['Cut','Copy','Paste','PasteText','PasteWord','-','Print','SpellCheck'],

       ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],

       ['Form','Checkbox','Radio','TextField','Textarea','Select','Button','ImageButton','HiddenField'],

       '/',

       ['Bold','Italic','Underline','StrikeThrough','-','Subscript','Superscript'],

       ['OrderedList','UnorderedList','-','Outdent','Indent'],

       ['JustifyLeft','JustifyCenter','JustifyRight','JustifyFull'],

       ['Link','Unlink','Anchor'],

       ['Image','Flash','Table','Rule','Smiley','SpecialChar','PageBreak'],

       '/',

       ['Style','FontFormat','FontName','FontSize'],

       ['TextColor','BGColor'],

       ['FitWindow','-','About']

]

在默认情况下,FCKeditor会调用上面定义的所有工具栏按钮。大家可以根据自己的需求进行设置。表1对上面的配置选项功能说明进行汇总。

代码名称

功能

代码名称

功能

Source

源代码

DocProps

页面属性

-

|分隔符

Save

保存

NewPage

新建

Preview

预览

Templates

模板

Cut

剪切

Copy

复制

Paste

粘贴

PasteText

粘贴为无格式文本

PasteWord

MS Word粘贴

Print

打印

SpellCheck

拼写检查

Undo

撤消

Redo

重做

Find

查找

Replace

替换

SelectAll

全选

RemoveFormat

清除格式

Form

表单

Checkbox

复选框

Radio

单选框

TextField

单行文本

Textarea

多行文本

Select

列表菜单

Button

按钮

ImageButton

图像域

HiddenField

隐藏域

Bold

加粗

Italic

倾斜

Underline

下划线

StrikeThrough

删除线

Subscript

下标

Superscript

上标

OrderedList

插入/删除编号列表

UnorderedList

插入/删除项目列表

Outdent

减少缩进

Indent

增加缩进

JustifyLeft

左对齐

JustifyCenter

居中对齐

JustifyRight

右对齐

JustifyFull

两端对齐

Link

插入/编辑链接

Unlink

取消链接

Anchor

插入/编辑锚点链接

Image

插入编辑图像

Flash

插入/编辑Flash

Table

插入/编辑表格

Rule

插入水平线

Smiley

插入表情

SpecialChar

插入特殊符号

PageBreak

插入分页

Style

样式

FontFormat

格式

FontName

字体

FontSize

大小

TextColor

文本颜色

BGColor

背景颜色

FitWindow

全屏编辑

About

关于Fuckeditor

 

 

1:工具栏配置选项功能进行汇总

 

你也可以创建一个非默认的工具栏按钮设置,您可以从FCKConfig.ToolbarSets["Default"]当中的代码重新复制一份,然后将Default改成您想要的名字。

 

注意:fckconfig.js配置选项采用JavaScript语法,如果您不懂JavaScript的话,请在配置之前进行备份。

 

笔者这里配置了一个适合于大部份网站使用的工栏目按钮(取消了一些不常用的工具栏按钮,并重新布局)。

FCKConfig.ToolbarSets["MyDesign"] = [

       ['Cut','Copy','Paste','PasteText','PasteWord','-','Undo','Redo','-','Find','Replace','-','RemoveFormat'],

       ['Link','Unlink','-','Image','Flash','Table'],

       ['FitWindow','-','Source'],

       '/',

       ['FontFormat','FontSize'],

       ['Bold','Italic','Underline'],

       ['OrderedList','UnorderedList','-','Outdent','Indent'],

       ['JustifyLeft','JustifyCenter','JustifyRight'],

       ['TextColor']

] ;

要想使用自定义的工具栏按钮,必须在创建FCKeditor实例后设置使用的工具栏选项。

$oFCKeditor->ToolbarSet = ' MyDesign ' ;    //PHP

oFCKeditor.ToolbarSet = "MyDesign";       //JavaScript

 

接下来,我们对常用的一些设置选项功能进行总结,读者可参考fckeditor目录下fckconfig.js文件进行阅读。见表2

 

FCKConfig.AutoDetectLanguage

自动语言检查

FCKConfig.DefaultLanguage       

默认语言设计,建议改成zh-cn

FCKConfig.ContextMenu

右键菜单内容

FCKConfig.ToolbarStartExpanded

当页面载入的时候,工具栏默认情况下是否展开

FCKConfig.FontColors

文字颜色列表

FCKConfig.FontNames 

字体列表,可加入国内常用的字体,如宋体、揩体、黑体等

FCKConfig.FontSizes

字号列表

FCKConfig.FontFormats

文字格式列表

FCKConfig.StylesXmlPath

指定风格XML文件路径

FCKConfig.TemplatesXmlPath

指定模板XML文件路径

FCKConfig.BodyId

设置编辑器的id

FCKConfig.BodyClass

设置编辑器的class

FCKConfig.DefaultLinkTarget

设置链接默认情况下的target属性

FCKConfig.BaseHref

相对链接的基地址

FCKConfig.SkinPath

设置默认皮肤路径

FCKConfig.SmileyPath

表情文件路径,您可以设置此项更改表情

FCKConfig.SmileyImage

表情文件

FCKConfig.SmileyColumns

将表情分成几列显示

FCKConfig.SmileyWindowWidth

显示表情窗口的宽度,单位像素

FCKConfig.SmileyWindowHeight

显示表情窗口的高度,单位像素

2:常用设置选项功能汇总

 

2是笔者认为最重要的几个常选项,如果读者还需要更多选项的详细信息,可访问http://warran./archives/2006/3467.shtml网址获得。

5、配置上传\文件浏览功能

5.1、配置上传 \浏览功能

要使您的FCKeditor2.6.6 能够使用上传功能,您必须进行以下配制。
 
注意:FCKeditor2.6.6 不支持虚拟目录,您的路径设置都是针对网站根目录的绝对路径而言的。这点对于发布到远程网站目录的开发者极为不便,后面我们会对此进行讨论。
 
一、打开fckeditor\editor\filemanager\connectors\php\config.php,找到代码$Config['Enabled'],将值设置为true
 
二、接下来几行,设置$Config['UserFilesPath'],设置上传路径。
 
三、打开fckeditor\fckconfig.js文件,找到代码_FileBrowserLanguage,将值设置为php。接下来一行,把_QuickUploadLanguage值也设置为php
 

5.2 、关于上传\文件浏览安全性问题

为了解决FCKeditor不支持虚拟目录问题,和FCKeditor文件上传的安全性考良。我们有必要在这里单论对此进行讨论。

     打开fckeditor\editor\filemanager\upload\php\config.php,找到$Config['UserFilesPath']代码,在此行代码之前定义变量$root_path = $_SERVER['PHP_SELF'];
     重新设置$Config['UserFilesPath']变量的值,示例如下。
$Config['UserFilesPath'] = $root_path . '您想上传的目录名/' ;
     
     打开fckeditor\editor\filemanager\browser\default\connectors\php\config.php,找到代码$Config['UserFilesPath'],在此行代码之前定义变量$root_path = $_SERVER['PHP_SELF'];
     重新设置$Config['UserFilesPath']变量的值,示例如下。
$Config['UserFilesPath'] = $root_path . '您想浏览的目录名/'
     至此,您的FCKeditor已解决不支持虚拟目录问题。接下来,我们介绍一种技巧配置只允许管理员才可以使用FCKeditor上传问题。
     解决方法其实很简单,假如网站采用$_SESSION['admin_id']验证管理员的登录id,您只需将相关的脚本文件引入即可。然后使用下面的代码配置文件上传\浏览开关。
$Config['Enabled'] = isset($_SESSION['admin_id']);
 
 

5.3 、上传文件自动生成随机文件名

Fckeditor默认使用上传的文件名作为放在服务器上的文件名,但很多时候我们需要它自动生成随机文件。下面介绍实现方法

打开fckeditor\editor\filemanager\connectors\php\Io.php,这个文件里有一个函数名叫

function SanitizeFileName( $sNewFileName ),这个函数原来的功能是清理掉文件名的非法字符,以阻止一些可能发生的问题。现在我们可以注释掉原来的代码,再加上我们返回生成随机文件名的代码。代码如下:

// Do a cleanup of the file name to avoid possible problems

function SanitizeFileName( $sNewFileName )

{

//     global $Config ;

//

//     $sNewFileName = stripslashes( $sNewFileName ) ;

//

//     // Replace dots in the name with underscores (only one dot can be there... security issue).

//     if ( $Config['ForceSingleExtension'] )

//            $sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;

//

//     // Remove \ / | : ? * " < >

//     $sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ;

//        print_r($sNewFileName);

//        return $sNewFileName;

 

        $ext = substr($sNewFileName,strripos($sNewFileName,'.')+1);

        $filename = rand_string();

 

       return  $filename.'.'.$ext;

 

}

6、FCKeditor Api

最详细的FCKeditor Api文档默过于官方wiki提供的文档了。

FCKeditor Api官方文档地址:http://wiki./Developer's_Guide/Javascript_API

下面提供国内某网友的翻译文档,转载地址:http://blog./article.asp?id=322

 

FCK 编辑器加载后,将会注册一个全局的 FCKeditorAPI 对象。


FCKeditorAPI
对象在页面加载期间是无效的,直到页面加载完成。如果需要交互式地知道 FCK 编辑器已经加载完成,可使用"FCKeditor_OnComplete"函数。
<script type="text/javascript">
function FCKeditor_OnComplete(editorInstance) {
  FCKeditorAPI.GetInstance('FCKeditor1').Commands.GetCommand('FitWindow').Execute();
}
</script>

在当前页获得 FCK 编辑器实例:
var Editor = FCKeditorAPI.GetInstance('InstanceName');

FCK 编辑器的弹出窗口中获得 FCK 编辑器实例:
var Editor = window.parent.InnerDialogLoaded().FCK;

从框架页面的子框架中获得其它子框架的 FCK 编辑器实例:
var Editor = window.FrameName.FCKeditorAPI.GetInstance('InstanceName');

从页面弹出窗口中获得父窗口的 FCK 编辑器实例:
var Editor = opener.FCKeditorAPI.GetInstance('InstanceName');

获得 FCK 编辑器的内容:
oEditor.GetXHTML(formatted); // formatted
为:true|false,表示是否按HTML格式取出
也可用:
oEditor.GetXHTML();

设置 FCK 编辑器的内容:
oEditor.SetHTML("content", false); //
第二个参数为:true|false,是否以所见即所得方式设置其内容。此方法常用于"设置初始值""表单重置"哦作。

插入内容到 FCK 编辑器:
oEditor.InsertHtml("html"); // "html"
HTML文本

检查 FCK 编辑器内容是否发生变化:
oEditor.IsDirty();

FCK 编辑器之外调用 FCK 编辑器工具条命令:
命令列表如下:
-------------------------------------
DocProps, Templates, Link, Unlink, Anchor, BulletedList, NumberedList, About, Find, Replace, Image, Flash, SpecialChar, Smiley, Table, TableProp, TableCellProp, UniversalKey, Style, FontName, FontSize, FontFormat, Source, Preview, Save, NewPage, PageBreak, TextColor, BGColor, PasteText, PasteWord, TableInsertRow, TableDeleteRows, TableInsertColumn, TableDeleteColumns, TableInsertCell, TableDeleteCells, TableMergeCells, TableSplitCell, TableDelete, Form, Checkbox, Radio, TextField, Textarea, HiddenField, Button, Select, ImageButton, SpellCheck, FitWindow, Undo, Redo
-------------------------------------
使用方法如下:
-------------------------------------
oEditor.Commands.GetCommand('FitWindow').Execute();
-------------------------------------

7、精简FCKeditor文件空间大小

正因为这个编辑器是支持多语言的,所以首先我们针对使用对其做相应的冗余文件删除。

1
、临时文件及文件夹删除:从根目录下开始删除一切以“_”开头的文件及文件夹,因为他们为临时文件和文件夹。删除这类临时文件及文件夹之后,我们还要删除一些根目录下的多余文件,根目录下我们只保留fckconfig.js(配置文件)、fckeditor.jsjs方式调用文件)、fckeditor.phpphp方式调用文件,新版本通过该文件统一调用php4或者php5的调用文件,fckeditor_php4.php/fckeditor_php5.php你可以根据自己服务器使用的情况删减,建议都保留)、fckeditor_php4.phpphp4的调用文件)、fckeditor_php5.phpphp5的调用文件)、fckstyles.xml(样式)、fcktemplates.xml(模板)文件和editor文件夹。

2
editor\lang目录:存放的是多语言配置文件,因为我们只可能用到enzh-cn(简体中文)所以,根据我的选择,我删掉其他的语言配置文件。

3
editor\skins界面目录:默认带有三个界面(default:默认界面,加载速度相对较快;office2003:相对pp的界面,不过速度确实要慢些;silver:银白色界面,加载速度也相对较快),可以自行决定是否删除其中一两个。

4
editor\filemanager\browser\default\connectors目录:存放编辑器所支持的Web动态语言,我们以php为例所以保留php目录,test.html文件可以帮助你查看某语言下的上传设置等(具体上传设置我将在后面的配置作较为详细讲解),可以自行决定是否删除。

5
editor\filemanager\upload目录:同理。

到此精简完成,你会发现整个编辑器确实瘦身不少,呵呵

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多