本帖最後由 lair 於 2012-12-24 21:27 編輯
接上篇的 editor.jsfunction SetFormat(data)
{
self.parent.SetFontFormat(data);
self.parent.close_insert_pop();
}
//--------------------------------------------------------------------------------------------------------------
function SetFontFormat(a,b)
{
var c=currenteditor;
var d;
if(isIE){d=frames[c];}
else{d=document.getElementById(c).contentWindow;}
var e=parent.command;
if(isIE)
{
var f=d.document.selection;
if(f!=null){var g=f.createRange();g=rng;g.select();}
}
else{d.focus();}
d.document.execCommand(e,false,a);
d.focus();
}
//--------------------------------------------------------------------------------------------------------------
var steditor=0;
document.onmouseup=new Function("dragapproved=false");
function close_insert_pop(){chkVK=0;document.getElementById("insert_pop").style.display="none";}
function NoError(){return(true);}
onerror=NoError;
//--------------------------------------------------------------------------------------------------------------
function WriteHTML(a,b) //常用字按鍵導引到輸入框
{
var c;
if(isIE)
{
c=frames;
c.focus();
var d=c.document.selection;
if(d!=null){rng=d.createRange();}
c.document.execCommand("removeformat",false,"");
rng.pasteHTML("");
rng.pasteHTML(a);
c.focus();
}
else if(isSafari)
{
c=document.getElementById(b).contentWindow;
c.focus();
a=a.replace(/</g,"[HOTAGOPEN]");
a=a.replace(/>/g,"[HOTAGCLOSE]");
c.document.execCommand('insertTEXT',false,a);
var e=document.getElementById(b).contentWindow.document.body.innerHTML;
e=e.replace(/\[HOTAGOPEN\]/g,'<');
e=e.replace(/\[HOTAGCLOSE\]/g,'>');
e=e.replace(/[\n\r]/ig,'');
document.getElementById(b).contentWindow.document.body.innerHTML=e;
c.focus();
}
else
{
c=document.getElementById(b).contentWindow;
c.focus();
c.document.execCommand('insertHTML',false," ");
c.document.execCommand("removeformat",false,"");
c.document.execCommand('insertHTML',false,a);
c.document.execCommand("removeformat",false,"");
c.focus();
}
}
//--------------------------------------------------------------------------------------------------------------
function htmlentities(a)
{
a=a.replace(/</g,'<');
a=a.replace(/>/g,'>');
a=a.replace(/&/g,'&');
return a;
}
//--------------------------------------------------------------------------------------------------------------
function BBCodeToHTML(a)
{
a=a.replace(/&/g,'&');
a=a.replace(/</g,'<');
a=a.replace(/>/g,'>');
a=a.replace(/ /g,' ');
a=a.replace(/\n/g,'<br>');
a=a.replace(/\[color=(.*?)\]/gi,'<font color="$1">');
a=a.replace(/\[\/(font|color)\]/gi,'</font>');
return a;
}
//--------------------------------------------------------------------------------------------------------------
function HTMLToBBCode(a)
{
if(starup=="0")
{
a=a.replace(/<br[^>]*>/gi,'\n');
a=a.replace(/<p([^>]*)>/gi,'<DIV$1>');
a=a.replace(/<\/p([^>]*)>/gi,'</DIV$1>\n');
}
else{a=htmlentities(a);}
var b=a.split("<");
var c=new Array();
var e=0;
if(b.length>1)
{
for(var i=0;i<b.length;i++)
{
if(i>0)b[i]='<'+b[i];
var f=b[i];
if(f.match(/<(font|div)( ([^>]{1,}.*?)){0,1}( {0,1}){0,1}>/i))
{
var g=RegExp.$1;
var h=RegExp.$3;
if(h.toLowerCase().indexOf("style=")!=-1&&h.toLowerCase().indexOf("color:")!=-1&&h.toLowerCase().indexOf("color=")!=-1)
{h=h.replace(/color="(.*?)"/gi,"");}
h=h.replace(/(color=|style=)/ig,"|$1");
h=h.replace(/("|")/g,""); /////// h=h.replace(/('|")/g,"");
h=h.replace(/ \|/g,"|");
var j=h.split("|");
var k=new Array();
if(j!=null)
{
for(var z=0;z<j.length;z++)
{
var l=j[z].split("=");
k[l[0].toLowerCase()]=j[z].replace(l[0].toLowerCase()+"=","");
}
}
var m='';
var g=g.toLowerCase();
if(g=="font")
{
if(j.length>0)
{
for(var r in k)
{
k[r]=k[r].replace(/^ +| +$/g,"");
if(r=="color"){m+='[color='+k['color']+']';}
else if(r=="style"){m+=AnalyzeHTMLBlock(g,k);}
}
}
}
else if(g=="div"||g=="span")
{
if(k['style']){m=AnalyzeHTMLBlock(g,k);}
else if(k['align']){m='['+k['align'].toUpperCase()+']';}
else{m='[HOTEDITOR_NEW_LINE]';}
}
b[i]=f.replace(/(<([^>]+)>)/,m);
if(g!="img"){c[e]=m;e++;}
}
else if(f.match(/<\/(font|div)>/i))
{
e--;
var w=c.pop();
if(w!=null)
{
var x="";
var A=w;
A=A.replace(/=(.*?)\]/g,"]");
A=A.replace(/\]/g,"],");
A=A.replace(/\[(.*?)\]/g,"[/$1]");
var B=A.split(",");
B.reverse();
for(var y=0;y<B.length;y++) {x+=B[y];}
x=x.replace(/\[\/\*\]/gi,"");
b[i]=b[i].replace(/(<([^>]+)>)/,x);
}
else{b[i]=b[i].replace(/(<([^>]+)>)/,"");}
}
}
var C=b.join("");
}
else{var C=a;}
C=C.replace(/</g,'<');
C=C.replace(/>/g,'>');
C=C.replace(/ /g,' ');
C=C.replace(/&/g,'&');
C=C.replace(/ /g,'\t');
C=C.replace(/\[HOTEDITOR_NEW_LINE\]/g,'\n');
C=C.replace(/\[\/HOTEDITOR_NEW_LINE\]\n+/g,'\n');
C=C.replace(/\[\/HOTEDITOR_NEW_LINE\]/g,'\n');
if(starup=="0")
{
C=C.replace(/\[\*\]/gi,'\n');
C=C.replace(/\n\n\[\*\]/gi,'\n');
}
C=C.replace(/\[color=#.\w*\]\[\/color\]/gi,"");
C=C.replace(/^\n+/,"");
C=C.replace(/\n+$/,"");
return C;
}
//--------------------------------------------------------------------------------------------------------------
function bbstyle(bbnumber)
{
if (bbnumber){
bbfontstyle(bbtags[bbnumber], '');
//document.forms[form_name].elements[text_name].focus();
}
return;
}
//--------------------------------------------------------------------------------------------------------------
function bbfontstyle(bbopen, bbclose)
{
theSelection = false;
var textarea = document.forms[form_name].elements[text_name];
textarea.focus();
if ((clientVer >= 4) && is_ie && is_win)
{
theSelection = document.selection.createRange().text;
if (theSelection)
{
document.selection.createRange().text = bbopen + theSelection + bbclose;
document.forms[form_name].elements[text_name].focus();
theSelection = '';
return;
}
}
else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
{
document.forms[form_name].elements[text_name].focus();
theSelection = '';
return;
}
var caret_pos = getCaretPosition(textarea).start;
var new_pos = caret_pos + bbopen.length;
insert_text(bbopen + bbclose);
if (!isNaN(textarea.selectionStart)){textarea.selectionStart = new_pos;textarea.selectionEnd = new_pos;}
else if (document.selection)
{var range = textarea.createTextRange();range.move("character", new_pos);range.select();storeCaret(textarea);}
textarea.focus();
return;
}
//--------------------------------------------------------------------------------------------------------------
function insert_text(text, spaces, popup)
{
text = convert(text);
text = BBCodeToHTML(text);
WriteHTML(text,"editor");
}
//--------------------------------------------------------------------------------------------------------------
function caretPosition(){var start = null;var end = null;}
//--------------------------------------------------------------------------------------------------------------
function getCaretPosition(txtarea)
{
var caretPos = new caretPosition();
if(txtarea.selectionStart || txtarea.selectionStart == 0){caretPos.start = txtarea.selectionStart;caretPos.end = txtarea.selectionEnd;}
else if(document.selection)
{
var range = document.selection.createRange();
var range_all = document.body.createTextRange();
range_all.moveToElementText(txtarea);
var sel_start;
for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++){range_all.moveStart('character', 1);}
txtarea.sel_start = sel_start;caretPos.start = txtarea.sel_start;caretPos.end = txtarea.sel_start;
}
return caretPos;
} |