📄 editor.js
字号:
return null;
}
}
WYSIWYD.prototype.getParentElement = function() {
var sel = this._getSelection();
var range = this._createRange(sel);
if (is_ie) {
switch(sel.type) {
case "Text":
case "None":
return range.parentElement();
case "Control":
return range.item(0);
default:
return this._doc.body;
}
} else try{
var p = range.commonAncestorContainer;
if (!range.collapsed && range.startContainer == range.endContainer &&
range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes())
p = range.startContainer.childNodes[range.startOffset];
while (p.nodeType == 3) {
p = p.parentNode;
}
return p;
} catch (e) {
return null;
}
}
WYSIWYD.prototype.selectNodeContents = function(node, pos) {
this.focusEditor();
this.forceRedraw();
var range;
var collapsed = (typeof pos != "undefined");
if (is_ie) {
range = this._doc.body.createTextRange();
range.moveToElementText(node);
(collapsed) && range.collapse(pos);
range.select();
} else {
var sel = this._getSelection();
range = this._doc.createRange();
range.selectNodeContents(node);
(collapsed) && range.collapse(pos);
sel.removeAllRanges();
sel.addRange(range);
}
}
WYSIWYD.prototype.GetSelectedValue = function(cmdID,value) {
this.focusEditor();
if (this._editMode == "textmode") {
windselect(cmdID,value);
} else {
this._comboSelected(cmdID,value);
}
this.updateToolbar();
closep();
return false;
}
WYSIWYD.prototype._comboSelected = function(cmdID,value) {
switch(cmdID) {
case "fontname":
case "fontsize": this._doc.execCommand(cmdID, false, value); break;
case "formatblock":
(is_ie) && (value = "<" + value + ">");
this._doc.execCommand(cmdID, false, value);
break;
}
}
WYSIWYD.prototype.execCommand = function(cmdID, UI, param) {
cmdID = cmdID.toLowerCase();
switch(cmdID) {
case "htmlmode" : break;
case "windcode" : this.setMode(); break;
case "undo":
case "redo":
this._doc.execCommand(cmdID, UI, param); break;
case "cut":
case "copy":
case "paste":
try{this._doc.execCommand(cmdID, UI, param);}
catch(e) {}
break;
default : this._doc.execCommand(cmdID, UI, param);
}
return false;
}
WYSIWYD.prototype._editorEvent = function(ev) {
var editor = this;
var keyEvent = (is_ie && ev.type == "keydown") || (ev.type == "keypress");
if (editor._timerToolbar) {
clearTimeout(editor._timerToolbar);
}
editor._timerToolbar = setTimeout(function() {
editor.updateToolbar();
editor._timerToolbar = null;
}, 50);
}
WYSIWYD.prototype.getHTML = function() {
switch (this._editMode) {
case "wysiwyg" : return WYSIWYD.getHTML(this._doc.body, false, this);
case "textmode" : return this._textArea.value;
default : alert("Mode <" + mode + "> not defined!");
}
return false;
}
WYSIWYD.prototype._getSelection = function() {
if (is_ie) {
return this._doc.selection;
} else {
return this._iframe.contentWindow.getSelection();
}
}
WYSIWYD.prototype._createRange = function(sel) {
if (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();
}
}
}
WYSIWYD._addEvent = function(el, evname, func) {
if (el.attachEvent) {
el.attachEvent("on" + evname, func);
} else {
el.addEventListener(evname, func, true);
}
}
WYSIWYD._addEvents = function(el, evs, func) {
for (var i in evs) {
WYSIWYD._addEvent(el, evs[i], func);
}
}
WYSIWYD._removeEvent = function(el, evname, func) {
if (el.detachEvent) {
el.detachEvent("on" + evname, func);
} else {
el.removeEventListener(evname, func, true);
}
}
WYSIWYD._stopEvent = function(ev) {
if (is_ie) {
ev.cancelBubble = true;
ev.returnValue = false;
} else {
ev.preventDefault();
ev.stopPropagation();
}
}
WYSIWYD._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(" ");
}
WYSIWYD._addClass = function(el, className) {
WYSIWYD._removeClass(el, className);
el.className += " " + className;
}
WYSIWYD.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);
}
WYSIWYD.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);
}
WYSIWYD.htmlEncode = function(str) {
str = str.replace(/&/ig, "&");
str = str.replace(/</ig, "<");
str = str.replace(/>/ig, ">");
str = str.replace(/\x22/ig, """);
return str;
}
WYSIWYD.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 (is_ie && root_tag == "head") {
if (outputRoot)
html += "<head>";
var save_multiline = RegExp.multiline;
RegExp.multiline = true;
var txt = root.innerHTML.replace(/(<\/|<)\s*([^ \t\n>]+)/ig, 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() || WYSIWYD.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" && typeof root[a.nodeName] != "function" && name != "href" && name != "src") {
value = root[a.nodeName];
} else {
value = a.nodeValue;
}
} else {
value = root.style.cssText;
}
if (/(_moz|^$)/.test(value)) {
continue;
}
html += " " + name + '="' + value + '"';
}
html += closed ? " />" : ">";
}
for (i = root.firstChild; i; i = i.nextSibling) {
html += WYSIWYD.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 = WYSIWYD.htmlEncode(root.data);
break;
case 8:
html = "<!--" + root.data + "-->";
break;
}
return html;
}
String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
}
WYSIWYD._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 + ")";
}
WYSIWYD._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;
}
WYSIWYD.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;
}
WYSIWYD.prototype.insertHTML = function(html) {
if (is_ie) {
var sel = this._getSelection();
var range = this._createRange(sel);
range.pasteHTML(html);
} else {
var fragment = this._doc.createDocumentFragment();
var div = this._doc.createElement("div");
div.innerHTML = html;
while (div.firstChild) {
fragment.appendChild(div.firstChild);
}
var node = this.insertNodeAtSelection(fragment);
}
}
WYSIWYD.prototype.getpos = function() {
if (!is_ie) return;
this.focusEditor();
if (this._editMode == 'wysiwyg') {
var obj = this._doc.body;
var s = document.selection.createRange();
s.setEndPoint("StartToStart", obj.createTextRange());
return s.text.length;
} else {
var obj = this._textArea;
var s = obj.scrollTop;
var r = document.selection.createRange();
var t = obj.createTextRange();
t.collapse(true);
t.select();
var j = document.selection.createRange();
r.setEndPoint("StartToStart",j);
var pos = r.text.replace(/\r?\n/g, ' ').length;
r.collapse(false);
r.select();
obj.scrollTop = s;
return pos;
}
}
WYSIWYD.prototype.setpos = function() {
this.focusEditor();
if (!is_ie || this.getpos()) return;
var obj = this._editMode == 'wysiwyg' ? this._doc.body : this._textArea;
var r = obj.createTextRange();
r.moveStart('character', this._pos);
r.collapse(true);
r.select();
}
function editorcode(cmdID) {
editor.focusEditor();
if (editor._editMode == "textmode") {
windcode(cmdID);
} else {
editor.execCommand(cmdID,false);
}
editor.updateToolbar();
}
function insertImage() {
editor.focusEditor();
txt=prompt('URL:',"http://");
if (txt!=null) {
if (editor._editMode == "textmode") {
sm="[img]"+txt+"[/img]";
AddText(sm,'');
} else {
try{editor._doc.execCommand("insertimage",false,txt);}
catch(e) {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -