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

📄 editor.js

📁 动网论坛系列产品倍受用户赞誉的就是强大的功能和出色的用户体验表现
💻 JS
📖 第 1 页 / 共 2 页
字号:
// ------------------------------------------------------------------------------------------
// Editor for Dvbbs bbs
// Copyright(c)2006-2007, Dvbbs. All rights reserved.
// version 1.0
// http://www.cndw.com | http://www.dvbbs.net
// ------------------------------------------------------------------------------------------


var originContent = "";
var bbsEditor = new Editor();
bbsEditor.initializate();
var isIE = navigator.appName == "Microsoft Internet Explorer";
var isNetscape = navigator.appName == "Netscape";

function Editor()
{
	this.editor			= null;
	this.design		  	= true;
	this.editor_design	= null;
	this.editor_html	= null;
  
	this.initializate 	= function()
	{	
		if (document.getElementById("htmlEditor") == null) return;
		var f = document.forms['editor_form'];
		if (!f) f = document.forms['__aspnetForm'];	
		if (!f) f = document.forms['aspnetForm'];	
		
		f.submit = function()
		{
			alert("本论坛不允许从地址栏提交,请按提交按钮提交!");
		}
		this.editor_html	= f.editor_content;
		this.editor_design	= htmlEditor;
		this.editor			= this.editor_design;
		this.editor.document.designMode = "on"; 
		this.initToolbar();						
	    this.initEmotion();
		setTimeout("getOrignContent();", 200); 
	}

	this.initToolbar = initToolbar;			
	this.initEmotion = initEmotion;
	
	this.cmd = function(sCmd, bInterface, vValue)	
	{
	//	if (!this.design) return;
		this.editor.focus();
		this.editor.document.execCommand(sCmd, bInterface, vValue);
	}
	
	this.setHTML = function(str)
	{
		this.editor_design.document.body.innerHTML = str;
	}
	
	this.getHTML = function()
	{
		return this.editor_design.document.body.innerHTML;
	}

	this.setText = function(str)
	{
		this.editor_html.value = str;
	}

	this.getText = function()
	{
		return this.editor_html.value;
	}

	this.isEmpty = function()
	{
		this.poiseData();
		if (this.getHTML().indexOf("IMG") >= 0 || this.getHTML().indexOf("img") >= 0)
		{
			return false;
		}
		var r;
		if (isIE)
			r = this.editor_design.document.body.innerText  == "";
		else
			r = this.editor_design.document.body.textContent  == "";
		return r;
	}
	
	this.poiseData = function()
	{
		if (this.design)
		{
			this.editor_html.value = this.getHTML();
		}
		else
		{
			this.setHTML(this.editor_html.value);
		}
	}
	this.getContent	= function()
	{
		this.poiseData();
		return this.filterUnsafeCode(this.getHTML());
	}

	this.setContent	= function(str)
	{
		this.setHTML(str);
		this.editor_html.value = str;
	}

	this.htmlMode = function()
	{
		this.toSafeCode();
		document.getElementById("htmlEditor").style.display = "none";
		this.editor = this.editor_html;
		this.editor_html.style.display = "";
		this.editor_html.value = this.getHTML().replace(/<FONT>(.*)<\/FONT>/ig, "$1");	
	}
	
	this.toSafeCode = function()
	{
		var toRemoveElement=new Array();
		var removeCount = 0;
		
		var elements;
		if (isIE)
		{
			elements = this.editor_design.document.all;
		}
		else
			elements = this.editor_design.document.getElementsByTagName("*");

		for(var i=0; i<elements.length; i++)
		{
			var current = elements[i];
			switch(current.tagName.toUpperCase())
			{
				case "A":
					this.reserveAttribute(current, "href,title");
					current.setAttribute("target", "_blank");
					break;
				case "B":
					this.reserveAttribute(current, "");
					break;
				case "BIG":
					this.reserveAttribute(current, "");
					break;
				case "BLOCKQUOTE":
					this.reserveAttribute(current, "");
					break;
				case "BR":
					this.reserveAttribute(current, "");
					break;
				case "CENTER":
					this.reserveAttribute(current, "");
					break;
				case "DIV":
					this.reserveAttribute(current, "");
					break;
				case "EM":
					this.reserveAttribute(current, "");
					break;
				case "FONT":
					this.reserveAttribute(current, "color,face,size");
					if (current.size == "+0")
					{
						current.removeAttribute("size");
					}
					break;
				case "HR":
					this.reserveAttribute(current, "");
					break;
				case "I":
					this.reserveAttribute(current, "");
					break;
				case "IMG":
					this.reserveAttribute(current, "src,alt,onload");
					current.setAttribute("onload", "if(this.width>515)this.width=515");
					break;
				case "LABEL":
					this.reserveAttribute(current, "title");
					break;
				case "LI":
					this.reserveAttribute(current, "");
					break;
				case "MARQUEE":
					this.reserveAttribute(current, "direction,loop");
					break;
				case "OL":
					this.reserveAttribute(current, "");
					break;
				case "P":
					this.reserveAttribute(current, "align");
					break;
				case "SPAN":
					this.reserveAttribute(current, isNetscape ? "style" : "");
					break;
				case "STRIKE":
					this.reserveAttribute(current, "");
					break;
				case "STRONG":
					this.reserveAttribute(current, "");
					break;
				case "SUP":
					this.reserveAttribute(current, "");
					break;
				case "SUB":
					this.reserveAttribute(current, "");
					break;
				case "TABLE":
					this.reserveAttribute(current, "ALIGN,BGCOLOR,BORDER,BORDERCOLOR,CELLPADDING,CELLSPACING,WIDTH");
					break;
				case "TBODY":
					this.reserveAttribute(current, "");
					break;
				case "TD":
					this.reserveAttribute(current, "ALIGN,BGCOLOR,BORDER,BORDERCOLOR,BACKGROUND,COLSPAN,NOWRAP,ROWSPAN,VALIGN,WIDTH");
					break;
				case "TR":
					this.reserveAttribute(current, "ALIGN,BGCOLOR,BORDER,BORDERCOLOR,BACKGROUND,COLSPAN,NOWRAP,ROWSPAN,VALIGN,WIDTH");
					break;
				case "U":
					this.reserveAttribute(current, "");
					break;
				case "UL":
					this.reserveAttribute(current, "");
					break;

				default:
				{
					var reverse = "HTML,HEAD,TITLE,META,LINK,BODY";
					if (reverse.indexOf(current.tagName) < 0)
					{
						toRemoveElement[removeCount++] = current;
						i--;
					}
					
					break;
				}
					
			}
			for(var k=removeCount-1; k>=0; k--)
				toRemoveElement[k].removeNode(true);
			
		}
	}
	
	this.reserveAttribute = function(tag, attributeNames)
	{
		for(var i=0; i<tag.attributes.length; i++)
		{
			if(attributeNames.toLowerCase().indexOf(tag.attributes[i].nodeName.toLowerCase()) == -1)
				tag.removeAttribute(tag.attributes[i].nodeName);
		}
	}
	this.designMode	= function()
	{
		document.getElementById("htmlEditor").style.display = "";
		this.editor_html.style.display = "none";				
		this.editor = this.editor_design;
		this.setHTML(this.editor_html.value);			
	}

	this.switchMode	= function()
	{
		if (this.design)
		{
			this.htmlMode();
		}
		else
		{
			this.designMode();
		}
		this.design = !this.design;
	}
	

	this.insert	= function(shtml)
	{   
		
		this.editor.focus();
		if (this.design)
		{
			if (isIE)
			{
				var range = this.editor.document.selection.createRange();
				range.pasteHTML(shtml.replace("$content", ""));
			}
			else
			{
				this.setHTML(this.getHTML() + shtml);
			}
			
		}
		else
		{
			if (isIE)
			{
				var range = document.selection.createRange()
				if (range != null)
				{
					range.text =  shtml.replace("$content", range.text);
					
				}
				else
					this.editor.value += shtml.replace("$content", "");
			}
			else
			{
				this.editor.value += shtml.replace("$content", "");
			}
		}
		
	}

	this.add = function(shtml)
	{  
		if (this.design)
		{
			if (isIE)
			{
				var range = this.editor.document.selection.createRange();
				range.pasteHTML(shtml);
			}
			else
			{
				this.setHTML(this.getHTML() + shtml);
			}
		}
		else
		{
			this.editor.value += shtml;
		}
		
	}
	
	this.filterUnsafeCode = function(str)
	{
		
		var uscode = "";
		if (getDisabledTools().indexOf('code') > -1)
		{
			//code
			str = str.replace(/\[code\][\s\S]*?\[\/code\]/gi, "");
		}
		str = str.replace(/<style.*?>[\s\S]*?<\/style>/ig, '');
		str = str.replace(/<script.*?>[\s\S]*?<\/script>/ig, '');
		str = str.replace(/<noscript.*?>[\s\S]*?<\/noscript>/ig, '');
		str = str.replace(/<select.*?>[\s\S]*?<\/select>/ig, '');
		str = str.replace(/<object.*?>[\s\S]*?<\/object>/ig, '');
	//	str= str.replace(/\s*\[code\]([\s\S]+?)\[\/code\]\s*/ig, function($1, $2) {return codetag($2);});
		
		for(var i = 0; i < codecount; i++)
			str = str.replace("[\tUBB_CODE_" + i + "\t]", codehtml[i]);
		
		if (getDisabledTools().indexOf('flash') > -1)
		{
			str = str.replace(/\[flash\](.[^\[]*\.swf)\[\/flash\]/gi, "");
			str = str.replace(/\[flash=(\d+),(\d+)\](.[^\[]*\.swf)\[\/flash\]/gi, "");
		}
		if (getDisabledTools().indexOf('media') > -1)
		{
			//media
			str = str.replace(/\*/gi, "*;");
			//	\.(avi|asx|wpl|aif|aifc|aiff|au|asf|mpeg|mpg|m1v|mp2|mpv2|mp2v|mpa|mid|midi|rmi|snd|wma|wmv|wm|wav|wax|m3u|wpl|mp3)
			str = str.replace(/\[(\/)?(MP(=([0-9]+),([0-9]+))?)\]/gi, "*$1$2*").replace(/\*MP=([0-9]+),([0-9]+)\*(.[^\*]*)\*\/MP\*/gi, "");
			str = str.replace(/\*(\/)?(MP(=([0-9]+),([0-9]+))?)\*/gi, "[$1$2]").replace(/\*;/gi, "*");
			//real
			str = str.replace(/\*/gi, "*;");
			//	\.(rm|rma|ra|ram|rmm|rmvb|au|aif|mid|mp3|wav|wov|wma|smi|smil|m3u|mov|mpg|mpeg)
			str = str.replace(/\[(\/)?(RM(=([0-9]+),([0-9]+))?)\]/gi, "*$1$2*").replace(/\*RM=([0-9]+),([0-9]+)\*(.[^\*]*)\*\/RM\*/gi, "");
			str = str.replace(/\*(\/)?(RM(=([0-9]+),([0-9]+))?)\*/gi, "[$1$2]").replace(/\*;/gi, "*");
		}

		if (getDisabledTools().indexOf('quote') > -1)
		{
			//quote
			str = str.replace(/\[quoteid\]([^\[]*)\[\/quoteid\]/gi, "");
			str = str.replace(/\[quotetitle\]([^\[]*)\[\/quotetitle\]/gi, "");
			str = str.replace(/\[quote\](<br>|&nbsp;)*/gi, "");
			str = str.replace(/(<br>)*\[\/quote\]/gi, "");
		}
		if (getDisabledTools().indexOf('hide') > -1)
		{
			//code
			str = str.replace(/\[hide\]([^\[]*)\[\/hide\]/gi, "");
		}
		return str;
	}
	
	this.resize = function(offset)
	{
		
		var o = parseInt(document.all.htmlEditor.style.height);

		if (o > 600 && offset > 0) return;
		if (o < 150 && offset < 0) return;

		var n = parseInt(o) + parseInt(offset);			

		document.all.htmlEditor.style.height	= parseInt(n);
		this.editor_html.style.height			= parseInt(n);
	}

}

function initEmotion()
{
	var emotions   = "";
	for (var i=1; i<=20; i++)
	{
		emotions += "<IMG style=\"CURSOR: hand\" onclick=\"emote('" + addZero(i) + "')\" src=\"emotion/" + addZero(i) + ".gif\">\n";
	}
	var e = document.getElementById("emotions");
	if (e != null)
	{
		e.innerHTML = emotions;
	}	

}

function addZero(n)
{
	if(n > 9) return n;
	return "0" + n;
}

function initToolbar()
{	
	var toolsArray = new Array(    
		"print", "打印当前内容",
		"removeformat", "清除文本格式",
		"redo", "取消撤消",
		"undo",	"撤消",
		"copy", "复制",
		"paste", "粘贴",
		"cut", "剪切",
		"bold", "粗体",
		"italic", "斜体",
		"underline", "带下划线",
		"strikethrough", "删除线",
		"justifyleft", "居左",
		"justifycenter", "居中",
		"justifyright", "居右",
		"insertorderedlist", "项目符号",
		"insertunorderedlist", "编号",
		"indent", "增加缩进量",
		"outdent", "减小缩进量",
		"subscript", "下标",
		"superscript", "上标",
		"-",
		"fontname",
		"forecolor",
		"fontsize",
		"#createlink","超级链接",
		"unlink","清除超级链接",
		"#email","邮件地址",
		"#image","插入图片",
		"#mediaplay","[MP(=高,宽)]插入 Media 视频[/MP]",
		"#real","[RM(=高,宽)]插入 Real 视频[/RM]",
		"#flash","[flash(=高,宽)]插入Flash[/flash]",
		"#hide","[hide]回复后可见[/hide]",
		"#quote","[quote]引用内容[/quote]",
		"#code","[code]插入代码段[/code]",
		"#increase","增大编辑区域",
		"#decrease", "减小编辑区域"
		);
	var bar = "";
	for (var i=0; i<toolsArray.length; i++ )
	{
	
		if (toolsArray[i] == "-")
		{
			bar += "<p style='padding-top:5px;'></p>";
			continue;
		}
		if (toolsArray[i] == "fontname")
		{
			bar += " <select onchange=\"setFont('fontname',  this)\"><option value=\"宋体\" selected=\"selected\">宋体</option><option value=\"黑体\">黑体</option><option value=\"sans-serif\">Sans-Serif</option><option value=\"courier new\">Courier New</option><option value=\"arial\">Arial</option><option value=\"tahoma\">Tahoma</option><option value=\"verdana\">Verdana</option><option value=\"fixedsys\">FixedSys</option></select>";
			continue;
		}
		if (toolsArray[i] == "forecolor")
		{
			bar += " <select onchange=\"setFont('forecolor', this)\"><option value=\"black\" selected=\"selected\" style=\"background:black;color:black\">Black</option><option value=\"red\" style=\"background:red;color:red\">Red</option><option value=\"pink\" style=\"background:pink;color:pink\">Pink</option><option value=\"green\" style=\"background:green;color:green\">Green</option><option value=\"orange\" style=\"background:orange;color:orange\">Orange</option><option value=\"purple\" style=\"background:purple;color:purple\">Purple</option><option value=\"blue\" style=\"background:blue;color:blue\">Blue</option><option value=\"brown\" style=\"background:brown;color:brown\">Brown</option><option value=\"teal\" style=\"background:teal;color:teal\">Teal</option><option value=\"navy\" style=\"background:navy;color:navy\">Navy</option><option value=\"maroon\" style=\"background:maroon;color:maroon\">Maroon</option><option value=\"limegreen\" style=\"background:limegreen;color:limegreen\">LimeGreen</option></select>";
			continue;
		}
		if (toolsArray[i] == "fontsize")
		{
			bar += " <select onchange=\"setFont('fontsize',  this)\"><option value=\"1\" selected=\"selected\">1</option><option value=\"2\">2</option><option value=\"4\">4</option><option value=\"6\">6</option><option value=\"8\">8</option></select>&nbsp;&nbsp;";
			continue;
		}
		if (toolsArray[i].indexOf("#") == 0)
		{   
			var cmdName = toolsArray[i].replace("#", "");
			bar += " <img src=\"images/ubb/" + cmdName + ".gif\" title=\"" + toolsArray[++i] + "\" onclick=\"" + cmdName + "()\" onmouseover=\"this.className='Dvbbs_BtnMouseOverUp';\" onmouseout=\"this.className='Dvbbs_Btn';\"/>";
			continue;
		}
		bar += " <img src=\"images/ubb/" + toolsArray[i] + ".gif\" title=\"" + toolsArray[++i] + "\" onclick=\"cmd('" + toolsArray[i-1] + "')\" onmouseover=\"this.className='Dvbbs_BtnMouseOverUp';\" onmouseout=\"this.className='Dvbbs_Btn';\"/>"
	}

	var tools = document.getElementById("tools");
	if (tools != null)
	{
		tools.innerHTML = bar;	
	}	
}

function cmd(sCmd)
{
	if (bbsEditor.design)
	{
		bbsEditor.cmd(sCmd, false, "");
		return;
	}
	else
	{
		switch (sCmd)
		{
		case "bold":
			bbsEditor.insert("<STRONG>$content</STRONG>");
			break;
		case "italic":
			bbsEditor.insert("<EM>$content</EM>");
			break;
		case "underline":

⌨️ 快捷键说明

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