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

📄 editor.js

📁 php最好的论坛程序! php最好的论坛程序! php最好的论坛程序!
💻 JS
📖 第 1 页 / 共 3 页
字号:
				var sel = this.editwin.getSelection();
				var range = this.editdoc.createRange();
				range.selectNodeContents(node);
				sel.removeAllRanges();
				sel.addRange(range);
			};

			this.read_nodes = function(root, toptag) {
				var html = "";
				var moz_check = /_moz/i;

				switch(root.nodeType) {
					case Node.ELEMENT_NODE:
					case Node.DOCUMENT_FRAGMENT_NODE:
						var closed;
						if(toptag) {
							closed = !root.hasChildNodes();
							html = '<' + root.tagName.toLowerCase();
							var attr = root.attributes;
							for(var i = 0; i < attr.length; ++i) {
								var a = attr.item(i);
								if(!a.specified || a.name.match(moz_check) || a.value.match(moz_check)) {
									continue;
								}
								html += " " + a.name.toLowerCase() + '="' + a.value + '"';
							}
							html += closed ? " />" : ">";
						}
						for(var i = root.firstChild; i; i = i.nextSibling) {
							html += this.read_nodes(i, true);
						}
						if(toptag && !closed) {
							html += "</" + root.tagName.toLowerCase() + ">";
						}
						break;

					case Node.TEXT_NODE:
						html = PHP.htmlspecialchars(root.data);
						break;
				}
				return html;
			};

			this.insert_node_at_selection = function(text) {
				this.checkFocus();

				var sel = this.editwin.getSelection();
				var range = sel ? sel.getRangeAt(0) : this.editdoc.createRange();
				sel.removeAllRanges();
				range.deleteContents();

				var node = range.startContainer;
				var pos = range.startOffset;

				switch(node.nodeType) {
					case Node.ELEMENT_NODE:
						if(text.nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
							selNode = text.firstChild;
						} else {
							selNode = text;
						}
						node.insertBefore(text, node.childNodes[pos]);
						this.add_range(selNode);
						break;

					case Node.TEXT_NODE:
						if(text.nodeType == Node.TEXT_NODE) {
							var text_length = pos + text.length;
							node.insertData(pos, text.data);
							range = this.editdoc.createRange();
							range.setEnd(node, text_length);
							range.setStart(node, text_length);
							sel.addRange(range);
						} else {
							node = node.splitText(pos);
							var selNode;
							if(text.nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
								selNode = text.firstChild;
							} else {
								selNode = text;
							}
							node.parentNode.insertBefore(text, node);
							this.add_range(selNode);
						}
						break;
				}
			};
		}

	} else {

		this.undo = function() {
			history.add_snapshot(this.getEditorContents());
			history.move_cursor(-1);
			if((str = history.get_snapshot()) !== false) {
				this.editdoc.value = str;
			}
		};

		this.redo = function() {
			history.move_cursor(1);
			if((str = history.get_snapshot()) !== false) {
				this.editdoc.value = str;
			}
		};

		this.strip_simple = function(tag, str, iterations) {
			var opentag = '[' + tag + ']';
			var closetag = '[/' + tag + ']';

			if(undefined(iterations)) {
				iterations = -1;
			}
			while((startindex = PHP.stripos(str, opentag)) !== false && iterations != 0) {
				iterations --;
				if((stopindex = PHP.stripos(str, closetag)) !== false) {
					var text = str.substr(startindex + opentag.length, stopindex - startindex - opentag.length);
					str = str.substr(0, startindex) + text + str.substr(stopindex + closetag.length);
				} else {
					break;
				}
			}
			return str;
		};

		this.strip_complex = function(tag, str, iterations) {
			var opentag = '[' + tag + '=';
			var closetag = '[/' + tag + ']';

			if(undefined(iterations)) {
				iterations = -1;
			}
			while((startindex = PHP.stripos(str, opentag)) !== false && iterations != 0) {
				iterations --;
				if((stopindex = PHP.stripos(str, closetag)) !== false) {
					var openend = PHP.stripos(str, ']', startindex);
					if(openend !== false && openend > startindex && openend < stopindex) {
						var text = str.substr(openend + 1, stopindex - openend - 1);
						str = str.substr(0, startindex) + text + str.substr(stopindex + closetag.length);
					} else {
						break;
					}
				} else {
					break;
				}
			}
			return str;
		};

		this.removeformat = function(e) {
			var simplestrip = new Array('b', 'i', 'u');
			var complexstrip = new Array('font', 'color', 'size');

			var str = this.getSel();
			if(str === false) {
				return;
			}
			for(var tag in simplestrip) {
				str = this.strip_simple(simplestrip[tag], str);
			}
			for(var tag in complexstrip) {
				str = this.strip_complex(complexstrip[tag], str);
			}
			this.insertText(str);
		};

		this.unlink = function(e) {
			var sel = this.getSel();
			sel = this.strip_simple('url', sel);
			sel = this.strip_complex('url', sel);
			this.insertText(sel);
		};

		this.prompt_link = function(tagname, value, phrase, iprompt) {
			if(undefined(value)) {
				value = this.showPrompt(phrase, iprompt);
			}
			if((value = this.verifyPrompt(value)) !== false) {
				if(this.getSel()) {
					this.applyFormat('unlink');
					this.wrapTags(tagname, value);
				} else {
					this.wrapTags(tagname, value, value);
				}
			}
			return true;
		};

		this.insertorderedlist = function(e) {
			this.insertlist(lang['insert_ordered_list'], '1');
		};

		this.insertunorderedlist = function(e) {
			this.insertlist(lang['insert_unordered_list'], '');
		};

		this.insertlist = function(phrase, listtype) {
			var opentag = '[list' + (listtype ? ('=' + listtype) : '') + ']\n';
			var closetag = '[/list]';

			if(txt = this.getSel()) {
				var regex = new RegExp('([\r\n]+|^[\r\n]*)(?!\\[\\*\\]|\\[\\/?list)(?=[^\r\n])', 'gi');
				txt = opentag + PHP.trim(txt).replace(regex, '$1[*]') + '\n' + closetag;
				this.insertText(txt, mb_strlen(txt), 0);
			} else {
				this.insertText(opentag + closetag, opentag.length, closetag.length);

				while(listvalue = prompt(lang['enter_list_item'], '')) {
					if(is_opera && opera.version() > 8) {
						listvalue = '\n' + '[*]' + listvalue;
						this.insertText(listvalue, mb_strlen(listvalue) + 1, 0);
					} else {
						listvalue = '[*]' + listvalue + '\n';
						this.insertText(listvalue, mb_strlen(listvalue), 0);
					}
				}
			}
		};

		this.outdent = function(e) {
			var sel = this.getSel();
			sel = this.strip_simple('indent', sel, 1);
			this.insertText(sel);
		};

		if(is_saf || (is_opera && (!opera.version || opera.version() < 8))) {

			this.insertlist = function(phrase, listtype) {
				var opentag = '[LIST' + (listtype ? ('=' + listtype) : '') + ']\n';
				var closetag = '[/LIST]';

				this.insertText(opentag);

				while(listvalue = prompt(vbphrase['enter_list_item'], '')) {
					listvalue = '[*]' + listvalue + '\n';
					this.insertText(listvalue);
				}

				this.insertText(closetag);
			};
		}
	}

	this.init();
}

function undefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function setUnselectable(obj) {
	if(!is_ie4 && typeof obj.tagName != 'undefined') {
		if(obj.hasChildNodes()) {
			for(var i = 0; i < obj.childNodes.length; i++) {
				setUnselectable(obj.childNodes[i]);
			}
		}
		obj.unselectable = 'on';
	}
}

function switchEditorMode(editorid, mode) {

	if(mode == Editor[editorid].wysiwyg || !validate(document.input, false, true))  {
		return;
	}

	popupmenu.hide();

	var url="ajax.php?";
	var ajaxdata = 'action=switcheditor&switchsubmit=yes'
		+ '&formhash=' + $("formhash").value
		+ '&wysiwyg='	+ mode
		+ '&fid=' + $("fid").value
		+ '&parseurloff=' + fetchCheckbox("parseurloff")
		+ '&smileyoff=' + fetchCheckbox("smileyoff")
		+ '&bbcodeoff=' + fetchCheckbox("bbcodeoff")
		+ '&htmlon=' + fetchCheckbox("htmlon")
		+ '&message=' + PHP.urlencode(Editor[editorid].getEditorContents());

	var request = createRequestObject();
	request.onreadystatechange = sendRequest(request, function() {ctrlAjax('disabled');}, function() {

		var parsesmilies = Editor[editorid].parsesmilies;
		Editor[editorid].destroy();

		var parsed_text = request.responseText;

		Editor[editorid] = null;

		Editor[editorid] = new wysiwygEditor(editorid, mode, parsesmilies, parsed_text);
		Editor[editorid].checkFocus();
		$(editorid + '_mode').value = mode;
		ctrlAjax('enabled');
	});

	request.open("POST", url, true);
 	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 	request.send(ajaxdata);
}

