seditor.js 6.8 KB

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