seditor.js
6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
[Discuz!] (C)2001-2099 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: seditor.js 28601 2012-03-06 02:49:55Z monkey $
*/
function seditor_showimgmenu(seditorkey) {
var imgurl = $(seditorkey + '_image_param_1').value;
var width = parseInt($(seditorkey + '_image_param_2').value);
var height = parseInt($(seditorkey + '_image_param_3').value);
var extparams = '';
if(width || height) {
extparams = '=' + width + ',' + height
}
seditor_insertunit(seditorkey, '[img' + extparams + ']' + imgurl, '[/img]', null, 1);
$(seditorkey + '_image_param_1').value = '';
hideMenu();
}
function seditor_menu(seditorkey, tag) {
var sel = false;
if(!isUndefined($(seditorkey + 'message').selectionStart)) {
sel = $(seditorkey + 'message').selectionEnd - $(seditorkey + 'message').selectionStart;
} else if(document.selection && document.selection.createRange) {
$(seditorkey + 'message').focus();
var sel = document.selection.createRange();
$(seditorkey + 'message').sel = sel;
sel = sel.text ? true : false;
}
if(sel) {
seditor_insertunit(seditorkey, '[' + tag + ']', '[/' + tag + ']');
return;
}
var ctrlid = seditorkey + tag;
var menuid = ctrlid + '_menu';
if(!$(menuid)) {
switch(tag) {
case 'at':
curatli = 0;
atsubmitid = ctrlid + '_submit';
setTimeout(function() {atFilter('', 'at_list','atListSet');$('atkeyword').focus();}, 100);
str = '请输用户名:<br /><input type="text" id="atkeyword" style="width:240px" value="" class="px" onkeydown="atFilter(this.value, \'at_list\',\'atListSet\',event);" /><div class="p_pop" id="at_list" style="width:250px;"><ul><li>@朋友账号,就能提醒他来看帖子</li></ul></div>';
submitstr = 'seditor_insertunit(\'' + seditorkey + '\', \'@\' + $(\'atkeyword\').value.replace(/<\\/?b>/g, \'\')+\' \'); hideMenu();';
break;
case 'url':
str = '请输入链接地址:<br /><input type="text" id="' + ctrlid + '_param_1" sautocomplete="off" style="width: 98%" value="" class="px" />' +
'<br />请输入链接文字:<br /><input type="text" id="' + ctrlid + '_param_2" style="width: 98%" value="" class="px" />';
submitstr = "$('" + ctrlid + "_param_2').value !== '' ? seditor_insertunit('" + seditorkey + "', '[url='+seditor_squarestrip($('" + ctrlid + "_param_1').value)+']'+$('" + ctrlid + "_param_2').value, '[/url]', null, 1) : seditor_insertunit('" + seditorkey + "', '[url]'+$('" + ctrlid + "_param_1').value, '[/url]', null, 1);hideMenu();";
break;
case 'code':
case 'quote':
var tagl = {'quote' : '请输入要插入的引用', 'code' : '请输入要插入的代码'};
str = tagl[tag] + ':<br /><textarea id="' + ctrlid + '_param_1" style="width: 98%" cols="50" rows="5" class="txtarea"></textarea>';
submitstr = "seditor_insertunit('" + seditorkey + "', '[" + tag + "]'+$('" + ctrlid + "_param_1').value, '[/" + tag + "]', null, 1);hideMenu();";
break;
case 'img':
str = '请输入图片地址:<br /><input type="text" id="' + ctrlid + '_param_1" style="width: 98%" value="" class="px" onchange="loadimgsize(this.value, \'' + seditorkey + '\',\'' + tag + '\')" />' +
'<p class="mtm">宽(可选): <input type="text" id="' + ctrlid + '_param_2" style="width: 15%" value="" class="px" /> ' +
'高(可选): <input type="text" id="' + ctrlid + '_param_3" style="width: 15%" value="" class="px" /></p>';
submitstr = "seditor_insertunit('" + seditorkey + "', '[img' + ($('" + ctrlid + "_param_2').value !== '' && $('" + ctrlid + "_param_3').value !== '' ? '='+$('" + ctrlid + "_param_2').value+','+$('" + ctrlid + "_param_3').value : '')+']'+seditor_squarestrip($('" + ctrlid + "_param_1').value), '[/img]', null, 1);hideMenu();";
break;
}
var menu = document.createElement('div');
menu.id = menuid;
menu.style.display = 'none';
menu.className = 'p_pof upf';
menu.style.width = '270px';
$('append_parent').appendChild(menu);
menu.innerHTML = '<span class="y"><a onclick="hideMenu()" class="flbc" href="javascript:;">关闭</a></span><div class="p_opt cl"><form onsubmit="' + submitstr + ';return false;" autocomplete="off"><div>' + str + '</div><div class="pns mtn"><button type="submit" id="' + ctrlid + '_submit" class="pn pnc"><strong>提交</strong></button><button type="button" onClick="hideMenu()" class="pn"><em>取消</em></button></div></form></div>';
}
showMenu({'ctrlid':ctrlid,'evt':'click','duration':3,'cache':0,'drag':1});
}
function seditor_squarestrip(str) {
str = str.replace('[', '%5B');
str = str.replace(']', '%5D');
return str;
}
function seditor_insertunit(key, text, textend, moveend, selappend) {
if($(key + 'message')) {
$(key + 'message').focus();
}
textend = isUndefined(textend) ? '' : textend;
moveend = isUndefined(textend) ? 0 : moveend;
selappend = isUndefined(selappend) ? 1 : selappend;
startlen = strlen(text);
endlen = strlen(textend);
if(!isUndefined($(key + 'message').selectionStart)) {
if(selappend) {
var opn = $(key + 'message').selectionStart + 0;
if(textend != '') {
text = text + $(key + 'message').value.substring($(key + 'message').selectionStart, $(key + 'message').selectionEnd) + textend;
}
$(key + 'message').value = $(key + 'message').value.substr(0, $(key + 'message').selectionStart) + text + $(key + 'message').value.substr($(key + 'message').selectionEnd);
if(!moveend) {
$(key + 'message').selectionStart = opn + strlen(text) - endlen;
$(key + 'message').selectionEnd = opn + strlen(text) - endlen;
}
} else {
text = text + textend;
$(key + 'message').value = $(key + 'message').value.substr(0, $(key + 'message').selectionStart) + text + $(key + 'message').value.substr($(key + 'message').selectionEnd);
}
} else if(document.selection && document.selection.createRange) {
var sel = document.selection.createRange();
if(!sel.text.length && $(key + 'message').sel) {
sel = $(key + 'message').sel;
$(key + 'message').sel = null;
}
if(selappend) {
if(textend != '') {
text = text + sel.text + textend;
}
sel.text = text.replace(/\r?\n/g, '\r\n');
if(!moveend) {
sel.moveStart('character', -endlen);
sel.moveEnd('character', -endlen);
}
sel.select();
} else {
sel.text = text + textend;
}
} else {
$(key + 'message').value += text;
}
hideMenu(2);
if(BROWSER.ie) {
doane();
}
}
function seditor_ctlent(event, script) {
if(event.ctrlKey && event.keyCode == 13 || event.altKey && event.keyCode == 83) {
eval(script);
}
}
function loadimgsize(imgurl, editor, p) {
var editor = !editor ? editorid : editor;
var s = new Object();
var p = !p ? '_image' : p;
s.img = new Image();
s.img.src = imgurl;
s.loadCheck = function () {
if(s.img.complete) {
$(editor + p + '_param_2').value = s.img.width ? s.img.width : '';
$(editor + p + '_param_3').value = s.img.height ? s.img.height : '';
} else {
setTimeout(function () {s.loadCheck();}, 100);
}
};
s.loadCheck();
}