📄 zpeditor.js
字号:
this.config.field.style.width=paneWidth+'px';this.config.field.style.height=paneHeight+'px';}Zapatec.MinimalEditor.prototype.getPaneSize=function(width,height){var paneWidth=width;var paneHeight=height;if(!this.config.excludeToolbar&&0<this.config.toolbarElements.length){paneHeight-=this.toolbar.offsetHeight;if(paneHeight<50){paneHeight=50;}}if(Zapatec.is_ie){paneWidth-=2;}else{if(Zapatec.is_khtml){paneWidth-=2;}else{if(this.isMaximize){paneWidth-=2;}else{paneWidth+=2;}}}return{width:paneWidth,height:paneHeight};}Zapatec.MinimalEditor.prototype.loadContentFromField=function(){if(this.mode=="WYSIWYG"){this.pane.setContent(this.config.field.value);}else{var html=this.config.field.value;this.setHTML(html);}}Zapatec.MinimalEditor.prototype.saveContentToField=function(){if(this.mode=="WYSIWYG"){this.config.field.value=this.getHTML();}}Zapatec.MinimalEditor.prototype.toggleDesignMode=function(enable){if(Zapatec.is_ie){this.pane.getContentElement().contentEditable=enable;}else{this.pane.getContainer().contentWindow.document.designMode=enable?"on":"off";}}Zapatec.MinimalEditor.prototype.execCommand=function(command,arg1,arg2){var object=null;if(Zapatec.is_ie){object=this.pane.getIframeDocument();}else{object=this.pane.getContainer().contentWindow.document;}return object.execCommand(command,arg1,arg2);}Zapatec.MinimalEditor.prototype.queryCommandValue=function(command){var object=this.pane.getIframeDocument();return object.queryCommandValue(command);}Zapatec.MinimalEditor.prototype.queryCommandState=function(command){var object=this.pane.getIframeDocument();return object.queryCommandState(command);}Zapatec.MinimalEditor.prototype.queryCommandEnabled=function(command){var object=this.pane.getIframeDocument();return object.queryCommandEnabled(command);}Zapatec.MinimalEditor.prototype.focus=function(){this.pane.getContainer().contentWindow.focus();}Zapatec.MinimalEditor.prototype.focusHtml=function(){if(Zapatec.is_ie7){var scrollTop=Zapatec.Utils.getPageScrollY();}this.config.field.focus();if(Zapatec.is_ie7){window.scroll(0,scrollTop);}}Zapatec.MinimalEditor.prototype.addButtons=function(){if(this.config.toolbarElements.length==0){return false;}this.toolbar=document.createElement("table");this.toolbar.id='zpEditor'+this.id+'ToolbarTable';this.container.insertBefore(this.toolbar,this.editorPanel);this.toolbar.border=0;this.toolbar.className=this.getClassName({prefix:"zpEditor",suffix:"Toolbar"});this.toolbar.style.width=this.config.field.clientWidth+'px';var tbody=document.createElement("tbody");this.toolbar.appendChild(tbody);var tr=document.createElement("tr");tbody.appendChild(tr)var td=document.createElement("td");td.setAttribute("nowrap","true");tr.appendChild(td);var span=document.createElement("span");span.className="toolbarPanel";td.appendChild(span)var self=this;for(var ii=0;ii<this.config.toolbarElements.length;ii++){var element=this.config.toolbarElements[ii];if(this.webKitVersion&&this.webKitMap[element]&&this.webKitVersion<this.webKitMap[element]){continue;}switch(element){case"maximize":var tooltip=this.getMessage('maximizeTooltip');var button=this.createButton("maximize",tooltip,null,function(){self.resizeEditor();self.focus();});button.id='zpEditor'+this.id+'Maximize';span.appendChild(button);break;case"fontname":var fontNameSelect=this.createSelect(["Font","Arial","Courier","Times New Roman"],["Font","Arial","Courier","Times New Roman"],function(){self.execCommand("fontname",false,this.options[this.options.selectedIndex].value);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});fontNameSelect.id='zpEditor'+this.id+'FontName';span.appendChild(fontNameSelect);break;case"fontsize":var fontSizeSelect=this.createSelect(["Size","1","2","3","4","5","6","7"],["Size","1","2","3","4","5","6","7"],function(){self.execCommand("fontsize",false,this.options[this.options.selectedIndex].value);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});fontSizeSelect.id='zpEditor'+this.id+'FontSize';span.appendChild(fontSizeSelect);break;case"forecolor":case"backcolor":var button=this.createColorButton(element=="forecolor");span.appendChild(button);break;case"insertlink":var tooltip=this.getMessage('insertLinkTooltip');var button=this.createButton("link",tooltip,function(){self.showInsertLinkWindow();self.focus();});button.id='zpEditor'+this.id+'InsertLink';span.appendChild(button);break;case"insertimage":var tooltip=this.getMessage('insertImageTooltip');var button=this.createButton("image",tooltip,function(){var text=self.getMessage('insertImagePrompt');var imgUrl=prompt(text,"http://");if(imgUrl!=null&&imgUrl!=""){self.execCommand("insertimage",false,imgUrl);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();}});button.id='zpEditor'+this.id+'InsertImage';span.appendChild(button);break;case"inserttable":var tooltip=this.getMessage('insertTableTooltip');var button=this.createButton("table",tooltip,function(){if(Zapatec.is_khtml){self.storeSelection();}var rowsText=this.getMessage('insertTableRowsPrompt');var rows=parseInt(prompt(rowsText,2));var colsText=this.getMessage('insertTableColsPrompt');var cols=parseInt(prompt(colsText,2));var borderWidthText=this.getMessage('insertTableBorderWidthPrompt');var borderWidth=parseInt(prompt(borderWidthText,2));if(isNaN(borderWidth)){borderWidth=1;}if(!isNaN(rows)&&rows>0&&!isNaN(cols)&&cols>0){var tbl=self.pane.getIframeDocument().createElement("table");tbl.setAttribute("border",borderWidth);tbl.setAttribute("cellpadding","1");tbl.setAttribute("cellspacing","1");var tbltbody=self.pane.getIframeDocument().createElement("tbody");for(var kk=0;kk<rows;kk++){var tbltr=self.pane.getIframeDocument().createElement("tr");for(var jj=0;jj<cols;jj++){var tbltd=self.pane.getIframeDocument().createElement("td");tbltd.setAttribute("width",10);var tblbr=self.pane.getIframeDocument().createElement("br");tbltd.appendChild(tblbr);tbltr.appendChild(tbltd);}tbltbody.appendChild(tbltr);}tbl.appendChild(tbltbody);self.insertNodeAtSelection(tbl);}self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'InsertTable';span.appendChild(button);break;case"inserthorizontalrule":var tooltip=this.getMessage('insertHorizontalRule');var button=this.createButton("hr",tooltip,function(){var hr=self.pane.getIframeDocument().createElement("hr");self.insertNodeAtSelection(hr);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'InsertHorizontalRule';span.appendChild(button);break;case"insertspecialchar":var tooltip=this.getMessage('insertSpecialCharacter');var button=this.createButton("insertspecial",tooltip,function(){self.showCharMapWindow();self.focus();});button.id='zpEditor'+this.id+'InsertSpecialChar';span.appendChild(button);break;case"selectall":var tooltip=this.getMessage('selectAllTooltip');var button=this.createButton("selectall",tooltip,function(){self.execCommand("selectall");self.focus();self.updateToolbar();});button.id='zpEditor'+this.id+'SelectAll';span.appendChild(button);break;case"bold":var button=this.createButton("bold","Bold",function(){self.execCommand("bold",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Bold';span.appendChild(button);break;case"italic":var button=this.createButton("italic","Italic",function(){self.execCommand("italic",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Italic';span.appendChild(button);break;case"underline":var tooltip=this.getMessage('underlineTooltip');var button=this.createButton("underline",tooltip,function(){self.execCommand("underline",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Underline';span.appendChild(button);break;case"justifyleft":var tooltip=this.getMessage('justifyLeftTooltip');var button=this.createButton("justifyleft",tooltip,function(){self.execCommand("justifyleft",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'JustifyLeft';span.appendChild(button);break;case"justifycenter":var tooltip=this.getMessage('justifyCenterTooltip');var button=this.createButton("justifycenter",tooltip,function(){self.execCommand("justifycenter",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'JustifyCenter';span.appendChild(button);break;case"justifyright":var tooltip=this.getMessage('justifyRightTooltip');var button=this.createButton("justifyright",tooltip,function(){self.execCommand("justifyright",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'JustifyRight';span.appendChild(button);break;case"justifyfull":var tooltip=this.getMessage('justifyFullTooltip');var button=this.createButton("justifyfull",tooltip,function(){self.execCommand("justifyfull",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'JustifyFull';span.appendChild(button);break;case"insertorderedlist":var tooltip=this.getMessage('orderedListTooltip');var button=this.createButton("orderedlist",tooltip,function(){self.execCommand("insertorderedlist",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'InsertOrderedList';span.appendChild(button);break;case"insertunorderedlist":var tooltip=this.getMessage('unorderedListTooltip');var button=this.createButton("unorderedlist",tooltip,function(){self.execCommand("insertunorderedlist",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'InsertUnorderedList';span.appendChild(button);break;case"copy":var tooltip=this.getMessage('copyTooltip');var button=this.createButton("copy",tooltip,function(){self.execCommand("copy",false,null);self.saveContentToField();self.focus();});button.id='zpEditor'+this.id+'Copy';span.appendChild(button);break;case"cut":var tooltip=this.getMessage('cutTooltip');var button=this.createButton("cut",tooltip,function(){self.execCommand("cut",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Cut';span.appendChild(button);break;case"paste":var tooltip=this.getMessage('pasteTooltip');var button=this.createButton("paste",tooltip,function(){self.execCommand("paste",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Paste';span.appendChild(button);break;case"outdent":var tooltip=this.getMessage('outdentTooltip');var button=this.createButton("outdent",tooltip,function(){self.execCommand("outdent",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Outdent';span.appendChild(button);break;case"indent":var tooltip=this.getMessage('indentTooltip');var button=this.createButton("indent",tooltip,function(){self.execCommand("indent",false,null);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Indent';span.appendChild(button);break;case"undo":var tooltip=this.getMessage('undoTooltip');var button=this.createButton("undo",tooltip,function(){self.undo.undo();self.saveContentToField();self.focus();self.updateToolbar();});button.id='zpEditor'+this.id+'Undo';span.appendChild(button);break;case"redo":var tooltip=this.getMessage('redoTooltip');var button=this.createButton("redo",tooltip,function(){self.undo.redo();self.saveContentToField();self.focus();self.updateToolbar();});button.id='zpEditor'+this.id+'Redo';span.appendChild(button);break;case"about":var tooltip=this.getMessage('aboutTooltip');var button=this.createButton("about",tooltip,null,function(){var aboutText=self.getMessage('aboutText');alert(aboutText);},"?");button.id='zpEditor'+this.id+'About';span.appendChild(button);break;case"fetch":var tooltip=this.getMessage('fetchTooltip');var button=this.createButton("fetch",tooltip,function(){self.undo.saveUndo();self.fetch();self.undo.saveUndo();self.updateToolbar();});button.id='zpEditor'+this.id+'Fetch';span.appendChild(button);break;case"save":var tooltip=this.getMessage('saveTooltip');var button=this.createButton("save",tooltip,function(){self.undo.saveUndo();self.save();self.updateToolbar();});button.id='zpEditor'+this.id+'Save';span.appendChild(button);break;case"browser":var tooltip=this.getMessage('browserTooltip');var button=this.createButton("browser",tooltip,null,function(){var attributes="status=1, width="+self.config.externalBrowserWidth+", height="+self.config.externalBrowserHeight+", resizable=yes";var win=window.open("","previewWindow",attributes);win.document.body.innerHTML=self.getHTML();});button.id='zpEditor'+this.id+'Browser';span.appendChild(button);break;case"switcher":var tooltip=this.getMessage('htmlTooltip');var htmlButton=this.createButton("html",tooltip,null,function(){if(self.mode=="WYSIWYG"){self.switchToHTML();var button=this;setTimeout(function(){htmlButton.style.display='none';htmlButton.zpEditorWYSIWYGButton.style.display='block';},10);self.focusHtml();}});tooltip=this.getMessage('wysiwygTooltip');var wysiwygButton=this.createButton("wysiwyg",tooltip,null,function(){if(self.mode=="HTML"){self.switchToWYSIWYG();setTimeout(function(){wysiwygButton.style.display='none';wysiwygButton.zpEditorHTMLButton.style.display='block';},10);self.focus();}});htmlButton.id='zpEditor'+this.id+'Html';wysiwygButton.id='zpEditor'+this.id+'Wysiwyg';htmlButton.zpEditorWYSIWYGButton=wysiwygButton;wysiwygButton.zpEditorHTMLButton=htmlButton;wysiwygButton.style.display='none';htmlButton.className+=" switch";wysiwygButton.className+=" switch";span.appendChild(htmlButton);span.appendChild(wysiwygButton);break;case"newpanel":if(span.childNodes.length==0){continue;}span=document.createElement("span");span.className="toolbarPanel"td.appendChild(span);break;case"newrow":if(td.childNodes.length==1&&span.childNodes.length==0){continue;}tr=document.createElement("tr");tbody.appendChild(tr);td=document.createElement("td");tr.appendChild(td);span=document.createElement("span");span.className="toolbarPanel";td.appendChild(span);break;default:Zapatec.Log({description:this.getMessage('unknownToolbarElementError',element)})}}}Zapatec.MinimalEditor.prototype.createColorButton=function(isForeground){var colorButton=[];var self=this;var getter,setter,command,id,strClass,tooltip;if(isForeground){getter=function(){return self.config.foreColor;};setter=function(color){self.config.foreColor=color;};command="forecolor";strClass="forecolor";tooltip=this.getMessage('fontColorTooltip');id='zpEditor'+this.id+'ForeColor';}else{getter=function(){return self.config.backColor;};setter=function(color){self.config.backColor=color;};if(Zapatec.is_ie||Zapatec.is_khtml){command="backcolor";}else{command="hilitecolor";}strClass="backcolor";tooltip=this.getMessage('bgColorTooltip');id='zpEditor'+this.id+'BackColor';}var setColor=function(color){colorButton[0].colorDiv.style.background=color;setter(color);if(Zapatec.is_khtml){self.restoreSelection();}self.execCommand(command,false,color);self.saveContentToField();self.focus();self.undo.saveUndo();self.updateToolbar();};colorButton[0]=this.createButton(strClass,tooltip,function(ev){if(Zapatec.is_khtml){self.storeSelection();}var target=Zapatec.Utils.getTargetElement(ev);var isDropAction=target.isDropArrow;var color=getter();var isShowPicker=isDropAction||null==color||""==color;if(isShowPicker){return;}setColor(color);},function(ev){var target=Zapatec.Utils.getTargetElement(ev);var isDropAction=target.isDropArrow;var color=getter();var isShowPicker=isDropAction||null==color||""==color;if(isShowPicker){var show=function(){var colorEventPicker=new Zapatec.ColorPicker({button:colorButton[0],color:color,handleButtonClick:false,eventListeners:{select:setColor}});colorEventPicker.isShown=true;colorEventPicker.show();};setTimeout(show,100);}},null,true,getter());colorButton[0].id=id;return colorButton[0];}Zapatec.MinimalEditor.prototype.insertNodeAtSelection=function(insertNode){if(Zapatec.is_ie){var self=this;setTimeout(function(){var sel=self.pane.getContainer().contentWindow.document.selection;var range=sel.createRange();range.pasteHTML(insertNode.outerHTML);},10);}else{if(Zapatec.is_khtml){var sel;if(this.oldSelection){sel=this.oldSelection;}else{sel=this.pane.getContainer().contentWindow.getSelection();}var range=this.pane.getContainer().contentWindow.document.createRange();var isRangeSet=false;if(sel.baseNode==sel.extentNode&&sel.baseOffset==sel.extentOffset){if(sel.type=="Range"){range.setStartBefore(sel.baseNode);range.setEndAfter(sel.extentNode);isRangeSet=true;}}if(!isRangeSet){range.setStart(sel.baseNode,sel.baseOffset);range.setEnd(sel.extentNode,sel.extentOffset);}}else{var sel=this.pane.getContainer().contentWindow.getSelection();var range=sel.getRangeAt(0);sel.removeAllRanges();}range.deleteContents();var container=range.startContainer;var pos=range.startOffset;var doc=this.pane.getIframeDocument();range=doc.createRange();if(container.nodeType==3&&insertNode.nodeType==3){container.insertData(pos,insertNode.nodeValue);range.setEnd(container,pos+insertNode.length);range.setStart(container,pos+insertNode.length);}else{var afterNode;if(container.nodeType==3){var textNode=container;container=textNode.parentNode;var text=textNode.nodeValue;var textBefore=text.substr(0,pos);var textAfter=text.substr(pos);var beforeNode=doc.createTextNode(textBefore);afterNode=doc.createTextNode(textAfter);container.insertBefore(afterNode,textNode);container.insertBefore(insertNode,afterNode);container.insertBefore(beforeNode,insertNode);container.removeChild(textNode);}else{afterNode=container.childNodes[pos];container.insertBefore(insertNode,afterNode);if(!afterNode){range.setEnd(insertNode,1);range.setStart(insertNode,1);}}if(afterNode){range.setEnd(afterNode,0);range.setStart(afterNode,0);}}sel.addRange(range);}}Zapatec.MinimalEditor.prototype.setHTML=function(html){this.pane.getIframeDocument().body.innerHTML=html;this.config.field.value=html;}Zapatec.MinimalEditor.prototype.getHTML=function(){var html;if(this.mode=="WYSIWYG"){if(this.config.dumpHtml){html=dump.getHTML(this.pane.getIframeDocument().body,false,0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -