📄 htmlarea.js
字号:
el.className = "space";
break;
case "linebreak":
newLine();
return false;
case "textindicator":
el = document.createElement("div");
el.appendChild(document.createTextNode("A"));
el.className = "indicator";
el.title = HTMLArea.I18N.tooltips.textindicator;
var obj = {
name : txt, // the button name (i.e. 'bold')
element : el, // the UI element (DIV)
enabled : true, // is it enabled?
active : false, // is it pressed?
text : false, // enabled in text mode?
cmd : "textindicator", // the command ID
state : setButtonStatus // for changing state
};
tb_objects[txt] = obj;
break;
default:
btn = editor.config.btnList[txt];
}
if (!el && btn) {
el = document.createElement("div");
el.title = btn[0];
el.className = "button";
// let's just pretend we have a button object, and
// assign all the needed information to it.
var obj = {
name : txt, // the button name (i.e. 'bold')
element : el, // the UI element (DIV)
enabled : true, // is it enabled?
active : false, // is it pressed?
text : btn[2], // enabled in text mode?
cmd : btn[3], // the command ID
state : setButtonStatus, // for changing state
context : btn[4] || null // enabled in a certain context?
};
tb_objects[txt] = obj;
// handlers to emulate nice flat toolbar buttons
HTMLArea._addEvent(el, "mouseover", function () {
if (obj.enabled) {
HTMLArea._addClass(el, "buttonHover");
}
});
HTMLArea._addEvent(el, "mouseout", function () {
if (obj.enabled) with (HTMLArea) {
_removeClass(el, "buttonHover");
_removeClass(el, "buttonActive");
(obj.active) && _addClass(el, "buttonPressed");
}
});
HTMLArea._addEvent(el, "mousedown", function (ev) {
if (obj.enabled) with (HTMLArea) {
_addClass(el, "buttonActive");
_removeClass(el, "buttonPressed");
_stopEvent(is_ie ? window.event : ev);
}
});
// when clicked, do the following:
HTMLArea._addEvent(el, "click", function (ev) {
if (obj.enabled) with (HTMLArea) {
_removeClass(el, "buttonActive");
_removeClass(el, "buttonHover");
obj.cmd(editor, obj.name, obj);
_stopEvent(is_ie ? window.event : ev);
}
});
var img = document.createElement("img");
img.src = editor.imgURL(btn[1]);
img.style.width = "18px";
img.style.height = "18px";
el.appendChild(img);
} else if (!el) {
el = createSelect(txt);
}
if (el) {
var tb_cell = document.createElement("td");
tb_row.appendChild(tb_cell);
tb_cell.appendChild(el);
} else {
alert("FIXME: Unknown toolbar item: " + txt);
}
return el;
};
var first = true;
for (var i in this.config.toolbar) {
if (!first) {
createButton("linebreak");
} else {
first = false;
}
var group = this.config.toolbar[i];
for (var j in group) {
var code = group[j];
if (/^([IT])\[(.*?)\]/.test(code)) {
// special case, create text label
var l7ed = RegExp.$1 == "I"; // localized?
var label = RegExp.$2;
if (l7ed) {
label = HTMLArea.I18N.custom[label];
}
var tb_cell = document.createElement("td");
tb_row.appendChild(tb_cell);
tb_cell.className = "label";
tb_cell.innerHTML = label;
} else {
createButton(code);
}
}
}
this._htmlArea.appendChild(toolbar);
};
HTMLArea.prototype._createStatusBar = function() {
var div = document.createElement("div");
div.className = "statusBar";
this._htmlArea.appendChild(div);
this._statusBar = div;
div.appendChild(document.createTextNode(HTMLArea.I18N.msg["Path"] + ": "));
// creates a holder for the path view
div = document.createElement("span");
div.className = "statusBarTree";
this._statusBarTree = div;
this._statusBar.appendChild(div);
if (!this.config.statusBar) {
// disable it...
div.style.display = "none";
}
};
// Creates the HTMLArea object and replaces the textarea with it.
HTMLArea.prototype.generate = function () {
var editor = this; // we'll need "this" in some nested functions
// get the textarea
var textarea = this._textArea;
if (typeof textarea == "string") {
// it's not element but ID
this._textArea = textarea = document.getElementById(textarea);
}
this._ta_size = {
w: textarea.offsetWidth,
h: textarea.offsetHeight
};
textarea.style.display = "none";
// create the editor framework
var htmlarea = document.createElement("div");
htmlarea.className = "htmlarea";
this._htmlArea = htmlarea;
// insert the editor before the textarea.
textarea.parentNode.insertBefore(htmlarea, textarea);
if (textarea.form) {
// we have a form, on submit get the HTMLArea content and
// update original textarea.
textarea.form.onsubmit = function() {
editor._textArea.value = editor.getHTML();
};
}
// add a handler for the "back/forward" case -- on body.unload we save
// the HTML content into the original textarea.
window.onunload = function() {
editor._textArea.value = editor.getHTML();
};
// creates & appends the toolbar
this._createToolbar();
// create the IFRAME
var iframe = document.createElement("iframe");
htmlarea.appendChild(iframe);
this._iframe = iframe;
// creates & appends the status bar, if the case
this._createStatusBar();
// remove the default border as it keeps us from computing correctly
// the sizes. (somebody tell me why doesn't this work in IE)
if (!HTMLArea.is_ie) {
iframe.style.borderWidth = "1px";
// iframe.frameBorder = "1";
// iframe.marginHeight = "0";
// iframe.marginWidth = "0";
}
// size the IFRAME according to user's prefs or initial textarea
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);
width = parseInt(width);
if (!HTMLArea.is_ie) {
height -= 2;
width -= 2;
}
iframe.style.width = width + "px";
if (this.config.sizeIncludesToolbar) {
// substract toolbar height
height -= this._toolbar.offsetHeight;
height -= this._statusBar.offsetHeight;
}
if (height < 0) {
height = 0;
}
iframe.style.height = height + "px";
// the editor including the toolbar now have the same size as the
// original textarea.. which means that we need to reduce that a bit.
textarea.style.width = iframe.style.width;
textarea.style.height = iframe.style.height;
// IMPORTANT: we have to allow Mozilla a short time to recognize the
// new frame. Otherwise we get a stupid exception.
function initIframe() {
var doc = editor._iframe.contentWindow.document;
if (!doc) {
// Try again..
// FIXME: don't know what else to do here. Normally
// we'll never reach this point.
if (HTMLArea.is_gecko) {
setTimeout(initIframe, 10);
return false;
} else {
alert("ERROR: IFRAME can't be initialized.");
}
}
if (HTMLArea.is_gecko) {
// enable editable mode for Mozilla
doc.designMode = "on";
}
editor._doc = doc;
doc.open();
var html = "<html>\n";
html += "<head>\n";
html += "<style>" + 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();
if (HTMLArea.is_ie) {
// enable editable mode for IE. For some reason this
// doesn't work if done in the same place as for Gecko
// (above).
doc.body.contentEditable = true;
}
editor.focusEditor();
// intercept some events; for updating the toolbar & keyboard handlers
HTMLArea._addEvents
(doc, ["keydown", "keypress", "mousedown", "mouseup", "drag"],
function (event) {
return editor._editorEvent(HTMLArea.is_ie ? editor._iframe.contentWindow.event : event);
});
editor.updateToolbar();
};
setTimeout(initIframe, HTMLArea.is_gecko ? 10 : 0);
};
// Switches editor mode; parameter can be "textmode" or "wysiwyg". If no
// parameter was passed this function toggles between modes.
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) {
// disable design mode before changing innerHTML
this._doc.designMode = "off";
}
this._doc.body.innerHTML = this.getHTML();
this._iframe.style.display = "block";
this._textArea.style.display = "none";
if (HTMLArea.is_gecko) {
// we need to refresh that info for Moz-1.3a
this._doc.designMode = "on";
}
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();
};
/***************************************************
* Category: PLUGINS
***************************************************/
// Create the specified plugin and register it with this HTMLArea
HTMLArea.prototype.registerPlugin = function(pluginName) {
this.plugins[pluginName] = eval("new " + pluginName + "(this);");
};
// static function that loads the required plugin and lang file, based on the
// language loaded already for HTMLArea. You better make sure that the plugin
// _has_ that language, otherwise shit might happen ;-)
HTMLArea.loadPlugin = function(pluginName) {
var editorurl = '';
if (typeof _editor_url != "undefined") {
editorurl = _editor_url + "/";
}
var dir = editorurl + "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";
document.write("<script type='text/javascript' src='" + dir + "/" + plugin + "'></script>");
document.write("<script type='text/javascript' src='" + dir + "/lang/" + HTMLArea.I18N.lang + ".js'></script>");
};
/***************************************************
* Category: EDITOR UTILITIES
***************************************************/
HTMLArea.prototype.forceRedraw = function() {
this._doc.body.style.visibility = "hidden";
this._doc.body.style.visibility = "visible";
// this._doc.body.innerHTML = this.getInnerHTML();
};
// focuses the iframe window. returns a reference to the editor document.
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;
};
// updates enabled/disable/active state of the toolbar elements
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 = ''; // clear
for (var i = ancestors.length; --i >= 0;) {
var el = ancestors[i];
if (!el) {
// hell knows why we get here; this
// could be a classic example of why
// it's good to check for conditions
// that are impossible to happen ;-)
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() {
// TODO: add context menu here
this.blur();
var info = "Inline style:\n\n";
info += this.el.style.cssText.split(/;\s*/).join(";\n");
alert(info);
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)));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -