📄 richtexteditor.js
字号:
// rich text area support
// x_core.js, main.js needed
/* global control */
var richTextCtrls = new Array();
function createRichText(textareaId){
var ordinal = richTextCtrls.length;
richTextCtrls.push(new richText(textareaId, ordinal));
return richTextCtrls[ordinal];
}
/* rich text definitions */
var ubbTypesMustWithAttribute = new Array('upload', 'size', 'color', 'font', 'quote');
var ubbTypesCanWithAttribute = new Array('url', 'email', 'img', 'wmp', 'real', 'hide');
richText = function(textareaId, ordinal){
this.textareaId = textareaId;
this.textareaCtrl = xGetElementById(this.textareaId);
this.richTextCtrlsOrdinal = ordinal;
}
richText.prototype = {
getSelectionRange : function(){
this.textareaCtrl.focus();
if(document.selection && document.selection.createRange){
// TextRange
return document.selection.createRange();
}else if(this.textareaCtrl.createTextRange){
// TextRange
return this.textareaCtrl.createTextRange();
}else if(window.getSelection){
// Range - not work properly
return window.getSelection().getRangeAt(0);
}else{
return null;
}
},
getSelectionText : function(){
var range = this.getSelectionRange();
if(range){
if(xStr(range.text)){
return range.text;
}else if(window.getSelection){
return window.getSelection().toString();
}else{
return range.toString();
}
}else{
if(document.getSelection){
return document.getSelection();
}else{
return '';
}
}
},
insertTags : function(tagName, attribute){
var pre_text = '[' + tagName;
if(attribute != null && attribute.length > 0){
pre_text += '=' + attribute;
}
pre_text += ']';
var suf_text = '[/' + tagName + ']';
var workRange = this.getSelectionRange();
if(workRange && xStr(workRange.text)){
// TextRange
workRange.text = pre_text + workRange.text + suf_text;
workRange.select();
}else{
this.textareaCtrl.value += pre_text + this.getSelectionText() + suf_text;
}
},
replaceText : function(newText){
var workRange = this.getSelectionRange();
if(workRange && xStr(workRange.text)){
// TextRange
workRange.text = newText;
workRange.select();
}else{
this.textareaCtrl.value += newText;
}
},
// in postEdit only
enableMoodSupport : function(){
try{
xGetElementById('PostMoodEnabled').checked = true;
}catch(e){
}
},
// in postEdit only
enableCodeSupport : function(){
try{
xGetElementById('PostCodeEnabled').checked = true;
}catch(e){
}
},
/* insert ubb tags */
insertUBB : function(ubbType){
if(!xStr(ubbType)){
return;
}
ubbType = ubbType.toLowerCase();
var attribute = null;
if(ubbTypesMustWithAttribute.contains(ubbType)){
switch(ubbType){
case 'upload':
var upload_type;
if(confirm('您是否已经上传该附件并且知道附件 ID ?')){
upload_type = 1;
}else{
upload_type = 0;
}
attribute = prompt('请输入该附件' + (upload_type == 0 ? '在本帖全部附件中的位次(1为起始位次):' : '的 ID :'), '') +
',' + upload_type.toString();
break;
case 'size':
attribute = prompt('请输入文字大小(1-7):', '3');
break;
case 'color':
attribute = prompt('请输入文字颜色(系统预置名或者颜色数值):', '');
break;
case 'font':
attribute = prompt('请输入字体名称:', '');
break;
case 'quote':
attribute = prompt('请输入被引用者的用户名或者所引用内容的出处:', '');
break;
}
if(attribute == null || attribute.length == 0){
alert('您已取消该操作。');
}
}else if(ubbTypesCanWithAttribute.contains(ubbType)){
switch(ubbType){
case 'url':
if(!confirm('需要将显示内容作为超链接吗?')){
attribute = prompt('请输入显示内容所指向的链接:', 'http://');
}
break;
case 'email':
if(!confirm('需要将显示内容作为邮件链接吗?')){
attribute = prompt('请输入显示内容所指向的邮件地址:', '');
}
break;
case 'img':
if(confirm('需要手动设定图片的大小吗?')){
var width = parseInt(prompt('请输入图片的宽度:', ''));
var height = parseInt(prompt('请输入图片的高度:', ''));
if(isNaN(width) || isNaN(height) || width <= 0 || height <= 0){
alert('图片大小应当为正整数。');
return;
}
attribute = width + ',' + height;
}
break;
case 'wmp':
case 'real':
if(confirm('需要设定媒体样式吗?')){
var width = parseInt(prompt('请输入媒体控件的宽度:', '460'));
var height = parseInt(prompt('请输入媒体控件的高度:', (ubbType == 'wmp') ? '45' : '50'));
var mode = parseInt(prompt('请输入媒体控件的显示模式(0-3:偶数则自动播放;大于1则显示图像):', ''));
if(isNaN(width) || isNaN(height) || isNaN(mode) || width <= 0 || height <= 0 || mode < 0 || mode > 3){
alert('控件大小应当为正整数,并且模式的值应当位于0和3之间。');
return;
}
attribute = width + ',' + height + ',' + mode;
}
break;
case 'hide':
if(confirm('需要限定阅读指定内容需要达到的经验值吗?在楼主帖中不限定经验值代表需回复后方可阅读指定内容。')){
attribute = prompt('请输入阅读隐藏内容需要达到的经验值:', '');
}
break;
}
}
if(ubbType.indexOf('align') == 0){
switch(ubbType){
case 'align_left':
ubbType = 'align';
attribute = 'left';
break;
case 'align_center':
ubbType = 'align';
attribute = 'center';
break;
case 'align_right':
ubbType = 'align';
attribute = 'right';
break;
}
}else if(ubbType == 'list'){
var text = prompt('请输入列表的标题:', '');
if(text == null){
return;
}
text = '[list]' + text + '\r\n';
var count = parseInt(prompt('请输入列表项的数目:', ''));
if(isNaN(count) || count < 1){
alert('列表项数应当为正整数。');
return;
}
var ele;
for(var i = 0; i < count; i++){
ele = prompt('请输入第' + (i + 1).toString() + '项的内容:', '');
if(ele == null){
break;
}
text += '[*]' + ele + '\r\n';
}
text += '[/list]';
this.replaceText(text);
this.enableCodeSupport();
return;
}
this.insertTags(ubbType, attribute);
this.enableCodeSupport();
},
/* insert mood tags */
insertMood : function(moodId){
moodId = parseInt(moodId);
if(isNaN(moodId) || moodId < 1){
return;
}
var text = '[m' + moodId + ']';
this.replaceText(text);
this.enableMoodSupport();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -