(function () { CKEDITOR.plugins.add("autoformat", { init: function (a) { a.addCommand( "autoformat", CKEDITOR.plugins.autoformat.commands.autoformat ); a.ui.addButton("autoformat", { label: "一键排版", command: "autoformat", icon: this.path + "images/autoformat.png" }); } }); CKEDITOR.plugins.autoformat = { commands: { autoformat: { exec: function (editor) { formatText(editor); } } } }; //格式化 function formatText(editor) { var myeditor = editor; if (myeditor.mode == "wysiwyg") { var tempimg = new Array(); var temptable = new Array(); var tempobject = new Array(); var isPart = false; //暂时无法实现局部格式化 if (!isPart) { var tmpDiv = document.createElement("DIV"); var editorhtml = myeditor.getData(); editorhtml = editorhtml.replace( /
[page]
" ); //将div span标签替换为p 标签 tmpDiv.innerHTML = editorhtml .replace(/ /gi, "") .replace(/\[page\]<\/p>/gi, '
' + tempobject[j] + "
"; html = html.replace( "#FormatObjectID_" + j + "#", objecthtml ); } } if (tempimg != null && tempimg.length > 0) { for (var j = 0; j < tempimg.length; j++) { var imgheight = ""; var imgwidth = ""; if (tempimg[j].height != 0) imgheight = ' height="' + tempimg[j].height + '"'; if (tempimg[j].width != 0) imgwidth = ' width="' + tempimg[j].width + '"'; var imgalign = ""; if (tempimg[j].align != "") imgalign = ' align="' + tempimg[j].align + '"'; var imghtml = '" + strongarray[i] + "
" ); } while (html.indexOf("") != -1) html = html.replace("", ""); while (html.indexOf('') != -1) html = html.replace( '
', '
' ); editor.setData(html); } else { } } else { alert("必须在设计模式下操作!"); } } function processFormatText(textContext) { var text = dbc2Sbc(textContext); var prefix = ""; var tmps = text.split("\n"); var html = ""; for (var i = 0; i < tmps.length; i++) { var tmp = tmps[i].trim(); if (tmp.length > 0) { var reg = /#Format[A-Za-z]+_\d+#/gi; var f = reg.exec(tmp); if (f != null) { tmp = tmp.replace(/#Format[A-Za-z]+_\d+#/gi, ""); html += f; if (tmp != "") html += '
' + tmp + "
\n"; } else { html += "" + tmp + "
\n"; } } } return html; } function dbc2Sbc(str) { var result = ""; for (var i = 0; i < str.length; i++) { var 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; } 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, ""); }; })();