📄 htmlarea.js
字号:
for (var i in editor.plugins) { var plugin = editor.plugins[i].instance; if (typeof plugin.onKeyPress == "function") plugin.onKeyPress(ev); } } if (keyEvent && ev.ctrlKey) { var sel = null; var range = null; var key = String.fromCharCode(HTMLArea.is_ie ? ev.keyCode : ev.charCode).toLowerCase(); var cmd = null; var value = null; switch (key) { case 'a': if (!HTMLArea.is_ie) { // KEY select all sel = this._getSelection(); sel.removeAllRanges(); range = this._createRange(); range.selectNodeContents(this._doc.body); sel.addRange(range); HTMLArea._stopEvent(ev); } break; // simple key commands follow case 'b': cmd = "bold"; break; case 'i': cmd = "italic"; break; case 'u': cmd = "underline"; break; case 's': cmd = "strikethrough"; break; case 'l': cmd = "justifyleft"; break; case 'e': cmd = "justifycenter"; break; case 'r': cmd = "justifyright"; break; case 'j': cmd = "justifyfull"; break; case 'z': cmd = "undo"; break; case 'y': cmd = "redo"; break; case 'v': cmd = "paste"; break; case '0': cmd = "killword"; break; // headings case '1': case '2': case '3': case '4': case '5': case '6': cmd = "formatblock"; value = "h" + key; if (HTMLArea.is_ie) { value = "<" + value + ">"; } break; } if (cmd) { // execute simple command this.execCommand(cmd, false, value); HTMLArea._stopEvent(ev); } } /* else if (keyEvent) { // other keys here switch (ev.keyCode) { case 13: // KEY enter // if (HTMLArea.is_ie) { this.insertHTML("<br />"); HTMLArea._stopEvent(ev); // } break; } } */ // update the toolbar state after some time if (editor._timerToolbar) { clearTimeout(editor._timerToolbar); } editor._timerToolbar = setTimeout(function() { editor.updateToolbar(); editor._timerToolbar = null; }, 50);};// retrieve the HTMLHTMLArea.prototype.getHTML = function() { switch (this._editMode) { case "wysiwyg" : if (!this.config.fullPage) { return HTMLArea.getHTML(this._doc.body, false, this); } else return this.doctype + "\n" + HTMLArea.getHTML(this._doc.documentElement, true, this); case "textmode" : return this._textArea.value; default : alert("Mode <" + mode + "> not defined!"); } return false;};// retrieve the HTML (fastest version, but uses innerHTML)HTMLArea.prototype.getInnerHTML = function() { switch (this._editMode) { case "wysiwyg" : if (!this.config.fullPage) return this._doc.body.innerHTML; else return this.doctype + "\n" + this._doc.documentElement.innerHTML; case "textmode" : return this._textArea.value; default : alert("Mode <" + mode + "> not defined!"); } return false;};// completely change the HTML insideHTMLArea.prototype.setHTML = function(html) { switch (this._editMode) { case "wysiwyg" : if (!this.config.fullPage) this._doc.body.innerHTML = html; else // this._doc.documentElement.innerHTML = html; this._doc.body.innerHTML = html; break; case "textmode" : this._textArea.value = html; break; default : alert("Mode <" + mode + "> not defined!"); } return false;};// sets the given doctype (useful when config.fullPage is true)HTMLArea.prototype.setDoctype = function(doctype) { this.doctype = doctype;};/*************************************************** * Category: UTILITY FUNCTIONS ***************************************************/// browser identificationHTMLArea.agt = navigator.userAgent.toLowerCase();HTMLArea.is_ie = ((HTMLArea.agt.indexOf("msie") != -1) && (HTMLArea.agt.indexOf("opera") == -1));HTMLArea.is_opera = (HTMLArea.agt.indexOf("opera") != -1);HTMLArea.is_mac = (HTMLArea.agt.indexOf("mac") != -1);HTMLArea.is_mac_ie = (HTMLArea.is_ie && HTMLArea.is_mac);HTMLArea.is_win_ie = (HTMLArea.is_ie && !HTMLArea.is_mac);HTMLArea.is_gecko = (navigator.product == "Gecko");// variable used to pass the object to the popup editor window.HTMLArea._object = null;// function that returns a clone of the given objectHTMLArea.cloneObject = function(obj) { var newObj = new Object; // check for array objects if (obj.constructor.toString().indexOf("function Array(") == 1) { newObj = obj.constructor(); } // check for function objects (as usual, IE is fucked up) if (obj.constructor.toString().indexOf("function Function(") == 1) { newObj = obj; // just copy reference to it } else for (var n in obj) { var node = obj[n]; if (typeof node == 'object') { newObj[n] = HTMLArea.cloneObject(node); } else { newObj[n] = node; } } return newObj;};// FIXME!!! this should return false for IE < 5.5HTMLArea.checkSupportedBrowser = function() { if (HTMLArea.is_gecko) { if (navigator.productSub < 20021201) { alert("You need at least Mozilla-1.3 Alpha.\n" + "Sorry, your Gecko is not supported."); return false; } if (navigator.productSub < 20030210) { alert("Mozilla < 1.3 Beta is not supported!\n" + "I'll try, though, but it might not work."); } } return HTMLArea.is_gecko || HTMLArea.is_ie;};// selection & ranges// returns the current selection objectHTMLArea.prototype._getSelection = function() { if (HTMLArea.is_ie) { return this._doc.selection; } else { return this._iframe.contentWindow.getSelection(); }};// returns a range for the current selectionHTMLArea.prototype._createRange = function(sel) { if (HTMLArea.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(); } }};// event handlingHTMLArea._addEvent = function(el, evname, func) { if (HTMLArea.is_ie) { el.attachEvent("on" + evname, func); } else { el.addEventListener(evname, func, true); }};HTMLArea._addEvents = function(el, evs, func) { for (var i in evs) { HTMLArea._addEvent(el, evs[i], func); }};HTMLArea._removeEvent = function(el, evname, func) { if (HTMLArea.is_ie) { el.detachEvent("on" + evname, func); } else { el.removeEventListener(evname, func, true); }};HTMLArea._removeEvents = function(el, evs, func) { for (var i in evs) { HTMLArea._removeEvent(el, evs[i], func); }};HTMLArea._stopEvent = function(ev) { if (HTMLArea.is_ie) { ev.cancelBubble = true; ev.returnValue = false; } else { ev.preventDefault(); ev.stopPropagation(); }};HTMLArea._removeClass = function(el, className) { if (!(el && el.className)) { return; } var cls = el.className.split(" "); var ar = new Array(); for (var i = cls.length; i > 0;) { if (cls[--i] != className) { ar[ar.length] = cls[i]; } } el.className = ar.join(" ");};HTMLArea._addClass = function(el, className) { // remove the class first, if already there HTMLArea._removeClass(el, className); el.className += " " + className;};HTMLArea._hasClass = function(el, className) { if (!(el && el.className)) { return false; } var cls = el.className.split(" "); for (var i = cls.length; i > 0;) { if (cls[--i] == className) { return true; } } return false;};HTMLArea.isBlockElement = function(el) { var blockTags = " body form textarea fieldset ul ol dl li div " + "p h1 h2 h3 h4 h5 h6 quote pre table thead " + "tbody tfoot tr td iframe address "; return (blockTags.indexOf(" " + el.tagName.toLowerCase() + " ") != -1);};HTMLArea.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);};// performs HTML encoding of some given stringHTMLArea.htmlEncode = function(str) { // we don't need regexp for that, but.. so be it for now. str = str.replace(/&/ig, "&"); str = str.replace(/</ig, "<"); str = str.replace(/>/ig, ">"); str = str.replace(/\x22/ig, """); // \x22 means '"' -- we use hex reprezentation so that we don't disturb // JS compressors (well, at least mine fails.. ;) return str;};// Retrieves the HTML code from the given node. This is a replacement for// getting innerHTML, using standard DOM calls.HTMLArea.getHTML = function(root, outputRoot, editor) { var html = ""; switch (root.nodeType) { case 1: // Node.ELEMENT_NODE case 11: // Node.DOCUMENT_FRAGMENT_NODE var closed; var i; var root_tag = (root.nodeType == 1) ? root.tagName.toLowerCase() : ''; if (HTMLArea.is_ie && root_tag == "head") { if (outputRoot) html += "<head>"; // lowercasize var save_multiline = RegExp.multiline; RegExp.multiline = true; var txt = root.innerHTML.replace(HTMLArea.RE_tagName, 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() || HTMLArea.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)) { // avoid certain attributes continue; } var value; if (name != "style") { // IE5.5 reports 25 when cellSpacing is // 1; other values might be doomed too. // For this reason we extract the // values directly from the root node. // I'm starting to HATE JavaScript // development. Browser differences // suck. // // Using Gecko the values of href and src are converted to absolute links // unless we get them using nodeValue() if (typeof root[a.nodeName] != "undefined" && name != "href" && name != "src") { value = root[a.nodeName]; } else { value = a.nodeValue; // IE seems not willing to return the original values - it converts to absolute // links using a.nodeValue, a.value, a.stringValue, root.getAttribute("href") // So we have to strip the baseurl manually -/ if (HTMLArea.is_ie && (name == "href" || name == "src")) { value = editor.stripBaseURL(value); } } } else { // IE fails to put style in attributes list // FIXME: cssText reported by IE is UPPERCASE value = root.style.cssText; } if (/(_moz|^$)/.test(value)) { // Mozilla reports some special tags // here; we don't need them. continue; } html += " " + name + '="' + value + '"'; } html += closed ? " />" : ">"; } for (i = root.firstChild; i; i = i.nextSibling) { html += HTMLArea.getHTML(i, true, editor); } if (outputRoot && !closed) { html += "</" + root.tagName.toLowerCase() + ">"; } break; case 3: // Node.TEXT_NODE // If a text node is alone in an element and all spaces, replace it with an non breaking one // This partially undoes the damage done by moz, which translates ' 's into spaces in the data element if ( !root.previousSibling && !root.nextSibling && root.data.match(/^\s*$/i) ) html = ' '; else html = HTMLArea.htmlEncode(root.data); break; case 8: // Node.COMMENT_NODE html = "<!--" + root.data + "-->"; break; // skip comments, for now. } return html;};HTMLArea.prototype.stripBaseURL = function(string) { var baseurl = this.config.baseURL; // strip to last director
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -