1
0

link.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * 超链接
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. /**
  7. * 插入超链接
  8. * @command link
  9. * @method execCommand
  10. * @param { String } cmd 命令字符串
  11. * @param { Object } options 设置自定义属性,例如:url、title、target
  12. * @example
  13. * ```javascript
  14. * editor.execCommand( 'link', '{
  15. * url:'ueditor.baidu.com',
  16. * title:'ueditor',
  17. * target:'_blank'
  18. * }' );
  19. * ```
  20. */
  21. /**
  22. * 返回当前选中的第一个超链接节点
  23. * @command link
  24. * @method queryCommandValue
  25. * @param { String } cmd 命令字符串
  26. * @return { Element } 超链接节点
  27. * @example
  28. * ```javascript
  29. * editor.queryCommandValue( 'link' );
  30. * ```
  31. */
  32. /**
  33. * 取消超链接
  34. * @command unlink
  35. * @method execCommand
  36. * @param { String } cmd 命令字符串
  37. * @example
  38. * ```javascript
  39. * editor.execCommand( 'unlink');
  40. * ```
  41. */
  42. UE.plugins["link"] = function() {
  43. function optimize(range) {
  44. var start = range.startContainer,
  45. end = range.endContainer;
  46. if ((start = domUtils.findParentByTagName(start, "a", true))) {
  47. range.setStartBefore(start);
  48. }
  49. if ((end = domUtils.findParentByTagName(end, "a", true))) {
  50. range.setEndAfter(end);
  51. }
  52. }
  53. UE.commands["unlink"] = {
  54. execCommand: function() {
  55. var range = this.selection.getRange(),
  56. bookmark;
  57. if (
  58. range.collapsed &&
  59. !domUtils.findParentByTagName(range.startContainer, "a", true)
  60. ) {
  61. return;
  62. }
  63. bookmark = range.createBookmark();
  64. optimize(range);
  65. range.removeInlineStyle("a").moveToBookmark(bookmark).select();
  66. },
  67. queryCommandState: function() {
  68. return !this.highlight && this.queryCommandValue("link") ? 0 : -1;
  69. }
  70. };
  71. function doLink(range, opt, me) {
  72. var rngClone = range.cloneRange(),
  73. link = me.queryCommandValue("link");
  74. optimize((range = range.adjustmentBoundary()));
  75. var start = range.startContainer;
  76. if (start.nodeType == 1 && link) {
  77. start = start.childNodes[range.startOffset];
  78. if (
  79. start &&
  80. start.nodeType == 1 &&
  81. start.tagName == "A" &&
  82. /^(?:https?|ftp|file)\s*:\s*\/\//.test(
  83. start[browser.ie ? "innerText" : "textContent"]
  84. )
  85. ) {
  86. start[browser.ie ? "innerText" : "textContent"] = utils.html(
  87. opt.textValue || opt.href
  88. );
  89. }
  90. }
  91. if (!rngClone.collapsed || link) {
  92. range.removeInlineStyle("a");
  93. rngClone = range.cloneRange();
  94. }
  95. if (rngClone.collapsed) {
  96. var a = range.document.createElement("a"),
  97. text = "";
  98. if (opt.textValue) {
  99. text = utils.html(opt.textValue);
  100. delete opt.textValue;
  101. } else {
  102. text = utils.html(opt.href);
  103. }
  104. domUtils.setAttributes(a, opt);
  105. start = domUtils.findParentByTagName(rngClone.startContainer, "a", true);
  106. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  107. range.setStartAfter(start).collapse(true);
  108. }
  109. a[browser.ie ? "innerText" : "textContent"] = text;
  110. range.insertNode(a).selectNode(a);
  111. } else {
  112. range.applyInlineStyle("a", opt);
  113. }
  114. }
  115. UE.commands["link"] = {
  116. execCommand: function(cmdName, opt) {
  117. var range;
  118. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g));
  119. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g));
  120. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g));
  121. doLink((range = this.selection.getRange()), opt, this);
  122. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  123. range.collapse().select(true);
  124. },
  125. queryCommandValue: function() {
  126. var range = this.selection.getRange(),
  127. node;
  128. if (range.collapsed) {
  129. // node = this.selection.getStart();
  130. //在ie下getstart()取值偏上了
  131. node = range.startContainer;
  132. node = node.nodeType == 1 ? node : node.parentNode;
  133. if (
  134. node &&
  135. (node = domUtils.findParentByTagName(node, "a", true)) &&
  136. !domUtils.isInNodeEndBoundary(range, node)
  137. ) {
  138. return node;
  139. }
  140. } else {
  141. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  142. range.shrinkBoundary();
  143. var start = range.startContainer.nodeType == 3 ||
  144. !range.startContainer.childNodes[range.startOffset]
  145. ? range.startContainer
  146. : range.startContainer.childNodes[range.startOffset],
  147. end = range.endContainer.nodeType == 3 || range.endOffset == 0
  148. ? range.endContainer
  149. : range.endContainer.childNodes[range.endOffset - 1],
  150. common = range.getCommonAncestor();
  151. node = domUtils.findParentByTagName(common, "a", true);
  152. if (!node && common.nodeType == 1) {
  153. var as = common.getElementsByTagName("a"),
  154. ps,
  155. pe;
  156. for (var i = 0, ci; (ci = as[i++]); ) {
  157. (ps = domUtils.getPosition(ci, start)), (pe = domUtils.getPosition(
  158. ci,
  159. end
  160. ));
  161. if (
  162. (ps & domUtils.POSITION_FOLLOWING ||
  163. ps & domUtils.POSITION_CONTAINS) &&
  164. (pe & domUtils.POSITION_PRECEDING ||
  165. pe & domUtils.POSITION_CONTAINS)
  166. ) {
  167. node = ci;
  168. break;
  169. }
  170. }
  171. }
  172. return node;
  173. }
  174. },
  175. queryCommandState: function() {
  176. //判断如果是视频的话连接不可用
  177. //fix 853
  178. var img = this.selection.getRange().getClosedNode(),
  179. flag =
  180. img &&
  181. (img.className == "edui-faked-video" ||
  182. img.className.indexOf("edui-upload-video") != -1);
  183. return flag ? -1 : 0;
  184. }
  185. };
  186. };