📄 htmlarea.js
字号:
this._htmlArea = htmlarea;
textarea.parentNode.insertBefore(htmlarea, textarea);
if (textarea.form) {
var f = textarea.form;
if (typeof f.onsubmit == "function") {
var funcref = f.onsubmit;
if (typeof f.__msh_prevOnSubmit == "undefined") {
f.__msh_prevOnSubmit = [];
}
f.__msh_prevOnSubmit.push(funcref);
}
f.onsubmit = function() {
editor._textArea.value = editor.getHTML();
var a = this.__msh_prevOnSubmit;
if (typeof a != "undefined") {
for (var i in a) {
a[i]();
}
}
};
}
window.onunload = function() {
editor._textArea.value = editor.getHTML();
};
this._createToolbar();
var iframe = document.createElement("iframe");
htmlarea.appendChild(iframe);
this._iframe = iframe;
this._createStatusBar();
if (!HTMLArea.is_ie) {
iframe.style.borderWidth = "1px";
}
var height = (this.config.height == "auto" ? (this._ta_size.h + "px") : this.config.height);
height = parseInt(height);
var width = (this.config.width == "auto" ? (this._ta_size.w + "px") : this.config.width);
if (!HTMLArea.is_ie) {
height -= 2;
}
iframe.style.width = width;
if (this.config.sizeIncludesToolbar) {
height -= this._toolbar.offsetHeight;
height -= this._statusBar.offsetHeight;
}
if (height < 0) {
height = 0;
}
iframe.style.height = height + "px";
textarea.style.width = iframe.style.width;
textarea.style.height = iframe.style.height;
function initIframe() {
var doc = editor._iframe.contentWindow.document;
if (!doc) {
if (HTMLArea.is_gecko) {
setTimeout(initIframe, 100);
return false;
} else {
alert("ERROR: IFRAME can't be initialized.");
}
}
if (HTMLArea.is_gecko) {
doc.designMode = "on";
}
editor._doc = doc;
if (!editor.config.fullPage) {
doc.open();
var html = "<html>\n";
html += "<head>\n";
if (editor.config.baseURL)
html += '<base href="' + editor.config.baseURL + '" />';
html += "<style> html,body { border: 0px; } " +
editor.config.pageStyle + "</style>\n";
html += "</head>\n";
html += "<body>\n";
html += editor._textArea.value;
html += "</body>\n";
html += "</html>";
doc.write(html);
doc.close();
} else {
var html = editor._textArea.value;
if (html.match(HTMLArea.RE_doctype)) {
editor.setDoctype(RegExp.$1);
html = html.replace(HTMLArea.RE_doctype, "");
}
doc.open();
doc.write(html);
doc.close();
}
if (HTMLArea.is_ie) {
doc.body.contentEditable = true;
}
editor.focusEditor();
HTMLArea._addEvents
(doc, ["keydown", "keypress", "mousedown", "mouseup", "drag"],
function (event) {
return editor._editorEvent(HTMLArea.is_ie ? editor._iframe.contentWindow.event : event);
});
for (var i in editor.plugins) {
var plugin = editor.plugins[i].instance;
if (typeof plugin.onGenerate == "function")
plugin.onGenerate();
}
setTimeout(function() {
editor.updateToolbar();
}, 250);
if (typeof editor.onGenerate == "function")
editor.onGenerate();
};
setTimeout(initIframe, 100);
};
HTMLArea.prototype.setMode = function(mode) {
if (typeof mode == "undefined") {
mode = ((this._editMode == "textmode") ? "wysiwyg" : "textmode");
}
switch (mode) {
case "textmode":
this._textArea.value = this.getHTML();
this._iframe.style.display = "none";
this._textArea.style.display = "block";
if (this.config.statusBar) {
this._statusBar.innerHTML = HTMLArea.I18N.msg["TEXT_MODE"];
}
break;
case "wysiwyg":
if (HTMLArea.is_gecko) {
try {
this._doc.designMode = "off";
} catch(e) {};
}
if (!this.config.fullPage)
this._doc.body.innerHTML = this.getHTML();
else
this.setFullHTML(this.getHTML());
this._iframe.style.display = "block";
this._textArea.style.display = "none";
if (HTMLArea.is_gecko) {
try {
this._doc.designMode = "on";
} catch(e) {};
}
if (this.config.statusBar) {
this._statusBar.innerHTML = '';
this._statusBar.appendChild(document.createTextNode(HTMLArea.I18N.msg["Path"] + ": "));
this._statusBar.appendChild(this._statusBarTree);
}
break;
default:
alert("Mode <" + mode + "> not defined!");
return false;
}
this._editMode = mode;
this.focusEditor();
};
HTMLArea.prototype.setFullHTML = function(html) {
var save_multiline = RegExp.multiline;
RegExp.multiline = true;
if (html.match(HTMLArea.RE_doctype)) {
this.setDoctype(RegExp.$1);
html = html.replace(HTMLArea.RE_doctype, "");
}
RegExp.multiline = save_multiline;
if (!HTMLArea.is_ie) {
if (html.match(HTMLArea.RE_head))
this._doc.getElementsByTagName("head")[0].innerHTML = RegExp.$1;
if (html.match(HTMLArea.RE_body))
this._doc.getElementsByTagName("body")[0].innerHTML = RegExp.$1;
} else {
var html_re = /<html>((.|\n)*?)<\/html>/i;
html = html.replace(html_re, "$1");
this._doc.open();
this._doc.write(html);
this._doc.close();
this._doc.body.contentEditable = true;
return true;
}
};
HTMLArea.prototype.registerPlugin2 = function(plugin, args) {
if (typeof plugin == "string")
plugin = eval(plugin);
var obj = new plugin(this, args);
if (obj) {
var clone = {};
var info = plugin._pluginInfo;
for (var i in info)
clone[i] = info[i];
clone.instance = obj;
clone.args = args;
this.plugins[plugin._pluginInfo.name] = clone;
} else
alert("Can't register plugin " + plugin.toString() + ".");
};
HTMLArea.prototype.registerPlugin = function() {
var plugin = arguments[0];
var args = [];
for (var i = 1; i < arguments.length; ++i)
args.push(arguments[i]);
this.registerPlugin2(plugin, args);
};
HTMLArea.loadPlugin = function(pluginName) {
var dir = _editor_url + "plugins/" + pluginName;
var plugin = pluginName.replace(/([a-z])([A-Z])([a-z])/g,
function (str, l1, l2, l3) {
return l1 + "-" + l2.toLowerCase() + l3;
}).toLowerCase() + ".js";
var plugin_file = dir + "/" + plugin;
var plugin_lang = dir + "/lang/" + HTMLArea.I18N.lang + ".js";
HTMLArea._scripts.push(plugin_file, plugin_lang);
document.write("<script type='text/javascript' src='" + plugin_file + "'></script>");
document.write("<script type='text/javascript' src='" + plugin_lang + "'></script>");
};
HTMLArea.loadStyle = function(style, plugin) {
var url = _editor_url || '';
if (typeof plugin != "undefined") {
url += "plugins/" + plugin + "/";
}
url += style;
document.write("<style type='text/css'>@import url(" + url + ");</style>");
};
HTMLArea.loadStyle("htmlarea.css");
HTMLArea.prototype._wordClean = function() {
var D = this.getInnerHTML();
if (D.indexOf('class=Mso') >= 0) {
D = D.replace(/\r\n/g, ' ').
replace(/\n/g, ' ').
replace(/\r/g, ' ').
replace(/\ \;/g,' ');
D = D.replace(/ class=[^\s|>]*/gi,'').
replace(/ style=\"[^>]*\"/gi,'').
replace(/ align=[^\s|>]*/gi,'');
D = D.replace(/<b [^>]*>/gi,'<b>').
replace(/<i [^>]*>/gi,'<i>').
replace(/<li [^>]*>/gi,'<li>').
replace(/<ul [^>]*>/gi,'<ul>');
D = D.replace(/<b>/gi,'<strong>').
replace(/<\/b>/gi,'</strong>');
D = D.replace(/<em>/gi,'<i>').
replace(/<\/em>/gi,'</i>');
D = D.replace(/<\?xml:[^>]*>/g, '').
replace(/<\/?st1:[^>]*>/g,'').
replace(/<\/?[a-z]\:[^>]*>/g,'').
replace(/<\/?font[^>]*>/gi,'').
replace(/<\/?span[^>]*>/gi,' ').
replace(/<\/?div[^>]*>/gi,' ').
replace(/<\/?pre[^>]*>/gi,' ').
replace(/<\/?h[1-6][^>]*>/gi,' ');
oldlen = D.length + 1;
while(oldlen > D.length) {
oldlen = D.length;
D = D.replace(/<([a-z][a-z]*)> *<\/\1>/gi,' ').
replace(/<([a-z][a-z]*)> *<([a-z][^>]*)> *<\/\1>/gi,'<$2>');
}
D = D.replace(/<([a-z][a-z]*)><\1>/gi,'<$1>').
replace(/<\/([a-z][a-z]*)><\/\1>/gi,'<\/$1>');
D = D.replace(/ */gi,' ');
this.setHTML(D);
this.updateToolbar();
}
};
HTMLArea.prototype.forceRedraw = function() {
this._doc.body.style.visibility = "hidden";
this._doc.body.style.visibility = "visible";
};
HTMLArea.prototype.focusEditor = function() {
switch (this._editMode) {
case "wysiwyg" : this._iframe.contentWindow.focus(); break;
case "textmode": this._textArea.focus(); break;
default : alert("ERROR: mode " + this._editMode + " is not defined");
}
return this._doc;
};
HTMLArea.prototype._undoTakeSnapshot = function() {
++this._undoPos;
if (this._undoPos >= this.config.undoSteps) {
this._undoQueue.shift();
--this._undoPos;
}
var take = true;
var txt = this.getInnerHTML();
if (this._undoPos > 0)
take = (this._undoQueue[this._undoPos - 1] != txt);
if (take) {
this._undoQueue[this._undoPos] = txt;
} else {
this._undoPos--;
}
};
HTMLArea.prototype.undo = function() {
if (this._undoPos > 0) {
var txt = this._undoQueue[--this._undoPos];
if (txt) this.setHTML(txt);
else ++this._undoPos;
}
};
HTMLArea.prototype.redo = function() {
if (this._undoPos < this._undoQueue.length - 1) {
var txt = this._undoQueue[++this._undoPos];
if (txt) this.setHTML(txt);
else --this._undoPos;
}
};
HTMLArea.prototype.updateToolbar = function(noStatus) {
var doc = this._doc;
var text = (this._editMode == "textmode");
var ancestors = null;
if (!text) {
ancestors = this.getAllAncestors();
if (this.config.statusBar && !noStatus) {
this._statusBarTree.innerHTML = HTMLArea.I18N.msg["Path"] + ": ";
for (var i = ancestors.length; --i >= 0;) {
var el = ancestors[i];
if (!el) {
continue;
}
var a = document.createElement("a");
a.href = "#";
a.el = el;
a.editor = this;
a.onclick = function() {
this.blur();
this.editor.selectNodeContents(this.el);
this.editor.updateToolbar(true);
return false;
};
a.oncontextmenu = function() {
this.blur();
var info = "Inline style:\n\n";
info += this.el.style.cssText.split(/;\s*/).join(";\n");
return false;
};
var txt = el.tagName.toLowerCase();
a.title = el.style.cssText;
if (el.id) {
txt += "#" + el.id;
}
if (el.className) {
txt += "." + el.className;
}
a.appendChild(document.createTextNode(txt));
this._statusBarTree.appendChild(a);
if (i != 0) {
this._statusBarTree.appendChild(document.createTextNode(String.fromCharCode(0xbb)));
}
}
}
}
for (var i in this._toolbarObjects) {
var btn = this._toolbarObjects[i];
var cmd = i;
var inContext = true;
if (btn.context && !text) {
inContext = false;
var context = btn.context;
var attrs = [];
if (/(.*)\[(.*?)\]/.test(context)) {
context = RegExp.$1;
attrs = RegExp.$2.split(",");
}
context = context.toLowerCase();
var match = (context == "*");
for (var k in ancestors) {
if (!ancestors[k]) {
continue;
}
if (match || (ancestors[k].tagName.toLowerCase() == context)) {
inContext = true;
for (var ka in attrs) {
if (!eval("ancestors[k]." + attrs[ka])) {
inContext = false;
break;
}
}
if (inContext) {
break;
}
}
}
}
btn.state("enabled", (!text || btn.text) && inContext);
if (typeof cmd == "function") {
continue;
}
var dropdown = this.config.customSelects[cmd];
if ((!text || btn.text) && (typeof dropdown != "undefined")) {
dropdown.refresh(this);
continue;
}
switch (cmd) {
case "fontname":
case "fontsize":
case "formatblock":
if (!text) try {
var value = ("" + doc.queryCommandValue(cmd)).toLowerCase();
if (!value) {
break;
}
var options = this.config[cmd];
var k = 0;
for (var j in options) {
if ((j.toLowerCase() == value) ||
(options[j].substr(0, value.length).toLowerCase() == value)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -