link.html 4.8 KB

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