link.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ///import core
  2. ///commands 超链接,取消链接
  3. ///commandsName Link,Unlink
  4. ///commandsTitle 超链接,取消链接
  5. ///commandsDialog dialogs\link
  6. /**
  7. * 超链接
  8. * @function
  9. * @name baidu.editor.execCommand
  10. * @param {String} cmdName link插入超链接
  11. * @param {Object} options url地址,title标题,target是否打开新页
  12. * @author zhanyi
  13. */
  14. /**
  15. * 取消链接
  16. * @function
  17. * @name baidu.editor.execCommand
  18. * @param {String} cmdName unlink取消链接
  19. * @author zhanyi
  20. */
  21. (function() {
  22. function optimize( range ) {
  23. var start = range.startContainer,end = range.endContainer;
  24. if ( start = domUtils.findParentByTagName( start, 'a', true ) ) {
  25. range.setStartBefore( start );
  26. }
  27. if ( end = domUtils.findParentByTagName( end, 'a', true ) ) {
  28. range.setEndAfter( end );
  29. }
  30. }
  31. UE.commands['unlink'] = {
  32. execCommand : function() {
  33. var range = this.selection.getRange(),
  34. bookmark;
  35. if(range.collapsed && !domUtils.findParentByTagName( range.startContainer, 'a', true )){
  36. return;
  37. }
  38. bookmark = range.createBookmark();
  39. optimize( range );
  40. range.removeInlineStyle( 'a' ).moveToBookmark( bookmark ).select();
  41. },
  42. queryCommandState : function(){
  43. return !this.highlight && this.queryCommandValue('link') ? 0 : -1;
  44. }
  45. };
  46. function doLink(range,opt,me){
  47. var rngClone = range.cloneRange(),
  48. link = me.queryCommandValue('link');
  49. optimize( range = range.adjustmentBoundary() );
  50. var start = range.startContainer;
  51. if(start.nodeType == 1 && link){
  52. start = start.childNodes[range.startOffset];
  53. if(start && start.nodeType == 1 && start.tagName == 'A' && /^(?:https?|ftp|file)\s*:\s*\/\//.test(start[browser.ie?'innerText':'textContent'])){
  54. start[browser.ie ? 'innerText' : 'textContent'] = utils.html(opt.textValue||opt.href);
  55. }
  56. }
  57. if( !rngClone.collapsed || link){
  58. range.removeInlineStyle( 'a' );
  59. rngClone = range.cloneRange();
  60. }
  61. if ( rngClone.collapsed ) {
  62. var a = range.document.createElement( 'a'),
  63. text = '';
  64. if(opt.textValue){
  65. text = utils.html(opt.textValue);
  66. delete opt.textValue;
  67. }else{
  68. text = utils.html(opt.href);
  69. }
  70. domUtils.setAttributes( a, opt );
  71. start = domUtils.findParentByTagName( rngClone.startContainer, 'a', true );
  72. if(start && domUtils.isInNodeEndBoundary(rngClone,start)){
  73. range.setStartAfter(start).collapse(true);
  74. }
  75. a[browser.ie ? 'innerText' : 'textContent'] = text;
  76. range.insertNode(a).selectNode( a );
  77. } else {
  78. range.applyInlineStyle( 'a', opt );
  79. }
  80. }
  81. UE.commands['link'] = {
  82. execCommand : function( cmdName, opt ) {
  83. var range;
  84. opt.data_ue_src && (opt.data_ue_src = utils.unhtml(opt.data_ue_src,/[<">]/g));
  85. opt.href && (opt.href = utils.unhtml(opt.href,/[<">]/g));
  86. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue,/[<">]/g));
  87. doLink(range=this.selection.getRange(),opt,this);
  88. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  89. range.collapse().select(true);
  90. },
  91. queryCommandValue : function() {
  92. var range = this.selection.getRange(),
  93. node;
  94. if ( range.collapsed ) {
  95. // node = this.selection.getStart();
  96. //在ie下getstart()取值偏上了
  97. node = range.startContainer;
  98. node = node.nodeType == 1 ? node : node.parentNode;
  99. if ( node && (node = domUtils.findParentByTagName( node, 'a', true )) && ! domUtils.isInNodeEndBoundary(range,node)) {
  100. return node;
  101. }
  102. } else {
  103. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  104. range.shrinkBoundary();
  105. var start = range.startContainer.nodeType == 3 || !range.startContainer.childNodes[range.startOffset] ? range.startContainer : range.startContainer.childNodes[range.startOffset],
  106. end = range.endContainer.nodeType == 3 || range.endOffset == 0 ? range.endContainer : range.endContainer.childNodes[range.endOffset-1],
  107. common = range.getCommonAncestor();
  108. node = domUtils.findParentByTagName( common, 'a', true );
  109. if ( !node && common.nodeType == 1){
  110. var as = common.getElementsByTagName( 'a' ),
  111. ps,pe;
  112. for ( var i = 0,ci; ci = as[i++]; ) {
  113. ps = domUtils.getPosition( ci, start ),pe = domUtils.getPosition( ci,end);
  114. if ( (ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS)
  115. &&
  116. (pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
  117. ) {
  118. node = ci;
  119. break;
  120. }
  121. }
  122. }
  123. return node;
  124. }
  125. },
  126. queryCommandState : function() {
  127. //判断如果是视频的话连接不可用
  128. //fix 853
  129. var img = this.selection.getRange().getClosedNode(),
  130. flag = img && (img.className == "edui-faked-video");
  131. return flag ? -1 : 0;
  132. }
  133. };
  134. })();