?? code.js
字號:
?//---------------------------------------------------------
// 雙魚文本編輯器 images V1.0
// 作者:月傷 melody
// 作品鏈接:http://www.2fstory.net/blog/View.aspx?blogID=47
//---------------------------------------------------------
var CodeDivBgColor = "#eeeeee";
var CodeDivFontColor = "#000000";
function code()
{
var str = document.getElementById('CodeText').innerHTML;
var language = document.getElementById('LanguageDropDownList').value;
var showLine = document.getElementById('chkLineNumberMarginVisible').checked;
var canCollapse = document.getElementById('chkOutliningEnabled').checked;
var canAllCollapse = document.getElementById('ckbIsCollapse').checked;
var allRegion = document.getElementById('txbCollapseText').value;
//window.returnValue = FormateCode(str,language,showLine,canCollapse,canAllCollapse,allRegion);
//------------------------------------------
//alert(FormateCode(str,language,showLine,canCollapse,canAllCollapse,allRegion));
var editor = window.dialogArguments.EDiaryEditor;
var oRTE = editor.iframe.contentWindow;
if(window.isIE) {
try{
var html = FormateCode(str,language,showLine,canCollapse,canAllCollapse,allRegion);
oRTE.focus();
var oRng = oRTE.document.selection.createRange();
oRng.pasteHTML(html);
oRng.collapse(false);
oRng.select();
}catch(e){}
}
else {
editor.runCMD('insertHTML', html);
}
//------------------------------------------
window.close();
}
// 轉化為代碼格式(代碼,語言,是否有行號,是否折疊,是否全部折疊,整個代碼的標題)
function FormateCode(str,language,showLine,canCollapse,canAllCollapse,allRegion)
{
var CodeDivStyle = "color: "+CodeDivFontColor+";BACKGROUND-COLOR: "+CodeDivBgColor+";font-family: Verdana,宋體;width: 98%;line-height:16px;BORDER: #cccccc 1px solid;PADDING: 4px;FONT-SIZE: 10pt;WORD-BREAK: break-all;";
if (language != "CSS") {
// 將雙引號替換成"
str = str.replace(/([^\\])"/g,"$1"");
}
switch (language)
{
case "C#": str = FormatCSharp(str,canCollapse); break;
case "HTML": str = FormatHTML(str,canCollapse); break;
case "VBScript": str = FormatVBScript(str,canCollapse); break;
case "JScript": str = FormatJScript(str,canCollapse); break;
case "CSS": str = FormatCSS(str,canCollapse); break;
case "XML": str = FormatXML(str,canCollapse); break;
case "SQL": str = FormatSQL(str,canCollapse); break;
}
if (showLine) str = AddLineNumber(str);
// 替換換行符為<BR>
str = str.replace(/\n/g,"<BR>");
// 替換1個Tab為4個空格,使界面美觀一些
str = str.replace(/\t/g," ");
if (canAllCollapse) str = GetHeader(" ",allRegion,"Code",canAllCollapse,true)+"<BR>"+str+"</span>";
// 給代碼加一個邊框
str = "<div style=\""+CodeDivStyle+"\">" + str + "</div>";
return(str);
}
//--------------------------------------- 格式化C#代碼 ----------------------------------------
// 格式化C#代碼
function FormatCSharp(str,canCollapse)
{
if (str.match(/using/g)!=null)
str = FormatUsing(str,canCollapse);
str = FormatRegion(str,canCollapse);
str = FormatBrace(str);
str = FormatFunctionRemark(str);
// 給所有沒有收縮功能的行的行頭加圖片
str = str.replace(/(^|\n)([^<])/g,"$1<IMG src=\"images/codeimages/InBlock.gif\" align=\"top\">$2");
// 所有關鍵字變成藍色
var re = /\b(abstract|event|new|struct|as|explicit|null|switch|base|extern|object|this|bool|false|operator|throw|break|finally|out|true|byte|fixed|override|try|case|float|params|typeof|catch|for|private|uint|char|foreach|protected|ulong|checked|goto|public|unchecked|class|if|readonly|unsafe|const|implicit|ref|ushort|continue|in|return|using|decimal|int|sbyte|virtual|default|interface|sealed|volatile|delegate|internal|short|void|do|is|sizeof|while|double|lock|stackalloc|else|long|static|enum|namespace|string)\b/g;
str = str.replace(re,"<font color=\"#0000FF\">$1</font>");
str = FormatCodeRemark(str);
// 值變為棕色
str = FormatValue(str);
// 展開和收縮在同一行時,去掉ExpandedSubBlockEnd.gif
// str = str.replace(/(<IMG src="images\/codeimages\/ContractedSubBlock.gif.*?>)<IMG src="images\/codeimages\/ExpandedSubBlockEnd.gif.*?>/gi,"$1");
str = str.replace(/(<IMG src="images\/codeimages\/Contracted(Sub)?Block.gif)([\s\S]*?)(\n)/gi,
function() {
var s = arguments[3].replace(/<IMG src="images\/codeimages\/Expanded(Sub)?BlockEnd.gif.*?>/gi,"");
return(arguments[1]+s+arguments[4]);
});
// 替換第一個收縮圖片為ExpandedBlockStart.gif,并替換第一個收縮圖片前的全部InBlock.gif圖片為None.gif
str = str.replace(/([\s\S]*?)(<IMG src="images\/codeimages\/Expanded)(?:Sub)(BlockStart.gif)/i,
function() {
var s = arguments[1].replace(/(<IMG src="images\/codeimages\/)(InBlock)(.gif".*?>)/gi,"$1None$3");
s += arguments[2]+arguments[3];
return (s);
});
str = str.replace(/(<IMG src="images\/codeimages\/Contracted)(?:Sub)(Block.gif)/i,"$1$2");
// 替換最后一個收縮圖片為ExpandedBlockEnd.gif,并替換最后一個收縮圖片后的全部InBlock.gif圖片為None.gif
str = str.replace(/([\s\S]*)(<IMG src=.*?Expanded)(?:Sub)(BlockEnd.gif)([\s\S]*?)$/i,
function() {
var s = arguments[1]+arguments[2]+arguments[3];
s += arguments[4].replace(/(<IMG src="images\/codeimages\/)(InBlock)(.gif".*?>)/gi,"$1None$3");
return (s);
});
// 雙引號中關鍵字不變色
str = str.replace(/(")([\s\S]*?)\1/g,
function() {
return(arguments[1]+arguments[2].replace(/(<[\s\S]*?>)/g,"")+arguments[1]);
});
str = str.replace(/<IMG src="images\/codeimages\/ExpandedSubBlockEnd.gif" align="top"><IMG src="images\/codeimages\/ExpandedSubBlockStart.gif[\s\S]*?><IMG src="images\/codeimages\/ContractedSubBlock.gif[\s\S]*?>/gi,"<IMG src=\"images/codeimages/InBlock.gif\" align=\"top\">");
return (str);
}
// 格式化using
function FormatUsing(str)
{
// 給using和正式代碼之間的行的行頭加上空白圖片
str = str.replace(/^([\s\S]*?)({)/g,
function() {
var s = arguments[1].replace(/(\n)([^u])/g,"$1<img src=\"images/codeimages/None.gif\" align=\"top\">$2");
s = s.replace(/(\n)([^u<])/g,"$1<img src=\"images/codeimages/None.gif\" align=\"top\">$2");
return(s+arguments[2]);
});
// using加一個收縮功能
str = str.replace(/^([\s\S]*?)\b(using\s)/i,
function() {
var rndnum = Math.floor((Math.random()*1000000)).toString().substr(0,4);
var s = arguments[1].replace(/(.*)([^\n]*)$/g,"$2")
+GetHeader(arguments[1].replace(/(.*)([^\n]*)$/g,"$2")+arguments[2],"...","CodeUsing",false,true);
return(s);
});
var arr = str.match(/.*using.*/g);
if (arr!=null)
str = str.replace(arr[arr.length-1],
"<IMG src=\"images/codeimages/ExpandedBlockEnd.gif\" align=\"top\">"+arr[arr.length-1]+"</span>");
// str = str.replace(/([\s\S]*\n)(using[\s\S]*?)\n/g,"$1<IMG src=\"images/codeimages/ExpandedBlockEnd.gif\" align=\"top\">$2</span>");
return(str);
}
// 格式化Region
function FormatRegion(str,canCollapse)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -