第一步:需要书写你的插件文件,这里必需留意你的目次布局,默认的插件路径是..../editor/plugins/ 。为了利便起见我们不改变默认路径,先在这个目次下建设一个存放插件的文件夹,这里我们起名为formatcommands。
然后我们在此目次下建设一个fckplugin.js,记着这里插件的名字必需为这个名字(巨细写也要一致),然后我们在建设一个语言包文件夹lang,最后把需要的图标文件也放在与插件文件fckplugin.js同目次下,详细的文件目次请看图:
增补说明一下:lang文件夹下面是语言包文件,这里我建设了一个en.js 留意 en是国别码都是小写的,假如要是中文可以写成 zh-cn.js
en.js 的源码为:
* FCKeditor - The text editor for internet
* Copyright (C) 2003-2006 Frederico Caldeira Knabben
*
* Licensed under the terms of the GNU Lesser General Public License:
*
*
* For further information visit:
*
*
* "Support Open Source software. What about a donation today?"
*
* File Name: en.js
* Marquee English language file.
*
* File Authors:
* Yogananthar Ananthapavan(rollbond@gmail.com)
*/
FCKLang.Format168Btn = 'format';
这个是为了给出鼠标悬停到按钮上的提示。插件文件的源代码为:
/**//** FCKeditor - The text editor for internet
* Copyright (C) 2003-2006 Frederico Caldeira Knabben
*
* Licensed under the terms of the GNU Lesser General Public License:
*
*
* For further information visit:
*
*
* "Support Open Source software. What about a donation today?"
*
* File Name: fckplugin.js
* Plugin for Format168 background
*
*
* File Authors:
* Yogananthar Ananthapavan (rollbond@gmail.com)
*/
// Create the "Format168" toolbar button
var oFormat168Item = new FCKToolbarButton('Format168', FCKLang.Format168Btn);
//配置按钮的图标路径
oFormat168Item.IconPath = FCKPlugins.Items['formatcommands'].Path + 'format168.jpg';
//注册按钮项
FCKToolbarItems.RegisterItem('Format168', oFormat168Item);
// The object used for all Format168 operations.
var FCKFormat168 = new Object();
FCKFormat168 = function(name){
this.Name = name;
}
//FCK_TRISTATE_ON为默认是选中状态
下面的两个要领是实现接口的两个必需的要领,不然会报剧本错误
FCKFormat168.prototype.GetState = function() {
return FCK_TRISTATE_OFF;
}
//此要领是点击按钮后要完成的操纵
FCKFormat168.prototype.Execute = function(){
FormatText();
}
//以下都是实现成果的要领
function FormatText() {
var oEditor = FCKeditorAPI.GetInstance('FCKeditor1') ;
if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
{
var temps = new Array();
var sec = oEditor.EditorDocument.selection.createRange();
var tmpText = sec.text;
var isPart = tmpText != null && tmpText.trim().length > 0;
isPart = false; //临时无法实现局部名目化
if (!isPart) {
var imgs = oEditor.EditorDocument.images;
if (imgs != null && imgs.length > 0) {
for (j = 0; j < imgs.length; j++) {
var t = document.createElement("IMG");
t.alt = imgs[j].alt;
t.src = imgs[j].src;
t.width = imgs[j].width;
t.height = imgs[j].height;
t.align = imgs[j].align;
temps[temps.length] = t;
}
var formatImgCount = 0;
for (j = 0; j < imgs.length;) {
imgs[j].outerHTML = "#FormatImgID_" + formatImgCount + "#";
formatImgCount++;
}
}
var html = processFormatText(oEditor.EditorDocument.body.innerText);
if (temps != null && temps.length > 0) {
for (j = 0; j < temps.length; j++) {
var imghtml = "<img src=\"" + temps[j].src + "\" alt=\"" + temps[j].alt + "\" width=\"" + temps[j].width + "\" height=\"" + temps[j].height + "\" align=\"" + temps[j].align + "\">";
html = html.replace("#FormatImgID_" + j + "#", imghtml);
}
}
oEditor.SetHTML(html);
} else {
var html = processFormatText(tmpText);
sec.pasteHTML(html);
}
}
else
alert( '必需在设计模式下操纵!' ) ;
}
function DBC2SBC(str) {
var result = '';
for (var i = 0; i < str.length; i++) {
code = str.charCodeAt(i);
// “65281”是“!”,“65373”是“}”,“65292”是“,”。不转换","
if (code >= 65281 && code < 65373 && code != 65292 && code != 65306){
// “65248”是转换码距
result += String.fromCharCode(str.charCodeAt(i) - 65248);
} else {
result += str.charAt(i);
}
}
return result;
}
function processFormatText(textContext) {
var text = DBC2SBC(textContext);
var prefix = " ";
var tmps = text.split("\n");
var html = "";
for (i = 0; i < tmps.length; i++) {
var tmp = tmps[i].trim();
if (tmp.length > 0) {
html += "<p> " + tmp + "</p>\n";
}
}
return html;
}
String.prototype.trim = function()
{
return this.replace(/(^[\s ]*)|([\s ]*$)/g, "");
};
String.prototype.leftTrim = function()
{
return this.replace(/(^\s*)/g, "");
};
String.prototype.rightTrim = function()
{
return this.replace(/(\s*$)/g, "");
};
// Register the related command
FCKCommands.RegisterCommand('Format168', new FCKFormat168('Format168'));
第二步:修改fck默认的设置文件
直接修改默认的设置文件 fckconfig.js。从FCKeditor文件夹下找到fckconfig.js。找到下面这句:
FCKConfig.PluginsPath = FCKConfig.BasePath + 'plugins/' ;然后增加:
FCKConfig.Plugins.Add('formatcommands') ;