You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

880 lines
22 KiB

  1. /**
  2. * plugin.js
  3. *
  4. * Released under LGPL License.
  5. * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /*jshint maxlen:255 */
  11. /*eslint max-len:0 */
  12. /*global tinymce:true */
  13. tinymce.PluginManager.add('media', function(editor, url) {
  14. var urlPatterns = [
  15. {regex: /youtu\.be\/([\w\-.]+)/, type: 'iframe', w: 560, h: 314, url: '//www.youtube.com/embed/$1', allowFullscreen: true},
  16. {regex: /youtube\.com(.+)v=([^&]+)/, type: 'iframe', w: 560, h: 314, url: '//www.youtube.com/embed/$2', allowFullscreen: true},
  17. {regex: /youtube.com\/embed\/([a-z0-9\-_]+(?:\?.+)?)/i, type: 'iframe', w: 560, h: 314, url: '//www.youtube.com/embed/$1', allowFullscreen: true},
  18. {regex: /vimeo\.com\/([0-9]+)/, type: 'iframe', w: 425, h: 350, url: '//player.vimeo.com/video/$1?title=0&byline=0&portrait=0&color=8dc7dc', allowfullscreen: true},
  19. {regex: /vimeo\.com\/(.*)\/([0-9]+)/, type: "iframe", w: 425, h: 350, url: "//player.vimeo.com/video/$2?title=0&byline=0", allowfullscreen: true},
  20. {regex: /maps\.google\.([a-z]{2,3})\/maps\/(.+)msid=(.+)/, type: 'iframe', w: 425, h: 350, url: '//maps.google.com/maps/ms?msid=$2&output=embed"', allowFullscreen: false},
  21. {regex: /dailymotion\.com\/video\/([^_]+)/, type: 'iframe', w: 480, h: 270, url: '//www.dailymotion.com/embed/video/$1', allowFullscreen: true}
  22. ];
  23. var embedChange = (tinymce.Env.ie && tinymce.Env.ie <= 8) ? 'onChange' : 'onInput';
  24. function guessMime(url) {
  25. url = url.toLowerCase();
  26. if (url.indexOf('.mp3') != -1) {
  27. return 'audio/mpeg';
  28. }
  29. if (url.indexOf('.wav') != -1) {
  30. return 'audio/wav';
  31. }
  32. if (url.indexOf('.mp4') != -1) {
  33. return 'video/mp4';
  34. }
  35. if (url.indexOf('.webm') != -1) {
  36. return 'video/webm';
  37. }
  38. if (url.indexOf('.ogg') != -1) {
  39. return 'video/ogg';
  40. }
  41. if (url.indexOf('.swf') != -1) {
  42. return 'application/x-shockwave-flash';
  43. }
  44. return '';
  45. }
  46. function getVideoScriptMatch(src) {
  47. var prefixes = editor.settings.media_scripts;
  48. if (prefixes) {
  49. for (var i = 0; i < prefixes.length; i++) {
  50. if (src.indexOf(prefixes[i].filter) !== -1) {
  51. return prefixes[i];
  52. }
  53. }
  54. }
  55. }
  56. function showDialog() {
  57. var win, width, height, data;
  58. var generalFormItems = [
  59. {
  60. name: 'source1',
  61. type: 'filepicker',
  62. filetype: 'media',
  63. size: 40,
  64. autofocus: true,
  65. label: 'Source',
  66. onchange: function(e) {
  67. tinymce.each(e.meta, function(value, key) {
  68. win.find('#' + key).value(value);
  69. });
  70. }
  71. }
  72. ];
  73. function recalcSize(e) {
  74. var widthCtrl, heightCtrl, newWidth, newHeight;
  75. widthCtrl = win.find('#width')[0];
  76. heightCtrl = win.find('#height')[0];
  77. newWidth = widthCtrl.value();
  78. newHeight = heightCtrl.value();
  79. if (win.find('#constrain')[0].checked() && width && height && newWidth && newHeight) {
  80. if (e.control == widthCtrl) {
  81. newHeight = Math.round((newWidth / width) * newHeight);
  82. if (!isNaN(newHeight)) {
  83. heightCtrl.value(newHeight);
  84. }
  85. } else {
  86. newWidth = Math.round((newHeight / height) * newWidth);
  87. if (!isNaN(newWidth)) {
  88. widthCtrl.value(newWidth);
  89. }
  90. }
  91. }
  92. width = newWidth;
  93. height = newHeight;
  94. }
  95. if (editor.settings.media_alt_source !== false) {
  96. generalFormItems.push({name: 'source2', type: 'filepicker', filetype: 'media', size: 40, label: 'Alternative source'});
  97. }
  98. if (editor.settings.media_poster !== false) {
  99. generalFormItems.push({name: 'poster', type: 'filepicker', filetype: 'image', size: 40, label: 'Poster'});
  100. }
  101. if (editor.settings.media_dimensions !== false) {
  102. generalFormItems.push({
  103. type: 'container',
  104. label: 'Dimensions',
  105. layout: 'flex',
  106. align: 'center',
  107. spacing: 5,
  108. items: [
  109. {name: 'width', type: 'textbox', maxLength: 5, size: 3, onchange: recalcSize, ariaLabel: 'Width'},
  110. {type: 'label', text: 'x'},
  111. {name: 'height', type: 'textbox', maxLength: 5, size: 3, onchange: recalcSize, ariaLabel: 'Height'},
  112. {name: 'constrain', type: 'checkbox', checked: true, text: 'Constrain proportions'}
  113. ]
  114. });
  115. }
  116. data = getData(editor.selection.getNode());
  117. width = data.width;
  118. height = data.height;
  119. var embedTextBox = {
  120. id: 'mcemediasource',
  121. type: 'textbox',
  122. flex: 1,
  123. name: 'embed',
  124. value: getSource(),
  125. multiline: true,
  126. label: 'Source'
  127. };
  128. function updateValueOnChange() {
  129. data = htmlToData(this.value());
  130. this.parent().parent().fromJSON(data);
  131. }
  132. embedTextBox[embedChange] = updateValueOnChange;
  133. win = editor.windowManager.open({
  134. title: 'Insert/edit video',
  135. data: data,
  136. bodyType: 'tabpanel',
  137. body: [
  138. {
  139. title: 'General',
  140. type: "form",
  141. onShowTab: function() {
  142. data = htmlToData(this.next().find('#embed').value());
  143. this.fromJSON(data);
  144. },
  145. items: generalFormItems
  146. },
  147. {
  148. title: 'Embed',
  149. type: "container",
  150. layout: 'flex',
  151. direction: 'column',
  152. align: 'stretch',
  153. padding: 10,
  154. spacing: 10,
  155. onShowTab: function() {
  156. this.find('#embed').value(dataToHtml(this.parent().toJSON()));
  157. },
  158. items: [
  159. {
  160. type: 'label',
  161. text: 'Paste your embed code below:',
  162. forId: 'mcemediasource'
  163. },
  164. embedTextBox
  165. ]
  166. }
  167. ],
  168. onSubmit: function() {
  169. var beforeObjects, afterObjects, i, y;
  170. beforeObjects = editor.dom.select('img[data-mce-object]');
  171. editor.insertContent(dataToHtml(this.toJSON()));
  172. afterObjects = editor.dom.select('img[data-mce-object]');
  173. // Find new image placeholder so we can select it
  174. for (i = 0; i < beforeObjects.length; i++) {
  175. for (y = afterObjects.length - 1; y >= 0; y--) {
  176. if (beforeObjects[i] == afterObjects[y]) {
  177. afterObjects.splice(y, 1);
  178. }
  179. }
  180. }
  181. editor.selection.select(afterObjects[0]);
  182. editor.nodeChanged();
  183. }
  184. });
  185. }
  186. function getSource() {
  187. var elm = editor.selection.getNode();
  188. if (elm.getAttribute('data-mce-object')) {
  189. return editor.selection.getContent();
  190. }
  191. }
  192. function dataToHtml(data) {
  193. var html = '';
  194. if (!data.source1) {
  195. tinymce.extend(data, htmlToData(data.embed));
  196. if (!data.source1) {
  197. return '';
  198. }
  199. }
  200. if (!data.source2) {
  201. data.source2 = '';
  202. }
  203. if (!data.poster) {
  204. data.poster = '';
  205. }
  206. data.source1 = editor.convertURL(data.source1, "source");
  207. data.source2 = editor.convertURL(data.source2, "source");
  208. data.source1mime = guessMime(data.source1);
  209. data.source2mime = guessMime(data.source2);
  210. data.poster = editor.convertURL(data.poster, "poster");
  211. data.flashPlayerUrl = editor.convertURL(url + '/moxieplayer.swf', "movie");
  212. tinymce.each(urlPatterns, function(pattern) {
  213. var match, i, url;
  214. if ((match = pattern.regex.exec(data.source1))) {
  215. url = pattern.url;
  216. for (i = 0; match[i]; i++) {
  217. /*jshint loopfunc:true*/
  218. /*eslint no-loop-func:0 */
  219. url = url.replace('$' + i, function() {
  220. return match[i];
  221. });
  222. }
  223. data.source1 = url;
  224. data.type = pattern.type;
  225. data.allowFullscreen = pattern.allowFullscreen;
  226. data.width = data.width || pattern.w;
  227. data.height = data.height || pattern.h;
  228. }
  229. });
  230. if (data.embed) {
  231. html = updateHtml(data.embed, data, true);
  232. } else {
  233. var videoScript = getVideoScriptMatch(data.source1);
  234. if (videoScript) {
  235. data.type = 'script';
  236. data.width = videoScript.width;
  237. data.height = videoScript.height;
  238. }
  239. data.width = data.width || 300;
  240. data.height = data.height || 150;
  241. tinymce.each(data, function(value, key) {
  242. data[key] = editor.dom.encode(value);
  243. });
  244. if (data.type == "iframe") {
  245. var allowFullscreen = data.allowFullscreen ? ' allowFullscreen="1"' : '';
  246. html += '<iframe src="' + data.source1 + '" width="' + data.width + '" height="' + data.height + '"' + allowFullscreen + '></iframe>';
  247. } else if (data.source1mime == "application/x-shockwave-flash") {
  248. html += '<object data="' + data.source1 + '" width="' + data.width + '" height="' + data.height + '" type="application/x-shockwave-flash">';
  249. if (data.poster) {
  250. html += '<img src="' + data.poster + '" width="' + data.width + '" height="' + data.height + '" />';
  251. }
  252. html += '</object>';
  253. } else if (data.source1mime.indexOf('audio') != -1) {
  254. if (editor.settings.audio_template_callback) {
  255. html = editor.settings.audio_template_callback(data);
  256. } else {
  257. html += (
  258. '<audio controls="controls" src="' + data.source1 + '">' +
  259. (data.source2 ? '\n<source src="' + data.source2 + '"' + (data.source2mime ? ' type="' + data.source2mime + '"' : '') + ' />\n' : '') +
  260. '</audio>'
  261. );
  262. }
  263. } else if (data.type == "script") {
  264. html += '<script src="' + data.source1 + '"></script>';
  265. } else {
  266. if (editor.settings.video_template_callback) {
  267. html = editor.settings.video_template_callback(data);
  268. } else {
  269. html = (
  270. '<video width="' + data.width + '" height="' + data.height + '"' + (data.poster ? ' poster="' + data.poster + '"' : '') + ' controls="controls">\n' +
  271. '<source src="' + data.source1 + '"' + (data.source1mime ? ' type="' + data.source1mime + '"' : '') + ' />\n' +
  272. (data.source2 ? '<source src="' + data.source2 + '"' + (data.source2mime ? ' type="' + data.source2mime + '"' : '') + ' />\n' : '') +
  273. '</video>'
  274. );
  275. }
  276. }
  277. }
  278. return html;
  279. }
  280. function htmlToData(html) {
  281. var data = {};
  282. new tinymce.html.SaxParser({
  283. validate: false,
  284. allow_conditional_comments: true,
  285. special: 'script,noscript',
  286. start: function(name, attrs) {
  287. if (!data.source1 && name == "param") {
  288. data.source1 = attrs.map.movie;
  289. }
  290. if (name == "iframe" || name == "object" || name == "embed" || name == "video" || name == "audio") {
  291. if (!data.type) {
  292. data.type = name;
  293. }
  294. data = tinymce.extend(attrs.map, data);
  295. }
  296. if (name == "script") {
  297. var videoScript = getVideoScriptMatch(attrs.map.src);
  298. if (!videoScript) {
  299. return;
  300. }
  301. data = {
  302. type: "script",
  303. source1: attrs.map.src,
  304. width: videoScript.width,
  305. height: videoScript.height
  306. };
  307. }
  308. if (name == "source") {
  309. if (!data.source1) {
  310. data.source1 = attrs.map.src;
  311. } else if (!data.source2) {
  312. data.source2 = attrs.map.src;
  313. }
  314. }
  315. if (name == "img" && !data.poster) {
  316. data.poster = attrs.map.src;
  317. }
  318. }
  319. }).parse(html);
  320. data.source1 = data.source1 || data.src || data.data;
  321. data.source2 = data.source2 || '';
  322. data.poster = data.poster || '';
  323. return data;
  324. }
  325. function getData(element) {
  326. if (element.getAttribute('data-mce-object')) {
  327. return htmlToData(editor.serializer.serialize(element, {selection: true}));
  328. }
  329. return {};
  330. }
  331. function sanitize(html) {
  332. if (editor.settings.media_filter_html === false) {
  333. return html;
  334. }
  335. var writer = new tinymce.html.Writer(), blocked;
  336. new tinymce.html.SaxParser({
  337. validate: false,
  338. allow_conditional_comments: false,
  339. special: 'script,noscript',
  340. comment: function(text) {
  341. writer.comment(text);
  342. },
  343. cdata: function(text) {
  344. writer.cdata(text);
  345. },
  346. text: function(text, raw) {
  347. writer.text(text, raw);
  348. },
  349. start: function(name, attrs, empty) {
  350. blocked = true;
  351. if (name == 'script' || name == 'noscript') {
  352. return;
  353. }
  354. for (var i = 0; i < attrs.length; i++) {
  355. if (attrs[i].name.indexOf('on') === 0) {
  356. return;
  357. }
  358. if (attrs[i].name == 'style') {
  359. attrs[i].value = editor.dom.serializeStyle(editor.dom.parseStyle(attrs[i].value), name);
  360. }
  361. }
  362. writer.start(name, attrs, empty);
  363. blocked = false;
  364. },
  365. end: function(name) {
  366. if (blocked) {
  367. return;
  368. }
  369. writer.end(name);
  370. }
  371. }, new tinymce.html.Schema({})).parse(html);
  372. return writer.getContent();
  373. }
  374. function updateHtml(html, data, updateAll) {
  375. var writer = new tinymce.html.Writer();
  376. var sourceCount = 0, hasImage;
  377. function setAttributes(attrs, updatedAttrs) {
  378. var name, i, value, attr;
  379. for (name in updatedAttrs) {
  380. value = "" + updatedAttrs[name];
  381. if (attrs.map[name]) {
  382. i = attrs.length;
  383. while (i--) {
  384. attr = attrs[i];
  385. if (attr.name == name) {
  386. if (value) {
  387. attrs.map[name] = value;
  388. attr.value = value;
  389. } else {
  390. delete attrs.map[name];
  391. attrs.splice(i, 1);
  392. }
  393. }
  394. }
  395. } else if (value) {
  396. attrs.push({
  397. name: name,
  398. value: value
  399. });
  400. attrs.map[name] = value;
  401. }
  402. }
  403. }
  404. new tinymce.html.SaxParser({
  405. validate: false,
  406. allow_conditional_comments: true,
  407. special: 'script,noscript',
  408. comment: function(text) {
  409. writer.comment(text);
  410. },
  411. cdata: function(text) {
  412. writer.cdata(text);
  413. },
  414. text: function(text, raw) {
  415. writer.text(text, raw);
  416. },
  417. start: function(name, attrs, empty) {
  418. switch (name) {
  419. case "video":
  420. case "object":
  421. case "embed":
  422. case "img":
  423. case "iframe":
  424. setAttributes(attrs, {
  425. width: data.width,
  426. height: data.height
  427. });
  428. break;
  429. }
  430. if (updateAll) {
  431. switch (name) {
  432. case "video":
  433. setAttributes(attrs, {
  434. poster: data.poster,
  435. src: ""
  436. });
  437. if (data.source2) {
  438. setAttributes(attrs, {
  439. src: ""
  440. });
  441. }
  442. break;
  443. case "iframe":
  444. setAttributes(attrs, {
  445. src: data.source1
  446. });
  447. break;
  448. case "source":
  449. sourceCount++;
  450. if (sourceCount <= 2) {
  451. setAttributes(attrs, {
  452. src: data["source" + sourceCount],
  453. type: data["source" + sourceCount + "mime"]
  454. });
  455. if (!data["source" + sourceCount]) {
  456. return;
  457. }
  458. }
  459. break;
  460. case "img":
  461. if (!data.poster) {
  462. return;
  463. }
  464. hasImage = true;
  465. break;
  466. }
  467. }
  468. writer.start(name, attrs, empty);
  469. },
  470. end: function(name) {
  471. if (name == "video" && updateAll) {
  472. for (var index = 1; index <= 2; index++) {
  473. if (data["source" + index]) {
  474. var attrs = [];
  475. attrs.map = {};
  476. if (sourceCount < index) {
  477. setAttributes(attrs, {
  478. src: data["source" + index],
  479. type: data["source" + index + "mime"]
  480. });
  481. writer.start("source", attrs, true);
  482. }
  483. }
  484. }
  485. }
  486. if (data.poster && name == "object" && updateAll && !hasImage) {
  487. var imgAttrs = [];
  488. imgAttrs.map = {};
  489. setAttributes(imgAttrs, {
  490. src: data.poster,
  491. width: data.width,
  492. height: data.height
  493. });
  494. writer.start("img", imgAttrs, true);
  495. }
  496. writer.end(name);
  497. }
  498. }, new tinymce.html.Schema({})).parse(html);
  499. return writer.getContent();
  500. }
  501. editor.on('ResolveName', function(e) {
  502. var name;
  503. if (e.target.nodeType == 1 && (name = e.target.getAttribute("data-mce-object"))) {
  504. e.name = name;
  505. }
  506. });
  507. function retainAttributesAndInnerHtml(sourceNode, targetNode) {
  508. var attrName, attrValue, attribs, ai, innerHtml;
  509. // Prefix all attributes except width, height and style since we
  510. // will add these to the placeholder
  511. attribs = sourceNode.attributes;
  512. ai = attribs.length;
  513. while (ai--) {
  514. attrName = attribs[ai].name;
  515. attrValue = attribs[ai].value;
  516. if (attrName !== "width" && attrName !== "height" && attrName !== "style") {
  517. if (attrName == "data" || attrName == "src") {
  518. attrValue = editor.convertURL(attrValue, attrName);
  519. }
  520. targetNode.attr('data-mce-p-' + attrName, attrValue);
  521. }
  522. }
  523. // Place the inner HTML contents inside an escaped attribute
  524. // This enables us to copy/paste the fake object
  525. innerHtml = sourceNode.firstChild && sourceNode.firstChild.value;
  526. if (innerHtml) {
  527. targetNode.attr("data-mce-html", escape(innerHtml));
  528. targetNode.firstChild = null;
  529. }
  530. }
  531. function createPlaceholderNode(node) {
  532. var placeHolder, name = node.name;
  533. placeHolder = new tinymce.html.Node('img', 1);
  534. placeHolder.shortEnded = true;
  535. retainAttributesAndInnerHtml(node, placeHolder);
  536. placeHolder.attr({
  537. width: node.attr('width') || "300",
  538. height: node.attr('height') || (name == "audio" ? "30" : "150"),
  539. style: node.attr('style'),
  540. src: tinymce.Env.transparentSrc,
  541. "data-mce-object": name,
  542. "class": "mce-object mce-object-" + name
  543. });
  544. return placeHolder;
  545. }
  546. function createPreviewNode(node) {
  547. var previewWrapper, previewNode, shimNode, name = node.name;
  548. previewWrapper = new tinymce.html.Node('span', 1);
  549. previewWrapper.attr({
  550. contentEditable: 'false',
  551. style: node.attr('style'),
  552. "data-mce-object": name,
  553. "class": "mce-preview-object mce-object-" + name
  554. });
  555. retainAttributesAndInnerHtml(node, previewWrapper);
  556. previewNode = new tinymce.html.Node(name, 1);
  557. previewNode.attr({
  558. src: node.attr('src'),
  559. allowfullscreen: node.attr('allowfullscreen'),
  560. width: node.attr('width') || "300",
  561. height: node.attr('height') || (name == "audio" ? "30" : "150"),
  562. frameborder: '0'
  563. });
  564. shimNode = new tinymce.html.Node('span', 1);
  565. shimNode.attr('class', 'mce-shim');
  566. previewWrapper.append(previewNode);
  567. previewWrapper.append(shimNode);
  568. return previewWrapper;
  569. }
  570. editor.on('preInit', function() {
  571. // Make sure that any messy HTML is retained inside these
  572. var specialElements = editor.schema.getSpecialElements();
  573. tinymce.each('video audio iframe object'.split(' '), function(name) {
  574. specialElements[name] = new RegExp('<\/' + name + '[^>]*>', 'gi');
  575. });
  576. // Allow elements
  577. //editor.schema.addValidElements('object[id|style|width|height|classid|codebase|*],embed[id|style|width|height|type|src|*],video[*],audio[*]');
  578. // Set allowFullscreen attribs as boolean
  579. var boolAttrs = editor.schema.getBoolAttrs();
  580. tinymce.each('webkitallowfullscreen mozallowfullscreen allowfullscreen'.split(' '), function(name) {
  581. boolAttrs[name] = {};
  582. });
  583. // Converts iframe, video etc into placeholder images
  584. editor.parser.addNodeFilter('iframe,video,audio,object,embed,script', function(nodes) {
  585. var i = nodes.length, node, placeHolder, videoScript;
  586. while (i--) {
  587. node = nodes[i];
  588. if (!node.parent) {
  589. continue;
  590. }
  591. if (node.parent.attr('data-mce-object')) {
  592. continue;
  593. }
  594. if (node.name == 'script') {
  595. videoScript = getVideoScriptMatch(node.attr('src'));
  596. if (!videoScript) {
  597. continue;
  598. }
  599. }
  600. if (videoScript) {
  601. if (videoScript.width) {
  602. node.attr('width', videoScript.width.toString());
  603. }
  604. if (videoScript.height) {
  605. node.attr('height', videoScript.height.toString());
  606. }
  607. }
  608. if (node.name == 'iframe' && editor.settings.media_live_embeds !== false && tinymce.Env.ceFalse) {
  609. placeHolder = createPreviewNode(node);
  610. } else {
  611. placeHolder = createPlaceholderNode(node);
  612. }
  613. node.replace(placeHolder);
  614. }
  615. });
  616. // Replaces placeholder images with real elements for video, object, iframe etc
  617. editor.serializer.addAttributeFilter('data-mce-object', function(nodes, name) {
  618. var i = nodes.length, node, realElm, ai, attribs, innerHtml, innerNode, realElmName, className;
  619. while (i--) {
  620. node = nodes[i];
  621. if (!node.parent) {
  622. continue;
  623. }
  624. realElmName = node.attr(name);
  625. realElm = new tinymce.html.Node(realElmName, 1);
  626. // Add width/height to everything but audio
  627. if (realElmName != "audio" && realElmName != "script") {
  628. className = node.attr('class');
  629. if (className && className.indexOf('mce-preview-object') !== -1) {
  630. realElm.attr({
  631. width: node.firstChild.attr('width'),
  632. height: node.firstChild.attr('height')
  633. });
  634. } else {
  635. realElm.attr({
  636. width: node.attr('width'),
  637. height: node.attr('height')
  638. });
  639. }
  640. }
  641. realElm.attr({
  642. style: node.attr('style')
  643. });
  644. // Unprefix all placeholder attributes
  645. attribs = node.attributes;
  646. ai = attribs.length;
  647. while (ai--) {
  648. var attrName = attribs[ai].name;
  649. if (attrName.indexOf('data-mce-p-') === 0) {
  650. realElm.attr(attrName.substr(11), attribs[ai].value);
  651. }
  652. }
  653. if (realElmName == "script") {
  654. realElm.attr('type', 'text/javascript');
  655. }
  656. // Inject innerhtml
  657. innerHtml = node.attr('data-mce-html');
  658. if (innerHtml) {
  659. innerNode = new tinymce.html.Node('#text', 3);
  660. innerNode.raw = true;
  661. innerNode.value = sanitize(unescape(innerHtml));
  662. realElm.append(innerNode);
  663. }
  664. node.replace(realElm);
  665. }
  666. });
  667. });
  668. editor.on('click keyup', function() {
  669. var selectedNode = editor.selection.getNode();
  670. if (selectedNode && editor.dom.hasClass(selectedNode, 'mce-preview-object')) {
  671. if (editor.dom.getAttrib(selectedNode, 'data-mce-selected')) {
  672. selectedNode.setAttribute('data-mce-selected', '2');
  673. }
  674. }
  675. });
  676. editor.on('ObjectSelected', function(e) {
  677. var objectType = e.target.getAttribute('data-mce-object');
  678. if (objectType == "audio" || objectType == "script") {
  679. e.preventDefault();
  680. }
  681. });
  682. editor.on('objectResized', function(e) {
  683. var target = e.target, html;
  684. if (target.getAttribute('data-mce-object')) {
  685. html = target.getAttribute('data-mce-html');
  686. if (html) {
  687. html = unescape(html);
  688. target.setAttribute('data-mce-html', escape(
  689. updateHtml(html, {
  690. width: e.width,
  691. height: e.height
  692. })
  693. ));
  694. }
  695. }
  696. });
  697. editor.addButton('media', {
  698. tooltip: 'Insert/edit video',
  699. onclick: showDialog,
  700. stateSelector: ['img[data-mce-object]', 'span[data-mce-object]']
  701. });
  702. editor.addMenuItem('media', {
  703. icon: 'media',
  704. text: 'Insert/edit video',
  705. onclick: showDialog,
  706. context: 'insert',
  707. prependToContext: true
  708. });
  709. editor.on('setContent', function() {
  710. // TODO: This shouldn't be needed there should be a way to mark bogus
  711. // elements so they are never removed except external save
  712. editor.$('span.mce-preview-object').each(function(index, elm) {
  713. var $elm = editor.$(elm);
  714. if ($elm.find('span.mce-shim', elm).length === 0) {
  715. $elm.append('<span class="mce-shim"></span>');
  716. }
  717. });
  718. });
  719. editor.addCommand('mceMedia', showDialog);
  720. this.showDialog = showDialog;
  721. });