link.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title></title>
  6. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  7. <script type="text/javascript" src="../internal.js?20220503"></script>
  8. <style type="text/css">
  9. *{margin:0;padding:0;color: #838383;}
  10. table{font-size: 12px;margin: 10px;line-height:36px;width:100%;}
  11. .txt{box-sizing:border-box;width:90%;height:30px;line-height:30px;border:1px solid #d7d7d7;border-radius:3px;padding:0 5px;outline:none;}
  12. </style>
  13. </head>
  14. <body>
  15. <div style="padding:10px;">
  16. <table>
  17. <tr>
  18. <td><label for="text"> <var id="lang_input_text"></var></label></td>
  19. <td><input class="txt" id="text" type="text" disabled="true"/></td>
  20. </tr>
  21. <tr>
  22. <td><label for="href"> <var id="lang_input_url"></var></label></td>
  23. <td><input class="txt" id="href" type="text" /></td>
  24. </tr>
  25. <tr>
  26. <td><label for="title"> <var id="lang_input_title"></var></label></td>
  27. <td><input class="txt" id="title" type="text"/></td>
  28. </tr>
  29. <tr>
  30. <td colspan="2">
  31. <label for="target"><var id="lang_input_target"></var></label>
  32. <input id="target" type="checkbox"/>
  33. </td>
  34. </tr>
  35. <tr>
  36. <td colspan="2" id="msg"></td>
  37. </tr>
  38. </table>
  39. </div>
  40. <script type="text/javascript">
  41. editor.setOpt('allowLinkProtocols', ['http:', 'https:', '#', '/', 'ftp:', 'mailto:', 'tel:']);
  42. var allowLinkProtocols = editor.getOpt('allowLinkProtocols');
  43. var range = editor.selection.getRange(),
  44. link = range.collapsed ? editor.queryCommandValue( "link" ) : editor.selection.getStart(),
  45. url,
  46. text = $G('text'),
  47. rangeLink = domUtils.findParentByTagName(range.getCommonAncestor(),'a',true),
  48. orgText;
  49. link = domUtils.findParentByTagName( link, "a", true );
  50. if(link){
  51. url = utils.html(link.getAttribute( '_href' ) || link.getAttribute( 'href', 2 ));
  52. if(rangeLink === link && !link.getElementsByTagName('img').length){
  53. text.removeAttribute('disabled');
  54. orgText = text.value = link[browser.ie ? 'innerText':'textContent'];
  55. }else{
  56. text.setAttribute('disabled','true');
  57. text.value = lang.validLink;
  58. }
  59. }else{
  60. if(range.collapsed){
  61. text.removeAttribute('disabled');
  62. text.value = '';
  63. }else{
  64. text.setAttribute('disabled','true');
  65. text.value = lang.validLink;
  66. }
  67. }
  68. $G("title").value = url ? link.title : "";
  69. $G("href").value = url ? url: '';
  70. $G("target").checked = url && link.target == "_blank" ? true : false;
  71. $focus($G("href"));
  72. function handleDialogOk(){
  73. var href =$G('href').value.replace(/^\s+|\s+$/g, '');
  74. if(href){
  75. if(!hrefStartWith(href, allowLinkProtocols)) {
  76. href = "http://" + href;
  77. }
  78. var obj = {
  79. 'href' : href,
  80. 'target' : $G("target").checked ? "_blank" : '_self',
  81. 'title' : $G("title").value.replace(/^\s+|\s+$/g, ''),
  82. '_href':href
  83. };
  84. //修改链接内容的情况太特殊了,所以先做到这里了
  85. //todo:情况多的时候,做到command里
  86. if(orgText && text.value != orgText){
  87. link[browser.ie ? 'innerText' : 'textContent'] = obj.textValue = text.value;
  88. range.selectNode(link).select()
  89. }
  90. if(range.collapsed){
  91. obj.textValue = text.value;
  92. }
  93. editor.execCommand('link',utils.clearEmptyAttrs(obj) );
  94. dialog.close();
  95. }
  96. }
  97. dialog.onok = handleDialogOk;
  98. $G('href').onkeydown = $G('title').onkeydown = function(evt){
  99. evt = evt || window.event;
  100. if (evt.keyCode == 13) {
  101. handleDialogOk();
  102. return false;
  103. }
  104. };
  105. $G('href').onblur = function(){
  106. if(!hrefStartWith(this.value, allowLinkProtocols)){
  107. $G("msg").innerHTML = "<span style='color: red'>"+lang.httpPrompt+"</span>";
  108. }else{
  109. $G("msg").innerHTML = "";
  110. }
  111. };
  112. function hrefStartWith(href,arr){
  113. href = href.replace(/^\s+|\s+$/g, '');
  114. for(var i=0,ai;ai=arr[i++];){
  115. if(href.indexOf(ai)==0){
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. </script>
  122. </body>
  123. </html>