?? edit.js
字號:
//功能:網頁編輯器腳本文件
//展亮編寫
SEP_PADDING = 5
HANDLE_PADDING = 7
//htmlload="index.html";
//Composition.document.location=(htmlload);
var yToolbars = new Array();
var YInitialized = false;
var addhead = '<link rel="STYLESHEET" type="text/css" href="../css/main.css">';
function document.onreadystatechange()
{
}
function InitDocument()
{
if (YInitialized) return;
YInitialized = true;
var i, s, curr;
for (i=0; i<document.body.all.length; i++)
{
curr=document.body.all[i];
if (curr.className == "yToolbar")
{
InitTB(curr);
yToolbars[yToolbars.length] = curr;
}
}
DoLayout();
window.onresize = DoLayout;
tmp=addhead+Composition.document.body.innerHTML;
Composition.document.open();
Composition.document.write(tmp);
Composition.document.designMode="On";
Composition.focus();
bLoad=true;
}
function InitBtn(btn)
{
btn.onmouseover = BtnMouseOver;
btn.onmouseout = BtnMouseOut;
btn.onmousedown = BtnMouseDown;
btn.onmouseup = BtnMouseUp;
btn.ondragstart = YCancelEvent;
btn.onselectstart = YCancelEvent;
btn.onselect = YCancelEvent;
btn.YUSERONCLICK = btn.onclick;
btn.onclick = YCancelEvent;
btn.YINITIALIZED = true;
return true;
}
function InitTB(y)
{
y.TBWidth = 0;
if (! PopulateTB(y)) return false;
y.style.posWidth = y.TBWidth;
return true;
}
function YCancelEvent()
{
event.returnValue=false;
event.cancelBubble=true;
return false;
}
function BtnMouseOver()
{
if (event.srcElement.tagName != "IMG") return false;
var image = event.srcElement;
var element = image.parentElement;
if (image.className == "Ico") element.className = "BtnMouseOverUp";
else if (image.className == "IcoDownPressed") element.className = "BtnMouseOverDown";
event.cancelBubble = true;
}
function BtnMouseOut()
{
if (event.srcElement.tagName != "IMG") {
event.cancelBubble = true;
return false;
}
var image = event.srcElement;
var element = image.parentElement;
yRaisedElement = null;
element.className = "Btn";
image.className = "Ico";
event.cancelBubble = true;
}
function BtnMouseDown()
{
Composition.focus();
if (event.srcElement.tagName != "IMG") {
event.cancelBubble = true;
event.returnValue=false;
return false;
}
var image = event.srcElement;
var element = image.parentElement;
element.className = "BtnMouseOverDown";
image.className = "IcoDownPressed";
event.cancelBubble = true;
event.returnValue=false;
return false;
}
function BtnMouseUp()
{
if (event.srcElement.tagName != "IMG") {
event.cancelBubble = true;
return false;
}
var image = event.srcElement;
var element = image.parentElement;
element.className = "BtnMouseOverUp";
image.className = "Ico";
if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");
event.cancelBubble = true;
return false;
}
function PopulateTB(y)
{
var i, elements, element;
elements = y.children;
for (i=0; i<elements.length; i++) {
element = elements[i];
if (element.tagName == "SCRIPT" || element.tagName == "!") continue;
switch (element.className) {
case "Btn":
if (element.YINITIALIZED == null) {
if (! InitBtn(element))
return false;
}
element.style.posLeft = y.TBWidth;
y.TBWidth += element.offsetWidth + 1;
break;
case "TBGen":
element.style.posLeft = y.TBWidth;
y.TBWidth += element.offsetWidth + 1;
break;
case "TBSep":
element.style.posLeft = y.TBWidth + 2;
y.TBWidth += SEP_PADDING;
break;
case "TBHandle":
element.style.posLeft = 2;
y.TBWidth += element.offsetWidth + HANDLE_PADDING;
break;
default:
return false;
}
}
y.TBWidth += 1;
return true;
}
function DebugObject(obj)
{
var msg = "";
for (var i in TB) {
ans=prompt(i+"="+TB[i]+"\n");
if (! ans) break;
}
}
function LayoutTBs()
{
NumTBs = yToolbars.length;
if (NumTBs == 0) return;
var i;
var ScrWid = (document.body.clientWidth);
var TotalLen = ScrWid;
for (i = 0 ; i < NumTBs ; i++) {
TB = yToolbars[i];
if (TB.TBWidth > TotalLen) TotalLen = TB.TBWidth;
}
var PrevTB;
var LastStart = 0;
var RelTop = 0;
var LastWid, CurrWid;
var TB = yToolbars[0];
TB.style.posTop = 0;
TB.style.posLeft = 0;
var Start = TB.TBWidth;
for (i = 1 ; i < yToolbars.length ; i++) {
PrevTB = TB;
TB = yToolbars[i];
CurrWid = TB.TBWidth;
if ((Start + CurrWid) > ScrWid) {
Start = 0;
LastWid = TotalLen - LastStart;
}
else {
LastWid = PrevTB.TBWidth;
RelTop -= TB.offsetHeight;
}
TB.style.posTop = RelTop;
TB.style.posLeft = Start;
PrevTB.style.width = LastWid;
LastStart = Start;
Start += CurrWid;
}
TB.style.width = TotalLen - LastStart
i--;
TB = yToolbars[i];
var TBInd = TB.sourceIndex;
var A = TB.document.all;
var item;
for (i in A) {
item = A.item(i);
if (! item) continue;
if (! item.style) continue;
if (item.sourceIndex <= TBInd) continue;
if (item.style.position == "absolute") continue;
item.style.posTop = RelTop;
}
}
function fixEditorSize()
{
document.all.Composition.style.height=Math.max(document.body.clientHeight-document.all.Composition.offsetTop-20,0);
document.all.switchDiv.style.top=document.all.Composition.offsetTop+document.all.Composition.offsetHeight-1;
document.all.BorderMode.style.top=document.all.Composition.offsetTop+document.all.Composition.offsetHeight-1;
}
function DoLayout()
{
LayoutTBs();
fixEditorSize();
}
function validateMode()
{
if (! bTextMode) return true;
alert("“查看HTML源代碼”或“預覽”時不能使用該功能!\n\n <請在“普通”編輯中執行該功能!>");
Composition.focus();
return false;
}
function format1(what,opt)
{
Composition.focus();
if (opt=="removeFormat")
{
what=opt;
opt=null;
}
if (opt==null) Composition.document.execCommand(what);
else Composition.document.execCommand(what,"",opt);
pureText = false;
Composition.focus();
}
function format(what,opt)
{
if (!validateMode()) return;
format1(what,opt);
}
function NoBorder(mod)
{
tmp=addhead+Composition.document.body.innerHTML;
if (SeleckBorder.checked && mod){
tmp="<STYLE> TD {border: 1 dotted #000000} </STYLE>"+tmp;
}
Composition.document.open();
Composition.document.write(tmp);
Composition.document.close();
document.all.SeleckBorder.disabled=!mod;
document.all.formatselect.disabled=!mod;
document.all.fontselect.disabled=!mod;
document.all.sizeselect.disabled=!mod;
document.all.specialtype.disabled=!mod;
Composition.focus();
}
var codeStyle="font-family: courier new; font-size: 12px;";
var elementCodeStyle="color: navy;"
var endTags=new Array("P","DIV","H1","H2","H3","H4","H5","H6","BLOCKQUOTE","OL","LI","PRE","UL","TITLE","STYLE","META","HEAD","BODY","TR","TD")
var startTags=new Array("BR","HR","META","HTML","BODY","HEAD","TR","TABLE","TBODY")
function showmodmenu(sid)
{
modmenu0.style.display="none";
modmenu1.style.display="none";
modmenu2.style.display="none";
eval("modmenu" + sid + ".style.display=\"block\";");
}
function formatCode(s){
var str="";var IN_TEXT=1;var IN_ELEMENT=2;var state=IN_TEXT;
while(s.length>0){
var endTagBreak,emptyTagBreak;
if(state==IN_ELEMENT){
var endIndex=s.indexOf(">");var endTag=(s.substring(0,1)=="/");
if(endIndex!= -1){str+=s.substring(0,endIndex)+"></span>";
if(endTag&&endTagBreak||emptyTagBreak) str+="<br>";
s=s.substring(endIndex+1,s.length);state=IN_TEXT;}
else{str+=s+"</span>";s="";}}else{var startIndex=s.indexOf("<");
if(startIndex!= -1){
var tagName;var nameStartIndex;var gtIndex=s.indexOf(">");var spaceIndex=s.indexOf(" ");var slashIndex=s.indexOf("/");
endTagBreak=false;
emptyTagBreak=false;
if(slashIndex!= -1&&slashIndex==startIndex+1) nameStartIndex=slashIndex+1;
else nameStartIndex=startIndex+1;
if(spaceIndex!= -1&&spaceIndex>startIndex&&spaceIndex<gtIndex) tagName=s.substring(nameStartIndex,spaceIndex);
else if(gtIndex!= -1) tagName=s.substring(nameStartIndex,gtIndex);
else tagName=s.substring(nameStartIndex,s.length);
for(var i=0;i<endTags.length;i++){
if(endTags[i]==tagName){endTagBreak=true;break;}
}
for(var i=0;i<startTags.length;i++){
if(startTags[i]==tagName){emptyTagBreak=true;break;}
}
str+=s.substring(0,startIndex)+"<span style='"+elementCodeStyle+"'><";
s=s.substring(startIndex+1,s.length);state=IN_ELEMENT;
}
else{str+=s;s="";}
}
}
return str;
}
function formatHTMLCode(str){
return"<div style='"+codeStyle+"'>"+formatCode(str.replace(/&/g,"&"))+"</div>";
}
function setMode(newMode)
{
bTextMode = newMode;
var cont;
if (bTextMode!=2 && aTextMode==2) {
Composition.document.open();
Composition.document.write(thishtml);
Composition.document.close();
}
if (bTextMode==0) {
if (aTextMode==1) {
tmp=""+addhead+Composition.document.body.innerText+"";
Composition.document.open();
Composition.document.write(tmp);
Composition.document.close();
}
if (aTextMode!=0) NoBorder(1);
if (Composition.document.designMode!="On") Composition.document.designMode="On";
Composition.focus();
showmodmenu(0);
}
if (bTextMode==1) {
if (aTextMode!=1) {
NoBorder(0);
tmp=""+Composition.document.body.innerHTML+"";
Composition.document.open();
Composition.document.write(formatHTMLCode(tmp));
Composition.document.close();
}
if (Composition.document.designMode!="On") Composition.document.designMode="On";
Composition.focus();
showmodmenu(1);
}
if (bTextMode==2 && aTextMode!=2) {
NoBorder(0);
if (aTextMode==1) {
tmp=""+addhead+Composition.document.body.innerText+"";
Composition.document.open();
Composition.document.write(tmp);
Composition.document.close();
}
thishtml=""+Composition.document.body.innerHTML+"";
Composition.document.designMode='Off';
showmodmenu(2);
}
aTextMode=bTextMode;
}
function getEl(sTag,start)
{
while ((start!=null) && (start.tagName!=sTag)) start = start.parentElement;
return start;
}
function UserDialog(what)
{
if (!validateMode()) return;
Composition.document.execCommand(what, true);
pureText = false;
Composition.focus();
}
function InsertImage()
{
if (!validateMode()) return;
window.open("../inc/insertpic.asp","insertimage","width=400,height=180,top=120");
}
function InsertWord()
{
if (!validateMode()) return;
window.open("../inc/insertword.asp","insertword","width=400,height=120,top=120");
}
function foreColor()
{
if (! validateMode()) return;
var arr = showModalDialog("../inc/selcolor.html", "", "dialogWidth:18.5em; dialogHeight:15.5em; center: Yes; scroll:No; resizable: No; status: No; help: No;");
if (arr != null) format('forecolor', arr);
else Composition.focus();
}
function fortable()
{
if (bTextMode==2) if (!validateMode()) return;
var arr = showModalDialog("../inc/table.html", "", "dialogWidth:27.5em; dialogHeight:13.5em; center: Yes; scroll:No; resizable: No; status: No; help: No;");
if (arr != null){
var ss;
ss=arr.split("*")
row=ss[0];
col=ss[1];
tablemod=ss[2];
if(row&&col){
var string;
string="<TABLE"+tablemod+">";
for(i=1;i<=row;i++){
string=string+"<TR>";
for(j=1;j<=col;j++){
string=string+"<TD></TD>";
}
string=string+"</TR>";
}
string=string+"</TABLE>";
if (bTextMode==1) string=formatCode(string);
if (bIsIE) selectRange();
if (RangeType == "None"){
strHTML = string;
edit.pasteHTML(strHTML);
}
}
}
else Composition.focus();
}
function getPureHtml()
{
var str = "";
var paras = Composition.document.body.all.tags("P");
if (paras.length > 0) {
for (var i=paras.length-1; i >= 0; i--) str = paras[i].innerHTML + "\n" + str;
} else {
str = Composition.document.body.innerHTML;
}
return str;
}
var bLoad=false
var pureText=true
var aTextMode=0
var bTextMode=0
var thishtml=""
public_description=new Editor
function Editor()
{
this.put_HtmlMode=setMode;
this.put_value=putText;
this.get_value=getText;
}
function getText()
{
if (bTextMode)
return Composition.document.body.innerText;
else
{
return Composition.document.body.innerHTML;
}
}
function putText(v)
{
if (bTextMode)
Composition.document.body.innerText = v;
else
Composition.document.body.innerHTML = v;
}
function doSelectClick(str, el) {
var Index = el.selectedIndex;
if (Index != 0){
el.selectedIndex = 0;
if (el.id == "specialtype")
specialtype(el.options[Index].value);
else
format(str,el.options[Index].value);
}
}
var bIsIE5 = navigator.userAgent.indexOf("IE 5") > -1;
var bIsIE6 = navigator.userAgent.indexOf("IE 6") > -1;
var bIsIE = bIsIE5||bIsIE6;
var edit;
var RangeType;
function specialtype(Mark){
Composition.focus();
var strHTML;
if (bIsIE) selectRange();
if (RangeType == "Text"){
strHTML = "<" + Mark + ">" + edit.text + "</" + Mark + ">";
edit.pasteHTML(strHTML);
Composition.focus();
edit.select();
}
}
function selectRange(){
edit = Composition.document.selection.createRange();
RangeType = Composition.document.selection.type;
}
function rCode(s,a,b){
var r = new RegExp(a,"gi");
return s.replace(r,b);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -