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

📄 editor.js

📁 论坛系统EasyJForum 是一个基于 Java 技术的免费社区论坛软件系统
💻 JS
📖 第 1 页 / 共 2 页
字号:
/*
	[EasyJForum] (C)2007 Hongshee Soft.
	$file: editor.js $
	$Date: 2007/11/30 $
*/

var editbox, editwin, editdoc;
var editcss = null;
var initialized = false;

function setContent(text) 
{
	if(wysiwyg) {
		if(trim(text) == '' && !is_ie) {
			text = '<br/>';
		}
		if(!initialized) {
			initEditDoc();
		}
		editdoc.body.innerHTML = text;
	} else {
		textobj.value = text;
	}
	setEditorStyle();
}

function createEditor(mode, initialtext) 
{
	wysiwyg = parseInt(mode);
	$('htmlmode').className = wysiwyg ? 'editor_switcher' : '';
	$('wysiwygmode').className = wysiwyg ? '' : 'editor_switcher';

	if(wysiwyg) {
		if($('htmleditor_iframe')) {
			editbox = $('htmleditor_iframe');
		} else {
			var iframe = document.createElement('iframe');
			editbox = textobj.parentNode.appendChild(iframe);
			editbox.id = 'htmleditor_iframe';
		}
		editwin = editbox.contentWindow;
		editdoc = editwin.document;
		if(!is_firefox || is_firefox >= '3.0')
		{
			initialized = false;
			editcss = null;
		}
		setContent(typeof(initialtext)=='undefined' ? textobj.value : initialtext);
	} else {
		editbox = editwin = editdoc = textobj;
		if(typeof(initialtext)!='undefined') {
			setContent(initialtext);
		}
	}
	setEditorEvents();
	initEditor();
}

function initEditor() 
{
	var buttons = $('htmleditor_controls').getElementsByTagName('a');
	for(var i = 0; i < buttons.length; i++) {
		if(buttons[i].id.indexOf('htmleditor_cmd_') != -1) {
			buttons[i].href = '###';
			buttons[i].onclick = function(e) {ejfcode(this.id.substr(this.id.lastIndexOf('_cmd_') + 5))};
		}else if(buttons[i].id.indexOf('htmleditor_popup_') != -1) {
			buttons[i].href = '###';
			buttons[i].onclick = function(e) {
				$((this.id + '_menu')).style.display == "none" ? showMenu(this.id, true) : hideMenu();
			};
		}
	}
	setUnselectable($('htmleditor_controls'));
	textobj.onselect = textobj.onclick = textobj.onkeyup = saveCaret;
	textobj.onkeydown = validateKeys;
	textobj.onbeforepaste = validatePaste;
	$('htmlmode').onclick = function(e) {switchEditor(0)};
	$('wysiwygmode').onclick = function(e) {switchEditor(1)};
}

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

function initEditDoc()
{
	editdoc.designMode = 'on';
	editdoc = editwin.document;
	editdoc.open('text/html', 'replace');
	if (is_ie)
		editdoc.write('');
	else
		editdoc.write('<br/>');
	editdoc.close();
	editdoc.body.contentEditable = true;
	initialized = true;
}

function getContent()
{
	return wysiwyg ? editdoc.body.innerHTML : editdoc.value;
}

function setEditorStyle() {
	if(wysiwyg) {
		textobj.style.display = 'none';
		editbox.style.display = '';

		if(editcss == null) {
			var cssarray = [forumcss, editorcss];
			for(var i = 0; i < 2; i++) {
				editcss = editdoc.createElement('link');
				editcss.type = 'text/css';
				editcss.rel = 'stylesheet';
				editcss.href = cssarray[i];
				var headNode = editdoc.getElementsByTagName("head")[0];
				headNode.appendChild(editcss);
			}	
		}

		if(is_ie) {
			editdoc.body.style.border = '1px';
			editdoc.body.addBehavior('#default#userData');
		} else {
			editbox.style.border = '0px';
		}
		editdoc.body.className = "editor_doc";
		editbox.style.width = textobj.style.width;
		editbox.style.height = textobj.style.height;
		editdoc.body.style.textAlign = 'left';
		editdoc.body.id = 'wysiwyg';

	} else {
		var iframe = textobj.parentNode.getElementsByTagName('iframe')[0];
		if(iframe) {
			textobj.style.display = '';
			textobj.style.width = iframe.style.width;
			textobj.style.height = iframe.style.height;
			iframe.style.display = 'none';
		}
	}
}

function setEditorEvents() 
{
	if(wysiwyg) {
		if(is_ie) {
			editdoc.onmouseup = function(e) {renderContext();};
			editdoc.onkeyup = function(e) {renderContext();};
		} else {
			editdoc.addEventListener('mouseup', function(e) {renderContext();}, true);
			editdoc.addEventListener('keyup', function(e) {renderContext();}, true);
			editwin.addEventListener('focus', function(e) {this.hasfocus = true;}, true);
			editwin.addEventListener('blur', function(e) {this.hasfocus = false;}, true);
		}
	}
	editwin.onfocus = function(e) {this.hasfocus = true;};
	editwin.onblur = function(e) {this.hasfocus = false;};
}

function getCaret() 
{
	var obj = editdoc.body;
	var sel = document.selection.createRange();
	sel.setEndPoint("StartToStart", obj.createTextRange());
	return sel.text.replace(/\r?\n/g, ' ').length;
}

function setCaret(pos) 
{
	var obj = wysiwyg ? editdoc.body : editbox;
	var txt = obj.createTextRange();
	txt.moveStart('character', pos);
	txt.collapse(true);
	txt.select();
}

function saveCaret()
{
	if(textobj.createTextRange){
		textobj.caretPos = document.selection.createRange().duplicate();
	}
}

