欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

fckeditor 插件实例 制作步骤

程序员文章站 2022-06-16 19:51:22
以创建一个简单的超级链接为例。可以从已经存在的placeholder插件的目录作为基本的骨架。 1. 命名插件名称为:"insertlink". ,并建立同名的目录,并且在...
以创建一个简单的超级链接为例。可以从已经存在的placeholder插件的目录作为基本的骨架。

1. 命名插件名称为:"insertlink". ,并建立同名的目录,并且在insertlink目录下创建一个lang的目录,lang目录下至少有一个文件en.js。该文件中至少要有按钮和对话框标题的国际化信息,比如:
fcklang.insertlinkbtn = 'insert/edit link' ; //按钮的标题
fcklang.insertlinkdlgtitle = 'link properties' ; //对话框的标题
2:图片,在insertlink文件夹中添加图片文件,最好将图片文件命名为和插件名一样的名称。图片的大小要求是20*21,并且是透明的。
3:javascript:
添加fckplugin.js文件到insertlink目录。
注册相关命令:
注册命令的方法是fckcommands.registercommand(命令名称,对话框命令)
创建对话框命令的格式:new fckdialogcommand( 命令名称, 对话框标题,url路径, 宽度,高度)

fckcommands.registercommand( 'insertlink', new fckdialogcommand( 'insertlink', fcklang.insertlinkdlgtitle,
fckplugins.items['insertlink'].path + 'fck_insertlink.html', 340, 200 ) ) ;

// 创建工具栏按钮 new fcktoolbarbutton( 按钮名称, 按钮标题 ) ;
var oinsertlinkitem = new fcktoolbarbutton( 'insertlink', fcklang.insertlinkbtn ) ;
oinsertlinkitem.iconpath = fckplugins.items['insertlink'].path + 'insertlink.gif' ;
fcktoolbaritems.registeritem( 'insertlink', oinsertlinkitem ) ;

//创建用于所有insertlink操作的对象
var fckinsertlink = new object() ;

//在当前的选择上插入一个超级链接
// 这个添加的方法将在弹出窗口点击ok按钮时被调用。
// 该方法将会接收从对话框中传来的值。

fckinsertlink.add = function( linkname, caption )
{
if(linkname.substr(0,4) != "http" && linkname.substr(0,4) != "http")
linkname = "http://"+linkname ;
fck.inserthtml("<a href='"+linkname+"'>"+caption+"</a>") ;
}

4:html
在insertlink目录下添加请求的文件。
请求文件的模板代码:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>link properties</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta content="noindex, nofollow" name="robots">
<script language="javascript">

var oeditor = window.parent.innerdialogloaded() ;
var fck = oeditor.fck ;
var fcklang = oeditor.fcklang ;
var fckinsertlink = oeditor.fckinsertlink ;

window.onload = function ()
{
loadselected() ; //see function below
window.parent.setokbutton( true ) ;
}

//从编辑器中得到当前的被选择的元素,有以下两种方法:

//1. 可用于image等元素的选择。
//var eselected = oeditor.fckselection.getselectedelement() ;

//2. 由于有内部文本的元素
var eselected = fck.selection.movetoancestornode( 'a' )
if ( eselected )
fck.selection.movetonode( eselected ) ;

//如果超级练级被选择,那么显示超级链接的属性
function loadselected()
{
if ( !eselected )
return ;

txthref.value = eselected.href ;
txtcaption.value = eselected.innertext ;

//适合于第一种选择操作的代码:
// if ( eselected.tagname == 'img' ) {
// -- code for setting dialog values -- }
// else
// eselected == null ; //this will replace the current selection if not the right type

}

//点击ok按钮发生的操作
function ok()
{
if ( document.getelementbyid('txthref').value.length > 0 )
fckinsertlink.add( txthref.value, txtcaption.value ) ;

return true ;
}
</script>
</head>

<body scroll="no" style="overflow: hidden">
<table height="100%" cellspacing="0" cellpadding="0" width="100%" border="0">
<tr>
<td>
<table cellspacing="0" cellpadding="0" align="center" border="0">
<tr>
<td>
type the url for the link<br>
<input id="txthref" type="text"><br>
type the caption for the link<br>
<input id="txtcaption" type="text">
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

<!-- end code -->

5:编辑fckconfig.js文件,并加入下列代码,注册插件。
fckconfig.plugins.add( 'insertlink', 'en' ) ;
//在工具栏集合中定义命令名称。
fckconfig.toolbarsets["default"] = [ , ['insertlink']