image.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ///import core
  2. ///import plugins\inserthtml.js
  3. ///import plugins\catchremoteimage.js
  4. ///commands 插入图片,操作图片的对齐方式
  5. ///commandsName InsertImage,ImageNone,ImageLeft,ImageRight,ImageCenter
  6. ///commandsTitle 图片,默认,居左,居右,居中
  7. ///commandsDialog dialogs\image\image.html
  8. /**
  9. * Created by .
  10. * User: zhanyi
  11. * for image
  12. */
  13. UE.commands['imagefloat'] = {
  14. execCommand : function (cmd, align){
  15. var me = this,
  16. range = me.selection.getRange();
  17. if(!range.collapsed ){
  18. var img = range.getClosedNode();
  19. if(img && img.tagName == 'IMG'){
  20. switch (align){
  21. case 'left':
  22. case 'right':
  23. case 'none':
  24. var pN = img.parentNode,tmpNode,pre,next;
  25. while(dtd.$inline[pN.tagName] || pN.tagName == 'A'){
  26. pN = pN.parentNode;
  27. }
  28. tmpNode = pN;
  29. if(tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode,'text-align') == 'center'){
  30. if(!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode,function(node){return !domUtils.isBr(node) && !domUtils.isWhitespace(node)}) == 1){
  31. pre = tmpNode.previousSibling;
  32. next = tmpNode.nextSibling;
  33. if(pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)){
  34. pre.appendChild(tmpNode.firstChild);
  35. while(next.firstChild){
  36. pre.appendChild(next.firstChild)
  37. }
  38. domUtils.remove(tmpNode);
  39. domUtils.remove(next);
  40. }else{
  41. domUtils.setStyle(tmpNode,'text-align','')
  42. }
  43. }
  44. range.selectNode(img).select()
  45. }
  46. domUtils.setStyle(img,'float',align);
  47. break;
  48. case 'center':
  49. if(me.queryCommandValue('imagefloat') != 'center'){
  50. pN = img.parentNode;
  51. domUtils.setStyle(img,'float','none');
  52. tmpNode = img;
  53. while(pN && domUtils.getChildCount(pN,function(node){return !domUtils.isBr(node) && !domUtils.isWhitespace(node)}) == 1
  54. && (dtd.$inline[pN.tagName] || pN.tagName == 'A')){
  55. tmpNode = pN;
  56. pN = pN.parentNode;
  57. }
  58. range.setStartBefore(tmpNode).setCursor(false);
  59. pN = me.document.createElement('div');
  60. pN.appendChild(tmpNode);
  61. domUtils.setStyle(tmpNode,'float','');
  62. me.execCommand('insertHtml','<p id="_img_parent_tmp" style="text-align:center">'+pN.innerHTML+'</p>');
  63. tmpNode = me.document.getElementById('_img_parent_tmp');
  64. tmpNode.removeAttribute('id');
  65. tmpNode = tmpNode.firstChild;
  66. range.selectNode(tmpNode).select();
  67. //去掉后边多余的元素
  68. next = tmpNode.parentNode.nextSibling;
  69. if(next && domUtils.isEmptyNode(next)){
  70. domUtils.remove(next)
  71. }
  72. }
  73. break;
  74. }
  75. }
  76. }
  77. },
  78. queryCommandValue : function() {
  79. var range = this.selection.getRange(),
  80. startNode,floatStyle;
  81. if(range.collapsed){
  82. return 'none';
  83. }
  84. startNode = range.getClosedNode();
  85. if(startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG'){
  86. floatStyle = domUtils.getComputedStyle(startNode,'float');
  87. if(floatStyle == 'none'){
  88. floatStyle = domUtils.getComputedStyle(startNode.parentNode,'text-align') == 'center' ? 'center' : floatStyle
  89. }
  90. return {
  91. left : 1,
  92. right : 1,
  93. center : 1
  94. }[floatStyle] ? floatStyle : 'none'
  95. }
  96. return 'none'
  97. },
  98. queryCommandState : function(){
  99. if(this.highlight){
  100. return -1;
  101. }
  102. var range = this.selection.getRange(),
  103. startNode;
  104. if(range.collapsed){
  105. return -1;
  106. }
  107. startNode = range.getClosedNode();
  108. if(startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG'){
  109. return 0;
  110. }
  111. return -1;
  112. }
  113. };
  114. UE.commands['insertimage'] = {
  115. execCommand : function (cmd, opt){
  116. opt = utils.isArray(opt) ? opt : [opt];
  117. if(!opt.length) return;
  118. var me = this,
  119. range = me.selection.getRange(),
  120. img = range.getClosedNode();
  121. if(img && /img/i.test( img.tagName ) && img.className != "edui-faked-video" &&!img.getAttribute("word_img") ){
  122. var first = opt.shift();
  123. var floatStyle = first['floatStyle'];
  124. delete first['floatStyle'];
  125. //// img.style.border = (first.border||0) +"px solid #000";
  126. //// img.style.margin = (first.margin||0) +"px";
  127. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  128. domUtils.setAttributes(img,first);
  129. me.execCommand('imagefloat',floatStyle);
  130. if(opt.length > 0){
  131. range.setStartAfter(img).setCursor(false,true);
  132. me.execCommand('insertimage',opt);
  133. }
  134. }else{
  135. var html = [],str = '',ci;
  136. ci = opt[0];
  137. if(opt.length == 1){
  138. str = '<img src="'+ci.src+'" '+ (ci.data_ue_src ? ' data_ue_src="' + ci.data_ue_src +'" ':'') +
  139. (ci.width ? 'width="'+ci.width+'" ':'') +
  140. (ci.height ? ' height="'+ci.height+'" ':'') +
  141. (ci['floatStyle']&&ci['floatStyle']!='center' ? ' style="float:'+ci['floatStyle']+';"':'') +
  142. (ci.title?' title="'+ci.title+'"':'') + ' border="'+ (ci.border||0) + '" hspace = "'+(ci.hspace||0)+'" vspace = "'+(ci.vspace||0)+'" />';
  143. if(ci['floatStyle'] == 'center'){
  144. str = '<p style="text-align: center">'+str+'</p>'
  145. }
  146. html.push(str)
  147. }else{
  148. for(var i=0;ci=opt[i++];){
  149. str = '<p ' + (ci['floatStyle'] == 'center' ? 'style="text-align: center" ' : '') + '><img src="'+ci.src+'" '+
  150. (ci.width ? 'width="'+ci.width+'" ':'') + (ci.data_ue_src ? ' data_ue_src="' + ci.data_ue_src +'" ':'') +
  151. (ci.height ? ' height="'+ci.height+'" ':'') +
  152. ' style="' + (ci['floatStyle']&&ci['floatStyle']!='center' ? 'float:'+ci['floatStyle']+';':'') +
  153. (ci.border||'') + '" ' +
  154. (ci.title?' title="'+ci.title+'"':'') + ' /></p>';
  155. // if(ci['floatStyle'] == 'center'){
  156. // str = '<p style="text-align: center">'+str+'</p>'
  157. // }
  158. html.push(str)
  159. }
  160. }
  161. me.execCommand('insertHtml',html.join(''));
  162. }
  163. },
  164. queryCommandState : function(){
  165. return this.highlight ? -1 :0;
  166. }
  167. };