function wysiwygEditorEvents() {

	this.smilie_onclick = function(e) {
		Editor[this.editorid].insertSmiley(e, this.alt ? this.alt : this.pop, this.src, this.id.substr(this.id.lastIndexOf('_') + 1));
		if(typeof smilie_window != 'undefined' && !smilie_window.closed) {
			smilie_window.focus();
		}
		return false;
	};

	this.command_button_onmouseevent = function(e) {
		e = doane(e);
		if(e.type == 'click') {
			Editor[this.editorid].format(e, this.cmd, false, true);
		}
		Editor[this.editorid].button_context(this, e.type);
	};

	this.popup_button_onmouseevent = function(e) {
		e = doane(e);
		if(e.type == 'click') {
			this._onclick(e);
			Editor[this.editorid].menu_context(this, 'mouseover');
		} else {
			Editor[this.editorid].menu_context(this, e.type);
		}
	};

	this.popup_button_show = function(obj) {
		if(typeof Editor[obj.editorid].popups[obj.cmd] == 'undefined' || Editor[obj.editorid].popups[obj.cmd] == null) {
			Editor[obj.editorid].initPopupMenu(obj);
		}
		this._show(obj);
	};

	this.formatting_select_onchange = function(e) {
		var arg = this.options[this.selectedIndex].value;
		if(arg != '') {
			Editor[this.editorid].format(e, this.cmd, arg);
		}
		this.selectedIndex = 0;
	};

	this.menuoption_onmouseevent = function(e) {
		e = doane(e);
		Editor[this.editorid].button_context(this, e.type, 'menu');
	};

	this.formatting_option_onclick = function(e) {
		Editor[this.editorid].format(e, this.cmd, this.firstChild.innerHTML);
		popupmenu.hide();
	};

	this.coloroption_onclick = function(e) {
		$(this.editorid + '_color_bar').style.backgroundColor = this.colorname;
		Editor[this.editorid].format(e, this.cmd, this.colorname);
		popupmenu.hide();
	};

	this.colorout_onclick = function(e) {
		e = doane(e);
		Editor[this.editorid].format(e, 'forecolor', $(this.editorid + '_color_bar').style.backgroundColor);
		return false;
	};

	this.editwin_onfocus = function(e) {this.hasfocus = true;};
	this.editwin_onblur = function(e) {this.hasfocus = false;};

	this.editdoc_onmouseup = function(e) {
		Editor[this.editorid].setContext();
		popupmenu.hide();
	};

	this.editdoc_onkeyup = function(e) {Editor[this.editorid].setContext();};
}

function History() {

	this.cursor = -1;
	this.stack = new Array();

	this.move_cursor = function(increment) {
		var test = this.cursor + increment;
		if(test >= 0 && this.stack[test] != null && typeof this.stack[test] != 'undefined') {
			this.cursor += increment;
		}
	};

	this.add_snapshot = function(str) {
		if(this.stack[this.cursor] == str) {
			return;
		} else {
			this.cursor++;
			this.stack[this.cursor] = str;

			if(typeof this.stack[this.cursor + 1] != 'undefined') {
				this.stack[this.cursor + 1] = null;
			}
		}
	};

	this.get_snapshot = function() {
		if(typeof this.stack[this.cursor] != 'undefined' && this.stack[this.cursor] != null) {
			return this.stack[this.cursor];
		} else {
			return false;
		}
	};
}

function phpEmulator() {

	this.trim = function(str) {
		return (str.replace(/(\s+)$/g, '')).replace(/^\s+/g, '');
	};

	this.stripos = function(haystack, needle, offset) {
		if(typeof offset == 'undefined') {
			offset = 0;
		}
		index = haystack.toLowerCase().indexOf(needle.toLowerCase(), offset);

		return (index == -1 ? false : index);
	};

	this.htmlspecialchars = function(str) {

		var f = new Array(
			(is_mac && is_ie ? new RegExp('&', 'g') : new RegExp('&(?!#[0-9]+;)', 'g')),
			new RegExp('<', 'g'),
			new RegExp('>', 'g'),
			new RegExp('"', 'g')
		);
		var r = new Array(
			'&amp;',
			'&lt;',
			'&gt;',
			'&quot;'
		);

		for(var i = 0; i < f.length; i++) {
			str = str.replace(f[i], r[i]);
		}

		return str;
	};

	this.str_pad = function(text, length, padstring) {
		text = new String(text);
		padstring = new String(padstring);

		if(text.length < length) {
			padtext = new String(padstring);

			while(padtext.length < (length - text.length)) {
				padtext += padstring;
			}

			text = padtext.substr(0, (length - text.length)) + text;
		}

		return text;
	};

	this.urlencode = function(text) {
		text = text.toString();

		var matches = text.match(/[\x90-\xFF]/g);
		if(matches) {
			for(var matchid = 0; matchid < matches.length; matchid++) {
				var char_code = matches[matchid].charCodeAt(0);
				text = text.replace(matches[matchid], '%u00' + (char_code & 0xFF).toString(16));
			}
		}

		return escape(text).replace(/\+/g, "%2B");
	};
}

⌨️ 快捷键说明

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