plugin.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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.10.2 (2021-11-17)
  8. */
  9. (function () {
  10. 'use strict';
  11. var Cell = function (initial) {
  12. var value = initial;
  13. var get = function () {
  14. return value;
  15. };
  16. var set = function (v) {
  17. value = v;
  18. };
  19. return {
  20. get: get,
  21. set: set
  22. };
  23. };
  24. var global$5 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  25. var global$4 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  26. var typeOf = function (x) {
  27. var t = typeof x;
  28. if (x === null) {
  29. return 'null';
  30. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  31. return 'array';
  32. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  33. return 'string';
  34. } else {
  35. return t;
  36. }
  37. };
  38. var isType = function (type) {
  39. return function (value) {
  40. return typeOf(value) === type;
  41. };
  42. };
  43. var isSimpleType = function (type) {
  44. return function (value) {
  45. return typeof value === type;
  46. };
  47. };
  48. var isArray = isType('array');
  49. var isNullable = function (a) {
  50. return a === null || a === undefined;
  51. };
  52. var isNonNullable = function (a) {
  53. return !isNullable(a);
  54. };
  55. var isFunction = isSimpleType('function');
  56. var noop = function () {
  57. };
  58. var constant = function (value) {
  59. return function () {
  60. return value;
  61. };
  62. };
  63. var identity = function (x) {
  64. return x;
  65. };
  66. var never = constant(false);
  67. var always = constant(true);
  68. var none = function () {
  69. return NONE;
  70. };
  71. var NONE = function () {
  72. var call = function (thunk) {
  73. return thunk();
  74. };
  75. var id = identity;
  76. var me = {
  77. fold: function (n, _s) {
  78. return n();
  79. },
  80. isSome: never,
  81. isNone: always,
  82. getOr: id,
  83. getOrThunk: call,
  84. getOrDie: function (msg) {
  85. throw new Error(msg || 'error: getOrDie called on none.');
  86. },
  87. getOrNull: constant(null),
  88. getOrUndefined: constant(undefined),
  89. or: id,
  90. orThunk: call,
  91. map: none,
  92. each: noop,
  93. bind: none,
  94. exists: never,
  95. forall: always,
  96. filter: function () {
  97. return none();
  98. },
  99. toArray: function () {
  100. return [];
  101. },
  102. toString: constant('none()')
  103. };
  104. return me;
  105. }();
  106. var some = function (a) {
  107. var constant_a = constant(a);
  108. var self = function () {
  109. return me;
  110. };
  111. var bind = function (f) {
  112. return f(a);
  113. };
  114. var me = {
  115. fold: function (n, s) {
  116. return s(a);
  117. },
  118. isSome: always,
  119. isNone: never,
  120. getOr: constant_a,
  121. getOrThunk: constant_a,
  122. getOrDie: constant_a,
  123. getOrNull: constant_a,
  124. getOrUndefined: constant_a,
  125. or: self,
  126. orThunk: self,
  127. map: function (f) {
  128. return some(f(a));
  129. },
  130. each: function (f) {
  131. f(a);
  132. },
  133. bind: bind,
  134. exists: bind,
  135. forall: bind,
  136. filter: function (f) {
  137. return f(a) ? me : NONE;
  138. },
  139. toArray: function () {
  140. return [a];
  141. },
  142. toString: function () {
  143. return 'some(' + a + ')';
  144. }
  145. };
  146. return me;
  147. };
  148. var from = function (value) {
  149. return value === null || value === undefined ? NONE : some(value);
  150. };
  151. var Optional = {
  152. some: some,
  153. none: none,
  154. from: from
  155. };
  156. var exports$1 = {}, module = { exports: exports$1 };
  157. (function (define, exports, module, require) {
  158. (function (global, factory) {
  159. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.EphoxContactWrapper = factory());
  160. }(this, function () {
  161. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  162. var promise = { exports: {} };
  163. (function (module) {
  164. (function (root) {
  165. var setTimeoutFunc = setTimeout;
  166. function noop() {
  167. }
  168. function bind(fn, thisArg) {
  169. return function () {
  170. fn.apply(thisArg, arguments);
  171. };
  172. }
  173. function Promise(fn) {
  174. if (typeof this !== 'object')
  175. throw new TypeError('Promises must be constructed via new');
  176. if (typeof fn !== 'function')
  177. throw new TypeError('not a function');
  178. this._state = 0;
  179. this._handled = false;
  180. this._value = undefined;
  181. this._deferreds = [];
  182. doResolve(fn, this);
  183. }
  184. function handle(self, deferred) {
  185. while (self._state === 3) {
  186. self = self._value;
  187. }
  188. if (self._state === 0) {
  189. self._deferreds.push(deferred);
  190. return;
  191. }
  192. self._handled = true;
  193. Promise._immediateFn(function () {
  194. var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected;
  195. if (cb === null) {
  196. (self._state === 1 ? resolve : reject)(deferred.promise, self._value);
  197. return;
  198. }
  199. var ret;
  200. try {
  201. ret = cb(self._value);
  202. } catch (e) {
  203. reject(deferred.promise, e);
  204. return;
  205. }
  206. resolve(deferred.promise, ret);
  207. });
  208. }
  209. function resolve(self, newValue) {
  210. try {
  211. if (newValue === self)
  212. throw new TypeError('A promise cannot be resolved with itself.');
  213. if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
  214. var then = newValue.then;
  215. if (newValue instanceof Promise) {
  216. self._state = 3;
  217. self._value = newValue;
  218. finale(self);
  219. return;
  220. } else if (typeof then === 'function') {
  221. doResolve(bind(then, newValue), self);
  222. return;
  223. }
  224. }
  225. self._state = 1;
  226. self._value = newValue;
  227. finale(self);
  228. } catch (e) {
  229. reject(self, e);
  230. }
  231. }
  232. function reject(self, newValue) {
  233. self._state = 2;
  234. self._value = newValue;
  235. finale(self);
  236. }
  237. function finale(self) {
  238. if (self._state === 2 && self._deferreds.length === 0) {
  239. Promise._immediateFn(function () {
  240. if (!self._handled) {
  241. Promise._unhandledRejectionFn(self._value);
  242. }
  243. });
  244. }
  245. for (var i = 0, len = self._deferreds.length; i < len; i++) {
  246. handle(self, self._deferreds[i]);
  247. }
  248. self._deferreds = null;
  249. }
  250. function Handler(onFulfilled, onRejected, promise) {
  251. this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
  252. this.onRejected = typeof onRejected === 'function' ? onRejected : null;
  253. this.promise = promise;
  254. }
  255. function doResolve(fn, self) {
  256. var done = false;
  257. try {
  258. fn(function (value) {
  259. if (done)
  260. return;
  261. done = true;
  262. resolve(self, value);
  263. }, function (reason) {
  264. if (done)
  265. return;
  266. done = true;
  267. reject(self, reason);
  268. });
  269. } catch (ex) {
  270. if (done)
  271. return;
  272. done = true;
  273. reject(self, ex);
  274. }
  275. }
  276. Promise.prototype['catch'] = function (onRejected) {
  277. return this.then(null, onRejected);
  278. };
  279. Promise.prototype.then = function (onFulfilled, onRejected) {
  280. var prom = new this.constructor(noop);
  281. handle(this, new Handler(onFulfilled, onRejected, prom));
  282. return prom;
  283. };
  284. Promise.all = function (arr) {
  285. var args = Array.prototype.slice.call(arr);
  286. return new Promise(function (resolve, reject) {
  287. if (args.length === 0)
  288. return resolve([]);
  289. var remaining = args.length;
  290. function res(i, val) {
  291. try {
  292. if (val && (typeof val === 'object' || typeof val === 'function')) {
  293. var then = val.then;
  294. if (typeof then === 'function') {
  295. then.call(val, function (val) {
  296. res(i, val);
  297. }, reject);
  298. return;
  299. }
  300. }
  301. args[i] = val;
  302. if (--remaining === 0) {
  303. resolve(args);
  304. }
  305. } catch (ex) {
  306. reject(ex);
  307. }
  308. }
  309. for (var i = 0; i < args.length; i++) {
  310. res(i, args[i]);
  311. }
  312. });
  313. };
  314. Promise.resolve = function (value) {
  315. if (value && typeof value === 'object' && value.constructor === Promise) {
  316. return value;
  317. }
  318. return new Promise(function (resolve) {
  319. resolve(value);
  320. });
  321. };
  322. Promise.reject = function (value) {
  323. return new Promise(function (resolve, reject) {
  324. reject(value);
  325. });
  326. };
  327. Promise.race = function (values) {
  328. return new Promise(function (resolve, reject) {
  329. for (var i = 0, len = values.length; i < len; i++) {
  330. values[i].then(resolve, reject);
  331. }
  332. });
  333. };
  334. Promise._immediateFn = typeof setImmediate === 'function' ? function (fn) {
  335. setImmediate(fn);
  336. } : function (fn) {
  337. setTimeoutFunc(fn, 0);
  338. };
  339. Promise._unhandledRejectionFn = function _unhandledRejectionFn(err) {
  340. if (typeof console !== 'undefined' && console) {
  341. console.warn('Possible Unhandled Promise Rejection:', err);
  342. }
  343. };
  344. Promise._setImmediateFn = function _setImmediateFn(fn) {
  345. Promise._immediateFn = fn;
  346. };
  347. Promise._setUnhandledRejectionFn = function _setUnhandledRejectionFn(fn) {
  348. Promise._unhandledRejectionFn = fn;
  349. };
  350. if (module.exports) {
  351. module.exports = Promise;
  352. } else if (!root.Promise) {
  353. root.Promise = Promise;
  354. }
  355. }(commonjsGlobal));
  356. }(promise));
  357. var promisePolyfill = promise.exports;
  358. var Global = function () {
  359. if (typeof window !== 'undefined') {
  360. return window;
  361. } else {
  362. return Function('return this;')();
  363. }
  364. }();
  365. var promisePolyfill_1 = { boltExport: Global.Promise || promisePolyfill };
  366. return promisePolyfill_1;
  367. }));
  368. }(undefined, exports$1, module));
  369. var Promise$1 = module.exports.boltExport;
  370. var create$1 = function (width, height) {
  371. return resize(document.createElement('canvas'), width, height);
  372. };
  373. var clone = function (canvas) {
  374. var tCanvas = create$1(canvas.width, canvas.height);
  375. var ctx = get2dContext(tCanvas);
  376. ctx.drawImage(canvas, 0, 0);
  377. return tCanvas;
  378. };
  379. var get2dContext = function (canvas) {
  380. return canvas.getContext('2d');
  381. };
  382. var resize = function (canvas, width, height) {
  383. canvas.width = width;
  384. canvas.height = height;
  385. return canvas;
  386. };
  387. var getWidth = function (image) {
  388. return image.naturalWidth || image.width;
  389. };
  390. var getHeight = function (image) {
  391. return image.naturalHeight || image.height;
  392. };
  393. var imageToBlob$2 = function (image) {
  394. var src = image.src;
  395. if (src.indexOf('data:') === 0) {
  396. return dataUriToBlob(src);
  397. }
  398. return anyUriToBlob(src);
  399. };
  400. var blobToImage$1 = function (blob) {
  401. return new Promise$1(function (resolve, reject) {
  402. var blobUrl = URL.createObjectURL(blob);
  403. var image = new Image();
  404. var removeListeners = function () {
  405. image.removeEventListener('load', loaded);
  406. image.removeEventListener('error', error);
  407. };
  408. var loaded = function () {
  409. removeListeners();
  410. resolve(image);
  411. };
  412. var error = function () {
  413. removeListeners();
  414. reject('Unable to load data of type ' + blob.type + ': ' + blobUrl);
  415. };
  416. image.addEventListener('load', loaded);
  417. image.addEventListener('error', error);
  418. image.src = blobUrl;
  419. if (image.complete) {
  420. setTimeout(loaded, 0);
  421. }
  422. });
  423. };
  424. var anyUriToBlob = function (url) {
  425. return new Promise$1(function (resolve, reject) {
  426. var xhr = new XMLHttpRequest();
  427. xhr.open('GET', url, true);
  428. xhr.responseType = 'blob';
  429. xhr.onload = function () {
  430. if (this.status === 200) {
  431. resolve(this.response);
  432. }
  433. };
  434. xhr.onerror = function () {
  435. var _this = this;
  436. var corsError = function () {
  437. var obj = new Error('No access to download image');
  438. obj.code = 18;
  439. obj.name = 'SecurityError';
  440. return obj;
  441. };
  442. var genericError = function () {
  443. return new Error('Error ' + _this.status + ' downloading image');
  444. };
  445. reject(this.status === 0 ? corsError() : genericError());
  446. };
  447. xhr.send();
  448. });
  449. };
  450. var dataUriToBlobSync = function (uri) {
  451. var data = uri.split(',');
  452. var matches = /data:([^;]+)/.exec(data[0]);
  453. if (!matches) {
  454. return Optional.none();
  455. }
  456. var mimetype = matches[1];
  457. var base64 = data[1];
  458. var sliceSize = 1024;
  459. var byteCharacters = atob(base64);
  460. var bytesLength = byteCharacters.length;
  461. var slicesCount = Math.ceil(bytesLength / sliceSize);
  462. var byteArrays = new Array(slicesCount);
  463. for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
  464. var begin = sliceIndex * sliceSize;
  465. var end = Math.min(begin + sliceSize, bytesLength);
  466. var bytes = new Array(end - begin);
  467. for (var offset = begin, i = 0; offset < end; ++i, ++offset) {
  468. bytes[i] = byteCharacters[offset].charCodeAt(0);
  469. }
  470. byteArrays[sliceIndex] = new Uint8Array(bytes);
  471. }
  472. return Optional.some(new Blob(byteArrays, { type: mimetype }));
  473. };
  474. var dataUriToBlob = function (uri) {
  475. return new Promise$1(function (resolve, reject) {
  476. dataUriToBlobSync(uri).fold(function () {
  477. reject('uri is not base64: ' + uri);
  478. }, resolve);
  479. });
  480. };
  481. var canvasToBlob = function (canvas, type, quality) {
  482. type = type || 'image/png';
  483. if (isFunction(HTMLCanvasElement.prototype.toBlob)) {
  484. return new Promise$1(function (resolve, reject) {
  485. canvas.toBlob(function (blob) {
  486. if (blob) {
  487. resolve(blob);
  488. } else {
  489. reject();
  490. }
  491. }, type, quality);
  492. });
  493. } else {
  494. return dataUriToBlob(canvas.toDataURL(type, quality));
  495. }
  496. };
  497. var canvasToDataURL = function (canvas, type, quality) {
  498. type = type || 'image/png';
  499. return canvas.toDataURL(type, quality);
  500. };
  501. var blobToCanvas = function (blob) {
  502. return blobToImage$1(blob).then(function (image) {
  503. revokeImageUrl(image);
  504. var canvas = create$1(getWidth(image), getHeight(image));
  505. var context = get2dContext(canvas);
  506. context.drawImage(image, 0, 0);
  507. return canvas;
  508. });
  509. };
  510. var blobToDataUri = function (blob) {
  511. return new Promise$1(function (resolve) {
  512. var reader = new FileReader();
  513. reader.onloadend = function () {
  514. resolve(reader.result);
  515. };
  516. reader.readAsDataURL(blob);
  517. });
  518. };
  519. var revokeImageUrl = function (image) {
  520. URL.revokeObjectURL(image.src);
  521. };
  522. var blobToImage = function (blob) {
  523. return blobToImage$1(blob);
  524. };
  525. var imageToBlob$1 = function (image) {
  526. return imageToBlob$2(image);
  527. };
  528. var nativeIndexOf = Array.prototype.indexOf;
  529. var rawIndexOf = function (ts, t) {
  530. return nativeIndexOf.call(ts, t);
  531. };
  532. var contains = function (xs, x) {
  533. return rawIndexOf(xs, x) > -1;
  534. };
  535. var each$1 = function (xs, f) {
  536. for (var i = 0, len = xs.length; i < len; i++) {
  537. var x = xs[i];
  538. f(x, i);
  539. }
  540. };
  541. var filter = function (xs, pred) {
  542. var r = [];
  543. for (var i = 0, len = xs.length; i < len; i++) {
  544. var x = xs[i];
  545. if (pred(x, i)) {
  546. r.push(x);
  547. }
  548. }
  549. return r;
  550. };
  551. var foldl = function (xs, f, acc) {
  552. each$1(xs, function (x, i) {
  553. acc = f(acc, x, i);
  554. });
  555. return acc;
  556. };
  557. var findUntil = function (xs, pred, until) {
  558. for (var i = 0, len = xs.length; i < len; i++) {
  559. var x = xs[i];
  560. if (pred(x, i)) {
  561. return Optional.some(x);
  562. } else if (until(x, i)) {
  563. break;
  564. }
  565. }
  566. return Optional.none();
  567. };
  568. var find = function (xs, pred) {
  569. return findUntil(xs, pred, never);
  570. };
  571. var forall = function (xs, pred) {
  572. for (var i = 0, len = xs.length; i < len; ++i) {
  573. var x = xs[i];
  574. if (pred(x, i) !== true) {
  575. return false;
  576. }
  577. }
  578. return true;
  579. };
  580. var keys = Object.keys;
  581. var each = function (obj, f) {
  582. var props = keys(obj);
  583. for (var k = 0, len = props.length; k < len; k++) {
  584. var i = props[k];
  585. var x = obj[i];
  586. f(x, i);
  587. }
  588. };
  589. var generate = function (cases) {
  590. if (!isArray(cases)) {
  591. throw new Error('cases must be an array');
  592. }
  593. if (cases.length === 0) {
  594. throw new Error('there must be at least one case');
  595. }
  596. var constructors = [];
  597. var adt = {};
  598. each$1(cases, function (acase, count) {
  599. var keys$1 = keys(acase);
  600. if (keys$1.length !== 1) {
  601. throw new Error('one and only one name per case');
  602. }
  603. var key = keys$1[0];
  604. var value = acase[key];
  605. if (adt[key] !== undefined) {
  606. throw new Error('duplicate key detected:' + key);
  607. } else if (key === 'cata') {
  608. throw new Error('cannot have a case named cata (sorry)');
  609. } else if (!isArray(value)) {
  610. throw new Error('case arguments must be an array');
  611. }
  612. constructors.push(key);
  613. adt[key] = function () {
  614. var args = [];
  615. for (var _i = 0; _i < arguments.length; _i++) {
  616. args[_i] = arguments[_i];
  617. }
  618. var argLength = args.length;
  619. if (argLength !== value.length) {
  620. throw new Error('Wrong number of arguments to case ' + key + '. Expected ' + value.length + ' (' + value + '), got ' + argLength);
  621. }
  622. var match = function (branches) {
  623. var branchKeys = keys(branches);
  624. if (constructors.length !== branchKeys.length) {
  625. throw new Error('Wrong number of arguments to match. Expected: ' + constructors.join(',') + '\nActual: ' + branchKeys.join(','));
  626. }
  627. var allReqd = forall(constructors, function (reqKey) {
  628. return contains(branchKeys, reqKey);
  629. });
  630. if (!allReqd) {
  631. throw new Error('Not all branches were specified when using match. Specified: ' + branchKeys.join(', ') + '\nRequired: ' + constructors.join(', '));
  632. }
  633. return branches[key].apply(null, args);
  634. };
  635. return {
  636. fold: function () {
  637. var foldArgs = [];
  638. for (var _i = 0; _i < arguments.length; _i++) {
  639. foldArgs[_i] = arguments[_i];
  640. }
  641. if (foldArgs.length !== cases.length) {
  642. throw new Error('Wrong number of arguments to fold. Expected ' + cases.length + ', got ' + foldArgs.length);
  643. }
  644. var target = foldArgs[count];
  645. return target.apply(null, args);
  646. },
  647. match: match,
  648. log: function (label) {
  649. console.log(label, {
  650. constructors: constructors,
  651. constructor: key,
  652. params: args
  653. });
  654. }
  655. };
  656. };
  657. });
  658. return adt;
  659. };
  660. var Adt = { generate: generate };
  661. Adt.generate([
  662. {
  663. bothErrors: [
  664. 'error1',
  665. 'error2'
  666. ]
  667. },
  668. {
  669. firstError: [
  670. 'error1',
  671. 'value2'
  672. ]
  673. },
  674. {
  675. secondError: [
  676. 'value1',
  677. 'error2'
  678. ]
  679. },
  680. {
  681. bothValues: [
  682. 'value1',
  683. 'value2'
  684. ]
  685. }
  686. ]);
  687. var create = function (getCanvas, blob, uri) {
  688. var initialType = blob.type;
  689. var getType = constant(initialType);
  690. var toBlob = function () {
  691. return Promise$1.resolve(blob);
  692. };
  693. var toDataURL = constant(uri);
  694. var toBase64 = function () {
  695. return uri.split(',')[1];
  696. };
  697. var toAdjustedBlob = function (type, quality) {
  698. return getCanvas.then(function (canvas) {
  699. return canvasToBlob(canvas, type, quality);
  700. });
  701. };
  702. var toAdjustedDataURL = function (type, quality) {
  703. return getCanvas.then(function (canvas) {
  704. return canvasToDataURL(canvas, type, quality);
  705. });
  706. };
  707. var toAdjustedBase64 = function (type, quality) {
  708. return toAdjustedDataURL(type, quality).then(function (dataurl) {
  709. return dataurl.split(',')[1];
  710. });
  711. };
  712. var toCanvas = function () {
  713. return getCanvas.then(clone);
  714. };
  715. return {
  716. getType: getType,
  717. toBlob: toBlob,
  718. toDataURL: toDataURL,
  719. toBase64: toBase64,
  720. toAdjustedBlob: toAdjustedBlob,
  721. toAdjustedDataURL: toAdjustedDataURL,
  722. toAdjustedBase64: toAdjustedBase64,
  723. toCanvas: toCanvas
  724. };
  725. };
  726. var fromBlob = function (blob) {
  727. return blobToDataUri(blob).then(function (uri) {
  728. return create(blobToCanvas(blob), blob, uri);
  729. });
  730. };
  731. var fromCanvas = function (canvas, type) {
  732. return canvasToBlob(canvas, type).then(function (blob) {
  733. return create(Promise$1.resolve(canvas), blob, canvas.toDataURL());
  734. });
  735. };
  736. var ceilWithPrecision = function (num, precision) {
  737. if (precision === void 0) {
  738. precision = 2;
  739. }
  740. var mul = Math.pow(10, precision);
  741. var upper = Math.round(num * mul);
  742. return Math.ceil(upper / mul);
  743. };
  744. var rotate$2 = function (ir, angle) {
  745. return ir.toCanvas().then(function (canvas) {
  746. return applyRotate(canvas, ir.getType(), angle);
  747. });
  748. };
  749. var applyRotate = function (image, type, angle) {
  750. var degrees = angle < 0 ? 360 + angle : angle;
  751. var rad = degrees * Math.PI / 180;
  752. var width = image.width;
  753. var height = image.height;
  754. var sin = Math.sin(rad);
  755. var cos = Math.cos(rad);
  756. var newWidth = ceilWithPrecision(Math.abs(width * cos) + Math.abs(height * sin));
  757. var newHeight = ceilWithPrecision(Math.abs(width * sin) + Math.abs(height * cos));
  758. var canvas = create$1(newWidth, newHeight);
  759. var context = get2dContext(canvas);
  760. context.translate(newWidth / 2, newHeight / 2);
  761. context.rotate(rad);
  762. context.drawImage(image, -width / 2, -height / 2);
  763. return fromCanvas(canvas, type);
  764. };
  765. var flip$2 = function (ir, axis) {
  766. return ir.toCanvas().then(function (canvas) {
  767. return applyFlip(canvas, ir.getType(), axis);
  768. });
  769. };
  770. var applyFlip = function (image, type, axis) {
  771. var canvas = create$1(image.width, image.height);
  772. var context = get2dContext(canvas);
  773. if (axis === 'v') {
  774. context.scale(1, -1);
  775. context.drawImage(image, 0, -canvas.height);
  776. } else {
  777. context.scale(-1, 1);
  778. context.drawImage(image, -canvas.width, 0);
  779. }
  780. return fromCanvas(canvas, type);
  781. };
  782. var flip$1 = function (ir, axis) {
  783. return flip$2(ir, axis);
  784. };
  785. var rotate$1 = function (ir, angle) {
  786. return rotate$2(ir, angle);
  787. };
  788. var sendRequest = function (url, headers, withCredentials) {
  789. if (withCredentials === void 0) {
  790. withCredentials = false;
  791. }
  792. return new Promise$1(function (resolve) {
  793. var xhr = new XMLHttpRequest();
  794. xhr.onreadystatechange = function () {
  795. if (xhr.readyState === 4) {
  796. resolve({
  797. status: xhr.status,
  798. blob: xhr.response
  799. });
  800. }
  801. };
  802. xhr.open('GET', url, true);
  803. xhr.withCredentials = withCredentials;
  804. each(headers, function (value, key) {
  805. xhr.setRequestHeader(key, value);
  806. });
  807. xhr.responseType = 'blob';
  808. xhr.send();
  809. });
  810. };
  811. var readBlobText = function (blob) {
  812. return new Promise$1(function (resolve, reject) {
  813. var reader = new FileReader();
  814. reader.onload = function () {
  815. resolve(reader.result);
  816. };
  817. reader.onerror = function (e) {
  818. reject(e);
  819. };
  820. reader.readAsText(blob);
  821. });
  822. };
  823. var parseJson = function (text) {
  824. try {
  825. return Optional.some(JSON.parse(text));
  826. } catch (ex) {
  827. return Optional.none();
  828. }
  829. };
  830. var friendlyHttpErrors = [
  831. {
  832. code: 404,
  833. message: 'Could not find Image Proxy'
  834. },
  835. {
  836. code: 403,
  837. message: 'Rejected request'
  838. },
  839. {
  840. code: 0,
  841. message: 'Incorrect Image Proxy URL'
  842. }
  843. ];
  844. var friendlyServiceErrors = [
  845. {
  846. type: 'not_found',
  847. message: 'Failed to load image.'
  848. },
  849. {
  850. type: 'key_missing',
  851. message: 'The request did not include an api key.'
  852. },
  853. {
  854. type: 'key_not_found',
  855. message: 'The provided api key could not be found.'
  856. },
  857. {
  858. type: 'domain_not_trusted',
  859. message: 'The api key is not valid for the request origins.'
  860. }
  861. ];
  862. var traverseJson = function (json, path) {
  863. var value = foldl(path, function (result, key) {
  864. return isNonNullable(result) ? result[key] : undefined;
  865. }, json);
  866. return Optional.from(value);
  867. };
  868. var isServiceErrorCode = function (code, blob) {
  869. return (blob === null || blob === void 0 ? void 0 : blob.type) === 'application/json' && (code === 400 || code === 403 || code === 404 || code === 500);
  870. };
  871. var getHttpErrorMsg = function (status) {
  872. var message = find(friendlyHttpErrors, function (error) {
  873. return status === error.code;
  874. }).fold(constant('Unknown ImageProxy error'), function (error) {
  875. return error.message;
  876. });
  877. return 'ImageProxy HTTP error: ' + message;
  878. };
  879. var handleHttpError = function (status) {
  880. var message = getHttpErrorMsg(status);
  881. return Promise$1.reject(message);
  882. };
  883. var getServiceErrorMsg = function (type) {
  884. return find(friendlyServiceErrors, function (error) {
  885. return error.type === type;
  886. }).fold(constant('Unknown service error'), function (error) {
  887. return error.message;
  888. });
  889. };
  890. var getServiceError = function (text) {
  891. var serviceError = parseJson(text);
  892. var errorMsg = serviceError.bind(function (err) {
  893. return traverseJson(err, [
  894. 'error',
  895. 'type'
  896. ]).map(getServiceErrorMsg);
  897. }).getOr('Invalid JSON in service error message');
  898. return 'ImageProxy Service error: ' + errorMsg;
  899. };
  900. var handleServiceError = function (blob) {
  901. return readBlobText(blob).then(function (text) {
  902. var serviceError = getServiceError(text);
  903. return Promise$1.reject(serviceError);
  904. });
  905. };
  906. var handleServiceErrorResponse = function (status, blob) {
  907. return isServiceErrorCode(status, blob) ? handleServiceError(blob) : handleHttpError(status);
  908. };
  909. var appendApiKey = function (url, apiKey) {
  910. var separator = url.indexOf('?') === -1 ? '?' : '&';
  911. if (/[?&]apiKey=/.test(url)) {
  912. return url;
  913. } else {
  914. return url + separator + 'apiKey=' + encodeURIComponent(apiKey);
  915. }
  916. };
  917. var isError = function (status) {
  918. return status < 200 || status >= 300;
  919. };
  920. var requestServiceBlob = function (url, apiKey) {
  921. var headers = {
  922. 'Content-Type': 'application/json;charset=UTF-8',
  923. 'tiny-api-key': apiKey
  924. };
  925. return sendRequest(appendApiKey(url, apiKey), headers).then(function (result) {
  926. return isError(result.status) ? handleServiceErrorResponse(result.status, result.blob) : Promise$1.resolve(result.blob);
  927. });
  928. };
  929. var requestBlob = function (url, withCredentials) {
  930. return sendRequest(url, {}, withCredentials).then(function (result) {
  931. return isError(result.status) ? handleHttpError(result.status) : Promise$1.resolve(result.blob);
  932. });
  933. };
  934. var getUrl = function (url, apiKey, withCredentials) {
  935. if (withCredentials === void 0) {
  936. withCredentials = false;
  937. }
  938. return apiKey ? requestServiceBlob(url, apiKey) : requestBlob(url, withCredentials);
  939. };
  940. var blobToImageResult = function (blob) {
  941. return fromBlob(blob);
  942. };
  943. var ELEMENT = 1;
  944. var fromHtml = function (html, scope) {
  945. var doc = scope || document;
  946. var div = doc.createElement('div');
  947. div.innerHTML = html;
  948. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  949. console.error('HTML does not have a single root node', html);
  950. throw new Error('HTML must have a single root node');
  951. }
  952. return fromDom(div.childNodes[0]);
  953. };
  954. var fromTag = function (tag, scope) {
  955. var doc = scope || document;
  956. var node = doc.createElement(tag);
  957. return fromDom(node);
  958. };
  959. var fromText = function (text, scope) {
  960. var doc = scope || document;
  961. var node = doc.createTextNode(text);
  962. return fromDom(node);
  963. };
  964. var fromDom = function (node) {
  965. if (node === null || node === undefined) {
  966. throw new Error('Node cannot be null or undefined');
  967. }
  968. return { dom: node };
  969. };
  970. var fromPoint = function (docElm, x, y) {
  971. return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  972. };
  973. var SugarElement = {
  974. fromHtml: fromHtml,
  975. fromTag: fromTag,
  976. fromText: fromText,
  977. fromDom: fromDom,
  978. fromPoint: fromPoint
  979. };
  980. var is = function (element, selector) {
  981. var dom = element.dom;
  982. if (dom.nodeType !== ELEMENT) {
  983. return false;
  984. } else {
  985. var elem = dom;
  986. if (elem.matches !== undefined) {
  987. return elem.matches(selector);
  988. } else if (elem.msMatchesSelector !== undefined) {
  989. return elem.msMatchesSelector(selector);
  990. } else if (elem.webkitMatchesSelector !== undefined) {
  991. return elem.webkitMatchesSelector(selector);
  992. } else if (elem.mozMatchesSelector !== undefined) {
  993. return elem.mozMatchesSelector(selector);
  994. } else {
  995. throw new Error('Browser lacks native selectors');
  996. }
  997. }
  998. };
  999. typeof window !== 'undefined' ? window : Function('return this;')();
  1000. var child$1 = function (scope, predicate) {
  1001. var pred = function (node) {
  1002. return predicate(SugarElement.fromDom(node));
  1003. };
  1004. var result = find(scope.dom.childNodes, pred);
  1005. return result.map(SugarElement.fromDom);
  1006. };
  1007. var child = function (scope, selector) {
  1008. return child$1(scope, function (e) {
  1009. return is(e, selector);
  1010. });
  1011. };
  1012. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  1013. var global$2 = tinymce.util.Tools.resolve('tinymce.util.Promise');
  1014. var global$1 = tinymce.util.Tools.resolve('tinymce.util.URI');
  1015. var getToolbarItems = function (editor) {
  1016. return editor.getParam('imagetools_toolbar', 'rotateleft rotateright flipv fliph editimage imageoptions');
  1017. };
  1018. var getProxyUrl = function (editor) {
  1019. return editor.getParam('imagetools_proxy');
  1020. };
  1021. var getCorsHosts = function (editor) {
  1022. return editor.getParam('imagetools_cors_hosts', [], 'string[]');
  1023. };
  1024. var getCredentialsHosts = function (editor) {
  1025. return editor.getParam('imagetools_credentials_hosts', [], 'string[]');
  1026. };
  1027. var getFetchImage = function (editor) {
  1028. return Optional.from(editor.getParam('imagetools_fetch_image', null, 'function'));
  1029. };
  1030. var getApiKey = function (editor) {
  1031. return editor.getParam('api_key', editor.getParam('imagetools_api_key', '', 'string'), 'string');
  1032. };
  1033. var getUploadTimeout = function (editor) {
  1034. return editor.getParam('images_upload_timeout', 30000, 'number');
  1035. };
  1036. var shouldReuseFilename = function (editor) {
  1037. return editor.getParam('images_reuse_filename', false, 'boolean');
  1038. };
  1039. var getImageSize = function (img) {
  1040. var width, height;
  1041. var isPxValue = function (value) {
  1042. return /^[0-9\.]+px$/.test(value);
  1043. };
  1044. width = img.style.width;
  1045. height = img.style.height;
  1046. if (width || height) {
  1047. if (isPxValue(width) && isPxValue(height)) {
  1048. return {
  1049. w: parseInt(width, 10),
  1050. h: parseInt(height, 10)
  1051. };
  1052. }
  1053. return null;
  1054. }
  1055. width = img.width;
  1056. height = img.height;
  1057. if (width && height) {
  1058. return {
  1059. w: parseInt(width, 10),
  1060. h: parseInt(height, 10)
  1061. };
  1062. }
  1063. return null;
  1064. };
  1065. var setImageSize = function (img, size) {
  1066. var width, height;
  1067. if (size) {
  1068. width = img.style.width;
  1069. height = img.style.height;
  1070. if (width || height) {
  1071. img.style.width = size.w + 'px';
  1072. img.style.height = size.h + 'px';
  1073. img.removeAttribute('data-mce-style');
  1074. }
  1075. width = img.width;
  1076. height = img.height;
  1077. if (width || height) {
  1078. img.setAttribute('width', String(size.w));
  1079. img.setAttribute('height', String(size.h));
  1080. }
  1081. }
  1082. };
  1083. var getNaturalImageSize = function (img) {
  1084. return {
  1085. w: img.naturalWidth,
  1086. h: img.naturalHeight
  1087. };
  1088. };
  1089. var count = 0;
  1090. var getFigureImg = function (elem) {
  1091. return child(SugarElement.fromDom(elem), 'img');
  1092. };
  1093. var isFigure = function (editor, elem) {
  1094. return editor.dom.is(elem, 'figure');
  1095. };
  1096. var isImage = function (editor, imgNode) {
  1097. return editor.dom.is(imgNode, 'img:not([data-mce-object],[data-mce-placeholder])');
  1098. };
  1099. var getEditableImage = function (editor, node) {
  1100. var isEditable = function (imgNode) {
  1101. return isImage(editor, imgNode) && (isLocalImage(editor, imgNode) || isCorsImage(editor, imgNode) || isNonNullable(getProxyUrl(editor)));
  1102. };
  1103. if (isFigure(editor, node)) {
  1104. return getFigureImg(node).bind(function (img) {
  1105. return isEditable(img.dom) ? Optional.some(img.dom) : Optional.none();
  1106. });
  1107. } else {
  1108. return isEditable(node) ? Optional.some(node) : Optional.none();
  1109. }
  1110. };
  1111. var displayError = function (editor, error) {
  1112. editor.notificationManager.open({
  1113. text: error,
  1114. type: 'error'
  1115. });
  1116. };
  1117. var getSelectedImage = function (editor) {
  1118. var elem = editor.selection.getNode();
  1119. var figureElm = editor.dom.getParent(elem, 'figure.image');
  1120. if (figureElm !== null && isFigure(editor, figureElm)) {
  1121. return getFigureImg(figureElm);
  1122. } else if (isImage(editor, elem)) {
  1123. return Optional.some(SugarElement.fromDom(elem));
  1124. } else {
  1125. return Optional.none();
  1126. }
  1127. };
  1128. var extractFilename = function (editor, url, group) {
  1129. var m = url.match(/(?:\/|^)(([^\/\?]+)\.(?:[a-z0-9.]+))(?:\?|$)/i);
  1130. return isNonNullable(m) ? editor.dom.encode(m[group]) : null;
  1131. };
  1132. var createId = function () {
  1133. return 'imagetools' + count++;
  1134. };
  1135. var isLocalImage = function (editor, img) {
  1136. var url = img.src;
  1137. return url.indexOf('data:') === 0 || url.indexOf('blob:') === 0 || new global$1(url).host === editor.documentBaseURI.host;
  1138. };
  1139. var isCorsImage = function (editor, img) {
  1140. return global$4.inArray(getCorsHosts(editor), new global$1(img.src).host) !== -1;
  1141. };
  1142. var isCorsWithCredentialsImage = function (editor, img) {
  1143. return global$4.inArray(getCredentialsHosts(editor), new global$1(img.src).host) !== -1;
  1144. };
  1145. var defaultFetchImage = function (editor, img) {
  1146. if (isCorsImage(editor, img)) {
  1147. return getUrl(img.src, null, isCorsWithCredentialsImage(editor, img));
  1148. }
  1149. if (!isLocalImage(editor, img)) {
  1150. var proxyUrl = getProxyUrl(editor);
  1151. var src = proxyUrl + (proxyUrl.indexOf('?') === -1 ? '?' : '&') + 'url=' + encodeURIComponent(img.src);
  1152. var apiKey = getApiKey(editor);
  1153. return getUrl(src, apiKey, false);
  1154. }
  1155. return imageToBlob$1(img);
  1156. };
  1157. var imageToBlob = function (editor, img) {
  1158. return getFetchImage(editor).fold(function () {
  1159. return defaultFetchImage(editor, img);
  1160. }, function (customFetchImage) {
  1161. return customFetchImage(img);
  1162. });
  1163. };
  1164. var findBlob = function (editor, img) {
  1165. var blobInfo = editor.editorUpload.blobCache.getByUri(img.src);
  1166. if (blobInfo) {
  1167. return global$2.resolve(blobInfo.blob());
  1168. }
  1169. return imageToBlob(editor, img);
  1170. };
  1171. var startTimedUpload = function (editor, imageUploadTimerState) {
  1172. var imageUploadTimer = global$3.setEditorTimeout(editor, function () {
  1173. editor.editorUpload.uploadImagesAuto();
  1174. }, getUploadTimeout(editor));
  1175. imageUploadTimerState.set(imageUploadTimer);
  1176. };
  1177. var cancelTimedUpload = function (imageUploadTimerState) {
  1178. global$3.clearTimeout(imageUploadTimerState.get());
  1179. };
  1180. var updateSelectedImage = function (editor, origBlob, ir, uploadImmediately, imageUploadTimerState, selectedImage, size) {
  1181. return ir.toBlob().then(function (blob) {
  1182. var uri, name, filename, blobInfo;
  1183. var blobCache = editor.editorUpload.blobCache;
  1184. uri = selectedImage.src;
  1185. var useFilename = origBlob.type === blob.type;
  1186. if (shouldReuseFilename(editor)) {
  1187. blobInfo = blobCache.getByUri(uri);
  1188. if (isNonNullable(blobInfo)) {
  1189. uri = blobInfo.uri();
  1190. name = blobInfo.name();
  1191. filename = blobInfo.filename();
  1192. } else {
  1193. name = extractFilename(editor, uri, 2);
  1194. filename = extractFilename(editor, uri, 1);
  1195. }
  1196. }
  1197. blobInfo = blobCache.create({
  1198. id: createId(),
  1199. blob: blob,
  1200. base64: ir.toBase64(),
  1201. uri: uri,
  1202. name: name,
  1203. filename: useFilename ? filename : undefined
  1204. });
  1205. blobCache.add(blobInfo);
  1206. editor.undoManager.transact(function () {
  1207. var imageLoadedHandler = function () {
  1208. editor.$(selectedImage).off('load', imageLoadedHandler);
  1209. editor.nodeChanged();
  1210. if (uploadImmediately) {
  1211. editor.editorUpload.uploadImagesAuto();
  1212. } else {
  1213. cancelTimedUpload(imageUploadTimerState);
  1214. startTimedUpload(editor, imageUploadTimerState);
  1215. }
  1216. };
  1217. editor.$(selectedImage).on('load', imageLoadedHandler);
  1218. if (size) {
  1219. editor.$(selectedImage).attr({
  1220. width: size.w,
  1221. height: size.h
  1222. });
  1223. }
  1224. editor.$(selectedImage).attr({ src: blobInfo.blobUri() }).removeAttr('data-mce-src');
  1225. });
  1226. return blobInfo;
  1227. });
  1228. };
  1229. var selectedImageOperation = function (editor, imageUploadTimerState, fn, size) {
  1230. return function () {
  1231. var imgOpt = getSelectedImage(editor);
  1232. return imgOpt.fold(function () {
  1233. displayError(editor, 'Could not find selected image');
  1234. }, function (img) {
  1235. return editor._scanForImages().then(function () {
  1236. return findBlob(editor, img.dom);
  1237. }).then(function (blob) {
  1238. return blobToImageResult(blob).then(fn).then(function (imageResult) {
  1239. return updateSelectedImage(editor, blob, imageResult, false, imageUploadTimerState, img.dom, size);
  1240. });
  1241. }).catch(function (error) {
  1242. displayError(editor, error);
  1243. });
  1244. });
  1245. };
  1246. };
  1247. var rotate = function (editor, imageUploadTimerState, angle) {
  1248. return function () {
  1249. var imgOpt = getSelectedImage(editor);
  1250. var flippedSize = imgOpt.map(function (img) {
  1251. var size = getImageSize(img.dom);
  1252. return size ? {
  1253. w: size.h,
  1254. h: size.w
  1255. } : null;
  1256. }).getOrNull();
  1257. return selectedImageOperation(editor, imageUploadTimerState, function (imageResult) {
  1258. return rotate$1(imageResult, angle);
  1259. }, flippedSize)();
  1260. };
  1261. };
  1262. var flip = function (editor, imageUploadTimerState, axis) {
  1263. return function () {
  1264. return selectedImageOperation(editor, imageUploadTimerState, function (imageResult) {
  1265. return flip$1(imageResult, axis);
  1266. })();
  1267. };
  1268. };
  1269. var handleDialogBlob = function (editor, imageUploadTimerState, img, originalSize, blob) {
  1270. return blobToImage(blob).then(function (newImage) {
  1271. var newSize = getNaturalImageSize(newImage);
  1272. if (originalSize.w !== newSize.w || originalSize.h !== newSize.h) {
  1273. if (getImageSize(img)) {
  1274. setImageSize(img, newSize);
  1275. }
  1276. }
  1277. URL.revokeObjectURL(newImage.src);
  1278. return blob;
  1279. }).then(blobToImageResult).then(function (imageResult) {
  1280. return updateSelectedImage(editor, blob, imageResult, true, imageUploadTimerState, img);
  1281. });
  1282. };
  1283. var saveState = 'save-state';
  1284. var disable = 'disable';
  1285. var enable = 'enable';
  1286. var createState = function (blob) {
  1287. return {
  1288. blob: blob,
  1289. url: URL.createObjectURL(blob)
  1290. };
  1291. };
  1292. var makeOpen = function (editor, imageUploadTimerState) {
  1293. return function () {
  1294. var getLoadedSpec = function (currentState) {
  1295. return {
  1296. title: 'Edit Image',
  1297. size: 'large',
  1298. body: {
  1299. type: 'panel',
  1300. items: [{
  1301. type: 'imagetools',
  1302. name: 'imagetools',
  1303. label: 'Edit Image',
  1304. currentState: currentState
  1305. }]
  1306. },
  1307. buttons: [
  1308. {
  1309. type: 'cancel',
  1310. name: 'cancel',
  1311. text: 'Cancel'
  1312. },
  1313. {
  1314. type: 'submit',
  1315. name: 'save',
  1316. text: 'Save',
  1317. primary: true,
  1318. disabled: true
  1319. }
  1320. ],
  1321. onSubmit: function (api) {
  1322. var blob = api.getData().imagetools.blob;
  1323. originalImgOpt.each(function (originalImg) {
  1324. originalSizeOpt.each(function (originalSize) {
  1325. handleDialogBlob(editor, imageUploadTimerState, originalImg.dom, originalSize, blob);
  1326. });
  1327. });
  1328. api.close();
  1329. },
  1330. onCancel: noop,
  1331. onAction: function (api, details) {
  1332. switch (details.name) {
  1333. case saveState:
  1334. if (details.value) {
  1335. api.enable('save');
  1336. } else {
  1337. api.disable('save');
  1338. }
  1339. break;
  1340. case disable:
  1341. api.disable('save');
  1342. api.disable('cancel');
  1343. break;
  1344. case enable:
  1345. api.enable('cancel');
  1346. break;
  1347. }
  1348. }
  1349. };
  1350. };
  1351. var originalImgOpt = getSelectedImage(editor);
  1352. var originalSizeOpt = originalImgOpt.map(function (origImg) {
  1353. return getNaturalImageSize(origImg.dom);
  1354. });
  1355. originalImgOpt.each(function (img) {
  1356. getEditableImage(editor, img.dom).each(function (_) {
  1357. findBlob(editor, img.dom).then(function (blob) {
  1358. var state = createState(blob);
  1359. editor.windowManager.open(getLoadedSpec(state));
  1360. });
  1361. });
  1362. });
  1363. };
  1364. };
  1365. var register$2 = function (editor, imageUploadTimerState) {
  1366. global$4.each({
  1367. mceImageRotateLeft: rotate(editor, imageUploadTimerState, -90),
  1368. mceImageRotateRight: rotate(editor, imageUploadTimerState, 90),
  1369. mceImageFlipVertical: flip(editor, imageUploadTimerState, 'v'),
  1370. mceImageFlipHorizontal: flip(editor, imageUploadTimerState, 'h'),
  1371. mceEditImage: makeOpen(editor, imageUploadTimerState)
  1372. }, function (fn, cmd) {
  1373. editor.addCommand(cmd, fn);
  1374. });
  1375. };
  1376. var setup = function (editor, imageUploadTimerState, lastSelectedImageState) {
  1377. editor.on('NodeChange', function (e) {
  1378. var lastSelectedImage = lastSelectedImageState.get();
  1379. var selectedImage = getEditableImage(editor, e.element);
  1380. if (lastSelectedImage && !selectedImage.exists(function (img) {
  1381. return lastSelectedImage.src === img.src;
  1382. })) {
  1383. cancelTimedUpload(imageUploadTimerState);
  1384. editor.editorUpload.uploadImagesAuto();
  1385. lastSelectedImageState.set(null);
  1386. }
  1387. selectedImage.each(lastSelectedImageState.set);
  1388. });
  1389. };
  1390. var register$1 = function (editor) {
  1391. var changeHandlers = [];
  1392. var cmd = function (command) {
  1393. return function () {
  1394. return editor.execCommand(command);
  1395. };
  1396. };
  1397. var isEditableImage = function () {
  1398. return getSelectedImage(editor).exists(function (element) {
  1399. return getEditableImage(editor, element.dom).isSome();
  1400. });
  1401. };
  1402. var onSetup = function (api) {
  1403. var handler = function (isEditableImage) {
  1404. return api.setDisabled(!isEditableImage);
  1405. };
  1406. handler(isEditableImage());
  1407. changeHandlers = changeHandlers.concat([handler]);
  1408. return function () {
  1409. changeHandlers = filter(changeHandlers, function (h) {
  1410. return h !== handler;
  1411. });
  1412. };
  1413. };
  1414. editor.on('NodeChange', function () {
  1415. var isEditable = isEditableImage();
  1416. each$1(changeHandlers, function (handler) {
  1417. return handler(isEditable);
  1418. });
  1419. });
  1420. editor.ui.registry.addButton('rotateleft', {
  1421. tooltip: 'Rotate counterclockwise',
  1422. icon: 'rotate-left',
  1423. onAction: cmd('mceImageRotateLeft'),
  1424. onSetup: onSetup
  1425. });
  1426. editor.ui.registry.addButton('rotateright', {
  1427. tooltip: 'Rotate clockwise',
  1428. icon: 'rotate-right',
  1429. onAction: cmd('mceImageRotateRight'),
  1430. onSetup: onSetup
  1431. });
  1432. editor.ui.registry.addButton('flipv', {
  1433. tooltip: 'Flip vertically',
  1434. icon: 'flip-vertically',
  1435. onAction: cmd('mceImageFlipVertical'),
  1436. onSetup: onSetup
  1437. });
  1438. editor.ui.registry.addButton('fliph', {
  1439. tooltip: 'Flip horizontally',
  1440. icon: 'flip-horizontally',
  1441. onAction: cmd('mceImageFlipHorizontal'),
  1442. onSetup: onSetup
  1443. });
  1444. editor.ui.registry.addButton('editimage', {
  1445. tooltip: 'Edit image',
  1446. icon: 'edit-image',
  1447. onAction: cmd('mceEditImage'),
  1448. onSetup: onSetup
  1449. });
  1450. editor.ui.registry.addButton('imageoptions', {
  1451. tooltip: 'Image options',
  1452. icon: 'image',
  1453. onAction: cmd('mceImage')
  1454. });
  1455. editor.ui.registry.addContextMenu('imagetools', {
  1456. update: function (element) {
  1457. return getEditableImage(editor, element).map(function (_) {
  1458. return {
  1459. text: 'Edit image',
  1460. icon: 'edit-image',
  1461. onAction: cmd('mceEditImage')
  1462. };
  1463. }).toArray();
  1464. }
  1465. });
  1466. };
  1467. var register = function (editor) {
  1468. editor.ui.registry.addContextToolbar('imagetools', {
  1469. items: getToolbarItems(editor),
  1470. predicate: function (elem) {
  1471. return getEditableImage(editor, elem).isSome();
  1472. },
  1473. position: 'node',
  1474. scope: 'node'
  1475. });
  1476. };
  1477. function Plugin () {
  1478. global$5.add('imagetools', function (editor) {
  1479. var imageUploadTimerState = Cell(0);
  1480. var lastSelectedImageState = Cell(null);
  1481. register$2(editor, imageUploadTimerState);
  1482. register$1(editor);
  1483. register(editor);
  1484. setup(editor, imageUploadTimerState, lastSelectedImageState);
  1485. });
  1486. }
  1487. Plugin();
  1488. }());