當(dāng)前位置:軟件學(xué)堂 > 資訊首頁 > 網(wǎng)絡(luò)編程 > 編程其他 > 文本編輯器代碼怎么寫

文本編輯器代碼怎么寫

2012/10/22 19:46:48作者:佚名來源:網(wǎng)絡(luò)

移動(dòng)端

【實(shí)例名稱】

文本編輯器

【實(shí)例描述】

 在網(wǎng)站提供的郵箱中,經(jīng)常會(huì)使用文本編輯器,以實(shí)現(xiàn)文本內(nèi)容的層次感和美觀性。本例將制作一個(gè)簡單的文本編輯器,主要學(xué)習(xí)實(shí)現(xiàn)文本編輯器的原理。

【實(shí)例代碼】

    <script language="javascript"> var edit;       //當(dāng)前選擇的文本編輯區(qū)域?qū)ο? var RangeType;  //對(duì)象類別 function start() //開始初始化編輯器-編輯區(qū)域是Iframe {  Editor.document.designMode="ON";  Editor.document.open();  Editor.document.write(myTextArea.value);  Editor.document.close();     fnInit() } function setFocus() {    Editor.focus();  //編輯器或去焦點(diǎn) } function selectRange(){  edit = Editor.document.selection.createRange();  //編輯器的文本選擇區(qū)域  RangeType = Editor.document.selection.type; } //包裝文本選定區(qū)域的執(zhí)行命令 function execCommand(command,para) {    setFocus();         selectRange();      if (para=="")   //沒有參數(shù)的情況         edit.execCommand(command)     else     edit.execCommand(command, false, arguments[1]);   Editor.focus();   if (RangeType != "Control") edit.select(); } //獲取或設(shè)置文本的格式-字體、字號(hào) function doSelectC(str,el) {

   var Index = el.selectedIndex;   if (Index != 0)   { el.selectedIndex = 0;   execCommand(str,el.options[Index].text);     } } //獲取或設(shè)置當(dāng)前選定塊的格式化標(biāo)簽 function doSelectCl(str,el) {    var Index = el.selectedIndex;   if (Index != 0)   { el.selectedIndex = 0;   execCommand(str,"<"+el.options[Index].value+">");   } } //初始化 function fnInit(){       for (i=0; i<document.all.length; i++)             document.all(i).unselectable = "off";  //指定不選中任何元素       getSystemFonts(); } //獲取系統(tǒng)字體的方法 function getSystemFonts() {  var a=dlgHelper.fonts.count; var fArray = new Array(); var oOption = document.createElement("OPTION"); oOption.text = "字體"; oOption.value = "0"; selectFontName.add(oOption); //使用DOM方法createElement將字體依次添加到復(fù)選列表中 for (i = 1;i < dlgHelper.fonts.count;i++)  {   fArray[i] = dlgHelper.fonts(i);   var oOption = document.createElement("OPTION");   oOption.text = fArray[i];   oOption.Value = i;   selectFontName.add(oOption);   } } //格式化,保全script、textarea、xmp、pre和style內(nèi)容 function formatfor(va) {   var t=va.replace(/\r/g,'');   t = t.replace(/(<(script|textarea|xmp|pre|style).*?>)([^\r]*?)(<\/\2>)/img, function (){return arguments[1]+arguments[3].replace(/\n/g, "\r")+arguments[4]})   t = t.replace(/\n/g, "");   return t } function fontsize(el) //改變字體的方法 {var Index=el.selectedIndex var addpre="<font size="+el.options[Index].value+">" if(Index>7)addpre="<font style='font-size:"+el.options[Index].value+"pt'>" var oSel = Editor.document.selection.createRange() var sBookmark = oSel.getBookmark() var oSelhtml=oSel.htmlText if(oSelhtml!="") { //定位選中內(nèi)容 var conts=oSelhtml var textLength = Editor.document.body.innerText.length oSel.moveStart("character", -1*textLength) var contp=formatfor(oSel.htmlText) var conta=formatfor(Editor.document.body.innerHTML) var contpa='' var partC="" var partB="" var partA="" var m=0 m=conta.indexOf(contp.substr(0,3)) var f=contp.length for(;f>0;f--) {if(conta.substr(m,f)==contp.substr(0, f)){contpa=contp.substr(0,f);partC=conta.substr(m+f);break}} var ko=contp.substr(f) var kol=ko.length var ty=conta.substr(m+f,kol) var hu="" for(var b=1;b<kol;b++)if(ko.substr(b)==ty.substr(0,kol-b)){hu=ko.substr(b);contpa+=hu;partC=partC.substr(kol-b);break} var k=contpa.length cont=conts.replace(/\n/g, "") var u=cont.length if(cont==contpa.substr(k-u)){partB=cont;partA=contpa.substr(0,k-u)}else{ for(u=cont.length;u>0;u--) {if(cont.lastIndexOf(contpa.substr(k-u))!=-1){partB0=contpa.substr(k-u);partA0=contpa.substr(0,k-u);break}} contt=formatfor(conts) if(hu!="")if(contt.substr(contt.length-kol)==ko)contt=contt.substr(0,contt.length-kol)+hu u=contt.length var youm=contpa.lastIndexOf(contt) if(youm!=-1){partB=contt;partA=contpa.substr(0,youm);partC=contpa.substr(youm+u)+partC}else{ for(;u>0;u--){if(contt.lastIndexOf(contpa.substr(k-u))!=-1){partB1=contpa.substr(k-u);partA1=contpa.substr(0,k-u);break}} if(partB1.length>partB0.length){partB=partB1;partA=partA1}else{partB=partB0;partA=partA0}  } } if(partB.substr(partB.length-1)=="<"){partB=partB.substr(0,partB.length-1);partC="<"+partC} if(partB.substr(partB.length-2)=="</"){partB=partB.substr(0,partB.length-2);partC="</"+partC} //保護(hù)textarea、xmp、script和style的內(nèi)容不被改變 var cook=[] partA=partA.replace(/(<(script|textarea|xmp|style).*?>)[\s\S]*?<\/\2>/ig, function (){co=cook.length cook[co]=arguments[0];return "<cook"+co+">"}) var ook="" partA=partA.replace(/(<(script|textarea|xmp|style).*?>)[\s\S]*?$/i, function (){co=cook.length ook=arguments[2] cook[co]=arguments[0];return "<cook"+co+">"}) if(ook!=""){fd="(^[\\s\\S]*?<\/"+ook+">)" jk=new RegExp(fd,["i"]) if(jk.test(partB)){jk.exec(partB) co=cook.length cook[co]=RegExp.$1 partB=partB.replace(jk,"<cook"+co+">")}} partB=partB.replace(/(<(script|textarea|xmp|style).*?>)[\s\S]*?<\/\2>/ig, function (){co=cook.length cook[co]=arguments[0];return "<cook"+co+">"}) ook="" partB=partB.replace(/(<(script|textarea|xmp|style).*?>)[\s\S]*?$/i, function (){co=cook.length ook=arguments[2] cook[co]=arguments[0];return "<cook"+co+">"}) if(ook!=""){fd="(^[\\s\\S]*?<\/"+ook+">)" jk=new RegExp(fd,["i"]) if(jk.test(partC)){jk.exec(partC) co=cook.length cook[co]=RegExp.$1 partC=partC.replace(jk,"<cook"+co+">")}} partC=partC.replace(/(<(script|textarea|xmp|style).*?>)[\s\S]*?<\/\2>/ig, function (){co=cook.length cook[co]=arguments[0];return "<cook"+co+">"})

