📄 htmlarea.js
字号:
}
}
}
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]) {
// the impossible really happens.
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;
}
// look-it-up in the custom dropdown boxes
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) {
var value = ("" + doc.queryCommandValue(cmd)).toLowerCase();
if (!value) {
// FIXME: what do we do here?
break;
}
// HACK -- retrieve the config option for this
// combo box. We rely on the fact that the
// variable in config has the same name as
// button name in the toolbar.
var options = this.config[cmd];
var k = 0;
// btn.element.selectedIndex = 0;
for (var j in options) {
// FIXME: the following line is scary.
if ((j.toLowerCase() == value) ||
(options[j].substr(0, value.length).toLowerCase() == value)) {
btn.element.selectedIndex = k;
break;
}
++k;
}
}
break;
case "textindicator":
if (!text) {
try {with (btn.element.style) {
backgroundColor = HTMLArea._makeColor(
doc.queryCommandValue(HTMLArea.is_ie ? "backcolor" : "hilitecolor"));
if (/transparent/i.test(backgroundColor)) {
// Mozilla
backgroundColor = HTMLArea._makeColor(doc.queryCommandValue("backcolor"));
}
color = HTMLArea._makeColor(doc.queryCommandValue("forecolor"));
fontFamily = doc.queryCommandValue("fontname");
fontWeight = doc.queryCommandState("bold") ? "bold" : "normal";
fontStyle = doc.queryCommandState("italic") ? "italic" : "normal";
}} catch (e) {
// alert(e + "\n\n" + cmd);
}
}
break;
case "htmlmode": btn.state("active", text); break;
default:
try {
btn.state("active", (!text && doc.queryCommandState(cmd)));
} catch (e) {}
}
}
};
/** Returns a node after which we can insert other nodes, in the current
* selection. The selection is removed. It splits a text node, if needed.
*/
HTMLArea.prototype.insertNodeAtSelection = function(toBeInserted) {
if (!HTMLArea.is_ie) {
var sel = this._getSelection();
var range = this._createRange(sel);
// remove the current selection
sel.removeAllRanges();
range.deleteContents();
var node = range.startContainer;
var pos = range.startOffset;
switch (node.nodeType) {
case 3: // Node.TEXT_NODE
// we have to split it at the caret position.
if (toBeInserted.nodeType == 3) {
// do optimized insertion
node.insertData(pos, toBeInserted.data);
range = this._createRange();
range.setEnd(node, pos + toBeInserted.length);
range.setStart(node, pos + toBeInserted.length);
sel.addRange(range);
} else {
node = node.splitText(pos);
var selnode = toBeInserted;
if (toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */) {
selnode = selnode.firstChild;
}
node.parentNode.insertBefore(toBeInserted, node);
this.selectNodeContents(selnode);
this.updateToolbar();
}
break;
case 1: // Node.ELEMENT_NODE
var selnode = toBeInserted;
if (toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */) {
selnode = selnode.firstChild;
}
node.insertBefore(toBeInserted, node.childNodes[pos]);
this.selectNodeContents(selnode);
this.updateToolbar();
break;
}
} else {
return null; // this function not yet used for IE <FIXME>
}
};
// Returns the deepest node that contains both endpoints of the selection.
HTMLArea.prototype.getParentElement = function() {
var sel = this._getSelection();
var range = this._createRange(sel);
if (HTMLArea.is_ie) {
return range.parentElement ? range.parentElement() : this._doc.body;
} else {
var p = range.commonAncestorContainer;
while (p.nodeType == 3) {
p = p.parentNode;
}
return p;
}
};
// Returns an array with all the ancestor nodes of the selection.
HTMLArea.prototype.getAllAncestors = function() {
var p = this.getParentElement();
var a = [];
while (p && (p.nodeType == 1) && (p.tagName.toLowerCase() != 'body')) {
a.push(p);
p = p.parentNode;
}
a.push(this._doc.body);
return a;
};
// Selects the contents inside the given node
HTMLArea.prototype.selectNodeContents = function(node, pos) {
this.focusEditor();
this.forceRedraw();
var range;
var collapsed = (typeof pos != "undefined");
if (HTMLArea.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);
}
};
/** Call this function to insert HTML code at the current position. It deletes
* the selection, if any.
*/
HTMLArea.prototype.insertHTML = function(html) {
var sel = this._getSelection();
var range = this._createRange(sel);
if (HTMLArea.is_ie) {
range.pasteHTML(html);
} else {
// construct a new document fragment with the given HTML
var fragment = this._doc.createDocumentFragment();
var div = this._doc.createElement("div");
div.innerHTML = html;
while (div.firstChild) {
// the following call also removes the node from div
fragment.appendChild(div.firstChild);
}
// this also removes the selection
var node = this.insertNodeAtSelection(fragment);
}
};
/**
* Call this function to surround the existing HTML code in the selection with
* your tags. FIXME: buggy! This function will be deprecated "soon".
*/
HTMLArea.prototype.surroundHTML = function(startTag, endTag) {
var html = this.getSelectedHTML();
// the following also deletes the selection
this.insertHTML(startTag + html + endTag);
};
/// Retrieve the selected block
HTMLArea.prototype.getSelectedHTML = function() {
var sel = this._getSelection();
var range = this._createRange(sel);
var existing = null;
if (HTMLArea.is_ie) {
existing = range.htmlText;
} else {
existing = HTMLArea.getHTML(range.cloneContents(), false);
}
return existing;
};
// Called when the user clicks on "InsertImage" button
HTMLArea.prototype._insertImage = function() {
var editor = this; // for nested functions
this._popupDialog("insert_image.html", function(param) {
if (!param) { // user must have pressed Cancel
return false;
}
var sel = editor._getSelection();
var range = editor._createRange(sel);
editor._doc.execCommand("insertimage", false, param["f_url"]);
var img = null;
if (HTMLArea.is_ie) {
img = range.parentElement();
// wonder if this works...
if (img.tagName.toLowerCase() != "img") {
img = img.previousSibling;
}
} else {
img = range.startContainer.previousSibling;
}
for (field in param) {
var value = param[field];
if (!value) {
continue;
}
switch (field) {
case "f_alt" : img.alt = value; break;
case "f_border" : img.border = parseInt(value); break;
case "f_align" : img.align = value; break;
case "f_vert" : img.vspace = parseInt(value); break;
case "f_horiz" : img.hspace = parseInt(value); break;
}
}
}, null);
};
// Called when the user clicks the Insert Table button
HTMLArea.prototype._insertTable = function() {
var sel = this._getSelection();
var range = this._createRange(sel);
var editor = this; // for nested functions
this._popupDialog("insert_table.html", function(param) {
if (!param) { // user must have pressed Cancel
return false;
}
var doc = editor._doc;
// create the table element
var table = doc.createElement("table");
// assign the given arguments
for (var field in param) {
var value = param[field];
if (!value) {
continue;
}
switch (field) {
case "f_width" : table.style.width = value + param["f_unit"]; break;
case "f_align" : table.align = value; break;
case "f_border" : table.border = parseInt(value); break;
case "f_spacing" : table.cellspacing = parseInt(value); break;
case "f_padding" : table.cellpadding = parseInt(value); break;
}
}
var tbody = doc.createElement("tbody");
table.appendChild(tbody);
for (var i = 0; i < param["f_rows"]; ++i) {
var tr = doc.createElement("tr");
tbody.appendChild(tr);
for (var j = 0; j < param["f_cols"]; ++j) {
var td = doc.createElement("td");
tr.appendChild(td);
// Mozilla likes to see something inside the cell.
(HTMLArea.is_gecko) && td.appendChild(doc.createElement("br"));
}
}
if (HTMLArea.is_ie) {
range.pasteHTML(table.outerHTML);
} else {
// insert the table
editor.insertNodeAtSelection(table);
}
return true;
}, null);
};
/***************************************************
* Category: EVENT HANDLERS
***************************************************/
// el is reference to the SELECT object
// txt is the name of the select field, as in config.toolbar
HTMLArea.prototype._comboSelected = function(el, txt) {
this.focusEditor();
var value = el.options[el.selectedIndex].value;
switch (txt) {
case "fontname":
case "fontsize": this.execCommand(txt, false, value); break;
case "formatblock":
(HTMLArea.is_ie) && (value = "<" + value + ">");
this.execCommand(txt, false, value);
break;
default:
// try to look it up in the registered dropdowns
var dropdown = this.config.customSelects[txt];
if (typeof dropdown != "undefined") {
dropdown.action(this);
} else {
alert("FIXME: combo box " + txt + " not implemented");
}
}
};
// the execCommand function (intercepts some commands and replaces them with
// our own implementation)
HTMLArea.prototype.execCommand = function(cmdID, UI, param) {
var editor = this; // for nested functions
this.focusEditor();
switch (cmdID.toLowerCase()) {
case "htmlmode" : this.setMode(); break;
case "hilitecolor":
(HTMLArea.is_ie) && (cmdID = "backcolor");
case "forecolor":
this._popupDialog("select_color.html", function(color) {
if (color) { // selection not canceled
editor._doc.execCommand(cmdID, false, "#" + color);
}
}, HTMLArea._colorToRgb(this._doc.queryCommandValue(cmdID)));
break;
case "createlink":
if (HTMLArea.is_ie || !UI) {
this._doc.execCommand(cmdID, UI, param);
} else {
// browser is Mozilla & wants UI
var param;
if ((param = prompt("Enter URL"))) {
this._doc.execCommand(cmdID, false, param);
}
}
break;
case "popupeditor":
if (HTMLArea.is_ie) {
window.open(this.popupURL("fullscreen.html"), "ha_fullscreen",
"toolbar=no,location=no,directories=no,status=no,menubar=no," +
"scrollbars=no,resizable=yes,width=640,height=480");
} else {
window.open(this.popupURL("fullscreen.html"), "ha_fullscreen",
"toolbar=no,menubar=no,personalbar=no,width=640,height=480," +
"scrollbars=no,resizable=yes");
}
// pass this object to the newly opened window
HTMLArea._object = this;
break;
case "inserttable": this._insertTable(); break;
case "insertimage": this._insertImage(); break;
case "about" : this._popupDialog("about.html", null, null); break;
case "showhelp" : window.open("reference.html", "ha_help"); break;
default: this._doc.execCommand(cmdID, UI, param);
}
this.updateToolbar();
return false;
};
/** A generic event handler for things that happen in the IFRAME's document.
* This function also handles key bindings. */
HTMLArea.prototype._editorEvent = function(ev) {
var editor = this;
var keyEvent = (HTMLArea.is_ie && ev.type == "keydown") || (ev.type == "keypress");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -