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 = "
";
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 += "
" + tmp + "
\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'));