//處理字體 var dol=[] var dos=[] var lon=[] fd="<FONT([^>]*?)>" jk=new RegExp(fd,["im"]) while(jk.test(partB)){ce=dol.length jk.exec(partB) dol[ce]=RegExp.$1 partB=partB.replace(jk,"<site"+ce+">") } ce=dol.length-1 for(;ce>-1;ce--) {kjc="<site"+ce+">" fd=kjc+'(.*?)<\/font>' jk=new RegExp(fd,["im"]) if(jk.test(partB)){dos[dos.length]=ce jk.exec(partB) ko3=kjc+RegExp.$1+"</site"+ce+">" partB=partB.replace(jk,ko3) }else{lon[lon.length]=ce} } partB=partB.replace(/<\/font>/img,"<lonf>") for(var c=dos.length-1;c>-1;c--) { uts=dol[dos[c]] if(""==(uts.replace(/size=[0-7+]+/i,"").replace(/style=("|')FONT-SIZE: [0-9.]+.*;*("|')/im,"").replace(/[\s\n\r]+/mg,""))){partB=partB.replace("<site"+dos[c]+">","") partB=partB.replace("</site"+dos[c]+">","")}else{partB=partB.replace("<site"+dos[c]+">","<font"+uts.replace(/ size=[0-7+]+/im,"").replace(/FONT-SIZE: [0-9.]+pt/im,"").replace(/ style=("|');*("|')/im,"")+">") partB=partB.replace("</site"+dos[c]+">","</font>")} } //處理其他標(biāo)簽 var addend="" var mio=[] partB=partB.replace(/<([^<> ]*?) [^<>]*?style=[^<>]*?FONT-SIZE: [0-9.]+[^<>]*?>/img, function (){notv="|select|input|option|object|" if(notv.indexOf("|"+arguments[1].toLowerCase()+"|")==-1){ op=mio.length mio[op]=arguments[0] return "<opis"+op+">"}else{return arguments[0]}}) kba=["h1","h2","h3","h4","h5","h6","pre","button","listing","big","small","tt","code","kbd","samp","td","\/td","caption","\/caption","th","\/th","tr","\/tr","table","\/table","thead","\/thead","tbody","\/tbody","tfoot","\/tfoot"] for(b in kba){ fd="<("+kba[b]+")[^<>]*?>" jk=new RegExp(fd,["img"]) partB=partB.replace(jk, function (){op=mio.length mio[op]=arguments[0] return "<opis"+op+">"}) } //收尾工作 liming=[] partB=partB.replace(/<(opis|site|lonf)([0-9]*)>/g, function(){var op=liming.length if(arguments[1]=="opis"){liming[op]=mio[parseInt(arguments[2])]} if(arguments[1]=="site"){liming[op]="<font"+dol[parseInt(arguments[2])]+">"} if(arguments[1]=="lonf"){liming[op]="</font>"} return "<duan"+op+">" }) if(liming.length>0){ partB=partB.replace(/^(.+?)(<duan0>)/m,function(){if(""!=arguments[1]){return addpre+arguments[1]+"</font>"+arguments[2]} else{return arguments[0]}}) var op=liming.length-1 for(var kc=0;kc<op;kc++){ fd="(<duan"+kc+">)(.+?)(<duan"+(kc+1)+">)" jk=new RegExp(fd,["m"]) partB=partB.replace(jk, function(){if(""!=arguments[2]){return arguments[1]+addpre+arguments[2]+"</font>"+arguments[3]} else{return arguments[0]}})} fd="(<duan"+op+">)(.+?)$" jk=new RegExp(fd,["m"]) partB=partB.replace(jk,function(){if(""!=arguments[2]){return arguments[1]+addpre+arguments[2]+"</font>"} else{return arguments[0]}}) } else{partB=addpre+partB+"</font>" } partB=partB.replace(/<duan([0-9]+)>/g,function(){return liming[parseInt(arguments[1])]}) var endtemp=partA+partB+partC for(vof in cook)endtemp=endtemp.replace("<cook"+vof+">",cook[vof]) Editor.document.body.innerHTML=endtemp var vrvd=Editor.document.selection.createRange() vrvd.moveToBookmark(sBookmark) vrvd.select() } else{ Editor.document.selection.createRange().pasteHTML(addpre+"</font>") }Editor.focus() el.blur() el.selectedIndex=0} </script>

 

【運(yùn)行效果】

運(yùn)行效果

【難點(diǎn)剖析】

本例的重點(diǎn)是字體的獲取,以及執(zhí)行命令“execCommand”。字體獲取使用的是方法getSystemFonts”,方法中的“dlgHelpel"”對(duì)象必須在頁面中注冊(cè),本例中注冊(cè)的代碼是“OBJECT id=dlgHelper CLASSID=“clsid:3050P819-98b5-11cf-bb82—00aa00bdceob”width=“Opx”height=“0px”></OBJECT>”。ExecCommand是文本編輯器中的一個(gè)重要方法,主要用于樣式的變更。

【源碼下載】

本實(shí)例JS代碼下載