function validateKeys(e)
{
	if (allowhtml)
		return true;
	else
	{
		e = e ? e : event;
    	if((e.keyCode>=65 && e.keyCode<=90) || (e.keyCode>=97 && e.keyCode<=122))
			return false;
	    else 
    	    return true;
	}
}

function validatePaste(e)
{
	if (allowhtml)
		return true;
	else
		return false;
}

function editMenu_keydown(e) 
{
	e = e ? e : event;
	var ctrlid = this.id.substr(0, this.id.lastIndexOf('_param_'));
	if((this.type == 'text' && e.keyCode == 13) 
		|| (this.type == 'textarea' && e.ctrlKey && e.keyCode == 13)) {
		$(ctrlid + '_submit').click();
		cancel(e);
	}else if(e.keyCode == 27) {
		hideMenu();
		document.body.removeChild($(ctrlid + '_menu'));
	}
}

function createEditMenu(ctrlid, text) 
{
	var div = document.createElement('div');
	div.id = ctrlid + '_menu';
	div.className = 'popmenu_popup';
	div.style.display = 'none';
	document.body.appendChild(div);
	div.innerHTML = '<div unselectable="on">' + text 
				  + '<br/><p align=center style="padding-top:6px"><input type="button" id="' + ctrlid 
				  + '_submit" value="' + msgs['submit'] 
				  + '"/> &nbsp; <input type="button" onClick="hideMenu();try{document.body.removeChild(' 
				  + div.id + ')}catch(e){}" value="' + msgs['cancel'] + '"/></p></div>';
	showMenu(ctrlid, true);
	return div;
}

function ejfcode(cmd, arg) 
{
	if (!wysiwyg) return;
	checkFocus();
	if(isInArray(cmd, ['quote', 'code'])) {
		var sel, pos;
		if(is_ie) {
			sel = editdoc.selection.createRange();
			pos = getCaret();
		}
		var selection = sel ? sel.htmlText : getSel();
		selection = trim(selection);
		var tmpStr = selection.toLowerCase().replace(/&nbsp;|\s/ig,'');
		tmpStr = tmpStr.replace(/<(.+)><\/\1>/ig,'');
		tmpStr = tmpStr.replace(/<p[^>]*><\/p>/ig,'');
		if (tmpStr == '')
			selection = '';
		if(selection) {
			if (cmd == 'quote')
				selection = "<BR/><div class='quote'><h5>"+msgs['quote']+":</h5><blockquote>"+selection
						  +"</blockquote></div><BR/>";
			else if (cmd == "code")
				selection = "<BR/><div class='anycode'><code>"+selection+"</code></div><BR/>";
			insertHTML(selection, 0, 0, true, sel);
		} else {
			var ctrlid = 'htmleditor_cmd_' + cmd;
			var text = 
				msgs['enter_'+cmd+'_title'] + '<br/><textarea id="'+ctrlid+'_param_1" cols="80" rows="10"></textarea>';
			var div = createEditMenu(ctrlid, text);
			$(ctrlid + '_param_1').focus();
			$(ctrlid + '_param_1').onkeydown = editMenu_keydown;
			$(ctrlid + '_submit').onclick = function() {
				checkFocus();
				if(is_ie) setCaret(pos);
				var txt = selection ? selection : $(ctrlid + '_param_1').value;
				txt = txt.replace(/\r?\n/g, '<br/>');
				if (cmd == 'quote')
					txt = "<BR/><div class='quote'><h5>"+msgs['quote']+":</h5><blockquote>"+txt+"</blockquote></div><BR/>";
				else if (cmd == "code")
					txt = "<BR/><div class='anycode'><code>"+txt+"</code></div><BR/>";
				insertHTML(txt, 0, 0, false, sel);
				hideMenu();
				document.body.removeChild(div);
			}
		}
	}else if(isInArray(cmd, ['url', 'img', 'media'])) {
		insertLink(cmd);
	}else{
		try {
			editdoc.execCommand(cmd, false, (typeof(arg)=='undefined' ? true : arg));
		} catch(e) {
			// Ignored
		}
	}
	renderContext(cmd);
}

function renderContext(cmd) 
{
	var fcolor = editdoc.queryCommandValue('forecolor');
	$('htmleditor_color_bar').style.backgroundColor = RGBToColor(fcolor);

	var ftname = editdoc.queryCommandValue('fontname');
	if(ftname == '' && !is_ie && window.getComputedStyle) {
		ftname = editdoc.body.style.fontFamily;
	} else if(ftname == null) {
		ftname = '';
	}
	ftname = ftname ? ftname : 'Helvetica';
	if(ftname != $('htmleditor_font_out').fontstate) {
		var ft1 = ftname.indexOf(',') > 0 ? ftname.substr(0, ftname.indexOf(',')) : ftname;
		$('htmleditor_font_out').innerHTML = ft1;
		$('htmleditor_font_out').fontstate = ftname;
	}

	var ftsize = editdoc.queryCommandValue('fontsize');
	if(ftsize == null || ftsize == '') {
		ftsize = getFontSize(editdoc.body.style.fontSize);
	}
	if(ftsize != $('htmleditor_size_out').sizestate) {
		if($('htmleditor_size_out').sizestate == null) {
			$('htmleditor_size_out').sizestate = '';
		}
		$('htmleditor_size_out').innerHTML = ftsize;
		$('htmleditor_size_out').sizestate = ftsize;
	}
}

function getFontSize(ftsize) 
{
	switch(ftsize) {
		case '7.5pt':
		case '10px': return 1;
		case '10pt': return 2;
		case '12pt': return 3;
		case '14pt': return 4;
		case '18pt': return 5;

⌨️ 快捷键说明

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