htmlarea.js
来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 2,000 行 · 第 1/5 页
JS
2,000 行
// 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();
cmdID = cmdID.toLowerCase();
switch (cmdID) {
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":
this._createLink();
break;
case "createdoclink":
this._createdocLink();
break;
case "popupeditor":
// this object will be passed to the newly opened window
HTMLArea._object = this;
if (HTMLArea.is_ie) {
//if (confirm(HTMLArea.I18N.msg["IE-sucks-full-screen"]))
{
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");
}
break;
case "undo":
case "redo":
if (this._customUndo)
this[cmdID]();
else
this._doc.execCommand(cmdID, UI, param);
break;
case "inserttable": this._insertTable(); break;
case "insertimage": this._insertImage(); break;
case "about" : this._popupDialog("about.html", null, this); break;
case "showhelp" : window.open(_editor_url + "reference.html", "ha_help"); break;
case "killword": this._wordClean(); break;
case "pastespecial": this._pastespecial(); break;
case "cut":
case "copy":
case "paste":
try {
if (this.config.killWordOnPaste)
this._wordClean();
this._doc.execCommand(cmdID, UI, param);
} catch (e) {
if (HTMLArea.is_gecko) {
if (confirm("Unprivileged scripts cannot access Cut/Copy/Paste programatically " +
"for security reasons. Click OK to see a technical note at mozilla.org " +
"which shows you how to allow a script to access the clipboard."))
window.open("http://mozilla.org/editor/midasdemo/securityprefs.html");
}
}
break;
case "lefttoright":
case "righttoleft":
var dir = (cmdID == "righttoleft") ? "rtl" : "ltr";
var el = this.getParentElement();
while (el && !HTMLArea.isBlockElement(el))
el = el.parentNode;
if (el) {
if (el.style.direction == dir)
el.style.direction = "";
else
el.style.direction = dir;
}
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");
if (keyEvent) {
for (var i in editor.plugins) {
var plugin = editor.plugins[i].instance;
if (typeof plugin.onKeyPress == "function") plugin.onKeyPress(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) {
// KEY select all
sel = this._getSelection();
sel.removeAllRanges();
range = this._createRange();
range.selectNodeContents(this._doc.body);
sel.addRange(range);
HTMLArea._stopEvent(ev);
}
break;
// simple key commands follow
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 = "pastespecial"; break;
case '0': cmd = "killword"; break;
// headings
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) {
// execute simple command
this.execCommand(cmd, false, value);
HTMLArea._stopEvent(ev);
}
}
/*
else if (keyEvent) {
// other keys here
switch (ev.keyCode) {
case 13: // KEY enter
// if (HTMLArea.is_ie) {
this.insertHTML("<br />");
HTMLArea._stopEvent(ev);
// }
break;
}
}
*/
// update the toolbar state after some time
if (editor._timerToolbar) {
clearTimeout(editor._timerToolbar);
}
editor._timerToolbar = setTimeout(function() {
editor.updateToolbar();
editor._timerToolbar = null;
}, 50);
};
// retrieve the HTML
HTMLArea.prototype.getHTML = function() {
switch (this._editMode) {
case "wysiwyg" :
if (!this.config.fullPage) {
return HTMLArea.getHTML(this._doc.body, false, this);
} else
return this.doctype + "\n" + HTMLArea.getHTML(this._doc.documentElement, true, this);
case "textmode" : return this._textArea.value;
default : alert("Mode <" + mode + "> not defined!");
}
return false;
};
// retrieve the HTML (fastest version, but uses innerHTML)
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;
};
// completely change the HTML inside
HTMLArea.prototype.setHTML = function(html) {
switch (this._editMode) {
case "wysiwyg" :
if (!this.config.fullPage)
this._doc.body.innerHTML = html;
else
// this._doc.documentElement.innerHTML = html;
this._doc.body.innerHTML = html;
break;
case "textmode" : this._textArea.value = html; break;
default : alert("Mode <" + mode + "> not defined!");
}
return false;
};
// sets the given doctype (useful when config.fullPage is true)
HTMLArea.prototype.setDoctype = function(doctype) {
this.doctype = doctype;
};
/***************************************************
* Category: UTILITY FUNCTIONS
***************************************************/
// browser identification
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");
// variable used to pass the object to the popup editor window.
HTMLArea._object = null;
// function that returns a clone of the given object
HTMLArea.cloneObject = function(obj) {
var newObj = new Object;
// check for array objects
if (obj.constructor.toString().indexOf("function Array(") == 1) {
newObj = obj.constructor();
}
// check for function objects (as usual, IE is fucked up)
if (obj.constructor.toString().indexOf("function Function(") == 1) {
newObj = obj; // just copy reference to it
} 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;
};
// FIXME!!! this should return false for IE < 5.5
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;
};
// selection & ranges
// returns the current selection object
HTMLArea.prototype._getSelection = function() {
if (HTMLArea.is_ie) {
return this._doc.selection;
} else {
return this._iframe.contentWindow.getSelection();
}
};
// returns a range for the current selection
HTMLArea.prototype._createRange = function(sel) {
if (HTMLArea.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();
}
}
};
// event handling
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) {
// remove the class first, if already there
HTMLAre
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?