Editor.js
上传用户:pengwei803
上传日期:2021-02-10
资源大小:3311k
文件大小:31k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

HTML/CSS

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