image.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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,
  42. tmpNode,
  43. pre,
  44. next;
  45. while (dtd.$inline[pN.tagName] || pN.tagName == "A") {
  46. pN = pN.parentNode;
  47. }
  48. tmpNode = pN;
  49. if (
  50. tmpNode.tagName == "P" &&
  51. domUtils.getStyle(tmpNode, "text-align") == "center"
  52. ) {
  53. if (
  54. !domUtils.isBody(tmpNode) &&
  55. domUtils.getChildCount(tmpNode, function(node) {
  56. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  57. }) == 1
  58. ) {
  59. pre = tmpNode.previousSibling;
  60. next = tmpNode.nextSibling;
  61. if (
  62. pre &&
  63. next &&
  64. pre.nodeType == 1 &&
  65. next.nodeType == 1 &&
  66. pre.tagName == next.tagName &&
  67. domUtils.isBlockElm(pre)
  68. ) {
  69. pre.appendChild(tmpNode.firstChild);
  70. while (next.firstChild) {
  71. pre.appendChild(next.firstChild);
  72. }
  73. domUtils.remove(tmpNode);
  74. domUtils.remove(next);
  75. } else {
  76. domUtils.setStyle(tmpNode, "text-align", "");
  77. }
  78. }
  79. range.selectNode(img).select();
  80. }
  81. domUtils.setStyle(img, "float", align == "none" ? "" : align);
  82. if (align == "none") {
  83. domUtils.removeAttributes(img, "align");
  84. }
  85. break;
  86. case "center":
  87. if (me.queryCommandValue("imagefloat") != "center") {
  88. pN = img.parentNode;
  89. domUtils.setStyle(img, "float", "");
  90. domUtils.removeAttributes(img, "align");
  91. tmpNode = img;
  92. while (
  93. pN &&
  94. domUtils.getChildCount(pN, function(node) {
  95. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  96. }) == 1 &&
  97. (dtd.$inline[pN.tagName] || pN.tagName == "A")
  98. ) {
  99. tmpNode = pN;
  100. pN = pN.parentNode;
  101. }
  102. range.setStartBefore(tmpNode).setCursor(false);
  103. pN = me.document.createElement("div");
  104. pN.appendChild(tmpNode);
  105. domUtils.setStyle(tmpNode, "float", "");
  106. me.execCommand(
  107. "insertHtml",
  108. '<p id="_img_parent_tmp" style="text-align:center">' +
  109. pN.innerHTML +
  110. "</p>"
  111. );
  112. tmpNode = me.document.getElementById("_img_parent_tmp");
  113. tmpNode.removeAttribute("id");
  114. tmpNode = tmpNode.firstChild;
  115. range.selectNode(tmpNode).select();
  116. //去掉后边多余的元素
  117. next = tmpNode.parentNode.nextSibling;
  118. if (next && domUtils.isEmptyNode(next)) {
  119. domUtils.remove(next);
  120. }
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. },
  127. queryCommandValue: function() {
  128. var range = this.selection.getRange(),
  129. startNode,
  130. floatStyle;
  131. if (range.collapsed) {
  132. return "none";
  133. }
  134. startNode = range.getClosedNode();
  135. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  136. floatStyle =
  137. domUtils.getComputedStyle(startNode, "float") ||
  138. startNode.getAttribute("align");
  139. if (floatStyle == "none") {
  140. floatStyle = domUtils.getComputedStyle(
  141. startNode.parentNode,
  142. "text-align"
  143. ) == "center"
  144. ? "center"
  145. : floatStyle;
  146. }
  147. return {
  148. left: 1,
  149. right: 1,
  150. center: 1
  151. }[floatStyle]
  152. ? floatStyle
  153. : "none";
  154. }
  155. return "none";
  156. },
  157. queryCommandState: function() {
  158. var range = this.selection.getRange(),
  159. startNode;
  160. if (range.collapsed) return -1;
  161. startNode = range.getClosedNode();
  162. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  163. return 0;
  164. }
  165. return -1;
  166. }
  167. };
  168. /**
  169. * 插入图片
  170. * @command insertimage
  171. * @method execCommand
  172. * @param { String } cmd 命令字符串
  173. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  174. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  175. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  176. * @example
  177. * ```javascript
  178. * editor.execCommand( 'insertimage', {
  179. * src:'a/b/c.jpg',
  180. * width:'100',
  181. * height:'100'
  182. * } );
  183. * ```
  184. * @example
  185. * ```javascript
  186. * editor.execCommand( 'insertimage', [{
  187. * src:'a/b/c.jpg',
  188. * width:'100',
  189. * height:'100'
  190. * },{
  191. * src:'a/b/d.jpg',
  192. * width:'100',
  193. * height:'100'
  194. * }] );
  195. * ```
  196. */
  197. UE.commands["insertimage"] = {
  198. execCommand: function(cmd, opt) {
  199. opt = utils.isArray(opt) ? opt : [opt];
  200. if (!opt.length) {
  201. return;
  202. }
  203. var me = this,
  204. range = me.selection.getRange(),
  205. img = range.getClosedNode();
  206. if (me.fireEvent("beforeinsertimage", opt) === true) {
  207. return;
  208. }
  209. if (
  210. img &&
  211. /img/i.test(img.tagName) &&
  212. (img.className != "edui-faked-video" ||
  213. img.className.indexOf("edui-upload-video") != -1) &&
  214. !img.getAttribute("word_img")
  215. ) {
  216. var first = opt.shift();
  217. var floatStyle = first["floatStyle"];
  218. delete first["floatStyle"];
  219. //// img.style.border = (first.border||0) +"px solid #000";
  220. //// img.style.margin = (first.margin||0) +"px";
  221. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  222. domUtils.setAttributes(img, first);
  223. me.execCommand("imagefloat", floatStyle);
  224. if (opt.length > 0) {
  225. range.setStartAfter(img).setCursor(false, true);
  226. me.execCommand("insertimage", opt);
  227. }
  228. } else {
  229. var html = [],
  230. str = "",
  231. ci;
  232. ci = opt[0];
  233. if (opt.length == 1) {
  234. str =
  235. '<img src="' +
  236. ci.src +
  237. '" ' +
  238. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  239. (ci.width ? 'width="' + ci.width + '" ' : "") +
  240. (ci.height ? ' height="' + ci.height + '" ' : "") +
  241. (ci["floatStyle"] == "left" || ci["floatStyle"] == "right"
  242. ? ' style="float:' + ci["floatStyle"] + ';"'
  243. : "") +
  244. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : "") +
  245. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : "") +
  246. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : "") +
  247. (ci.hspace && ci.hspace != "0"
  248. ? ' hspace = "' + ci.hspace + '"'
  249. : "") +
  250. (ci.vspace && ci.vspace != "0"
  251. ? ' vspace = "' + ci.vspace + '"'
  252. : "") +
  253. "/>";
  254. if (ci["floatStyle"] == "center") {
  255. str = '<p style="text-align: center">' + str + "</p>";
  256. }
  257. html.push(str);
  258. } else {
  259. for (var i = 0; (ci = opt[i++]); ) {
  260. str =
  261. "<p " +
  262. (ci["floatStyle"] == "center"
  263. ? 'style="text-align: center" '
  264. : "") +
  265. '><img src="' +
  266. ci.src +
  267. '" ' +
  268. (ci.width ? 'width="' + ci.width + '" ' : "") +
  269. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  270. (ci.height ? ' height="' + ci.height + '" ' : "") +
  271. ' style="' +
  272. (ci["floatStyle"] && ci["floatStyle"] != "center"
  273. ? "float:" + ci["floatStyle"] + ";"
  274. : "") +
  275. (ci.border || "") +
  276. '" ' +
  277. (ci.title ? ' title="' + ci.title + '"' : "") +
  278. " /></p>";
  279. html.push(str);
  280. }
  281. }
  282. me.execCommand("insertHtml", html.join(""));
  283. }
  284. me.fireEvent("afterinsertimage", opt);
  285. }
  286. };