⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htmlarea.js

📁 hudong维客系统
💻 JS
📖 第 1 页 / 共 4 页
字号:
						btn.element.selectedIndex = k;
						break;
					}
					++k;
				}
			} catch(e) {};
			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)) {

						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) {

				}
			}
			break;
			case "htmlmode": btn.state("active", text); break;
			case "innerlink":
			case "outerlink":
			case "createsection":
			btn.state("active", (!text && doc.queryCommandState("Underline")));
			break;
			case "lefttoright":
			case "righttoleft":
			var el = this.getParentElement();
			while (el && !HTMLArea.isBlockElement(el))
			el = el.parentNode;
			if (el)
			btn.state("active", (el.style.direction == ((cmd == "righttoleft") ? "rtl" : "ltr")));
			break;
			default:
			try {
				btn.state("active", (!text && doc.queryCommandState(cmd)));
			} catch (e) {}
		}
	}

	if (this._customUndo && !this._timerUndo) {
		this._undoTakeSnapshot();
		var editor = this;
		this._timerUndo = setTimeout(function() {
			editor._timerUndo = null;
		}, this.config.undoTimeout);
	}

	for (var i in this.plugins) {
		var plugin = this.plugins[i].instance;
		if (typeof plugin.onUpdateToolbar == "function")
		plugin.onUpdateToolbar();
	}
};


HTMLArea.prototype.insertNodeAtSelection = function(toBeInserted) {
	if (!HTMLArea.is_ie) {
		var sel = this._getSelection();
		var range = this._createRange(sel);

		sel.removeAllRanges();
		range.deleteContents();
		var node = range.startContainer;
		var pos = range.startOffset;
		switch (node.nodeType) {
			case 3:

			if (toBeInserted.nodeType == 3) {

				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) {
					selnode = selnode.firstChild;
				}
				node.parentNode.insertBefore(toBeInserted, node);
				this.selectNodeContents(selnode);
				this.updateToolbar();
			}
			break;
			case 1:
			var selnode = toBeInserted;
			if (toBeInserted.nodeType == 11 ) {
				selnode = selnode.firstChild;
			}
			node.insertBefore(toBeInserted, node.childNodes[pos]);
			this.selectNodeContents(selnode);
			this.updateToolbar();
			break;
		}
	} else {
		return null;
	}
};


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];
		while (p.nodeType == 3) {
			p = p.parentNode;
		}
		return p;
	} catch (e) {
		return null;
	}
};


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;
};


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);
	}
};



HTMLArea.prototype.insertHTML = function(html) {
	var sel = this._getSelection();
	var range = this._createRange(sel);
	if (HTMLArea.is_ie) {
		range.pasteHTML(html);
		range.collapse(false);
		range.select();
	} else {
		var fragment = this._doc.createDocumentFragment();
		var div = this._doc.createElement("div");
		div.innerHTML = html;
		while (div.firstChild) {
			fragment.appendChild(div.firstChild);
		}
		var node = this.insertNodeAtSelection(fragment);
	}
};


HTMLArea.prototype.surroundHTML = function(startTag, endTag) {
	var html = this.getSelectedHTML();
	this.insertHTML(startTag + html + endTag);
};


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, this);
	}
	return existing;
};

HTMLArea.prototype.getSelectedText= function() {
	var text=null;
	if(this.hasSelectedText())
	{
		text=this.stripHTML(this.getSelectedHTML());
	}
	return text;
};


HTMLArea.prototype.hasSelectedText = function() {
	return this.getSelectedHTML() != '';
};


HTMLArea.prototype._createLink = function(link) {
	var editor = this;
	var outparam = null;
	if (typeof link == "undefined") {
		link = this.getParentElement();
		if (link && !/^a$/i.test(link.tagName))
		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
	};
	this._popupDialog("link.html", function(param) {
		if (!param)
		return false;
		var a = link;
		if (!a) {
			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;
			}
		} else a.href = param.f_href.trim();
		if (!/^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);
};



HTMLArea.prototype._insertImage = function(image) {
	var editor = this;
	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
	};
	this._popupDialog("insert_image.php", function(param) {
		if (!param) {
			return false;
		}
		var img = image;
		if (!img) {
			var sel = editor._getSelection();
			var range = editor._createRange(sel);

			editor._doc.execCommand("insertimage", false, param.f_url);

			var url=param.f_url;
			var tag1=url.lastIndexOf("/");
			var url_temp=url.substring(tag1+1,tag1+3);
			var url_b=url.substring(0,tag1+1);
			var url_e;
			if(url_temp=='s_')
			{
				url_e=url.substring(tag1+3,url.length);
			}else
			{
				url_e=url.substring(tag1+1,url.length);
			}
			var urln=url_b+url_e;
			if (HTMLArea.is_ie) {
				img = range.parentElement();

				if (img.tagName.toLowerCase() != "img") {
					img = img.previousSibling;
				}
			} else {
				img = range.startContainer.previousSibling;
			}
		} else {
			img.src = param.f_url;
		}
		for (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;
			}
		}
		if(urln!=null) img.outerHTML="<a href='"+urln+"' target='_blank' title='点击查看原图' >"+img.outerHTML+"</a>";
	}, outparam);
};


HTMLArea.prototype._insertTable = function() {
	var sel = this._getSelection();
	var range = this._createRange(sel);
	var editor = this;
	this._popupDialog("insert_table.html", function(param) {
		if (!param) {
			return false;
		}
		var doc = editor._doc;

		var table = doc.createElement("table");

		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");
				td.innerHTML="&nbsp;";
				tr.appendChild(td);

				(HTMLArea.is_gecko) && td.appendChild(doc.createElement("br"));
			}
		}
		if (HTMLArea.is_ie) {
			range.pasteHTML(table.outerHTML);
		} else {

			editor.insertNodeAtSelection(table);
		}
		return true;
	}, null);
};



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:

		var dropdown = this.config.customSelects[txt];
		if (typeof dropdown != "undefined") {
			dropdown.action(this);
		} else {
			alert("FIXME: combo box " + txt + " not implemented");
		}
	}
};



HTMLArea.prototype.execCommand = function(cmdID, UI, param) {
	var editor = this;
	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) {
				editor._doc.execCommand(cmdID, false, "#" + color);
			}
		}, HTMLArea._colorToRgb(this._doc.queryCommandValue(cmdID)));
		break;
		case "createlink":
		this._createLink();
		break;
		case "innerlink":
		case "outerlink":
		case "createsection":
		var text=this.getSelectedText();
		if(HTMLArea.is_ie)
		{
			this._doc.execCommand("RemoveFormat");
		}
		if(text==''||text==null)break;
		if(cmdID=="innerlink"){linkvalue="doc.php?action=view&title="+encodeURI(text);fontclass="innerlink";}
		if(cmdID=="outerlink"){linkvalue="http://www.hoodong.com/entryview.do?doc_title="+text; fontclass="outerlink";}
		if(cmdID!="createsection"){str="<a class=\""+fontclass+"\" href=\""+linkvalue+"\">"+text+"</a>";}
		else
		{
			var p=this.getParentElement();
			if(p.tagName=='DIV'||p.tagName=='TD'){break;}
			str="<div class=\"hdwiki_tmml\">"+text+"</div>";
		}
		this.insertHTML(str);
		break;
		case "popupeditor":
		HTMLArea._object = this;
		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");
		}
		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 "showhelp" : window.open(_editor_url + "reference.html", "ha_help"); break;
		case "killword": this._wordClean(); 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;
};


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) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -