/* Commentor for Emeditor --------------- $Date: 2008-08-02 23:32:18 Written by KevinYuan whyork@21cn.com You are free to use this in any way you want feature: 1> Multiline support. 2> SingleLine with commented date. 3> CSS, HTML, Javascript, C#, C-like, Java comment are supported; */ function GetBlankComment() { if (Filetype.indexOf('htm') > -1 || Filetype.indexOf('ml') > -1) { return "\r\n"; } else if (Filetype.indexOf('css') > -1) { return "/* " + GetCurrentTime() + "*/\r\n"; } else { return "// " + GetCurrentTime() + "\r\n"; } } function escapeRegExp(code) { return code.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'); } //获取匹配当前文档的配置名如 html,htm,xml,css,js,c# var Filetype; Filetype = document.ConfigName.toLowerCase().replace(/ /g, ''); var cursorFix = 3; // //或者/* if (Filetype.indexOf('htm') > -1 || Filetype.indexOf('ml') > -1) { cursorFix = 5; } //类似 var start_tag = '//'; var end_tag; //设定注释类型 function SetCommentTag() { if (Filetype.indexOf('htm') > -1 || Filetype.indexOf('ml') > -1) { start_tag = ''; } else if (Filetype.indexOf('css') > -1) { start_tag = "/*"; end_tag = '*/'; } else { start_tag = '//'; end_tag = ''; } } (function() { var word = document.selection.Text; SetCommentTag(); //如果当前选择的文字为空,则选择光标当前行 if (word == '') { //选择当前行 document.selection.SelectLine(); //获取当前行的文字 var str = document.selection.Text; //如果该行没有文字,只有缩进字符 if (str.replace(/\s/g, '') == '') { //去除尾部的换行 str = str.replace(/[\r\n]*/g, ''); //拼成字符串 var bc = GetBlankComment(); str = str + bc; document.selection.Text = str; //移动到注释处,并修正光标位置 document.selection.CharLeft(false, bc.length - cursorFix); return; } //该行有文字 else { //去除尾部的换行 str = str.replace(/[\r\n]*/g, ''); //拼装注释字符串,并在尾部加入换行 str = str.replace(/^([\s]*)([\w\W]+)/, '$1' + start_tag + '$2' + end_tag + '\r\n'); //不计缩进,获取实际字符开始的位置 var cp = str.indexOf(start_tag); document.selection.Text = str; //移动到注释行的行首 document.selection.CharLeft(false, str.length - cursorFix); document.selection.StartOfLine(); //移动到字符开始的位置 document.selection.CharRight(false, cp + start_tag.length); return; } } //选择了多行文字 else { //将多行文字回车后加注释 word = start_tag + word.replace(/\r*\n/g, end_tag + '\r\n' + start_tag) + end_tag; //删除空注释 //word = word.replace(new RegExp(escapeRegExp(start_tag+end_tag),'img'),''); document.selection.Text = word; return; } })(); function GetCurrentTime() { var d1 = new Date(); var cmtstr = d1.getFullYear() + '-' + (d1.getMonth() + 1) + '-' + d1.getDate() + ' ' + d1.getHours() + ':' + d1.getMinutes() + ':' + d1.getSeconds(); cmtstr = cmtstr.replace(/([^\d])(\d)/img, '$10$2'); cmtstr = cmtstr.replace(/([^\d])\d([\d]{2})/img, '$1$2'); return cmtstr; }