video.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * video插件, 为UEditor提供视频插入支持
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. UE.plugins["video"] = function() {
  7. var me = this;
  8. /**
  9. * 创建插入视频字符窜
  10. * @param url 视频地址
  11. * @param width 视频宽度
  12. * @param height 视频高度
  13. * @param align 视频对齐
  14. * @param toEmbed 是否以flash代替显示
  15. * @param addParagraph 是否需要添加P 标签
  16. */
  17. function creatInsertStr(url, width, height, id, align, classname, type) {
  18. var str;
  19. switch (type) {
  20. case "image":
  21. str =
  22. "<img " +
  23. (id ? 'id="' + id + '"' : "") +
  24. ' width="' +
  25. width +
  26. '" height="' +
  27. height +
  28. '" _url="' +
  29. url +
  30. '" class="' +
  31. classname.replace(/\bvideo-js\b/, "") +
  32. '"' +
  33. ' src="' +
  34. me.options.UEDITOR_HOME_URL +
  35. 'themes/default/images/spacer.gif" style="background:url(' +
  36. me.options.UEDITOR_HOME_URL +
  37. "themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;" +
  38. (align ? "float:" + align + ";" : "") +
  39. '" />';
  40. break;
  41. case "embed":
  42. str =
  43. '<embed type="application/x-shockwave-flash" class="' +
  44. classname +
  45. '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  46. ' src="' +
  47. utils.html(url) +
  48. '" width="' +
  49. width +
  50. '" height="' +
  51. height +
  52. '"' +
  53. (align ? ' style="float:' + align + '"' : "") +
  54. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  55. break;
  56. case "video":
  57. var ext = url.substr(url.lastIndexOf(".") + 1);
  58. if (ext == "ogv") ext = "ogg";
  59. str =
  60. "<video" +
  61. (id ? ' id="' + id + '"' : "") +
  62. ' class="' +
  63. classname +
  64. ' video-js" ' +
  65. (align ? ' style="float:' + align + '"' : "") +
  66. ' controls preload="none" width="' +
  67. width +
  68. '" height="' +
  69. height +
  70. '" src="' +
  71. url +
  72. '" data-setup="{}">' +
  73. '<source src="' +
  74. url +
  75. '" type="video/' +
  76. ext +
  77. '" /></video>';
  78. break;
  79. }
  80. return str;
  81. }
  82. function switchImgAndVideo(root, img2video) {
  83. utils.each(
  84. root.getNodesByTagName(img2video ? "img" : "embed video"),
  85. function(node) {
  86. var className = node.getAttr("class");
  87. if (className && className.indexOf("edui-faked-video") != -1) {
  88. var html = creatInsertStr(
  89. img2video ? node.getAttr("_url") : node.getAttr("src"),
  90. node.getAttr("width"),
  91. node.getAttr("height"),
  92. null,
  93. node.getStyle("float") || "",
  94. className,
  95. img2video ? "embed" : "image"
  96. );
  97. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  98. }
  99. if (className && className.indexOf("edui-upload-video") != -1) {
  100. var html = creatInsertStr(
  101. img2video ? node.getAttr("_url") : node.getAttr("src"),
  102. node.getAttr("width"),
  103. node.getAttr("height"),
  104. null,
  105. node.getStyle("float") || "",
  106. className,
  107. img2video ? "video" : "image"
  108. );
  109. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  110. }
  111. }
  112. );
  113. }
  114. me.addOutputRule(function(root) {
  115. switchImgAndVideo(root, true);
  116. });
  117. me.addInputRule(function(root) {
  118. switchImgAndVideo(root);
  119. });
  120. /**
  121. * 插入视频
  122. * @command insertvideo
  123. * @method execCommand
  124. * @param { String } cmd 命令字符串
  125. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  126. * @example
  127. * ```javascript
  128. *
  129. * var videoAttr = {
  130. * //视频地址
  131. * url: 'http://www.youku.com/xxx',
  132. * //视频宽高值, 单位px
  133. * width: 200,
  134. * height: 100
  135. * };
  136. *
  137. * //editor 是编辑器实例
  138. * //向编辑器插入单个视频
  139. * editor.execCommand( 'insertvideo', videoAttr );
  140. * ```
  141. */
  142. /**
  143. * 插入视频
  144. * @command insertvideo
  145. * @method execCommand
  146. * @param { String } cmd 命令字符串
  147. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  148. * @example
  149. * ```javascript
  150. *
  151. * var videoAttr1 = {
  152. * //视频地址
  153. * url: 'http://www.youku.com/xxx',
  154. * //视频宽高值, 单位px
  155. * width: 200,
  156. * height: 100
  157. * },
  158. * videoAttr2 = {
  159. * //视频地址
  160. * url: 'http://www.youku.com/xxx',
  161. * //视频宽高值, 单位px
  162. * width: 200,
  163. * height: 100
  164. * }
  165. *
  166. * //editor 是编辑器实例
  167. * //该方法将会向编辑器内插入两个视频
  168. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  169. * ```
  170. */
  171. /**
  172. * 查询当前光标所在处是否是一个视频
  173. * @command insertvideo
  174. * @method queryCommandState
  175. * @param { String } cmd 需要查询的命令字符串
  176. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  177. * @example
  178. * ```javascript
  179. *
  180. * //editor 是编辑器实例
  181. * editor.queryCommandState( 'insertvideo' );
  182. * ```
  183. */
  184. me.commands["insertvideo"] = {
  185. execCommand: function(cmd, videoObjs, type) {
  186. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs];
  187. if (me.fireEvent("beforeinsertvideo", videoObjs) === true) {
  188. return;
  189. }
  190. var html = [],
  191. id = "tmpVedio",
  192. cl;
  193. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  194. vi = videoObjs[i];
  195. cl = type == "upload"
  196. ? "edui-upload-video video-js vjs-default-skin"
  197. : "edui-faked-video";
  198. html.push(
  199. creatInsertStr(
  200. vi.url,
  201. vi.width || 420,
  202. vi.height || 280,
  203. id + i,
  204. null,
  205. cl,
  206. "image"
  207. )
  208. );
  209. }
  210. me.execCommand("inserthtml", html.join(""), true);
  211. var rng = this.selection.getRange();
  212. for (var i = 0, len = videoObjs.length; i < len; i++) {
  213. var img = this.document.getElementById("tmpVedio" + i);
  214. domUtils.removeAttributes(img, "id");
  215. rng.selectNode(img).select();
  216. me.execCommand("imagefloat", videoObjs[i].align);
  217. }
  218. me.fireEvent("afterinsertvideo", videoObjs);
  219. },
  220. queryCommandState: function() {
  221. var img = me.selection.getRange().getClosedNode(),
  222. flag =
  223. img &&
  224. (img.className == "edui-faked-video" ||
  225. img.className.indexOf("edui-upload-video") != -1);
  226. return flag ? 1 : 0;
  227. }
  228. };
  229. };