📄 editor.js
字号:
} else {
var fragment = this.doc.createDocumentFragment();
var div = this.doc.createElement("div");
div.innerHTML = html;
while (div.firstChild) {
fragment.appendChild(div.firstChild);
}
var node = this.insertNodeAtSelection(fragment);
}
},
getSelection : function() {
return is_ie ? this.doc.selection : this.iframe.contentWindow.getSelection();
},
createRange : function(sel) {
if (is_ie) {
return sel.createRange();
} else {
this.focusEditor();
if (typeof sel != "undefined") {
try {
return sel.getRangeAt(0);
} catch(e) {
return this.doc.createRange();
}
} else {
return this.doc.createRange();
}
}
},
insertNodeAtSelection : function(toBeInserted) {
if (!is_ie) {
var sel = this.getSelection();
var range = this.createRange(sel);
sel.removeAllRanges();
range.deleteContents();
var node = range.startContainer;
var pos = range.startOffset;
switch (node.nodeType) {
case 3:
if (toBeInserted.nodeType == 3) {
node.insertData(pos, toBeInserted.data);
range = this._createRange();
range.setEnd(node, pos + toBeInserted.length);
range.setStart(node, pos + toBeInserted.length);
sel.addRange(range);
} else {
node = node.splitText(pos);
var selnode = toBeInserted;
if (toBeInserted.nodeType == 11) {
selnode = selnode.firstChild;
}
node.parentNode.insertBefore(toBeInserted, node);
this.selectNodeContents(selnode);
this.updateToolbar();
}
break;
case 1:
var selnode = toBeInserted;
if (toBeInserted.nodeType == 11) {
selnode = selnode.firstChild;
}
node.insertBefore(toBeInserted, node.childNodes[pos]);
this.selectNodeContents(selnode);
this.updateToolbar();
break;
}
} else {
return null;
}
},
selectNodeContents : function(node, pos) {
this.focusEditor();
this.forceRedraw();
var range;
var collapsed = (typeof pos != "undefined");
if (is_ie) {
range = this.doc.body.createTextRange();
range.moveToElementText(node);
(collapsed) && range.collapse(pos);
range.select();
} else {
var sel = this.getSelection();
range = this.doc.createRange();
range.selectNodeContents(node);
(collapsed) && range.collapse(pos);
sel.removeAllRanges();
sel.addRange(range);
}
},
forceRedraw : function() {
this.doc.body.style.visibility = "hidden";
this.doc.body.style.visibility = "visible";
},
colorToRgb : function(v) {
if(!v) return '';
function hex(d){
return (d < 16) ? ("0" + d.toString(16)) : d.toString(16);
};
if (typeof v == "number") {
var r = v & 0xFF;
var g = (v >> 8) & 0xFF;
var b = (v >> 16) & 0xFF;
return "#" + hex(r) + hex(g) + hex(b);
}
if (v.substr(0, 3) == "rgb") {
var re = /rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)/;
if (v.match(re)) {
var r = parseInt(RegExp.$1);
var g = parseInt(RegExp.$2);
var b = parseInt(RegExp.$3);
return "#" + hex(r) + hex(g) + hex(b);
}
return null;
}
if (v.substr(0, 1) == "#") {
return v;
}
return null;
},
needsClosingTag : function(el) {
var closingTags = " head script style div span tr td tbody table em strong font a title ";
return (closingTags.indexOf(" " + el.tagName.toLowerCase() + " ") != -1);
},
htmlEncode : function(str) {
str = str.replace(/&/ig, "&");
str = str.replace(/</ig, "<");
str = str.replace(/>/ig, ">");
str = str.replace(/\x22/ig, """);
return str;
},
getHTML : function(root, outputRoot, editor) {
var html = "";
switch (root.nodeType) {
case 1:
case 11:
var closed;
var i;
var root_tag = (root.nodeType == 1) ? root.tagName.toLowerCase() : '';
if (is_ie && root_tag == "head") {
if(outputRoot)
html += "<head>";
var save_multiline = RegExp.multiline;
RegExp.multiline = true;
var txt = root.innerHTML.replace(/(<\/|<)\s*([^ \t\n>]+)/ig, function(str, p1, p2){
return p1 + p2.toLowerCase();
});
RegExp.multiline = save_multiline;
html += txt;
if (outputRoot)
html += "</head>";
break;
} else if (outputRoot) {
closed = (!(root.hasChildNodes() || this.needsClosingTag(root)));
html = "<" + root.tagName.toLowerCase();
var attrs = root.attributes;
for (i = 0; i < attrs.length; ++i) {
var a = attrs.item(i);
if (!a.specified) {
continue;
}
var name = a.nodeName.toLowerCase();
if (/_moz|contenteditable|_msh/.test(name)) {
continue;
}
var value;
if (name != "style") {
if (typeof root[a.nodeName] != "undefined" && name != "href" && name != "src") {
value = root[a.nodeName];
} else {
value = a.nodeValue;
}
} else {
value = root.style.cssText;
}
if (/(_moz|^$)/.test(value)) {
continue;
}
html += " " + name + '="' + value + '"';
}
html += closed ? " />" : ">";
}
for (i = root.firstChild; i; i = i.nextSibling) {
html += this.getHTML(i, true, editor);
}
if (outputRoot && !closed) {
html += "</" + root.tagName.toLowerCase() + ">";
}
break;
case 3:
if (!root.previousSibling && !root.nextSibling && root.data.match(/^\s*$/i)) {
html = ' ';
} else {
html = this.htmlEncode(root.data);
}
break;
case 8:
html = "<!--" + root.data + "-->";break;
}
return html;
}
}
function codetohtml(str) {
if (!IsChecked('atc_html')) {
str = str.replace(/</ig,'<');
str = str.replace(/>/ig,'>');
}
str = str.replace(/\n/ig,'<br />');
str = str.replace(/\[hr\]/ig,'<hr />');
str = str.replace(/\[\/(size|color|font|backcolor)\]/ig,'</font>');
str = str.replace(/\[(sub|sup|u|i|strike|b|blockquote|li)\]/ig,'<$1>');
str = str.replace(/\[\/(sub|sup|u|i|strike|b|blockquote|li)\]/ig,'</$1>');
str = str.replace(/\[\/align\]/ig,'</p>');
str = str.replace(/\[align=(left|center|right|justify)\]/ig,'<p align="$1">');
str = str.replace(/\[size=(\d+?)\]/ig,'<font size="$1">');
str = str.replace(/\[color=([^\[\<]+?)\]/ig, '<font color="$1">');
str = str.replace(/\[backcolor=([^\[\<]+?)\]/ig, '<font style="background-color:$1">');
str = str.replace(/\[font=([^\[\<]+?)\]/ig, '<font face="$1">');
str = str.replace(/\[list=(a|A|1)\](.+?)\[\/list\]/ig,'<ol type="$1">$2</ol>');
str = str.replace(/\[(\/)?list\]/ig,'<$1ul>');
str = str.replace(/\[img\](.*?)\[\/img\]/ig,'<img src="$1" border="0" />');
str = str.replace(/\[url=(.+?)\](.+?)\[\/url\]/ig, '<a href="$1">'+'$2'+'</a>');
return str;
}
function htmltocode(str){
if (IsChecked('atc_html')) {
return str;
}
str = str.replace(/(\r\n|\n|\r)/ig, '');
str = str.replace(/<br[^>]*>/ig,'\n');
str = str.replace(/<p[^>\/]*\/>/ig,'\n');
str = str.replace(/\son[\w]{3,16}\s?=\s*([\'\"]).+?\1/ig,'');
str = str.replace(/<hr[^>]*>/ig,'[hr]');
str = str.replace(/<(sub|sup|u|strike|b|i|pre)>/ig,'[$1]');
str = str.replace(/<\/(sub|sup|u|strike|b|i|pre)>/ig,'[/$1]');
str = str.replace(/<(\/)?strong>/ig,'[$1b]');
str = str.replace(/<(\/)?em>/ig,'[$1i]');
str = str.replace(/<(\/)?blockquote([^>]*)>/ig,'[$1blockquote]');
str = str.replace(/<img[^>]*src=[\'\"\s]*([^\s\'\"]+)[^>]*>/ig,'[img]'+'$1'+'[/img]');
str = str.replace(/<a[^>]*href=[\'\"\s]*([^\s\'\"]*)[^>]*>(.+?)<\/a>/ig,'[url=$1]'+'$2'+'[/url]');
str = searchtag('font',str,'fonttag');
str = searchtag('div',str,'dstag');
str = searchtag('p',str,'ptag');
str = searchtag('span',str,'dstag');
str = searchtag('ol',str,'listtag');
str = searchtag('ul',str,'listtag');
str = str.replace(/<[^>]*?>/ig, '');
str = str.replace(/&/ig, '&');
str = str.replace(/</ig, '<');
str = str.replace(/>/ig, '>');
return str;
}
function searchtag(tagname,str,action){
var tag = ['<','>'];
var head = tag[0] + tagname;
var head_len = head.length;
var foot = tag[0] + '/' + tagname + tag[1];
var foot_len = foot.length;
var strpos = 0;
do {
var strlower = str.toLowerCase();
var begin = strlower.indexOf(head,strpos);
if (begin == -1) {
break;
}
var strlen = str.length;
for (var i = begin + head_len; i < strlen; i++) {
if (str.charAt(i)==tag[1]) break;
}
if(i>=strlen) break;
var firsttag = i;
var style = str.substr(begin + head_len, firsttag - begin - head_len);
var end = strlower.indexOf(foot,firsttag);
if (end == -1) break;
var nexttag = strlower.indexOf(head,firsttag);
while (nexttag != -1 && end != -1) {
if(nexttag > end) break;
end = strlower.indexOf(foot, end + foot_len);
nexttag = strlower.indexOf(head, nexttag + head_len);
}
if (end == -1) {
strpos = firsttag;
continue;
}
firsttag++;
var findstr = str.substr(firsttag, end - firsttag);
str = str.substr(0,begin) + eval(action)(style,findstr,tagname) + str.substr(end+foot_len);
strpos = begin;
} while (begin != -1);
return str;
}
function ptag(style,code){
if (style.indexOf('align=') != -1) {
style = fetchvalue(style,'align=');
code = '[align=' + style + ']' + code + '[/align]';
} else {
code += "\n";
}
return code;
}
function dstag(style,code){
var styles = [
['align' , 1, 'align='],
['align', 1 , 'text-align:'],
['color' , 2 , 'color:'],
['font' , 1 , 'font-family:'],
['b' , 0 , 'font-weight:' , 'bold'],
['i' , 0 , 'font-style:' , 'italic'],
['u' , 0 , 'text-decoration:' , 'underline'],
['strike' , 0 , 'text-decoration:' , 'line-through']
];
style = style.toLowerCase();
for (var i=0;i<styles.length;i++) {
var begin = style.indexOf(styles[i][2]);
if (begin == -1) {
continue;
}
var value = '';
if (styles[i][1] < 2) {
value = fetchvalue(style,styles[i][2]);
} else {
begin = style.indexOf('rgb',begin);
if (begin == -1) {
value = fetchvalue(style,styles[i][2]);
} else {
value = editor.colorToRgb(style.substr(begin,style.indexOf(')')-begin+1));
}
}
if (styles[i][1] == 0) {
if (value == styles[i][3]) {
code = '[' + styles[i][0] + ']' + code + '[/' + styles[i][0] + ']';
}
} else {
code = '[' + styles[i][0] + '=' + value + ']' + code + '[/' + styles[i][0] + ']';
}
}
return code;
}
function listtag(type,code,tagname){
code = code.replace(/<(\/)?li>/ig,'[$1li]');
if (tagname == 'ul') {
return '[list]'+code+'[/list]';
}
if (type && type.indexOf('type=')!='-1') {
type = fetchvalue(type,'type=');
if (type!='a' && type!='A' && type!='1') {
type='1';
}
return '[list=' + type + ']' + code + '[/list]';
} else {
return '[list=1]'+code+'[/list]';
}
}
function fonttag(style,str) {
var styles = new Array();
styles ={'size' : 'size=','color' : 'color=','font' : 'face=','backcolor' : 'background-color:'};
style = style.toLowerCase();
for (st in styles) {
var begin = style.indexOf(styles[st]);
if (begin == -1) {
continue;
}
var value = fetchvalue(style,styles[st]);
str = '[' + st + '=' + value + ']' + str + '[/' + st + ']';
}
return str;
}
function fetchvalue(style,find) {
var firstpos = style.indexOf(find)+find.length;
var len = style.length;
var start = 0;
for (var i=firstpos;i<len;i++) {
var t_char = style.charAt(i);
if (start==0) {
if (t_char == '"' || t_char == "'") {
start = i+1;
} else if (t_char != ' ') {
start = i;
}
continue;
}
if (t_char=='"' || t_char=="'" || t_char==' ' || t_char==';') {
break;
}
}
return style.substr(start,i-start);
}
function IsChecked(id) {
return document.getElementById(id) && document.getElementById(id).checked === true ? true : false;
}
function newAttach() {
aid++;
attachnum--;
var s = $('att_mode').firstChild.cloneNode(true);
var id = aid;
s.id = 'att_div' + id;
var tags = s.getElementsByTagName('input');
for (var i=0;i<tags.length;i++) {
tags[i].name += id;
tags[i].id = tags[i].name;
if (tags[i].name == 'attachment_'+id) {
tags[i].onchange = function(){upAttach(id);};
}
}
$('attach').appendChild(s);
}
function upAttach(id) {
var div = $('att_div'+id);
var path = $('attachment_'+id).value;
var attach_ext = path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
if (allow_ext != ' ' && (attach_ext == '' || allow_ext.indexOf(' ' + attach_ext + ' ') == -1)) {
if (IsElement('att_span_'+id)) {
div.removeChild($('att_span_'+id));
}
if (path != '') alert('附件类型不匹配!');
return false;
}
$('attachment_'+id).onmouseover = function(){viewimg(id)};
if (!IsElement('att_span_'+id)) {
var s = $('opr_mode').firstChild.cloneNode(true);
s.id = 'att_span_'+id;
div.appendChild(s);
}
if (attachnum>0 && $('attach').lastChild.id == 'att_div'+id) {
newAttach();
}
}
function viewimg(id) {
var path = $('attachment_'+id).value;
var attach_ext = path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
if(!is_ie || !in_array(attach_ext,['jpg','gif','png','bmp','jpeg']))
return;
$('viewimg').innerHTML = getimage(path,320);
read.open('viewimg','att_div'+id);
}
function getimage(path,maxwh) {
var str = '<div style="padding:6px;border:1px solid #cecfce"><img src="' + path + '" ';
var img = new Image();
img.src = path;
if (img.width>maxwh || img.width>maxwh) {
str += ((img.width/img.height)>1 ? 'width' : 'height') + '="' + maxwh + '" ';
}
str += '/></div>';
return str;
}
function addupload(obj) {
var aid = obj.parentNode.id;
aid = aid.substr(aid.lastIndexOf('_')+1);
editor.focusEditor();
editor.insertText(' [upload='+aid+'] ');
}
function delupload(obj) {
obj = obj.parentNode.parentNode;
obj.parentNode.removeChild(obj);
attachnum++;
if (!$('attach').hasChildNodes()) {
newAttach();
}
}
function flex(type) {
var height = parseInt(editor.textArea.style.height);
if (type==0) {
if(height<300) return;
height-=100;
} else {
height+=100;
}
editor.textArea.style.height = height + 'px';
if (editor.iframe != null) {
editor.iframe.style.height = editor.textArea.style.height;
}
}
function quickpost(event) {
if ((event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83)) {
document.goods.Submit.click();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -