Editor.js
上传用户:luoweizhao
上传日期:2021-11-29
资源大小:15269k
文件大小:32k
源码类别:

OA系统

开发平台:

ASP/ASPX

  1. /*
  2. *☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆
  3. *★                                                                  ★
  4. *☆                eWebEditor - eWebSoft在线编辑器                   ☆
  5. *★                                                                  ★
  6. *☆  版权所有: eWebSoft.com                                          ☆
  7. *★                                                                  ★
  8. *☆  程序制作: eWeb开发团队                                          ☆
  9. *★            email:webmaster@webasp.net                            ★
  10. *☆            QQ:589808                                             ☆
  11. *★                                                                  ★
  12. *☆  相关网址: [产品介绍]http://www.eWebSoft.com/Product/eWebEditor/ ☆
  13. *★            [支持论坛]http://bbs.eWebSoft.com/                    ★
  14. *☆                                                                  ☆
  15. *★  主页地址: http://www.eWebSoft.com/   eWebSoft团队及产品         ★
  16. *☆            http://www.webasp.net/     WEB技术及应用资源网站      ☆
  17. *★            http://bbs.webasp.net/     WEB技术交流论坛            ★
  18. *★                                                                  ★
  19. *☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆
  20. */
  21. // 当前模式
  22. var sCurrMode = null;
  23. var bEditMode = null;
  24. // 连接对象
  25. var oLinkField = null;
  26. // 浏览器版本检测
  27. var BrowserInfo = new Object() ;
  28. BrowserInfo.MajorVer = navigator.appVersion.match(/MSIE (.)/)[1] ;
  29. BrowserInfo.MinorVer = navigator.appVersion.match(/MSIE ..(.)/)[1] ;
  30. BrowserInfo.IsIE55OrMore = BrowserInfo.MajorVer >= 6 || ( BrowserInfo.MajorVer >= 5 && BrowserInfo.MinorVer >= 5 ) ;
  31. var yToolbars = new Array();  // 工具栏数组
  32. // 当文档完全调入时,进行初始化
  33. var bInitialized = false;
  34. function document.onreadystatechange(){
  35. if (document.readyState!="complete") return;
  36. if (bInitialized) return;
  37. bInitialized = true;
  38. var i, s, curr;
  39. // 初始每个工具栏
  40. for (i=0; i<document.body.all.length;i++){
  41. curr=document.body.all[i];
  42. if (curr.className == "yToolbar"){
  43. InitTB(curr);
  44. yToolbars[yToolbars.length] = curr;
  45. }
  46. }
  47. oLinkField = parent.document.getElementsByName(sLinkFieldName)[0];
  48. if (!config.License){
  49. try{
  50. eWebEditor_License.innerHTML = "&copy; <a href='http://www.eWebSoft.com' target='_blank'><font color=#000000>eWebSoft.com</font></a>";
  51. }
  52. catch(e){
  53. }
  54. }
  55. // IE5.5以下版本只能使用纯文本模式
  56. if (!BrowserInfo.IsIE55OrMore){
  57. config.InitMode = "TEXT";
  58. }
  59. if (ContentFlag.value=="0") { 
  60. ContentEdit.value = oLinkField.value;
  61. ContentLoad.value = oLinkField.value;
  62. ModeEdit.value = config.InitMode;
  63. ContentFlag.value = "1";
  64. }
  65. setMode(ModeEdit.value);
  66. setLinkedField() ;
  67. }
  68. // 初始化一个工具栏上的按钮
  69. function InitBtn(btn) {
  70. btn.onmouseover = BtnMouseOver;
  71. btn.onmouseout = BtnMouseOut;
  72. btn.onmousedown = BtnMouseDown;
  73. btn.onmouseup = BtnMouseUp;
  74. btn.ondragstart = YCancelEvent;
  75. btn.onselectstart = YCancelEvent;
  76. btn.onselect = YCancelEvent;
  77. btn.YUSERONCLICK = btn.onclick;
  78. btn.onclick = YCancelEvent;
  79. btn.YINITIALIZED = true;
  80. return true;
  81. }
  82. //Initialize a toolbar. 
  83. function InitTB(y) {
  84. // Set initial size of toolbar to that of the handle
  85. y.TBWidth = 0;
  86. // Populate the toolbar with its contents
  87. if (! PopulateTB(y)) return false;
  88. // Set the toolbar width and put in the handle
  89. y.style.posWidth = y.TBWidth;
  90. return true;
  91. }
  92. // Hander that simply cancels an event
  93. function YCancelEvent() {
  94. event.returnValue=false;
  95. event.cancelBubble=true;
  96. return false;
  97. }
  98. // Toolbar button onmouseover handler
  99. function BtnMouseOver() {
  100. if (event.srcElement.tagName != "IMG") return false;
  101. var image = event.srcElement;
  102. var element = image.parentElement;
  103. // Change button look based on current state of image.
  104. if (image.className == "Ico") element.className = "BtnMouseOverUp";
  105. else if (image.className == "IcoDown") element.className = "BtnMouseOverDown";
  106. event.cancelBubble = true;
  107. }
  108. // Toolbar button onmouseout handler
  109. function BtnMouseOut() {
  110. if (event.srcElement.tagName != "IMG") {
  111. event.cancelBubble = true;
  112. return false;
  113. }
  114. var image = event.srcElement;
  115. var element = image.parentElement;
  116. yRaisedElement = null;
  117. element.className = "Btn";
  118. image.className = "Ico";
  119. event.cancelBubble = true;
  120. }
  121. // Toolbar button onmousedown handler
  122. function BtnMouseDown() {
  123. if (event.srcElement.tagName != "IMG") {
  124. event.cancelBubble = true;
  125. event.returnValue=false;
  126. return false;
  127. }
  128. var image = event.srcElement;
  129. var element = image.parentElement;
  130. element.className = "BtnMouseOverDown";
  131. image.className = "IcoDown";
  132. event.cancelBubble = true;
  133. event.returnValue=false;
  134. return false;
  135. }
  136. // Toolbar button onmouseup handler
  137. function BtnMouseUp() {
  138. if (event.srcElement.tagName != "IMG") {
  139. event.cancelBubble = true;
  140. return false;
  141. }
  142. var image = event.srcElement;
  143. var element = image.parentElement;
  144. if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");
  145. element.className = "BtnMouseOverUp";
  146. image.className = "Ico";
  147. event.cancelBubble = true;
  148. return false;
  149. }
  150. // Populate a toolbar with the elements within it
  151. function PopulateTB(y) {
  152. var i, elements, element;
  153. // Iterate through all the top-level elements in the toolbar
  154. elements = y.children;
  155. for (i=0; i<elements.length; i++) {
  156. element = elements[i];
  157. if (element.tagName == "SCRIPT" || element.tagName == "!") continue;
  158. switch (element.className) {
  159. case "Btn":
  160. if (element.YINITIALIZED == null) {
  161. if (! InitBtn(element)) {
  162. alert("Problem initializing:" + element.id);
  163. return false;
  164. }
  165. }
  166. element.style.posLeft = y.TBWidth;
  167. y.TBWidth += element.offsetWidth + 1;
  168. break;
  169. case "TBGen":
  170. element.style.posLeft = y.TBWidth;
  171. y.TBWidth += element.offsetWidth + 1;
  172. break;
  173. case "TBSep":
  174. element.style.posLeft = y.TBWidth + 2;
  175. y.TBWidth += 5;
  176. break;
  177. case "TBHandle":
  178. element.style.posLeft = 2;
  179. y.TBWidth += element.offsetWidth + 7;
  180. break;
  181. default:
  182. alert("Invalid class: " + element.className + " on Element: " + element.id + " <" + element.tagName + ">");
  183. return false;
  184. }
  185. }
  186. y.TBWidth += 1;
  187. return true;
  188. }
  189. // 设置所属表单的提交或reset事件
  190. function setLinkedField() {
  191. if (! oLinkField) return ;
  192. var oForm = oLinkField.form ;
  193. if (!oForm) return ;
  194. // 附加submit事件
  195. oForm.attachEvent("onsubmit", AttachSubmit) ;
  196. if (! oForm.submitEditor) oForm.submitEditor = new Array() ;
  197. oForm.submitEditor[oForm.submitEditor.length] = AttachSubmit ;
  198. if (! oForm.originalSubmit) {
  199. oForm.originalSubmit = oForm.submit ;
  200. oForm.submit = function() {
  201. if (this.submitEditor) {
  202. for (var i = 0 ; i < this.submitEditor.length ; i++) {
  203. this.submitEditor[i]() ;
  204. }
  205. }
  206. this.originalSubmit() ;
  207. }
  208. }
  209. // 附加reset事件
  210. oForm.attachEvent("onreset", AttachReset) ;
  211. if (! oForm.resetEditor) oForm.resetEditor = new Array() ;
  212. oForm.resetEditor[oForm.resetEditor.length] = AttachReset ;
  213. if (! oForm.originalReset) {
  214. oForm.originalReset = oForm.reset ;
  215. oForm.reset = function() {
  216. if (this.resetEditor) {
  217. for (var i = 0 ; i < this.resetEditor.length ; i++) {
  218. this.resetEditor[i]() ;
  219. }
  220. }
  221. this.originalReset() ;
  222. }
  223. }
  224. }
  225. // 附加submit提交事件,大表单数据提交,保存eWebEditor中的内容
  226. function AttachSubmit() { 
  227. var oForm = oLinkField.form ;
  228. if (!oForm) return;
  229. var html = getHTML();
  230. ContentEdit.value = html;
  231. if (sCurrMode=="TEXT"){
  232. html = HTMLEncode(html);
  233. }
  234. splitTextField(oLinkField, html);
  235. // 表单提交
  236. function doSubmit(){
  237. var oForm = oLinkField.form ;
  238. if (!oForm) return ;
  239. oForm.submit();
  240. }
  241. // 附加Reset事件
  242. function AttachReset() {
  243. if(bEditMode){
  244. eWebEditor.document.body.innerHTML = ContentLoad.value;
  245. }else{
  246. eWebEditor.document.body.innerText = ContentLoad.value;
  247. }
  248. }
  249. // 显示帮助
  250. function onHelp(){
  251. ShowDialog('dialog/help.htm','400','300');
  252. return false;
  253. }
  254. // 粘贴时自动检测是否来源于Word格式
  255. function onPaste() {
  256. if (sCurrMode=="VIEW") return false;
  257. if (sCurrMode=="EDIT"){
  258. var sHTML = GetClipboardHTML() ;
  259. if (config.AutoDetectPasteFromWord && BrowserInfo.IsIE55OrMore) {
  260. var re = /<w[^>]* class="?MsoNormal"?/gi ;
  261. if ( re.test(sHTML)){
  262. if ( confirm( "你要粘贴的内容好象是从Word中拷出来的,是否要先清除Word格式再粘贴?" ) ){
  263. cleanAndPaste( sHTML ) ;
  264. return false ;
  265. }
  266. }
  267. }
  268. eWebEditor.document.selection.createRange().pasteHTML(sHTML) ;
  269. return false;
  270. }else{
  271. eWebEditor.document.selection.createRange().pasteHTML(HTMLEncode( clipboardData.getData("Text"))) ;
  272. return false;
  273. }
  274. }
  275. // 快捷键
  276. function onKeyDown(event){
  277. var key = String.fromCharCode(event.keyCode).toUpperCase();
  278. // F2:显示或隐藏指导方针
  279. if (event.keyCode==113){
  280. showBorders();
  281. return false;
  282. }
  283. if (event.ctrlKey){
  284. // Ctrl+Enter:提交
  285. if (event.keyCode==10){
  286. doSubmit();
  287. return false;
  288. }
  289. // Ctrl++:增加编辑区
  290. if (key=="+"){
  291. sizeChange(300);
  292. return false;
  293. }
  294. // Ctrl+-:减小编辑区
  295. if (key=="-"){
  296. sizeChange(-300);
  297. return false;
  298. }
  299. // Ctrl+1:代码模式
  300. if (key=="1"){
  301. setMode("CODE");
  302. return false;
  303. }
  304. // Ctrl+2:设计模式
  305. if (key=="2"){
  306. setMode("EDIT");
  307. return false;
  308. }
  309. // Ctrl+3:纯文本
  310. if (key=="3"){
  311. setMode("TEXT");
  312. return false;
  313. }
  314. // Ctrl+4:预览
  315. if (key=="4"){
  316. setMode("VIEW");
  317. return false;
  318. }
  319. }
  320. switch(sCurrMode){
  321. case "VIEW":
  322. return true;
  323. break;
  324. case "EDIT":
  325. if (event.ctrlKey){
  326. // Ctrl+D:从Word粘贴
  327. if (key == "D"){
  328. PasteWord();
  329. return false;
  330. }
  331. // Ctrl+R:查找替换
  332. if (key == "R"){
  333. findReplace();
  334. return false;
  335. }
  336. // Ctrl+Z:Undo
  337. if (key == "Z"){
  338. goHistory(-1);
  339. return false;
  340. }
  341. // Ctrl+Y:Redo
  342. if (key == "Y"){
  343. goHistory(1);
  344. return false;
  345. }
  346. }
  347. if(!event.ctrlKey && event.keyCode != 90 && event.keyCode != 89) {
  348. if (event.keyCode == 32 || event.keyCode == 13){
  349. saveHistory()
  350. }
  351. }
  352. return true;
  353. break;
  354. default:
  355. if (event.keyCode==13){
  356. var sel = eWebEditor.document.selection.createRange();
  357. sel.pasteHTML("<BR>");
  358. event.cancelBubble = true;
  359. event.returnValue = false;
  360. sel.select();
  361. sel.moveEnd("character", 1);
  362. sel.moveStart("character", 1);
  363. sel.collapse(false);
  364. return false;
  365. }
  366. // 屏蔽事件
  367. if (event.ctrlKey){
  368. // Ctrl+B,I,U
  369. if ((key == "B")||(key == "I")||(key == "U")){
  370. return false;
  371. }
  372. }
  373. }
  374. }
  375. // 取剪粘板中的HTML格式数据
  376. function GetClipboardHTML() {
  377. var oDiv = document.getElementById("eWebEditor_Temp_HTML")
  378. oDiv.innerHTML = "" ;
  379. var oTextRange = document.body.createTextRange() ;
  380. oTextRange.moveToElementText(oDiv) ;
  381. oTextRange.execCommand("Paste") ;
  382. var sData = oDiv.innerHTML ;
  383. oDiv.innerHTML = "" ;
  384. return sData ;
  385. }
  386. // 清除WORD冗余格式并粘贴
  387. function cleanAndPaste( html ) {
  388. // Remove all SPAN tags
  389. html = html.replace(/</?SPAN[^>]*>/gi, "" );
  390. // Remove Class attributes
  391. html = html.replace(/<(w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
  392. // Remove Style attributes
  393. html = html.replace(/<(w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
  394. // Remove Lang attributes
  395. html = html.replace(/<(w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
  396. // Remove XML elements and declarations
  397. html = html.replace(/<\??xml[^>]*>/gi, "") ;
  398. // Remove Tags with XML namespace declarations: <o:p></o:p>
  399. html = html.replace(/</?w+:[^>]*>/gi, "") ;
  400. // Replace the &nbsp;
  401. html = html.replace(/&nbsp;/, " " );
  402. // Transform <P> to <DIV>
  403. var re = new RegExp("(<P)([^>]*>.*?)(</P>)","gi") ; // Different because of a IE 5.0 error
  404. html = html.replace( re, "<div$2</div>" ) ;
  405. insertHTML( html ) ;
  406. }
  407. // 在当前文档位置插入.
  408. function insertHTML(html) {
  409. if (isModeView()) return false;
  410. if (eWebEditor.document.selection.type.toLowerCase() != "none"){
  411. eWebEditor.document.selection.clear() ;
  412. }
  413. if (sCurrMode!="EDIT"){
  414. html=HTMLEncode(html);
  415. }
  416. eWebEditor.document.selection.createRange().pasteHTML(html) ; 
  417. }
  418. // 设置编辑器的内容
  419. function setHTML(html) {
  420. ContentEdit.value = html;
  421. switch (sCurrMode){
  422. case "CODE":
  423. eWebEditor.document.designMode="On";
  424. eWebEditor.document.open();
  425. eWebEditor.document.write(config.StyleEditorHeader);
  426. eWebEditor.document.body.innerText=html;
  427. eWebEditor.document.body.contentEditable="true";
  428. eWebEditor.document.close();
  429. bEditMode=false;
  430. break;
  431. case "EDIT":
  432. eWebEditor.document.designMode="On";
  433. eWebEditor.document.open();
  434. eWebEditor.document.write(config.StyleEditorHeader+html);
  435. eWebEditor.document.body.contentEditable="true";
  436. eWebEditor.document.execCommand("2D-Position",true,true);
  437. eWebEditor.document.execCommand("MultipleSelection", true, true);
  438. eWebEditor.document.execCommand("LiveResize", true, true);
  439. eWebEditor.document.close();
  440. doZoom(nCurrZoomSize);
  441. bEditMode=true;
  442. eWebEditor.document.onselectionchange = function () { doToolbar();}
  443. break;
  444. case "TEXT":
  445. eWebEditor.document.designMode="On";
  446. eWebEditor.document.open();
  447. eWebEditor.document.write(config.StyleEditorHeader);
  448. eWebEditor.document.body.innerText=html;
  449. eWebEditor.document.body.contentEditable="true";
  450. eWebEditor.document.close();
  451. bEditMode=false;
  452. break;
  453. case "VIEW":
  454. eWebEditor.document.designMode="off";
  455. eWebEditor.document.open();
  456. eWebEditor.document.write(config.StyleEditorHeader+html);
  457. eWebEditor.document.body.contentEditable="false";
  458. eWebEditor.document.close();
  459. bEditMode=false;
  460. break;
  461. }
  462. eWebEditor.document.body.onpaste = onPaste ;
  463. eWebEditor.document.body.onhelp = onHelp ;
  464. eWebEditor.document.onkeydown = new Function("return onKeyDown(eWebEditor.event);");
  465. eWebEditor.document.oncontextmenu=new Function("return showContextMenu(eWebEditor.event);");
  466. if ((borderShown != "0")&&bEditMode) {
  467. borderShown = "0";
  468. showBorders();
  469. }
  470. initHistory();
  471. }
  472. // 取编辑器的内容
  473. function getHTML() {
  474. var html;
  475. if((sCurrMode=="EDIT")||(sCurrMode=="VIEW")){
  476. html = eWebEditor.document.body.innerHTML;
  477. }else{
  478. html = eWebEditor.document.body.innerText;
  479. }
  480. if (sCurrMode!="TEXT"){
  481. if ((html.toLowerCase()=="<p>&nbsp;</p>")||(html.toLowerCase()=="<p></p>")){
  482. html = "";
  483. }
  484. }
  485. return html;
  486. }
  487. // 在尾部追加内容
  488. function appendHTML(html) {
  489. if (isModeView()) return false;
  490. if(sCurrMode=="EDIT"){
  491. eWebEditor.document.body.innerHTML += html;
  492. }else{
  493. eWebEditor.document.body.innerText += html;
  494. }
  495. }
  496. // 从Word中粘贴,去除格式
  497. function PasteWord(){
  498. if (!validateMode()) return;
  499. eWebEditor.focus();
  500. if (BrowserInfo.IsIE55OrMore)
  501. cleanAndPaste( GetClipboardHTML() ) ;
  502. else if ( confirm( "此功能要求IE5.5版本以上,你当前的浏览器不支持,是否按常规粘贴进行?" ) )
  503. format("paste") ;
  504. eWebEditor.focus();
  505. }
  506. // 粘贴纯文本
  507. function PasteText(){
  508. if (!validateMode()) return;
  509. eWebEditor.focus();
  510. var sText = HTMLEncode( clipboardData.getData("Text") ) ;
  511. insertHTML(sText);
  512. eWebEditor.focus();
  513. }
  514. // 检测当前是否允许编辑
  515. function validateMode() {
  516. if (sCurrMode=="EDIT") return true;
  517. alert("需转换为编辑状态后才能使用编辑功能!");
  518. eWebEditor.focus();
  519. return false;
  520. }
  521. // 检测当前是否在预览模式
  522. function isModeView(){
  523. if (sCurrMode=="VIEW"){
  524. alert("预览时不允许设置编辑区内容。");
  525. return true;
  526. }
  527. return false;
  528. }
  529. // 格式化编辑器中的内容
  530. function format(what,opt) {
  531. if (!validateMode()) return;
  532. eWebEditor.focus();
  533. if (opt=="RemoveFormat") {
  534. what=opt;
  535. opt=null;
  536. }
  537. if (opt==null) eWebEditor.document.execCommand(what);
  538. else eWebEditor.document.execCommand(what,"",opt);
  539. eWebEditor.focus();
  540. }
  541. // 确保焦点在 eWebEditor 内
  542. function VerifyFocus() {
  543. if ( eWebEditor )
  544. eWebEditor.focus();
  545. }
  546. // 改变模式:代码、编辑、文本、预览
  547. function setMode(NewMode){
  548. if (NewMode!=sCurrMode){
  549. if (!BrowserInfo.IsIE55OrMore){
  550. if ((NewMode=="CODE") || (NewMode=="EDIT") || (NewMode=="VIEW")){
  551. alert("HTML编辑模式需要IE5.5版本以上的支持!");
  552. return false;
  553. }
  554. }
  555. if (NewMode=="TEXT"){
  556. if (sCurrMode==ModeEdit.value){
  557. if (!confirm("警告!切换到纯文本模式会丢失您所有的HTML格式,您确认切换吗?")){
  558. return false;
  559. }
  560. }
  561. }
  562. var sBody = "";
  563. switch(sCurrMode){
  564. case "CODE":
  565. if (NewMode=="TEXT"){
  566. eWebEditor_Temp_HTML.innerHTML = eWebEditor.document.body.innerText;
  567. sBody = eWebEditor_Temp_HTML.innerText;
  568. }else{
  569. sBody = eWebEditor.document.body.innerText;
  570. }
  571. break;
  572. case "TEXT":
  573. sBody = eWebEditor.document.body.innerText;
  574. sBody = HTMLEncode(sBody);
  575. break;
  576. case "EDIT":
  577. case "VIEW":
  578. if (NewMode=="TEXT"){
  579. sBody = eWebEditor.document.body.innerText;
  580. }else{
  581. sBody = eWebEditor.document.body.innerHTML;
  582. }
  583. break;
  584. default:
  585. sBody = ContentEdit.value;
  586. break;
  587. }
  588. // 换图片
  589. try{
  590. document.all["eWebEditor_CODE"].className = "StatusBarBtnOff";
  591. document.all["eWebEditor_EDIT"].className = "StatusBarBtnOff";
  592. document.all["eWebEditor_TEXT"].className = "StatusBarBtnOff";
  593. document.all["eWebEditor_VIEW"].className = "StatusBarBtnOff";
  594. document.all["eWebEditor_"+NewMode].className = "StatusBarBtnOn";
  595. }
  596. catch(e){
  597. }
  598. sCurrMode = NewMode;
  599. ModeEdit.value = NewMode;
  600. setHTML(sBody);
  601. disableChildren(eWebEditor_Toolbar);
  602. }
  603. }
  604. // 使工具栏无效
  605. function disableChildren(obj){
  606. if (obj){
  607. obj.disabled=(!bEditMode);
  608. for (var i=0; i<obj.children.length; i++){
  609. disableChildren(obj.children[i]);
  610. }
  611. }
  612. }
  613. // 显示无模式对话框
  614. function ShowDialog(url, width, height, optValidate) {
  615. if (optValidate) {
  616. if (!validateMode()) return;
  617. }
  618. eWebEditor.focus();
  619. var arr = showModalDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:no;status:no");
  620. eWebEditor.focus();
  621. }
  622. // 全屏编辑
  623. function Maximize() {
  624. if (!validateMode()) return;
  625. window.open("dialog/fullscreen.htm?style="+config.StyleName, 'FullScreen'+sLinkFieldName, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,fullscreen=yes');
  626. }
  627. // 创建或修改超级链接
  628. function createLink(){
  629. if (!validateMode()) return;
  630. if (eWebEditor.document.selection.type == "Control") {
  631. var oControlRange = eWebEditor.document.selection.createRange();
  632. if (oControlRange(0).tagName.toUpperCase() != "IMG") {
  633. alert("链接只能是图片或文本");
  634. return;
  635. }
  636. }
  637. ShowDialog("dialog/hyperlink.htm", 350, 170, true);
  638. }
  639. // 替换特殊字符
  640. function HTMLEncode(text){
  641. text = text.replace(/&/g, "&amp;") ;
  642. text = text.replace(/"/g, "&quot;") ;
  643. text = text.replace(/</g, "&lt;") ;
  644. text = text.replace(/>/g, "&gt;") ;
  645. text = text.replace(/'/g, "&#146;") ;
  646. text = text.replace(/ /g,"&nbsp;");
  647. text = text.replace(/n/g,"<br>");
  648. text = text.replace(/t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
  649. return text;
  650. }
  651. // 插入特殊对象
  652. function insert(what) {
  653. if (!validateMode()) return;
  654. eWebEditor.focus();
  655. var sel = eWebEditor.document.selection.createRange();
  656. switch(what){
  657. case "excel": // 插入EXCEL表格
  658. insertHTML("<object classid='clsid:0002E510-0000-0000-C000-000000000046' id='Spreadsheet1' codebase='file:\Bobsoftwareoffice2000msowc.cab' width='100%' height='250'><param name='HTMLURL' value><param name='HTMLData' value='&lt;html xmlns:x=&quot;urn:schemas-microsoft-com:office:excel&quot;xmlns=&quot;http://www.w3.org/TR/REC-html40&quot;&gt;&lt;head&gt;&lt;style type=&quot;text/css&quot;&gt;&lt;!--tr{mso-height-source:auto;}td{black-space:nowrap;}.wc4590F88{black-space:nowrap;font-family:宋体;mso-number-format:General;font-size:auto;font-weight:auto;font-style:auto;text-decoration:auto;mso-background-source:auto;mso-pattern:auto;mso-color-source:auto;text-align:general;vertical-align:bottom;border-top:none;border-left:none;border-right:none;border-bottom:none;mso-protection:locked;}--&gt;&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt;&lt;x:ExcelWorksheet&gt;&lt;x:OWCVersion&gt;9.0.0.2710&lt;/x:OWCVersion&gt;&lt;x:Label Style='border-top:solid .5pt silver;border-left:solid .5pt silver;border-right:solid .5pt silver;border-bottom:solid .5pt silver'&gt;&lt;x:Caption&gt;Microsoft Office Spreadsheet&lt;/x:Caption&gt; &lt;/x:Label&gt;&lt;x:Name&gt;Sheet1&lt;/x:Name&gt;&lt;x:WorksheetOptions&gt;&lt;x:Selected/&gt;&lt;x:Height&gt;7620&lt;/x:Height&gt;&lt;x:Width&gt;15240&lt;/x:Width&gt;&lt;x:TopRowVisible&gt;0&lt;/x:TopRowVisible&gt;&lt;x:LeftColumnVisible&gt;0&lt;/x:LeftColumnVisible&gt; &lt;x:ProtectContents&gt;False&lt;/x:ProtectContents&gt; &lt;x:DefaultRowHeight&gt;210&lt;/x:DefaultRowHeight&gt; &lt;x:StandardWidth&gt;2389&lt;/x:StandardWidth&gt; &lt;/x:WorksheetOptions&gt; &lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt; &lt;x:MaxHeight&gt;80%&lt;/x:MaxHeight&gt;&lt;x:MaxWidth&gt;80%&lt;/x:MaxWidth&gt;&lt;/x:ExcelWorkbook&gt;&lt;/xml&gt;&lt;![endif]--&gt;&lt;table class=wc4590F88 x:str&gt;&lt;col width=&quot;56&quot;&gt;&lt;tr height=&quot;14&quot;&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;'> <param name='DataType' value='HTMLDATA'> <param name='AutoFit' value='0'><param name='DisplayColHeaders' value='-1'><param name='DisplayGridlines' value='-1'><param name='DisplayHorizontalScrollBar' value='-1'><param name='DisplayRowHeaders' value='-1'><param name='DisplayTitleBar' value='-1'><param name='DisplayToolbar' value='-1'><param name='DisplayVerticalScrollBar' value='-1'> <param name='EnableAutoCalculate' value='-1'> <param name='EnableEvents' value='-1'><param name='MoveAfterReturn' value='-1'><param name='MoveAfterReturnDirection' value='0'><param name='RightToLeft' value='0'><param name='ViewableRange' value='1:65536'></object>");
  659. break;
  660. case "nowdate": // 插入当前系统日期
  661. var d = new Date();
  662. insertHTML(d.toLocaleDateString());
  663. break;
  664. case "nowtime": // 插入当前系统时间
  665. var d = new Date();
  666. insertHTML(d.toLocaleTimeString());
  667. break;
  668. case "br": // 插入换行符
  669. insertHTML("<br>")
  670. break;
  671. case "code": // 代码片段样式
  672. insertHTML('<table width=95% border="0" align="Center" cellpadding="6" cellspacing="0" style="border: 1px Dotted #CCCCCC; TABLE-LAYOUT: fixed"><tr><td bgcolor=#FDFDDF style="WORD-WRAP: break-word"><font style="color: #990000;font-weight:bold">以下是代码片段:</font><br>'+HTMLEncode(sel.text)+'</td></tr></table>');
  673. break;
  674. case "quote": // 引用片段样式
  675. insertHTML('<table width=95% border="0" align="Center" cellpadding="6" cellspacing="0" style="border: 1px Dotted #CCCCCC; TABLE-LAYOUT: fixed"><tr><td bgcolor=#F3F3F3 style="WORD-WRAP: break-word"><font style="color: #990000;font-weight:bold">以下是引用片段:</font><br>'+HTMLEncode(sel.text)+'</td></tr></table>');
  676. break;
  677. case "big": // 字体变大
  678. insertHTML("<big>" + sel.text + "</big>");
  679. break;
  680. case "small": // 字体变小
  681. insertHTML("<small>" + sel.text + "</small>");
  682. break;
  683. default:
  684. alert("错误参数调用!");
  685. break;
  686. }
  687. sel=null;
  688. }
  689. // 显示或隐藏指导方针
  690. var borderShown = config.ShowBorder;
  691. function showBorders() {
  692. if (!validateMode()) return;
  693. var allForms = eWebEditor.document.body.getElementsByTagName("FORM");
  694. var allInputs = eWebEditor.document.body.getElementsByTagName("INPUT");
  695. var allTables = eWebEditor.document.body.getElementsByTagName("TABLE");
  696. var allLinks = eWebEditor.document.body.getElementsByTagName("A");
  697. // 表单
  698. for (a=0; a < allForms.length; a++) {
  699. if (borderShown == "0") {
  700. allForms[a].runtimeStyle.border = "1px dotted #FF0000"
  701. } else {
  702. allForms[a].runtimeStyle.cssText = ""
  703. }
  704. }
  705. // Input Hidden类
  706. for (b=0; b < allInputs.length; b++) {
  707. if (borderShown == "0") {
  708. if (allInputs[b].type.toUpperCase() == "HIDDEN") {
  709. allInputs[b].runtimeStyle.border = "1px dashed #000000"
  710. allInputs[b].runtimeStyle.width = "15px"
  711. allInputs[b].runtimeStyle.height = "15px"
  712. allInputs[b].runtimeStyle.backgroundColor = "#FDADAD"
  713. allInputs[b].runtimeStyle.color = "#FDADAD"
  714. }
  715. } else {
  716. if (allInputs[b].type.toUpperCase() == "HIDDEN")
  717. allInputs[b].runtimeStyle.cssText = ""
  718. }
  719. }
  720. // 表格
  721. for (i=0; i < allTables.length; i++) {
  722. if (borderShown == "0") {
  723. allTables[i].runtimeStyle.border = "1px dotted #BFBFBF"
  724. } else {
  725. allTables[i].runtimeStyle.cssText = ""
  726. }
  727. allRows = allTables[i].rows
  728. for (y=0; y < allRows.length; y++) {
  729.   allCellsInRow = allRows[y].cells
  730. for (x=0; x < allCellsInRow.length; x++) {
  731. if (borderShown == "0") {
  732. allCellsInRow[x].runtimeStyle.border = "1px dotted #BFBFBF"
  733. } else {
  734. allCellsInRow[x].runtimeStyle.cssText = ""
  735. }
  736. }
  737. }
  738. }
  739. // 链接 A
  740. for (a=0; a < allLinks.length; a++) {
  741. if (borderShown == "0") {
  742. if (allLinks[a].href.toUpperCase() == "") {
  743. allLinks[a].runtimeStyle.borderBottom = "1px dashed #000000"
  744. }
  745. } else {
  746. allLinks[a].runtimeStyle.cssText = ""
  747. }
  748. }
  749. if (borderShown == "0") {
  750. borderShown = "1"
  751. } else {
  752. borderShown = "0"
  753. }
  754. scrollUp()
  755. }
  756. // 返回页面最上部
  757. function scrollUp() {
  758. eWebEditor.scrollBy(0,0);
  759. }
  760. // 缩放操作
  761. var nCurrZoomSize = 100;
  762. var aZoomSize = new Array(10, 25, 50, 75, 100, 150, 200, 500);
  763. function doZoom(size) {
  764. eWebEditor.document.body.runtimeStyle.zoom = size + "%";
  765. nCurrZoomSize = size;
  766. }
  767. // 拼写检查
  768. function spellCheck(){
  769. ShowDialog('dialog/spellcheck.htm', 300, 220, true)
  770. }
  771. // 查找替换
  772. function findReplace(){
  773. ShowDialog('dialog/findreplace.htm', 320, 165, true)
  774. }
  775. // 相对(absolute)或绝对位置(static)
  776. function absolutePosition(){
  777. var objReference = null;
  778. var RangeType = eWebEditor.document.selection.type;
  779. if (RangeType != "Control") return;
  780. var selectedRange = eWebEditor.document.selection.createRange();
  781. for (var i=0; i<selectedRange.length; i++){
  782. objReference = selectedRange.item(i);
  783. if (objReference.style.position != 'absolute') {
  784. objReference.style.position='absolute';
  785. }else{
  786. objReference.style.position='static';
  787. }
  788. }
  789. }
  790. // 上移(forward)或下移(backward)一层
  791. function zIndex(action){
  792. var objReference = null;
  793. var RangeType = eWebEditor.document.selection.type;
  794. if (RangeType != "Control") return;
  795. var selectedRange = eWebEditor.document.selection.createRange();
  796. for (var i=0; i<selectedRange.length; i++){
  797. objReference = selectedRange.item(i);
  798. if (action=='forward'){
  799. objReference.style.zIndex  +=1;
  800. }else{
  801. objReference.style.zIndex  -=1;
  802. }
  803. objReference.style.position='absolute';
  804. }
  805. }
  806. // 是否选中指定类型的控件
  807. function isControlSelected(tag){
  808. if (eWebEditor.document.selection.type == "Control") {
  809. var oControlRange = eWebEditor.document.selection.createRange();
  810. if (oControlRange(0).tagName.toUpperCase() == tag) {
  811. return true;
  812. }
  813. }
  814. return false;
  815. }
  816. // 改变编辑区高度
  817. function sizeChange(size){
  818. if (!BrowserInfo.IsIE55OrMore){
  819. alert("此功能需要IE5.5版本以上的支持!");
  820. return false;
  821. }
  822. for (var i=0; i<parent.frames.length; i++){
  823. if (parent.frames[i].document==self.document){
  824. var obj=parent.frames[i].frameElement;
  825. var height = parseInt(obj.offsetHeight);
  826. if (height+size>=300){
  827. obj.height=height+size;
  828. }
  829. break;
  830. }
  831. }
  832. }
  833. // 热点链接
  834. function mapEdit(){
  835. if (!validateMode()) return;
  836. var b = false;
  837. if (eWebEditor.document.selection.type == "Control") {
  838. var oControlRange = eWebEditor.document.selection.createRange();
  839. if (oControlRange(0).tagName.toUpperCase() == "IMG") {
  840. b = true;
  841. }
  842. }
  843. if (!b){
  844. alert("热点链接只能作用于图片");
  845. return;
  846. }
  847. window.open("dialog/map.htm", 'mapEdit'+sLinkFieldName, 'toolbar=no,location=no,directories=no,status=not,menubar=no,scrollbars=no,resizable=yes,width=450,height=300');
  848. }
  849. // 上传文件成功返回原文件名、保存后的文件名、保存后的路径文件名,提供接口
  850. function addUploadFile(originalFileName, saveFileName, savePathFileName){
  851. doInterfaceUpload(sLinkOriginalFileName, originalFileName);
  852. doInterfaceUpload(sLinkSaveFileName, saveFileName);
  853. doInterfaceUpload(sLinkSavePathFileName, savePathFileName);
  854. }
  855. // 文件上传成功接口操作
  856. function doInterfaceUpload(strLinkName, strValue){
  857. if (strValue=="") return;
  858. if (strLinkName){
  859. var objLinkUpload = parent.document.getElementsByName(strLinkName)[0];
  860. if (objLinkUpload){
  861. if (objLinkUpload.value!=""){
  862. objLinkUpload.value = objLinkUpload.value + "|";
  863. }
  864. objLinkUpload.value = objLinkUpload.value + strValue;
  865. objLinkUpload.fireEvent("onchange");
  866. }
  867. }
  868. }
  869. // 大文件内容自动拆分
  870. function splitTextField(objField, html) { 
  871. var strFieldName = objField.name;
  872. var objForm = objField.form;
  873. var objDocument = objField.document;
  874. objField.value = html;
  875. //表单限制值设定,限制值是102399,考虑到中文设为一半
  876. var FormLimit = 50000 ;
  877. // 再次处理时,先赋空值
  878. for (var i=1;i<objDocument.getElementsByName(strFieldName).length;i++) {
  879. objDocument.getElementsByName(strFieldName)[i].value = "";
  880. }
  881. //如果表单值超过限制,拆成多个对象
  882. if (html.length > FormLimit) { 
  883. objField.value = html.substr(0, FormLimit) ;
  884. html = html.substr(FormLimit) ;
  885. while (html.length > 0) { 
  886. var objTEXTAREA = objDocument.createElement("TEXTAREA") ;
  887. objTEXTAREA.name = strFieldName ;
  888. objTEXTAREA.style.display = "none" ;
  889. objTEXTAREA.value = html.substr(0, FormLimit) ;
  890. objForm.appendChild(objTEXTAREA) ;
  891. html = html.substr(FormLimit) ;
  892. // 远程上传
  893. var sEventUploadAfter;
  894. function remoteUpload(strEventUploadAfter) { 
  895. if (config.AutoRemote!="1") return;
  896. if (sCurrMode=="TEXT") return;
  897. sEventUploadAfter = strEventUploadAfter;
  898. var objField = document.getElementsByName("eWebEditor_UploadText")[0];
  899. splitTextField(objField, getHTML());
  900. divProcessing.style.top = (document.body.clientHeight-parseFloat(divProcessing.style.height))/2;
  901. divProcessing.style.left = (document.body.clientWidth-parseFloat(divProcessing.style.width))/2;
  902. divProcessing.style.display = "";
  903. eWebEditor_UploadForm.submit();
  904. // 远程上传完成
  905. function remoteUploadOK() {
  906. divProcessing.style.display = "none";
  907. if (oLinkField){
  908. if (sEventUploadAfter){
  909. eval("parent."+sEventUploadAfter);
  910. }
  911. }
  912. }
  913. // 修正Undo/Redo
  914. var history = new Object;
  915. history.data = [];
  916. history.position = 0;
  917. history.bookmark = [];
  918. // 保存历史
  919. function saveHistory() {
  920. if (bEditMode){
  921. if (history.data[history.position] != eWebEditor.document.body.innerHTML){
  922. var nBeginLen = history.data.length;
  923. var nPopLen = history.data.length - history.position;
  924. for (var i=1; i<nPopLen; i++){
  925. history.data.pop();
  926. history.bookmark.pop();
  927. }
  928. history.data[history.data.length] = eWebEditor.document.body.innerHTML;
  929. if (eWebEditor.document.selection.type != "Control"){
  930. history.bookmark[history.bookmark.length] = eWebEditor.document.selection.createRange().getBookmark();
  931. } else {
  932. var oControl = eWebEditor.document.selection.createRange();
  933. history.bookmark[history.bookmark.length] = oControl[0];
  934. }
  935. if (nBeginLen!=0){
  936. history.position++;
  937. }
  938. }
  939. }
  940. }
  941. // 初始历史
  942. function initHistory() {
  943. history.data.length = 0;
  944. history.bookmark.length = 0;
  945. history.position = 0;
  946. }
  947. // 返回历史
  948. function goHistory(value) {
  949. saveHistory();
  950. // undo
  951. if (value == -1){
  952. if (history.position > 0){
  953. eWebEditor.document.body.innerHTML = history.data[--history.position];
  954. setHistoryCursor();
  955. }
  956. // redo
  957. } else {
  958. if (history.position < history.data.length -1){
  959. eWebEditor.document.body.innerHTML = history.data[++history.position];
  960. setHistoryCursor();
  961. }
  962. }
  963. }
  964. // 设置当前书签
  965. function setHistoryCursor() {
  966. if (history.bookmark[history.position]){
  967. r = eWebEditor.document.body.createTextRange()
  968. if (history.bookmark[history.position] != "[object]"){
  969. if (r.moveToBookmark(history.bookmark[history.position])){
  970. r.collapse(false);
  971. r.select();
  972. }
  973. }
  974. }
  975. }
  976. // End Undo / Redo Fix
  977. // 工具栏事件发生
  978. function doToolbar(){
  979. if (bEditMode){
  980. saveHistory();
  981. }
  982. }