plugin.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.3.0 (2020-05-21)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var __assign = function () {
  13. __assign = Object.assign || function __assign(t) {
  14. for (var s, i = 1, n = arguments.length; i < n; i++) {
  15. s = arguments[i];
  16. for (var p in s)
  17. if (Object.prototype.hasOwnProperty.call(s, p))
  18. t[p] = s[p];
  19. }
  20. return t;
  21. };
  22. return __assign.apply(this, arguments);
  23. };
  24. var noop = function () {
  25. };
  26. var constant = function (value) {
  27. return function () {
  28. return value;
  29. };
  30. };
  31. var never = constant(false);
  32. var always = constant(true);
  33. var none = function () {
  34. return NONE;
  35. };
  36. var NONE = function () {
  37. var eq = function (o) {
  38. return o.isNone();
  39. };
  40. var call = function (thunk) {
  41. return thunk();
  42. };
  43. var id = function (n) {
  44. return n;
  45. };
  46. var me = {
  47. fold: function (n, _s) {
  48. return n();
  49. },
  50. is: never,
  51. isSome: never,
  52. isNone: always,
  53. getOr: id,
  54. getOrThunk: call,
  55. getOrDie: function (msg) {
  56. throw new Error(msg || 'error: getOrDie called on none.');
  57. },
  58. getOrNull: constant(null),
  59. getOrUndefined: constant(undefined),
  60. or: id,
  61. orThunk: call,
  62. map: none,
  63. each: noop,
  64. bind: none,
  65. exists: never,
  66. forall: always,
  67. filter: none,
  68. equals: eq,
  69. equals_: eq,
  70. toArray: function () {
  71. return [];
  72. },
  73. toString: constant('none()')
  74. };
  75. return me;
  76. }();
  77. var some = function (a) {
  78. var constant_a = constant(a);
  79. var self = function () {
  80. return me;
  81. };
  82. var bind = function (f) {
  83. return f(a);
  84. };
  85. var me = {
  86. fold: function (n, s) {
  87. return s(a);
  88. },
  89. is: function (v) {
  90. return a === v;
  91. },
  92. isSome: always,
  93. isNone: never,
  94. getOr: constant_a,
  95. getOrThunk: constant_a,
  96. getOrDie: constant_a,
  97. getOrNull: constant_a,
  98. getOrUndefined: constant_a,
  99. or: self,
  100. orThunk: self,
  101. map: function (f) {
  102. return some(f(a));
  103. },
  104. each: function (f) {
  105. f(a);
  106. },
  107. bind: bind,
  108. exists: bind,
  109. forall: bind,
  110. filter: function (f) {
  111. return f(a) ? me : NONE;
  112. },
  113. toArray: function () {
  114. return [a];
  115. },
  116. toString: function () {
  117. return 'some(' + a + ')';
  118. },
  119. equals: function (o) {
  120. return o.is(a);
  121. },
  122. equals_: function (o, elementEq) {
  123. return o.fold(never, function (b) {
  124. return elementEq(a, b);
  125. });
  126. }
  127. };
  128. return me;
  129. };
  130. var from = function (value) {
  131. return value === null || value === undefined ? NONE : some(value);
  132. };
  133. var Option = {
  134. some: some,
  135. none: none,
  136. from: from
  137. };
  138. var typeOf = function (x) {
  139. var t = typeof x;
  140. if (x === null) {
  141. return 'null';
  142. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  143. return 'array';
  144. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  145. return 'string';
  146. } else {
  147. return t;
  148. }
  149. };
  150. var isType = function (type) {
  151. return function (value) {
  152. return typeOf(value) === type;
  153. };
  154. };
  155. var isString = isType('string');
  156. var isObject = isType('object');
  157. var isArray = isType('array');
  158. var nativePush = Array.prototype.push;
  159. var each = function (xs, f) {
  160. for (var i = 0, len = xs.length; i < len; i++) {
  161. var x = xs[i];
  162. f(x, i);
  163. }
  164. };
  165. var flatten = function (xs) {
  166. var r = [];
  167. for (var i = 0, len = xs.length; i < len; ++i) {
  168. if (!isArray(xs[i])) {
  169. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  170. }
  171. nativePush.apply(r, xs[i]);
  172. }
  173. return r;
  174. };
  175. var Cell = function (initial) {
  176. var value = initial;
  177. var get = function () {
  178. return value;
  179. };
  180. var set = function (v) {
  181. value = v;
  182. };
  183. return {
  184. get: get,
  185. set: set
  186. };
  187. };
  188. var keys = Object.keys;
  189. var hasOwnProperty = Object.hasOwnProperty;
  190. var each$1 = function (obj, f) {
  191. var props = keys(obj);
  192. for (var k = 0, len = props.length; k < len; k++) {
  193. var i = props[k];
  194. var x = obj[i];
  195. f(x, i);
  196. }
  197. };
  198. var get = function (obj, key) {
  199. return has(obj, key) ? Option.from(obj[key]) : Option.none();
  200. };
  201. var has = function (obj, key) {
  202. return hasOwnProperty.call(obj, key);
  203. };
  204. var getScripts = function (editor) {
  205. return editor.getParam('media_scripts');
  206. };
  207. var getAudioTemplateCallback = function (editor) {
  208. return editor.getParam('audio_template_callback');
  209. };
  210. var getVideoTemplateCallback = function (editor) {
  211. return editor.getParam('video_template_callback');
  212. };
  213. var hasLiveEmbeds = function (editor) {
  214. return editor.getParam('media_live_embeds', true);
  215. };
  216. var shouldFilterHtml = function (editor) {
  217. return editor.getParam('media_filter_html', true);
  218. };
  219. var getUrlResolver = function (editor) {
  220. return editor.getParam('media_url_resolver');
  221. };
  222. var hasAltSource = function (editor) {
  223. return editor.getParam('media_alt_source', true);
  224. };
  225. var hasPoster = function (editor) {
  226. return editor.getParam('media_poster', true);
  227. };
  228. var hasDimensions = function (editor) {
  229. return editor.getParam('media_dimensions', true);
  230. };
  231. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  232. var global$2 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  233. var global$3 = tinymce.util.Tools.resolve('tinymce.html.SaxParser');
  234. var getVideoScriptMatch = function (prefixes, src) {
  235. if (prefixes) {
  236. for (var i = 0; i < prefixes.length; i++) {
  237. if (src.indexOf(prefixes[i].filter) !== -1) {
  238. return prefixes[i];
  239. }
  240. }
  241. }
  242. };
  243. var DOM = global$2.DOM;
  244. var trimPx = function (value) {
  245. return value.replace(/px$/, '');
  246. };
  247. var getEphoxEmbedData = function (attrs) {
  248. var style = attrs.map.style;
  249. var styles = style ? DOM.parseStyle(style) : {};
  250. return {
  251. type: 'ephox-embed-iri',
  252. source: attrs.map['data-ephox-embed-iri'],
  253. altsource: '',
  254. poster: '',
  255. width: get(styles, 'max-width').map(trimPx).getOr(''),
  256. height: get(styles, 'max-height').map(trimPx).getOr('')
  257. };
  258. };
  259. var htmlToData = function (prefixes, html) {
  260. var isEphoxEmbed = Cell(false);
  261. var data = {};
  262. global$3({
  263. validate: false,
  264. allow_conditional_comments: true,
  265. start: function (name, attrs) {
  266. if (isEphoxEmbed.get()) ; else if (has(attrs.map, 'data-ephox-embed-iri')) {
  267. isEphoxEmbed.set(true);
  268. data = getEphoxEmbedData(attrs);
  269. } else {
  270. if (!data.source && name === 'param') {
  271. data.source = attrs.map.movie;
  272. }
  273. if (name === 'iframe' || name === 'object' || name === 'embed' || name === 'video' || name === 'audio') {
  274. if (!data.type) {
  275. data.type = name;
  276. }
  277. data = global$1.extend(attrs.map, data);
  278. }
  279. if (name === 'script') {
  280. var videoScript = getVideoScriptMatch(prefixes, attrs.map.src);
  281. if (!videoScript) {
  282. return;
  283. }
  284. data = {
  285. type: 'script',
  286. source: attrs.map.src,
  287. width: String(videoScript.width),
  288. height: String(videoScript.height)
  289. };
  290. }
  291. if (name === 'source') {
  292. if (!data.source) {
  293. data.source = attrs.map.src;
  294. } else if (!data.altsource) {
  295. data.altsource = attrs.map.src;
  296. }
  297. }
  298. if (name === 'img' && !data.poster) {
  299. data.poster = attrs.map.src;
  300. }
  301. }
  302. }
  303. }).parse(html);
  304. data.source = data.source || data.src || data.data;
  305. data.altsource = data.altsource || '';
  306. data.poster = data.poster || '';
  307. return data;
  308. };
  309. var guess = function (url) {
  310. var mimes = {
  311. mp3: 'audio/mpeg',
  312. m4a: 'audio/x-m4a',
  313. wav: 'audio/wav',
  314. mp4: 'video/mp4',
  315. webm: 'video/webm',
  316. ogg: 'video/ogg',
  317. swf: 'application/x-shockwave-flash'
  318. };
  319. var fileEnd = url.toLowerCase().split('.').pop();
  320. var mime = mimes[fileEnd];
  321. return mime ? mime : '';
  322. };
  323. var global$4 = tinymce.util.Tools.resolve('tinymce.html.Schema');
  324. var global$5 = tinymce.util.Tools.resolve('tinymce.html.Writer');
  325. var DOM$1 = global$2.DOM;
  326. var addPx = function (value) {
  327. return /^[0-9.]+$/.test(value) ? value + 'px' : value;
  328. };
  329. var setAttributes = function (attrs, updatedAttrs) {
  330. each$1(updatedAttrs, function (val, name) {
  331. var value = '' + val;
  332. if (attrs.map[name]) {
  333. var i = attrs.length;
  334. while (i--) {
  335. var attr = attrs[i];
  336. if (attr.name === name) {
  337. if (value) {
  338. attrs.map[name] = value;
  339. attr.value = value;
  340. } else {
  341. delete attrs.map[name];
  342. attrs.splice(i, 1);
  343. }
  344. }
  345. }
  346. } else if (value) {
  347. attrs.push({
  348. name: name,
  349. value: value
  350. });
  351. attrs.map[name] = value;
  352. }
  353. });
  354. };
  355. var updateEphoxEmbed = function (data, attrs) {
  356. var style = attrs.map.style;
  357. var styleMap = style ? DOM$1.parseStyle(style) : {};
  358. styleMap['max-width'] = addPx(data.width);
  359. styleMap['max-height'] = addPx(data.height);
  360. setAttributes(attrs, { style: DOM$1.serializeStyle(styleMap) });
  361. };
  362. var sources = [
  363. 'source',
  364. 'altsource'
  365. ];
  366. var updateHtml = function (html, data, updateAll) {
  367. var writer = global$5();
  368. var isEphoxEmbed = Cell(false);
  369. var sourceCount = 0;
  370. var hasImage;
  371. global$3({
  372. validate: false,
  373. allow_conditional_comments: true,
  374. comment: function (text) {
  375. writer.comment(text);
  376. },
  377. cdata: function (text) {
  378. writer.cdata(text);
  379. },
  380. text: function (text, raw) {
  381. writer.text(text, raw);
  382. },
  383. start: function (name, attrs, empty) {
  384. if (isEphoxEmbed.get()) ; else if (has(attrs.map, 'data-ephox-embed-iri')) {
  385. isEphoxEmbed.set(true);
  386. updateEphoxEmbed(data, attrs);
  387. } else {
  388. switch (name) {
  389. case 'video':
  390. case 'object':
  391. case 'embed':
  392. case 'img':
  393. case 'iframe':
  394. if (data.height !== undefined && data.width !== undefined) {
  395. setAttributes(attrs, {
  396. width: data.width,
  397. height: data.height
  398. });
  399. }
  400. break;
  401. }
  402. if (updateAll) {
  403. switch (name) {
  404. case 'video':
  405. setAttributes(attrs, {
  406. poster: data.poster,
  407. src: ''
  408. });
  409. if (data.altsource) {
  410. setAttributes(attrs, { src: '' });
  411. }
  412. break;
  413. case 'iframe':
  414. setAttributes(attrs, { src: data.source });
  415. break;
  416. case 'source':
  417. if (sourceCount < 2) {
  418. setAttributes(attrs, {
  419. src: data[sources[sourceCount]],
  420. type: data[sources[sourceCount] + 'mime']
  421. });
  422. if (!data[sources[sourceCount]]) {
  423. return;
  424. }
  425. }
  426. sourceCount++;
  427. break;
  428. case 'img':
  429. if (!data.poster) {
  430. return;
  431. }
  432. hasImage = true;
  433. break;
  434. }
  435. }
  436. }
  437. writer.start(name, attrs, empty);
  438. },
  439. end: function (name) {
  440. if (!isEphoxEmbed.get()) {
  441. if (name === 'video' && updateAll) {
  442. for (var index = 0; index < 2; index++) {
  443. if (data[sources[index]]) {
  444. var attrs = [];
  445. attrs.map = {};
  446. if (sourceCount < index) {
  447. setAttributes(attrs, {
  448. src: data[sources[index]],
  449. type: data[sources[index] + 'mime']
  450. });
  451. writer.start('source', attrs, true);
  452. }
  453. }
  454. }
  455. }
  456. if (data.poster && name === 'object' && updateAll && !hasImage) {
  457. var imgAttrs = [];
  458. imgAttrs.map = {};
  459. setAttributes(imgAttrs, {
  460. src: data.poster,
  461. width: data.width,
  462. height: data.height
  463. });
  464. writer.start('img', imgAttrs, true);
  465. }
  466. }
  467. writer.end(name);
  468. }
  469. }, global$4({})).parse(html);
  470. return writer.getContent();
  471. };
  472. var urlPatterns = [
  473. {
  474. regex: /youtu\.be\/([\w\-_\?&=.]+)/i,
  475. type: 'iframe',
  476. w: 560,
  477. h: 314,
  478. url: 'www.youtube.com/embed/$1',
  479. allowFullscreen: true
  480. },
  481. {
  482. regex: /youtube\.com(.+)v=([^&]+)(&([a-z0-9&=\-_]+))?/i,
  483. type: 'iframe',
  484. w: 560,
  485. h: 314,
  486. url: 'www.youtube.com/embed/$2?$4',
  487. allowFullscreen: true
  488. },
  489. {
  490. regex: /youtube.com\/embed\/([a-z0-9\?&=\-_]+)/i,
  491. type: 'iframe',
  492. w: 560,
  493. h: 314,
  494. url: 'www.youtube.com/embed/$1',
  495. allowFullscreen: true
  496. },
  497. {
  498. regex: /vimeo\.com\/([0-9]+)/,
  499. type: 'iframe',
  500. w: 425,
  501. h: 350,
  502. url: 'player.vimeo.com/video/$1?title=0&byline=0&portrait=0&color=8dc7dc',
  503. allowFullscreen: true
  504. },
  505. {
  506. regex: /vimeo\.com\/(.*)\/([0-9]+)/,
  507. type: 'iframe',
  508. w: 425,
  509. h: 350,
  510. url: 'player.vimeo.com/video/$2?title=0&amp;byline=0',
  511. allowFullscreen: true
  512. },
  513. {
  514. regex: /maps\.google\.([a-z]{2,3})\/maps\/(.+)msid=(.+)/,
  515. type: 'iframe',
  516. w: 425,
  517. h: 350,
  518. url: 'maps.google.com/maps/ms?msid=$2&output=embed"',
  519. allowFullscreen: false
  520. },
  521. {
  522. regex: /dailymotion\.com\/video\/([^_]+)/,
  523. type: 'iframe',
  524. w: 480,
  525. h: 270,
  526. url: 'www.dailymotion.com/embed/video/$1',
  527. allowFullscreen: true
  528. },
  529. {
  530. regex: /dai\.ly\/([^_]+)/,
  531. type: 'iframe',
  532. w: 480,
  533. h: 270,
  534. url: 'www.dailymotion.com/embed/video/$1',
  535. allowFullscreen: true
  536. }
  537. ];
  538. var getProtocol = function (url) {
  539. var protocolMatches = url.match(/^(https?:\/\/|www\.)(.+)$/i);
  540. if (protocolMatches && protocolMatches.length > 1) {
  541. return protocolMatches[1] === 'www.' ? 'https://' : protocolMatches[1];
  542. } else {
  543. return 'https://';
  544. }
  545. };
  546. var getUrl = function (pattern, url) {
  547. var protocol = getProtocol(url);
  548. var match = pattern.regex.exec(url);
  549. var newUrl = protocol + pattern.url;
  550. var _loop_1 = function (i) {
  551. newUrl = newUrl.replace('$' + i, function () {
  552. return match[i] ? match[i] : '';
  553. });
  554. };
  555. for (var i = 0; i < match.length; i++) {
  556. _loop_1(i);
  557. }
  558. return newUrl.replace(/\?$/, '');
  559. };
  560. var matchPattern = function (url) {
  561. var patterns = urlPatterns.filter(function (pattern) {
  562. return pattern.regex.test(url);
  563. });
  564. if (patterns.length > 0) {
  565. return global$1.extend({}, patterns[0], { url: getUrl(patterns[0], url) });
  566. } else {
  567. return null;
  568. }
  569. };
  570. var getIframeHtml = function (data) {
  571. var allowFullscreen = data.allowFullscreen ? ' allowFullscreen="1"' : '';
  572. return '<iframe src="' + data.source + '" width="' + data.width + '" height="' + data.height + '"' + allowFullscreen + '></iframe>';
  573. };
  574. var getFlashHtml = function (data) {
  575. var html = '<object data="' + data.source + '" width="' + data.width + '" height="' + data.height + '" type="application/x-shockwave-flash">';
  576. if (data.poster) {
  577. html += '<img src="' + data.poster + '" width="' + data.width + '" height="' + data.height + '" />';
  578. }
  579. html += '</object>';
  580. return html;
  581. };
  582. var getAudioHtml = function (data, audioTemplateCallback) {
  583. if (audioTemplateCallback) {
  584. return audioTemplateCallback(data);
  585. } else {
  586. return '<audio controls="controls" src="' + data.source + '">' + (data.altsource ? '\n<source src="' + data.altsource + '"' + (data.altsourcemime ? ' type="' + data.altsourcemime + '"' : '') + ' />\n' : '') + '</audio>';
  587. }
  588. };
  589. var getVideoHtml = function (data, videoTemplateCallback) {
  590. if (videoTemplateCallback) {
  591. return videoTemplateCallback(data);
  592. } else {
  593. return '<video width="' + data.width + '" height="' + data.height + '"' + (data.poster ? ' poster="' + data.poster + '"' : '') + ' controls="controls">\n' + '<source src="' + data.source + '"' + (data.sourcemime ? ' type="' + data.sourcemime + '"' : '') + ' />\n' + (data.altsource ? '<source src="' + data.altsource + '"' + (data.altsourcemime ? ' type="' + data.altsourcemime + '"' : '') + ' />\n' : '') + '</video>';
  594. }
  595. };
  596. var getScriptHtml = function (data) {
  597. return '<script src="' + data.source + '"></script>';
  598. };
  599. var dataToHtml = function (editor, dataIn) {
  600. var data = global$1.extend({}, dataIn);
  601. if (!data.source) {
  602. global$1.extend(data, htmlToData(getScripts(editor), data.embed));
  603. if (!data.source) {
  604. return '';
  605. }
  606. }
  607. if (!data.altsource) {
  608. data.altsource = '';
  609. }
  610. if (!data.poster) {
  611. data.poster = '';
  612. }
  613. data.source = editor.convertURL(data.source, 'source');
  614. data.altsource = editor.convertURL(data.altsource, 'source');
  615. data.sourcemime = guess(data.source);
  616. data.altsourcemime = guess(data.altsource);
  617. data.poster = editor.convertURL(data.poster, 'poster');
  618. var pattern = matchPattern(data.source);
  619. if (pattern) {
  620. data.source = pattern.url;
  621. data.type = pattern.type;
  622. data.allowFullscreen = pattern.allowFullscreen;
  623. data.width = data.width || String(pattern.w);
  624. data.height = data.height || String(pattern.h);
  625. }
  626. if (data.embed) {
  627. return updateHtml(data.embed, data, true);
  628. } else {
  629. var videoScript = getVideoScriptMatch(getScripts(editor), data.source);
  630. if (videoScript) {
  631. data.type = 'script';
  632. data.width = String(videoScript.width);
  633. data.height = String(videoScript.height);
  634. }
  635. var audioTemplateCallback = getAudioTemplateCallback(editor);
  636. var videoTemplateCallback = getVideoTemplateCallback(editor);
  637. data.width = data.width || '300';
  638. data.height = data.height || '150';
  639. global$1.each(data, function (value, key) {
  640. data[key] = editor.dom.encode('' + value);
  641. });
  642. if (data.type === 'iframe') {
  643. return getIframeHtml(data);
  644. } else if (data.sourcemime === 'application/x-shockwave-flash') {
  645. return getFlashHtml(data);
  646. } else if (data.sourcemime.indexOf('audio') !== -1) {
  647. return getAudioHtml(data, audioTemplateCallback);
  648. } else if (data.type === 'script') {
  649. return getScriptHtml(data);
  650. } else {
  651. return getVideoHtml(data, videoTemplateCallback);
  652. }
  653. }
  654. };
  655. var global$6 = tinymce.util.Tools.resolve('tinymce.util.Promise');
  656. var cache = {};
  657. var embedPromise = function (data, dataToHtml, handler) {
  658. return new global$6(function (res, rej) {
  659. var wrappedResolve = function (response) {
  660. if (response.html) {
  661. cache[data.source] = response;
  662. }
  663. return res({
  664. url: data.source,
  665. html: response.html ? response.html : dataToHtml(data)
  666. });
  667. };
  668. if (cache[data.source]) {
  669. wrappedResolve(cache[data.source]);
  670. } else {
  671. handler({ url: data.source }, wrappedResolve, rej);
  672. }
  673. });
  674. };
  675. var defaultPromise = function (data, dataToHtml) {
  676. return new global$6(function (res) {
  677. res({
  678. html: dataToHtml(data),
  679. url: data.source
  680. });
  681. });
  682. };
  683. var loadedData = function (editor) {
  684. return function (data) {
  685. return dataToHtml(editor, data);
  686. };
  687. };
  688. var getEmbedHtml = function (editor, data) {
  689. var embedHandler = getUrlResolver(editor);
  690. return embedHandler ? embedPromise(data, loadedData(editor), embedHandler) : defaultPromise(data, loadedData(editor));
  691. };
  692. var isCached = function (url) {
  693. return cache.hasOwnProperty(url);
  694. };
  695. var extractMeta = function (sourceInput, data) {
  696. return get(data, sourceInput).bind(function (mainData) {
  697. return get(mainData, 'meta');
  698. });
  699. };
  700. var getValue = function (data, metaData, sourceInput) {
  701. return function (prop) {
  702. var _a;
  703. var getFromData = function () {
  704. return get(data, prop);
  705. };
  706. var getFromMetaData = function () {
  707. return get(metaData, prop);
  708. };
  709. var getNonEmptyValue = function (c) {
  710. return get(c, 'value').bind(function (v) {
  711. return v.length > 0 ? Option.some(v) : Option.none();
  712. });
  713. };
  714. var getFromValueFirst = function () {
  715. return getFromData().bind(function (child) {
  716. return isObject(child) ? getNonEmptyValue(child).orThunk(getFromMetaData) : getFromMetaData().orThunk(function () {
  717. return Option.from(child);
  718. });
  719. });
  720. };
  721. var getFromMetaFirst = function () {
  722. return getFromMetaData().orThunk(function () {
  723. return getFromData().bind(function (child) {
  724. return isObject(child) ? getNonEmptyValue(child) : Option.from(child);
  725. });
  726. });
  727. };
  728. return _a = {}, _a[prop] = (prop === sourceInput ? getFromValueFirst() : getFromMetaFirst()).getOr(''), _a;
  729. };
  730. };
  731. var getDimensions = function (data, metaData) {
  732. var dimensions = {};
  733. get(data, 'dimensions').each(function (dims) {
  734. each([
  735. 'width',
  736. 'height'
  737. ], function (prop) {
  738. get(metaData, prop).orThunk(function () {
  739. return get(dims, prop);
  740. }).each(function (value) {
  741. return dimensions[prop] = value;
  742. });
  743. });
  744. });
  745. return dimensions;
  746. };
  747. var unwrap = function (data, sourceInput) {
  748. var metaData = sourceInput ? extractMeta(sourceInput, data).getOr({}) : {};
  749. var get = getValue(data, metaData, sourceInput);
  750. return __assign(__assign(__assign(__assign(__assign({}, get('source')), get('altsource')), get('poster')), get('embed')), getDimensions(data, metaData));
  751. };
  752. var wrap = function (data) {
  753. var wrapped = __assign(__assign({}, data), {
  754. source: { value: get(data, 'source').getOr('') },
  755. altsource: { value: get(data, 'altsource').getOr('') },
  756. poster: { value: get(data, 'poster').getOr('') }
  757. });
  758. each([
  759. 'width',
  760. 'height'
  761. ], function (prop) {
  762. get(data, prop).each(function (value) {
  763. var dimensions = wrapped.dimensions || {};
  764. dimensions[prop] = value;
  765. wrapped.dimensions = dimensions;
  766. });
  767. });
  768. return wrapped;
  769. };
  770. var handleError = function (editor) {
  771. return function (error) {
  772. var errorMessage = error && error.msg ? 'Media embed handler error: ' + error.msg : 'Media embed handler threw unknown error.';
  773. editor.notificationManager.open({
  774. type: 'error',
  775. text: errorMessage
  776. });
  777. };
  778. };
  779. var snippetToData = function (editor, embedSnippet) {
  780. return htmlToData(getScripts(editor), embedSnippet);
  781. };
  782. var isMediaElement = function (element) {
  783. return element.getAttribute('data-mce-object') || element.getAttribute('data-ephox-embed-iri');
  784. };
  785. var getEditorData = function (editor) {
  786. var element = editor.selection.getNode();
  787. var snippet = isMediaElement(element) ? editor.serializer.serialize(element, { selection: true }) : '';
  788. return __assign({ embed: snippet }, htmlToData(getScripts(editor), snippet));
  789. };
  790. var addEmbedHtml = function (api, editor) {
  791. return function (response) {
  792. if (isString(response.url) && response.url.trim().length > 0) {
  793. var html = response.html;
  794. var snippetData = snippetToData(editor, html);
  795. var nuData = __assign(__assign({}, snippetData), {
  796. source: response.url,
  797. embed: html
  798. });
  799. api.setData(wrap(nuData));
  800. }
  801. };
  802. };
  803. var selectPlaceholder = function (editor, beforeObjects) {
  804. var afterObjects = editor.dom.select('img[data-mce-object]');
  805. for (var i = 0; i < beforeObjects.length; i++) {
  806. for (var y = afterObjects.length - 1; y >= 0; y--) {
  807. if (beforeObjects[i] === afterObjects[y]) {
  808. afterObjects.splice(y, 1);
  809. }
  810. }
  811. }
  812. editor.selection.select(afterObjects[0]);
  813. };
  814. var handleInsert = function (editor, html) {
  815. var beforeObjects = editor.dom.select('img[data-mce-object]');
  816. editor.insertContent(html);
  817. selectPlaceholder(editor, beforeObjects);
  818. editor.nodeChanged();
  819. };
  820. var submitForm = function (prevData, newData, editor) {
  821. newData.embed = updateHtml(newData.embed, newData);
  822. if (newData.embed && (prevData.source === newData.source || isCached(newData.source))) {
  823. handleInsert(editor, newData.embed);
  824. } else {
  825. getEmbedHtml(editor, newData).then(function (response) {
  826. handleInsert(editor, response.html);
  827. }).catch(handleError(editor));
  828. }
  829. };
  830. var showDialog = function (editor) {
  831. var editorData = getEditorData(editor);
  832. var currentData = Cell(editorData);
  833. var initialData = wrap(editorData);
  834. var handleSource = function (prevData, api) {
  835. var serviceData = unwrap(api.getData(), 'source');
  836. if (prevData.source !== serviceData.source) {
  837. addEmbedHtml(win, editor)({
  838. url: serviceData.source,
  839. html: ''
  840. });
  841. getEmbedHtml(editor, serviceData).then(addEmbedHtml(win, editor)).catch(handleError(editor));
  842. }
  843. };
  844. var handleEmbed = function (api) {
  845. var data = unwrap(api.getData());
  846. var dataFromEmbed = snippetToData(editor, data.embed);
  847. api.setData(wrap(dataFromEmbed));
  848. };
  849. var handleUpdate = function (api, sourceInput) {
  850. var data = unwrap(api.getData(), sourceInput);
  851. var embed = dataToHtml(editor, data);
  852. api.setData(wrap(__assign(__assign({}, data), { embed: embed })));
  853. };
  854. var mediaInput = [{
  855. name: 'source',
  856. type: 'urlinput',
  857. filetype: 'media',
  858. label: 'Source'
  859. }];
  860. var sizeInput = !hasDimensions(editor) ? [] : [{
  861. type: 'sizeinput',
  862. name: 'dimensions',
  863. label: 'Constrain proportions',
  864. constrain: true
  865. }];
  866. var generalTab = {
  867. title: 'General',
  868. name: 'general',
  869. items: flatten([
  870. mediaInput,
  871. sizeInput
  872. ])
  873. };
  874. var embedTextarea = {
  875. type: 'textarea',
  876. name: 'embed',
  877. label: 'Paste your embed code below:'
  878. };
  879. var embedTab = {
  880. title: 'Embed',
  881. items: [embedTextarea]
  882. };
  883. var advancedFormItems = [];
  884. if (hasAltSource(editor)) {
  885. advancedFormItems.push({
  886. name: 'altsource',
  887. type: 'urlinput',
  888. filetype: 'media',
  889. label: 'Alternative source URL'
  890. });
  891. }
  892. if (hasPoster(editor)) {
  893. advancedFormItems.push({
  894. name: 'poster',
  895. type: 'urlinput',
  896. filetype: 'image',
  897. label: 'Media poster (Image URL)'
  898. });
  899. }
  900. var advancedTab = {
  901. title: 'Advanced',
  902. name: 'advanced',
  903. items: advancedFormItems
  904. };
  905. var tabs = [
  906. generalTab,
  907. embedTab
  908. ];
  909. if (advancedFormItems.length > 0) {
  910. tabs.push(advancedTab);
  911. }
  912. var body = {
  913. type: 'tabpanel',
  914. tabs: tabs
  915. };
  916. var win = editor.windowManager.open({
  917. title: 'Insert/Edit Media',
  918. size: 'normal',
  919. body: body,
  920. buttons: [
  921. {
  922. type: 'cancel',
  923. name: 'cancel',
  924. text: 'Cancel'
  925. },
  926. {
  927. type: 'submit',
  928. name: 'save',
  929. text: 'Save',
  930. primary: true
  931. }
  932. ],
  933. onSubmit: function (api) {
  934. var serviceData = unwrap(api.getData());
  935. submitForm(currentData.get(), serviceData, editor);
  936. api.close();
  937. },
  938. onChange: function (api, detail) {
  939. switch (detail.name) {
  940. case 'source':
  941. handleSource(currentData.get(), api);
  942. break;
  943. case 'embed':
  944. handleEmbed(api);
  945. break;
  946. case 'dimensions':
  947. case 'altsource':
  948. case 'poster':
  949. handleUpdate(api, detail.name);
  950. break;
  951. }
  952. currentData.set(unwrap(api.getData()));
  953. },
  954. initialData: initialData
  955. });
  956. };
  957. var get$1 = function (editor) {
  958. var showDialog$1 = function () {
  959. showDialog(editor);
  960. };
  961. return { showDialog: showDialog$1 };
  962. };
  963. var register = function (editor) {
  964. var showDialog$1 = function () {
  965. showDialog(editor);
  966. };
  967. editor.addCommand('mceMedia', showDialog$1);
  968. };
  969. var global$7 = tinymce.util.Tools.resolve('tinymce.html.Node');
  970. var global$8 = tinymce.util.Tools.resolve('tinymce.Env');
  971. var sanitize = function (editor, html) {
  972. if (shouldFilterHtml(editor) === false) {
  973. return html;
  974. }
  975. var writer = global$5();
  976. var blocked;
  977. global$3({
  978. validate: false,
  979. allow_conditional_comments: false,
  980. comment: function (text) {
  981. writer.comment(text);
  982. },
  983. cdata: function (text) {
  984. writer.cdata(text);
  985. },
  986. text: function (text, raw) {
  987. writer.text(text, raw);
  988. },
  989. start: function (name, attrs, empty) {
  990. blocked = true;
  991. if (name === 'script' || name === 'noscript' || name === 'svg') {
  992. return;
  993. }
  994. for (var i = attrs.length - 1; i >= 0; i--) {
  995. var attrName = attrs[i].name;
  996. if (attrName.indexOf('on') === 0) {
  997. delete attrs.map[attrName];
  998. attrs.splice(i, 1);
  999. }
  1000. if (attrName === 'style') {
  1001. attrs[i].value = editor.dom.serializeStyle(editor.dom.parseStyle(attrs[i].value), name);
  1002. }
  1003. }
  1004. writer.start(name, attrs, empty);
  1005. blocked = false;
  1006. },
  1007. end: function (name) {
  1008. if (blocked) {
  1009. return;
  1010. }
  1011. writer.end(name);
  1012. }
  1013. }, global$4({})).parse(html);
  1014. return writer.getContent();
  1015. };
  1016. var createPlaceholderNode = function (editor, node) {
  1017. var placeHolder;
  1018. var name = node.name;
  1019. placeHolder = new global$7('img', 1);
  1020. placeHolder.shortEnded = true;
  1021. retainAttributesAndInnerHtml(editor, node, placeHolder);
  1022. placeHolder.attr({
  1023. 'width': node.attr('width') || '300',
  1024. 'height': node.attr('height') || (name === 'audio' ? '30' : '150'),
  1025. 'style': node.attr('style'),
  1026. 'src': global$8.transparentSrc,
  1027. 'data-mce-object': name,
  1028. 'class': 'mce-object mce-object-' + name
  1029. });
  1030. return placeHolder;
  1031. };
  1032. var createPreviewIframeNode = function (editor, node) {
  1033. var previewWrapper;
  1034. var previewNode;
  1035. var shimNode;
  1036. var name = node.name;
  1037. previewWrapper = new global$7('span', 1);
  1038. previewWrapper.attr({
  1039. 'contentEditable': 'false',
  1040. 'style': node.attr('style'),
  1041. 'data-mce-object': name,
  1042. 'class': 'mce-preview-object mce-object-' + name
  1043. });
  1044. retainAttributesAndInnerHtml(editor, node, previewWrapper);
  1045. previewNode = new global$7(name, 1);
  1046. previewNode.attr({
  1047. src: node.attr('src'),
  1048. allowfullscreen: node.attr('allowfullscreen'),
  1049. style: node.attr('style'),
  1050. class: node.attr('class'),
  1051. width: node.attr('width'),
  1052. height: node.attr('height'),
  1053. frameborder: '0'
  1054. });
  1055. shimNode = new global$7('span', 1);
  1056. shimNode.attr('class', 'mce-shim');
  1057. previewWrapper.append(previewNode);
  1058. previewWrapper.append(shimNode);
  1059. return previewWrapper;
  1060. };
  1061. var retainAttributesAndInnerHtml = function (editor, sourceNode, targetNode) {
  1062. var attrName;
  1063. var attrValue;
  1064. var attribs;
  1065. var ai;
  1066. var innerHtml;
  1067. attribs = sourceNode.attributes;
  1068. ai = attribs.length;
  1069. while (ai--) {
  1070. attrName = attribs[ai].name;
  1071. attrValue = attribs[ai].value;
  1072. if (attrName !== 'width' && attrName !== 'height' && attrName !== 'style') {
  1073. if (attrName === 'data' || attrName === 'src') {
  1074. attrValue = editor.convertURL(attrValue, attrName);
  1075. }
  1076. targetNode.attr('data-mce-p-' + attrName, attrValue);
  1077. }
  1078. }
  1079. innerHtml = sourceNode.firstChild && sourceNode.firstChild.value;
  1080. if (innerHtml) {
  1081. targetNode.attr('data-mce-html', escape(sanitize(editor, innerHtml)));
  1082. targetNode.firstChild = null;
  1083. }
  1084. };
  1085. var isPageEmbedWrapper = function (node) {
  1086. var nodeClass = node.attr('class');
  1087. return nodeClass && /\btiny-pageembed\b/.test(nodeClass);
  1088. };
  1089. var isWithinEmbedWrapper = function (node) {
  1090. while (node = node.parent) {
  1091. if (node.attr('data-ephox-embed-iri') || isPageEmbedWrapper(node)) {
  1092. return true;
  1093. }
  1094. }
  1095. return false;
  1096. };
  1097. var placeHolderConverter = function (editor) {
  1098. return function (nodes) {
  1099. var i = nodes.length;
  1100. var node;
  1101. var videoScript;
  1102. while (i--) {
  1103. node = nodes[i];
  1104. if (!node.parent) {
  1105. continue;
  1106. }
  1107. if (node.parent.attr('data-mce-object')) {
  1108. continue;
  1109. }
  1110. if (node.name === 'script') {
  1111. videoScript = getVideoScriptMatch(getScripts(editor), node.attr('src'));
  1112. if (!videoScript) {
  1113. continue;
  1114. }
  1115. }
  1116. if (videoScript) {
  1117. if (videoScript.width) {
  1118. node.attr('width', videoScript.width.toString());
  1119. }
  1120. if (videoScript.height) {
  1121. node.attr('height', videoScript.height.toString());
  1122. }
  1123. }
  1124. if (node.name === 'iframe' && hasLiveEmbeds(editor) && global$8.ceFalse) {
  1125. if (!isWithinEmbedWrapper(node)) {
  1126. node.replace(createPreviewIframeNode(editor, node));
  1127. }
  1128. } else {
  1129. if (!isWithinEmbedWrapper(node)) {
  1130. node.replace(createPlaceholderNode(editor, node));
  1131. }
  1132. }
  1133. }
  1134. };
  1135. };
  1136. var setup = function (editor) {
  1137. editor.on('preInit', function () {
  1138. var specialElements = editor.schema.getSpecialElements();
  1139. global$1.each('video audio iframe object'.split(' '), function (name) {
  1140. specialElements[name] = new RegExp('</' + name + '[^>]*>', 'gi');
  1141. });
  1142. var boolAttrs = editor.schema.getBoolAttrs();
  1143. global$1.each('webkitallowfullscreen mozallowfullscreen allowfullscreen'.split(' '), function (name) {
  1144. boolAttrs[name] = {};
  1145. });
  1146. editor.parser.addNodeFilter('iframe,video,audio,object,embed,script', placeHolderConverter(editor));
  1147. editor.serializer.addAttributeFilter('data-mce-object', function (nodes, name) {
  1148. var i = nodes.length;
  1149. var node;
  1150. var realElm;
  1151. var ai;
  1152. var attribs;
  1153. var innerHtml;
  1154. var innerNode;
  1155. var realElmName;
  1156. var className;
  1157. while (i--) {
  1158. node = nodes[i];
  1159. if (!node.parent) {
  1160. continue;
  1161. }
  1162. realElmName = node.attr(name);
  1163. realElm = new global$7(realElmName, 1);
  1164. if (realElmName !== 'audio' && realElmName !== 'script') {
  1165. className = node.attr('class');
  1166. if (className && className.indexOf('mce-preview-object') !== -1) {
  1167. realElm.attr({
  1168. width: node.firstChild.attr('width'),
  1169. height: node.firstChild.attr('height')
  1170. });
  1171. } else {
  1172. realElm.attr({
  1173. width: node.attr('width'),
  1174. height: node.attr('height')
  1175. });
  1176. }
  1177. }
  1178. realElm.attr({ style: node.attr('style') });
  1179. attribs = node.attributes;
  1180. ai = attribs.length;
  1181. while (ai--) {
  1182. var attrName = attribs[ai].name;
  1183. if (attrName.indexOf('data-mce-p-') === 0) {
  1184. realElm.attr(attrName.substr(11), attribs[ai].value);
  1185. }
  1186. }
  1187. if (realElmName === 'script') {
  1188. realElm.attr('type', 'text/javascript');
  1189. }
  1190. innerHtml = node.attr('data-mce-html');
  1191. if (innerHtml) {
  1192. innerNode = new global$7('#text', 3);
  1193. innerNode.raw = true;
  1194. innerNode.value = sanitize(editor, unescape(innerHtml));
  1195. realElm.append(innerNode);
  1196. }
  1197. node.replace(realElm);
  1198. }
  1199. });
  1200. });
  1201. editor.on('SetContent', function () {
  1202. editor.$('span.mce-preview-object').each(function (index, elm) {
  1203. var $elm = editor.$(elm);
  1204. if ($elm.find('span.mce-shim').length === 0) {
  1205. $elm.append('<span class="mce-shim"></span>');
  1206. }
  1207. });
  1208. });
  1209. };
  1210. var setup$1 = function (editor) {
  1211. editor.on('ResolveName', function (e) {
  1212. var name;
  1213. if (e.target.nodeType === 1 && (name = e.target.getAttribute('data-mce-object'))) {
  1214. e.name = name;
  1215. }
  1216. });
  1217. };
  1218. var setup$2 = function (editor) {
  1219. editor.on('click keyup touchend', function () {
  1220. var selectedNode = editor.selection.getNode();
  1221. if (selectedNode && editor.dom.hasClass(selectedNode, 'mce-preview-object')) {
  1222. if (editor.dom.getAttrib(selectedNode, 'data-mce-selected')) {
  1223. selectedNode.setAttribute('data-mce-selected', '2');
  1224. }
  1225. }
  1226. });
  1227. editor.on('ObjectSelected', function (e) {
  1228. var objectType = e.target.getAttribute('data-mce-object');
  1229. if (objectType === 'audio' || objectType === 'script') {
  1230. e.preventDefault();
  1231. }
  1232. });
  1233. editor.on('ObjectResized', function (e) {
  1234. var target = e.target;
  1235. var html;
  1236. if (target.getAttribute('data-mce-object')) {
  1237. html = target.getAttribute('data-mce-html');
  1238. if (html) {
  1239. html = unescape(html);
  1240. target.setAttribute('data-mce-html', escape(updateHtml(html, {
  1241. width: String(e.width),
  1242. height: String(e.height)
  1243. })));
  1244. }
  1245. }
  1246. });
  1247. };
  1248. var stateSelectorAdapter = function (editor, selector) {
  1249. return function (buttonApi) {
  1250. return editor.selection.selectorChangedWithUnbind(selector.join(','), buttonApi.setActive).unbind;
  1251. };
  1252. };
  1253. var register$1 = function (editor) {
  1254. editor.ui.registry.addToggleButton('media', {
  1255. tooltip: 'Insert/edit media',
  1256. icon: 'embed',
  1257. onAction: function () {
  1258. editor.execCommand('mceMedia');
  1259. },
  1260. onSetup: stateSelectorAdapter(editor, [
  1261. 'img[data-mce-object]',
  1262. 'span[data-mce-object]',
  1263. 'div[data-ephox-embed-iri]'
  1264. ])
  1265. });
  1266. editor.ui.registry.addMenuItem('media', {
  1267. icon: 'embed',
  1268. text: 'Media...',
  1269. onAction: function () {
  1270. editor.execCommand('mceMedia');
  1271. }
  1272. });
  1273. };
  1274. function Plugin () {
  1275. global.add('media', function (editor) {
  1276. register(editor);
  1277. register$1(editor);
  1278. setup$1(editor);
  1279. setup(editor);
  1280. setup$2(editor);
  1281. return get$1(editor);
  1282. });
  1283. }
  1284. Plugin();
  1285. }());