1
0

autolink.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ///import core
  2. ///commands 为非ie浏览器自动添加a标签
  3. ///commandsName AutoLink
  4. ///commandsTitle 自动增加链接
  5. /**
  6. * @description 为非ie浏览器自动添加a标签
  7. * @author zhanyi
  8. */
  9. UE.plugin.register(
  10. "autolink",
  11. function() {
  12. var cont = 0;
  13. return !browser.ie
  14. ? {
  15. bindEvents: {
  16. reset: function() {
  17. cont = 0;
  18. },
  19. keydown: function(type, evt) {
  20. var me = this;
  21. var keyCode = evt.keyCode || evt.which;
  22. if (keyCode == 32 || keyCode == 13) {
  23. var sel = me.selection.getNative(),
  24. range = sel.getRangeAt(0).cloneRange(),
  25. offset,
  26. charCode;
  27. var start = range.startContainer;
  28. while (start.nodeType == 1 && range.startOffset > 0) {
  29. start =
  30. range.startContainer.childNodes[range.startOffset - 1];
  31. if (!start) {
  32. break;
  33. }
  34. range.setStart(
  35. start,
  36. start.nodeType == 1
  37. ? start.childNodes.length
  38. : start.nodeValue.length
  39. );
  40. range.collapse(true);
  41. start = range.startContainer;
  42. }
  43. do {
  44. if (range.startOffset == 0) {
  45. start = range.startContainer.previousSibling;
  46. while (start && start.nodeType == 1) {
  47. start = start.lastChild;
  48. }
  49. if (!start || domUtils.isFillChar(start)) {
  50. break;
  51. }
  52. offset = start.nodeValue.length;
  53. } else {
  54. start = range.startContainer;
  55. offset = range.startOffset;
  56. }
  57. range.setStart(start, offset - 1);
  58. charCode = range.toString().charCodeAt(0);
  59. } while (charCode != 160 && charCode != 32);
  60. if (
  61. range
  62. .toString()
  63. .replace(new RegExp(domUtils.fillChar, "g"), "")
  64. .match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)
  65. ) {
  66. while (range.toString().length) {
  67. if (
  68. /^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(
  69. range.toString()
  70. )
  71. ) {
  72. break;
  73. }
  74. try {
  75. range.setStart(
  76. range.startContainer,
  77. range.startOffset + 1
  78. );
  79. } catch (e) {
  80. //trace:2121
  81. var start = range.startContainer;
  82. while (!(next = start.nextSibling)) {
  83. if (domUtils.isBody(start)) {
  84. return;
  85. }
  86. start = start.parentNode;
  87. }
  88. range.setStart(next, 0);
  89. }
  90. }
  91. //range的开始边界已经在a标签里的不再处理
  92. if (
  93. domUtils.findParentByTagName(
  94. range.startContainer,
  95. "a",
  96. true
  97. )
  98. ) {
  99. return;
  100. }
  101. var a = me.document.createElement("a"),
  102. text = me.document.createTextNode(" "),
  103. href;
  104. me.undoManger && me.undoManger.save();
  105. a.appendChild(range.extractContents());
  106. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, "");
  107. href = a
  108. .getAttribute("href")
  109. .replace(new RegExp(domUtils.fillChar, "g"), "");
  110. href = /^(?:https?:\/\/)/gi.test(href)
  111. ? href
  112. : "http://" + href;
  113. a.setAttribute("_src", utils.html(href));
  114. a.href = utils.html(href);
  115. range.insertNode(a);
  116. a.parentNode.insertBefore(text, a.nextSibling);
  117. range.setStart(text, 0);
  118. range.collapse(true);
  119. sel.removeAllRanges();
  120. sel.addRange(range);
  121. me.undoManger && me.undoManger.save();
  122. }
  123. }
  124. }
  125. }
  126. }
  127. : {};
  128. },
  129. function() {
  130. var keyCodes = {
  131. 37: 1,
  132. 38: 1,
  133. 39: 1,
  134. 40: 1,
  135. 13: 1,
  136. 32: 1
  137. };
  138. function checkIsCludeLink(node) {
  139. if (node.nodeType == 3) {
  140. return null;
  141. }
  142. if (node.nodeName == "A") {
  143. return node;
  144. }
  145. var lastChild = node.lastChild;
  146. while (lastChild) {
  147. if (lastChild.nodeName == "A") {
  148. return lastChild;
  149. }
  150. if (lastChild.nodeType == 3) {
  151. if (domUtils.isWhitespace(lastChild)) {
  152. lastChild = lastChild.previousSibling;
  153. continue;
  154. }
  155. return null;
  156. }
  157. lastChild = lastChild.lastChild;
  158. }
  159. }
  160. browser.ie &&
  161. this.addListener("keyup", function(cmd, evt) {
  162. var me = this,
  163. keyCode = evt.keyCode;
  164. if (keyCodes[keyCode]) {
  165. var rng = me.selection.getRange();
  166. var start = rng.startContainer;
  167. if (keyCode == 13) {
  168. while (
  169. start &&
  170. !domUtils.isBody(start) &&
  171. !domUtils.isBlockElm(start)
  172. ) {
  173. start = start.parentNode;
  174. }
  175. if (start && !domUtils.isBody(start) && start.nodeName == "P") {
  176. var pre = start.previousSibling;
  177. if (pre && pre.nodeType == 1) {
  178. var pre = checkIsCludeLink(pre);
  179. if (pre && !pre.getAttribute("_href")) {
  180. domUtils.remove(pre, true);
  181. }
  182. }
  183. }
  184. } else if (keyCode == 32) {
  185. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  186. start = start.previousSibling;
  187. if (
  188. start &&
  189. start.nodeName == "A" &&
  190. !start.getAttribute("_href")
  191. ) {
  192. domUtils.remove(start, true);
  193. }
  194. }
  195. } else {
  196. start = domUtils.findParentByTagName(start, "a", true);
  197. if (start && !start.getAttribute("_href")) {
  198. var bk = rng.createBookmark();
  199. domUtils.remove(start, true);
  200. rng.moveToBookmark(bk).select(true);
  201. }
  202. }
  203. }
  204. });
  205. }
  206. );