image.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * 图片插入、排版插件
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. /**
  7. * 图片对齐方式
  8. * @command imagefloat
  9. * @method execCommand
  10. * @remind 值center为独占一行居中
  11. * @param { String } cmd 命令字符串
  12. * @param { String } align 对齐方式,可传left、right、none、center
  13. * @remaind center表示图片独占一行
  14. * @example
  15. * ```javascript
  16. * editor.execCommand( 'imagefloat', 'center' );
  17. * ```
  18. */
  19. /**
  20. * 如果选区所在位置是图片区域
  21. * @command imagefloat
  22. * @method queryCommandValue
  23. * @param { String } cmd 命令字符串
  24. * @return { String } 返回图片对齐方式
  25. * @example
  26. * ```javascript
  27. * editor.queryCommandValue( 'imagefloat' );
  28. * ```
  29. */
  30. UE.commands['imagefloat'] = {
  31. execCommand:function (cmd, align) {
  32. var me = this,
  33. range = me.selection.getRange();
  34. if (!range.collapsed) {
  35. var img = range.getClosedNode();
  36. if (img && img.tagName == 'IMG') {
  37. switch (align) {
  38. case 'left':
  39. case 'right':
  40. case 'none':
  41. var pN = img.parentNode, tmpNode, pre, next;
  42. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  43. pN = pN.parentNode;
  44. }
  45. tmpNode = pN;
  46. if (tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode, 'text-align') == 'center') {
  47. if (!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode, function (node) {
  48. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  49. }) == 1) {
  50. pre = tmpNode.previousSibling;
  51. next = tmpNode.nextSibling;
  52. if (pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)) {
  53. pre.appendChild(tmpNode.firstChild);
  54. while (next.firstChild) {
  55. pre.appendChild(next.firstChild);
  56. }
  57. domUtils.remove(tmpNode);
  58. domUtils.remove(next);
  59. } else {
  60. domUtils.setStyle(tmpNode, 'text-align', '');
  61. }
  62. }
  63. range.selectNode(img).select();
  64. }
  65. domUtils.setStyle(img, 'float', align == 'none' ? '' : align);
  66. if(align == 'none'){
  67. domUtils.removeAttributes(img,'align');
  68. }
  69. break;
  70. case 'center':
  71. if (me.queryCommandValue('imagefloat') != 'center') {
  72. pN = img.parentNode;
  73. domUtils.setStyle(img, 'float', '');
  74. domUtils.removeAttributes(img,'align');
  75. tmpNode = img;
  76. while (pN && domUtils.getChildCount(pN, function (node) {
  77. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  78. }) == 1
  79. && (dtd.$inline[pN.tagName] || pN.tagName == 'A')) {
  80. tmpNode = pN;
  81. pN = pN.parentNode;
  82. }
  83. range.setStartBefore(tmpNode).setCursor(false);
  84. pN = me.document.createElement('div');
  85. pN.appendChild(tmpNode);
  86. domUtils.setStyle(tmpNode, 'float', '');
  87. me.execCommand('insertHtml', '<p id="_img_parent_tmp" style="text-align:center">' + pN.innerHTML + '</p>');
  88. tmpNode = me.document.getElementById('_img_parent_tmp');
  89. tmpNode.removeAttribute('id');
  90. tmpNode = tmpNode.firstChild;
  91. range.selectNode(tmpNode).select();
  92. //去掉后边多余的元素
  93. next = tmpNode.parentNode.nextSibling;
  94. if (next && domUtils.isEmptyNode(next)) {
  95. domUtils.remove(next);
  96. }
  97. }
  98. break;
  99. }
  100. }
  101. }
  102. },
  103. queryCommandValue:function () {
  104. var range = this.selection.getRange(),
  105. startNode, floatStyle;
  106. if (range.collapsed) {
  107. return 'none';
  108. }
  109. startNode = range.getClosedNode();
  110. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  111. floatStyle = domUtils.getComputedStyle(startNode, 'float') || startNode.getAttribute('align');
  112. if (floatStyle == 'none') {
  113. floatStyle = domUtils.getComputedStyle(startNode.parentNode, 'text-align') == 'center' ? 'center' : floatStyle;
  114. }
  115. return {
  116. left:1,
  117. right:1,
  118. center:1
  119. }[floatStyle] ? floatStyle : 'none';
  120. }
  121. return 'none';
  122. },
  123. queryCommandState:function () {
  124. var range = this.selection.getRange(),
  125. startNode;
  126. if (range.collapsed) return -1;
  127. startNode = range.getClosedNode();
  128. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  129. return 0;
  130. }
  131. return -1;
  132. }
  133. };
  134. /**
  135. * 插入图片
  136. * @command insertimage
  137. * @method execCommand
  138. * @param { String } cmd 命令字符串
  139. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  140. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  141. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  142. * @example
  143. * ```javascript
  144. * editor.execCommand( 'insertimage', {
  145. * src:'a/b/c.jpg',
  146. * width:'100',
  147. * height:'100'
  148. * } );
  149. * ```
  150. * @example
  151. * ```javascript
  152. * editor.execCommand( 'insertimage', [{
  153. * src:'a/b/c.jpg',
  154. * width:'100',
  155. * height:'100'
  156. * },{
  157. * src:'a/b/d.jpg',
  158. * width:'100',
  159. * height:'100'
  160. * }] );
  161. * ```
  162. */
  163. UE.commands['insertimage'] = {
  164. execCommand:function (cmd, opt) {
  165. opt = utils.isArray(opt) ? opt : [opt];
  166. if (!opt.length) {
  167. return;
  168. }
  169. var me = this,
  170. range = me.selection.getRange(),
  171. img = range.getClosedNode();
  172. if(me.fireEvent('beforeinsertimage', opt) === true){
  173. return;
  174. }
  175. if (img && /img/i.test(img.tagName) && (img.className != "edui-faked-video" || img.className.indexOf("edui-upload-video")!=-1) && !img.getAttribute("word_img")) {
  176. var first = opt.shift();
  177. var floatStyle = first['floatStyle'];
  178. delete first['floatStyle'];
  179. //// img.style.border = (first.border||0) +"px solid #000";
  180. //// img.style.margin = (first.margin||0) +"px";
  181. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  182. domUtils.setAttributes(img, first);
  183. me.execCommand('imagefloat', floatStyle);
  184. if (opt.length > 0) {
  185. range.setStartAfter(img).setCursor(false, true);
  186. me.execCommand('insertimage', opt);
  187. }
  188. } else {
  189. var html = [], str = '', ci;
  190. ci = opt[0];
  191. if (opt.length == 1) {
  192. str = '<img src="' + ci.src + '" ' + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  193. (ci.width ? 'width="' + ci.width + '" ' : '') +
  194. (ci.height ? ' height="' + ci.height + '" ' : '') +
  195. (ci['floatStyle'] == 'left' || ci['floatStyle'] == 'right' ? ' style="float:' + ci['floatStyle'] + ';"' : '') +
  196. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : '') +
  197. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : '') +
  198. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : '') +
  199. (ci.hspace && ci.hspace != "0" ? ' hspace = "' + ci.hspace + '"' : '') +
  200. (ci.vspace && ci.vspace != "0" ? ' vspace = "' + ci.vspace + '"' : '') + '/>';
  201. if (ci['floatStyle'] == 'center') {
  202. str = '<p style="text-align: center">' + str + '</p>';
  203. }
  204. html.push(str);
  205. } else {
  206. for (var i = 0; ci = opt[i++];) {
  207. str = '<p ' + (ci['floatStyle'] == 'center' ? 'style="text-align: center" ' : '') + '><img src="' + ci.src + '" ' +
  208. (ci.width ? 'width="' + ci.width + '" ' : '') + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  209. (ci.height ? ' height="' + ci.height + '" ' : '') +
  210. ' style="' + (ci['floatStyle'] && ci['floatStyle'] != 'center' ? 'float:' + ci['floatStyle'] + ';' : '') +
  211. (ci.border || '') + '" ' +
  212. (ci.title ? ' title="' + ci.title + '"' : '') + ' /></p>';
  213. html.push(str);
  214. }
  215. }
  216. me.execCommand('insertHtml', html.join(''));
  217. }
  218. me.fireEvent('afterinsertimage', opt)
  219. }
  220. };