📄 coder-debug.js
字号:
reg = this.$COM_REG;
code=code.replace(/\\\r?\n/g,"");
//special chars
code=code.replace(/([\x01\x02\x03\x04])/g,"$1-");
//1.protect those
code=linb.Coder.replace(code,[
// /**/
[reg.BLOCK_COMMENT.source,reverse?'':function(s,i){var ret="\x01d" + index4++ +"\x02"; cache.d[ret]=s[i]; return ret;}],
// //
[reg.LINE_COMMENT.source,reverse?'':function(s,i){var ret="\x01e" + index5++ +"\x02"; cache.e[ret]=s[i]; return ret;}],
// /*@
[/\/\*@|@\*\/|\/\/@[^\n]*\n/.source,function(s,i){var ret="\x01c" + index3++ +"\x02"; cache.c[ret]=s[i]; return ret;}],
// ''
[reg.SQ_STRING.source,function(s,i){var ret="\x03a" + index1++ +"\x04"; cache.a[ret]=s[i]; return ret;}],
// ""
[reg.DQ_STRING.source,function(s,i){var ret="\x03b" + index2++ +"\x04"; cache.b[ret]=s[i]; return ret;}],
// regexp
[reg.REG.source,function(s,i){var ret="\x03f" + index6++ +"\x04"; cache.f[ret]=s[i]; return ret;}],
// function(a,b,c)
[/function\s*\([^)]*\)/.source,function(s,i){var ret="\x03g" + index7++ +"\x04"; cache.g[ret]=s[i]; return ret;}]
]);
arr=[
// a++ +b;
["([+-])\\s+([+-])", "$1 $2"],
// var a;
["\\b[\\s]+\\b", " "],
// [ a
["[\\s]+", ""]
];
if(type=='css'){
_.arr.insertAny(arr,[/\s+(\.)/.source, " $1"],2,true);
_.arr.insertAny(arr,[/(\d*\.?\d+|\d+\.?\d*)(cm|em|ex|pt|px|%|\:)?/, " $0 "],-1,true);
}
// prepare space
code=linb.Coder.replace(code,arr);
if(!reverse){
arr=[
[/[\{]/.source, function(a,i){return a[i]+'\n'+space[++deep]}],
[/[\x02\;]/.source, function(a,i){return a[i]+'\n'+space[deep]}],
[/(\,)([\x03\x04\w_\-]+\:)/.source, function(a,i){return a[i+1]+'\n'+space[deep]+a[i+2]}],
[/\x01/.source, function(a,i){return '\n'+space[deep]+a[i]}],
[/[\}]\s*[\,\;]*/.source, function(a,i){return '\n'+space[--deep]+a[i]+'\n'+space[deep]}]
];
if(type!='css'){
arr.push([/for\s*\([\w ]+\sin\s/.source, "$0"],
[/for\s*\(([^;]*);([^;]*);([^)]*)\)/.source, "for($1; $2; $3)"],
// '=>' is for php
[/(,)(("[^"\n\r]*"|'[^'\n\r]*'|\w+)?(:|=>))/.source, function(a,i){return a[i+1]+"\n"+space[deep]+a[i+2]}],
[/\b(case|default)\b[^:]+:/.source, function(a,i){return a[i]+"\n"+space[deep]}]
);
}
// add \n
code=linb.Coder.replace(code,arr);
// add detail
code=linb.Coder.replace(code,[
[/ *[\n\r]/.source,'\n'],
[/\{\s+\}/.source,'{}'],
[/\}\n *(else|catch|finnally)/.source, '}$1'],
//protect number
[reg.NUMBER, '$0'],
//protect "//" and "/*" comments , that does not start with new line.
[/(\/\/)|(\/\*)|(\*\/)/.source,'$0'],
// + - * / and so on
//[/->|=>/.source,' $0 '],
[/\s*((\+\+|\-\-|\&\&|\|\||!!)|([=!]==)|((<<|>>>|>>)=?)|([\+\-\*\/\%\&|^<>!=~]=?)|([?:]))\s*/.source,' $1 ']
]);
}
//restore those protection
code=linb.Coder.replace(code,[
[/[\n\r]+/.source,'\n'],
[/( *)(\x01[d]\d+\x02)/.source, function(s,i){s[i+1]=s[i+1]||'';return s[i+1] + cache.d[s[i+2]].replace(/(\n)(\s*)/g,'$1'+s[i+1])}],
[/\x03[g]\d+\x04/.source, function(s,i){return cache.g[s[i]].replace(/\s*,\s*/g,','+(reverse?'':' '))}],
[/[\x01\x03]([\w])\d+[\x02\x04]/.source, function(s,i){return cache[s[i+1]][s[i]]}],
[/\}\s*([\)\]])/.source,'}$1']
]);
//return special chars
code=code.replace(/([\x01\x02\x03\x04])-/g,"$1");
}
return code;
},
$id:1,
/*parse code
str:source code
key:js/css/php
arrActions:command actions,['plain',]
height:control height
id:specify id
*/
formatHTML:function(code, type, arrActions, id, height){
if(!id)id=''+this.$id++;
var _key = this.$key,
_str = code,
_this=this,
_encode=function(str){
return str.replace(/\x02|\x03/g, function(a){return a=="\x02"?"<":"&"});
},
_decode=function(str){
return str.replace(/<|\&/g, function(a){return a=='<'?"\x02":"\x03"});
};
//clear begin and end
code = code.replace(/^\s*(.*)\s*$/g,'$1');
code = _decode(code);
var a,_t;
//get pro to a
if(!this._profiles[type])
type='js';
a=_.copy(this._profiles[type]);
//for clear begin/end, for platform
code = this.replace(code, [[/(\r\n|\r)/g, "\n"],[/( +)(\n)/g, "$2"],[/\t/g, " "],[/ /g,' ']]);
var arr=[]; //[[/<[^>]+>[^<]*<\/[^>]+>/,'$0']];
var f = function(o,s,r){
if(o){
if(!_.isArr(o))o=[o];
_.arr.each(o,function(o){
if(typeof o =='string')o="\\b(" + o + ")\\b";
arr.push([o, r?r:"<span class='"+s+"'>$0<\/span>"]);
});
}
};
//ignore
if(_t=a['multicomment']){
f(_t, 'multicomment', function(a,i){
return "<span class='comment'>"+a[i].split('\n').join("</span>\n<span class='comment'>")+"</span>";
});
delete a['multicomment'];
}
if(_t=a['comment']){
f(_t, 'comment', function(a,i){
return "<span class='comment'>"+a[i].replace('\n','')+"</span>";
});
delete a['comment'];
}
if(_t=a['IGNORE']){
f(_t, 'IGNORE', "$0");
delete a['IGNORE'];
}
//sort items
if(a['reg']){
(function(o,s){
if(o){
if(!_.isArr(o))o=[o];
_.arr.each(o,function(o){
if(typeof o =='string')o="\\b(" + o + ")\\b";
arr.push([o, "$1<span class='"+s+"'>$2<\/span>$5"]);
});
}
})(a['reg'],'reg');
delete a['reg'];
}
_.arr.each(["string1", "string2", "number"],function(s){
if(a[s]){
f(a[s],s);
delete a[s];
}
});
//others
_.each(a,function(o,i){
f(o,i);
});
code = this.replace(code,arr);
code = _encode(code);
var strR='';
var alist = code.split('\n');
if(alist[0]==""){alist.shift();}
if(alist[alist.length-1]==""){alist.pop();}
var aa=[];
aa.push("<div id='"+_key+":"+id+":' class='sh'>");
if(arrActions && arrActions[0]){
aa.push("<div id='"+_key+"-"+'sh-cmd'+":"+id+":' class='sh-cmd'>");
aa.push("<span class='sh-toggle' href='javascript:;' onclick='linb.Coder._toggle(this);'>-</span> <span class='cmd-ruler'></span>");
_.arr.each(arrActions,function(s){
aa.push("<a id='"+_key+"-"+s+":"+id+":' href='javascript:;' onclick='linb.Coder._action(this,\""+s+"\",arguments[0]);'>"+s+"</a>");
});
aa.push("<span>"+type+" source code viewer, powered by <a href='http://www.linb.net' target='_blank' style='font-size:9px;color:#000157;'>LINB</a></span>");
aa.push("</div>");
}
aa.push("<pre style='display:none'>");
aa.push(_str.replace(/<([\w\/])/g,"<$1"));
aa.push("</pre>");
aa.push("<div id='"+_key+"-"+'sh-con'+":"+id+":'class='sh-con' "+(height?"style='overflow:auto;height:"+height+"px;'":"")+"><ol id='"+_key+"-"+'ol'+":"+id+":' start='1' class='"+type+"'><li>");
aa.push(alist.join(' </li><li>'));
aa.push("</li></ol></div>");
aa.push("</div>");
_encode=_decode=null;
_.asyRun(function(){linb.Coder._remedy(id)});
return aa.join('');
},
formatAll:function(code, type, arrActions, id, height){
var arr = _.toArr(arguments);
arr[0] = this.formatText.call(this, code, type);
return this.formatHTML.apply(this, arr);
},
applyById:function(id, formatAll){
var i = 0, self = this, fun = function(threadid){
if(linb.Dom.byId(id)){
var _t, o, cls;
i ++ ;
_t = linb(id);
cls = (_t.get(0).className || "").split(/\s+/g);
o = _.str.toDom(linb.Coder[formatAll ? "formatAll" : "formatHTML"](_t.text(), cls[0], cls[1]&& cls[1].split("-"), id + ":" + i));
_t.replace(o);
}else{
linb.Thread(threadid).abort();
}
};
linb.Thread(null, [fun], null, null, null, null, true).start();
},
_remedy:function(id){
var _t=this.$key+":"+id+":";
if(!(_t=linb(_t)).isEmpty()){
var p=_t.parent(), w=p.scrollWidth(), w2 = p.width();
p.css('positoin','relative');
if(w>w2)_t.width(w);
}
},
$action:{
run:function(o,e){
var code=linb(o).parent().next().text(),
fun=new Function([], code);
fun.call(linb(o).parent(2),e);
},
plain:function(o){
var code = linb(o).parent().next().text();
var code = code.replace(/</g, '<');
var wnd = window.open('', '_blank', 'width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=1');
wnd.document.write('<pre style="width:100%;height:100%;border:none;">' + code + '</pre>');
wnd.document.close();
wnd=null;
}
},
_action:function(o,key,e){
if(this.$action[key]){
this.$action[key](o,e||window.event);
}
},
_toggle:function(node){
var s=node.parentNode.nextSibling.nextSibling.style;
if(s.display=='none'){
s.display='';
node.innerHTML='-';
}else{
s.display='none';
node.innerHTML='+';
}
}
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -