📄 htmlarea.js
字号:
var plugin = editor.plugins[i].instance;
if (typeof plugin.onKeyPress == "function") plugin.onKeyPress(ev);
}
}
if (keyEvent && ev.altKey) {
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 'q': cmd = "innerlink"; break;
case 'p': cmd = "createsection"; break;
}
if (cmd) {
this.execCommand(cmd, false, value);
HTMLArea._stopEvent(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) {
sel = this._getSelection();
sel.removeAllRanges();
range = this._createRange();
range.selectNodeContents(this._doc.body);
sel.addRange(range);
HTMLArea._stopEvent(ev);
}
break;
case 'q': cmd = "innerlink"; break;
case 'p': cmd = "createsection"; break;
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;
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) {
this.execCommand(cmd, false, value);
HTMLArea._stopEvent(ev);
}
}
if (editor._timerToolbar) {
clearTimeout(editor._timerToolbar);
}
editor._timerToolbar = setTimeout(function() {
editor.updateToolbar();
editor._timerToolbar = null;
}, 50);
};
HTMLArea.prototype.getHTML = function() {
var html=false;
switch (this._editMode) {
case "wysiwyg" :
if (!this.config.fullPage) {
html=HTMLArea.getHTML(this._doc.body, false, this);
break;
} else
{
html=this.doctype + "\n" + HTMLArea.getHTML(this._doc.documentElement, true, this);
break;
}
case "textmode" :
{
html=this._textArea.value;
break;
}
// default: alert("Mode <" + mode + "> not defined!");
}
return html;
};
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;
};
HTMLArea.prototype.setHTML = function(html) {
switch (this._editMode) {
case "wysiwyg" :
if (!this.config.fullPage)
this._doc.body.innerHTML = html;
else
this._doc.body.innerHTML = html;
break;
case "textmode" : this._textArea.value = html; break;
default : alert("Mode <" + mode + "> not defined!");
}
return false;
};
HTMLArea.prototype.setDoctype = function(doctype) {
this.doctype = doctype;
};
HTMLArea.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");
HTMLArea._object = null;
HTMLArea.cloneObject = function(obj) {
var newObj = new Object;
if (obj.constructor.toString().indexOf("function Array(") == 1) {
newObj = obj.constructor();
}
if (obj.constructor.toString().indexOf("function Function(") == 1) {
newObj = obj;
} 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;
};
HTMLArea.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;
};
HTMLArea.prototype._getSelection = function() {
if (HTMLArea.is_ie) {
return this._doc.selection;
} else {
return this._iframe.contentWindow.getSelection();
}
};
HTMLArea.prototype._createRange = function(sel) {
this.focusEditor();
if (HTMLArea.is_ie) {
return sel.createRange();
} else {
if (typeof sel != "undefined") {
try {
return sel.getRangeAt(0);
} catch(e) {
return this._doc.createRange();
}
} else {
return this._doc.createRange();
}
}
};
HTMLArea._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) {
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);
};
HTMLArea.htmlEncode = function(str) {
str = str.replace(/&/ig, "&");
str = str.replace(/</ig, "<");
str = str.replace(/>/ig, ">");
str = str.replace(/\x22/ig, """);
return str;
};
HTMLArea.getHTML = function(root, outputRoot, editor) {
var html = "";
switch (root.nodeType) {
case 1:
case 11:
var closed;
var i;
var root_tag = (root.nodeType == 1) ? root.tagName.toLowerCase() : '';
if (HTMLArea.is_ie && root_tag == "head") {
if (outputRoot)
html += "<head>";
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)) {
continue;
}
var value;
if (name != "style") {
if (typeof root[a.nodeName] != "undefined" && name != "href" && name != "src") {
value = root[a.nodeName];
} else {
value = a.nodeValue;
if (HTMLArea.is_ie && (name == "href" || name == "src")) {
value = editor.stripBaseURL(value);
}
}
} else {
value = root.style.cssText;
}
if (/(_moz|^$)/.test(value)) {
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:
if ( !root.previousSibling && !root.nextSibling && root.data.match(/^\s*$/i) ) html = ' ';
else html = HTMLArea.htmlEncode(root.data);
break;
case 8:
html = "<!--" + root.data + "-->";
break;
}
return html;
};
HTMLArea.prototype.stripBaseURL = function(string) {
//var baseurl = this.config.baseURL;
//baseurl = baseurl.replace(/[^\/]+$/, '');
//var basere = new RegExp(baseurl);
//string = string.replace(basere, "");
//baseurl = baseurl.replace(/^(https?:\/\/[^\/]+)(.*)$/, '$1');
//basere = new RegExp(baseurl);
return string;
};
String.prototype.trim = function() {
return a.replace(/^\s*|\s*$/g,"");
};
HTMLArea.prototype.stripHTML=function(html){
html = html.replace(/(<([^>]+)>)/ig,"");
html = html.replace(/^\s*|\s*$/g,"");
return html;
};
HTMLArea._makeColor = function(v) {
if (typeof v != "number") {
return v;
}
var r = v & 0xFF;
var g = (v >> 8) & 0xFF;
var b = (v >> 16) & 0xFF;
return "rgb(" + r + "," + g + "," + b + ")";
};
HTMLArea._colorToRgb = function(v) {
if (!v)
return '';
function hex(d) {
return (d < 16) ? ("0" + d.toString(16)) : d.toString(16);
};
if (typeof v == "number") {
var r = v & 0xFF;
var g = (v >> 8) & 0xFF;
var b = (v >> 16) & 0xFF;
return "#" + hex(r) + hex(g) + hex(b);
}
if (v.substr(0, 3) == "rgb") {
var re = /rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)/;
if (v.match(re)) {
var r = parseInt(RegExp.$1);
var g = parseInt(RegExp.$2);
var b = parseInt(RegExp.$3);
return "#" + hex(r) + hex(g) + hex(b);
}
return null;
}
if (v.substr(0, 1) == "#") {
return v;
}
return null;
};
HTMLArea.prototype._popupDialog = function(url, action, init) {
Dialog(this.popupURL(url), action, init);
};
HTMLArea.prototype.imgURL = function(file, plugin) {
if (typeof plugin == "undefined")
return _editor_url + file;
else
return _editor_url + "plugins/" + plugin + "/img/" + file;
};
HTMLArea.prototype.popupURL = function(file) {
var url = "";
if (file.match(/^plugin:\/\/(.*?)\/(.*)/)) {
var plugin = RegExp.$1;
var popup = RegExp.$2;
if (!/\.html$/.test(popup))
popup += ".html";
url = _editor_url + "plugins/" + plugin + "/popups/" + popup;
} else
url = _editor_url + this.config.popupURL + file;
return url;
};
HTMLArea.getElementById = function(tag, id) {
var el, i, objs = document.getElementsByTagName(tag);
for (i = objs.length; --i >= 0 && (el = objs[i]);)
if (el.id == id)
return el;
return null;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -