📄 htmlarea.js
字号:
compare = range.compareEndPoints("StartToEnd", range);
} else {
compare = range.compareBoundaryPoints(range.START_TO_END, range);
}
if (compare == 0) {
alert("You need to select some text before creating a link");
return;
}
outparam = {
f_href : '',
f_title : '',
f_target : '',
f_usetarget : editor.config.makeLinkShowsTarget
};
} else
outparam = {
f_href : HTMLArea.is_ie ? editor.stripBaseURL(link.href) : link.getAttribute("href"),
f_title : link.title,
f_target : link.target,
f_usetarget : editor.config.makeLinkShowsTarget
};
this._popupDialog("link.html", function(param) {
if (!param)
return false;
var a = link;
if (!a) try {
editor._doc.execCommand("createlink", false, param.f_href);
a = editor.getParentElement();
var sel = editor._getSelection();
var range = editor._createRange(sel);
if (!HTMLArea.is_ie) {
a = range.startContainer;
if (!/^a$/i.test(a.tagName)) {
a = a.nextSibling;
if (a == null)
a = range.startContainer.parentNode;
}
}
} catch(e) {}
else {
var href = param.f_href.trim();
editor.selectNodeContents(a);
if (href == "") {
editor._doc.execCommand("unlink", false, null);
editor.updateToolbar();
return false;
}
else {
a.href = href;
}
}
if (!(a && /^a$/i.test(a.tagName)))
return false;
a.target = param.f_target.trim();
a.title = param.f_title.trim();
editor.selectNodeContents(a);
editor.updateToolbar();
}, outparam);
};
// Called when the user clicks on "InsertImage" button. If an image is already
// there, it will just modify it's properties.
HTMLArea.prototype._insertImage = function(image) {
var editor = this; // for nested functions
var outparam = null;
if (typeof image == "undefined") {
image = this.getParentElement();
if (image && !/^img$/i.test(image.tagName))
image = null;
}
if (image) outparam = {
f_base : editor.config.baseURL,
f_url : HTMLArea.is_ie ? editor.stripBaseURL(image.src) : image.getAttribute("src"),
f_alt : image.alt,
f_border : image.border,
f_align : image.align,
f_vert : image.vspace,
f_horiz : image.hspace
};
this._popupDialog("insert_image.html", function(param) {
if (!param) { // user must have pressed Cancel
return false;
}
var img = image;
if (!img) {
var sel = editor._getSelection();
var range = editor._createRange(sel);
editor._doc.execCommand("insertimage", false, param.f_url);
if (HTMLArea.is_ie) {
img = range.parentElement();
// wonder if this works...
if (img.tagName.toLowerCase() != "img") {
img = img.previousSibling;
}
} else {
img = range.startContainer.previousSibling;
}
} else {
img.src = param.f_url;
}
for (var field in param) {
var value = param[field];
switch (field) {
case "f_alt" : img.alt = value; break;
case "f_border" : img.border = parseInt(value || "0"); break;
case "f_align" : img.align = value; break;
case "f_vert" : img.vspace = parseInt(value || "0"); break;
case "f_horiz" : img.hspace = parseInt(value || "0"); break;
}
}
}, outparam);
};
// 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 cellwidth = 0;
if (param.f_fixed)
cellwidth = Math.floor(100 / parseInt(param.f_cols));
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");
if (cellwidth)
td.style.width = cellwidth + "%";
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();
cmdID = cmdID.toLowerCase();
if (HTMLArea.is_gecko) try { this._doc.execCommand('useCSS', false, true); } catch (e) {};
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 "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 "cut":
case "copy":
case "paste":
try {
this._doc.execCommand(cmdID, UI, param);
if (this.config.killWordOnPaste)
this._wordClean();
} catch (e) {
if (HTMLArea.is_gecko) {
if (typeof HTMLArea.I18N.msg["Moz-Clipboard"] == "undefined") {
HTMLArea.I18N.msg["Moz-Clipboard"] =
"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.\n\n" +
"[FIXME: please translate this message in your language definition file.]";
}
if (confirm(HTMLArea.I18N.msg["Moz-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: try { this._doc.execCommand(cmdID, UI, param); }
catch(e) { if (this.config.debug) { alert(e + "\n\nby execCommand(" + cmdID + ");"); } }
}
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") || (!HTMLArea.is_ie && ev.type == "keypress");
if (keyEvent)
for (var i in editor.plugins) {
var plugin = editor.plugins[i].instance;
if (typeof plugin.onKeyPress == "function")
if (plugin.onKeyPress(ev))
return false;
}
if (keyEvent && ev.ctrlKey && !ev.altKey) {
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': if (HTMLArea.is_ie || editor.config.htmlareaPaste) { cmd = "paste"; } break;
case 'n': cmd = "formatblock"; value = HTMLArea.is_ie ? "<p>" : "p"; 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_gecko && !ev.shiftKey) {
this.dom_checkInsertP();
HTMLArea._stopEvent(ev);
}
break;
case 8: // KEY backspace
case 46: // KEY delete
if (HTMLArea.is_gecko && !ev.shiftKey) {
if (this.dom_checkBackspace())
HTMLArea._stopEvent(ev);
} else if (HTMLArea.is_ie) {
if (this.ie_checkBackspace())
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);
};
HTMLArea.prototype.scrollToCaret = function() {
var
e = this.getParentElement(),
w = this._iframe.contentWindow,
h = w.innerHeight || w.height,
d = this._doc,
t = d.documentEl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -