📄 htmlarea.php
字号:
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) { switch (sel.type) { case "Text": case "None": return range.parentElement(); case "Control": return range.item(0); default: return this._doc.body; } } else try { var p = range.commonAncestorContainer; if (!range.collapsed && range.startContainer == range.endContainer && range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes()) p = range.startContainer.childNodes[range.startOffset]; /* alert(range.startContainer + ":" + range.startOffset + "\n" + range.endContainer + ":" + range.endOffset); */ while (p.nodeType == 3) { p = p.parentNode; } return p; } catch (e) { return null; }};// 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 nodeHTMLArea.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 blockHTMLArea.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, this); } return existing;};/// Return true if we have some selectionHTMLArea.prototype.hasSelectedText = function() { // FIXME: come _on_ mishoo, you can do better than this ;-) return this.getSelectedHTML() != '';};HTMLArea.prototype._createLink = function(link) { var editor = this; var allinks = editor._doc.getElementsByTagName('A'); var anchors = new Array(); for(var i = 0; i < allinks.length; i++) { var attrname = allinks[i].getAttribute('name'); if((HTMLArea.is_ie ? attrname.length > 0 : attrname != null)) { anchors[i] = allinks[i].getAttribute('name'); } } var outparam = null; if (typeof link == "undefined") { link = this.getParentElement(); if (link && !/^a$/i.test(link.tagName)) { if(link.tagName.toLowerCase() != 'img') { link = null; var sel = this._getSelection(); var rng = this._createRange(sel); var len = HTMLArea.is_ie ? rng.text.toString().length : sel.toString().length; if(len < 1) { alert("<?php print_string("alertnoselectedtext","editor");?>"); return false; } } link = null; } } if (link) { outparam = { f_href : HTMLArea.is_ie ? editor.stripBaseURL(link.href) : link.getAttribute("href"), f_title : link.title, f_target : link.target, f_anchors: anchors }; } else { outparam = { f_anchors:anchors }; } this._popupDialog("link_std.php?id=<?php echo $id; ?>", function(param) { if (!param) { return false; } var a = link; if (!a) { // Create a temporary unique link, insert it then find it and set the correct parameters var tmpLink = 'http://www.moodle.org/'+Math.random(); var elm = editor._doc.execCommand("createlink",false,tmpLink); var links=editor._doc.getElementsByTagName("a"); for(var i=0;i<links.length;i++){ var link=links[i]; if(link.href==tmpLink) { link.href=param.f_href.trim(); if(param.f_target){ link.target=param.f_target.trim(); } if(param.f_title){ link.title=param.f_title.trim(); } break; } } } 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) { // Make sure that editor has focus this.focusEditor(); 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_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, f_width : image.width, f_height : image.height }; this._popupDialog("<?php if(!empty($id) and has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $id))) { echo "insert_image.php?id=$id"; } else { echo "insert_image_std.php?id=$id"; }?>", 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); if (HTMLArea.is_ie) { 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 { // MOODLE HACK: startContainer.perviousSibling // Doesn't work so we'll use createElement and // insertNodeAtSelection //img = range.startContainer.previousSibling; var img = editor._doc.createElement("img"); img.setAttribute("src",""+ param.f_url +""); img.setAttribute("alt",""+ param.f_alt +""); editor.insertNodeAtSelection(img); } } else { img.src = param.f_url; } for (field in param) { var value = param[field]; switch (field) { case "f_alt" : img.alt = value; img.title = 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; case "f_width" : if(value != 0) { img.width = parseInt(value); } else { break; } break; case "f_height" : if(value != 0) { img.height = parseInt(value); } else { break; } break; } } }, outparam);};// Called when the user clicks the Insert Table buttonHTMLArea.prototype._insertTable = function() { var sel = this._getSelection(); var range = this._createRange(sel); var editor = this; // for nested functions this._popupDialog("insert_table.php?id=<?php echo $id; ?>", 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.width = value + param["f_unit"]; break; case "f_align" : table.align = value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -