plugin.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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 global$6 = 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 typeOf = function (x) {
  25. var t = typeof x;
  26. if (x === null) {
  27. return 'null';
  28. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  29. return 'array';
  30. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  31. return 'string';
  32. } else {
  33. return t;
  34. }
  35. };
  36. var isType = function (type) {
  37. return function (value) {
  38. return typeOf(value) === type;
  39. };
  40. };
  41. var isSimpleType = function (type) {
  42. return function (value) {
  43. return typeof value === type;
  44. };
  45. };
  46. var eq = function (t) {
  47. return function (a) {
  48. return t === a;
  49. };
  50. };
  51. var isString = isType('string');
  52. var isObject = isType('object');
  53. var isArray = isType('array');
  54. var isNull = eq(null);
  55. var isBoolean = isSimpleType('boolean');
  56. var isNullable = function (a) {
  57. return a === null || a === undefined;
  58. };
  59. var isNonNullable = function (a) {
  60. return !isNullable(a);
  61. };
  62. var isFunction = isSimpleType('function');
  63. var isNumber = isSimpleType('number');
  64. var noop = function () {
  65. };
  66. var constant = function (value) {
  67. return function () {
  68. return value;
  69. };
  70. };
  71. var identity = function (x) {
  72. return x;
  73. };
  74. var never = constant(false);
  75. var always = constant(true);
  76. var none = function () {
  77. return NONE;
  78. };
  79. var NONE = function () {
  80. var call = function (thunk) {
  81. return thunk();
  82. };
  83. var id = identity;
  84. var me = {
  85. fold: function (n, _s) {
  86. return n();
  87. },
  88. isSome: never,
  89. isNone: always,
  90. getOr: id,
  91. getOrThunk: call,
  92. getOrDie: function (msg) {
  93. throw new Error(msg || 'error: getOrDie called on none.');
  94. },
  95. getOrNull: constant(null),
  96. getOrUndefined: constant(undefined),
  97. or: id,
  98. orThunk: call,
  99. map: none,
  100. each: noop,
  101. bind: none,
  102. exists: never,
  103. forall: always,
  104. filter: function () {
  105. return none();
  106. },
  107. toArray: function () {
  108. return [];
  109. },
  110. toString: constant('none()')
  111. };
  112. return me;
  113. }();
  114. var some = function (a) {
  115. var constant_a = constant(a);
  116. var self = function () {
  117. return me;
  118. };
  119. var bind = function (f) {
  120. return f(a);
  121. };
  122. var me = {
  123. fold: function (n, s) {
  124. return s(a);
  125. },
  126. isSome: always,
  127. isNone: never,
  128. getOr: constant_a,
  129. getOrThunk: constant_a,
  130. getOrDie: constant_a,
  131. getOrNull: constant_a,
  132. getOrUndefined: constant_a,
  133. or: self,
  134. orThunk: self,
  135. map: function (f) {
  136. return some(f(a));
  137. },
  138. each: function (f) {
  139. f(a);
  140. },
  141. bind: bind,
  142. exists: bind,
  143. forall: bind,
  144. filter: function (f) {
  145. return f(a) ? me : NONE;
  146. },
  147. toArray: function () {
  148. return [a];
  149. },
  150. toString: function () {
  151. return 'some(' + a + ')';
  152. }
  153. };
  154. return me;
  155. };
  156. var from = function (value) {
  157. return value === null || value === undefined ? NONE : some(value);
  158. };
  159. var Optional = {
  160. some: some,
  161. none: none,
  162. from: from
  163. };
  164. var keys = Object.keys;
  165. var hasOwnProperty = Object.hasOwnProperty;
  166. var each = function (obj, f) {
  167. var props = keys(obj);
  168. for (var k = 0, len = props.length; k < len; k++) {
  169. var i = props[k];
  170. var x = obj[i];
  171. f(x, i);
  172. }
  173. };
  174. var objAcc = function (r) {
  175. return function (x, i) {
  176. r[i] = x;
  177. };
  178. };
  179. var internalFilter = function (obj, pred, onTrue, onFalse) {
  180. var r = {};
  181. each(obj, function (x, i) {
  182. (pred(x, i) ? onTrue : onFalse)(x, i);
  183. });
  184. return r;
  185. };
  186. var filter = function (obj, pred) {
  187. var t = {};
  188. internalFilter(obj, pred, objAcc(t), noop);
  189. return t;
  190. };
  191. var has = function (obj, key) {
  192. return hasOwnProperty.call(obj, key);
  193. };
  194. var hasNonNullableKey = function (obj, key) {
  195. return has(obj, key) && obj[key] !== undefined && obj[key] !== null;
  196. };
  197. var nativePush = Array.prototype.push;
  198. var flatten = function (xs) {
  199. var r = [];
  200. for (var i = 0, len = xs.length; i < len; ++i) {
  201. if (!isArray(xs[i])) {
  202. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  203. }
  204. nativePush.apply(r, xs[i]);
  205. }
  206. return r;
  207. };
  208. var get = function (xs, i) {
  209. return i >= 0 && i < xs.length ? Optional.some(xs[i]) : Optional.none();
  210. };
  211. var head = function (xs) {
  212. return get(xs, 0);
  213. };
  214. var findMap = function (arr, f) {
  215. for (var i = 0; i < arr.length; i++) {
  216. var r = f(arr[i], i);
  217. if (r.isSome()) {
  218. return r;
  219. }
  220. }
  221. return Optional.none();
  222. };
  223. typeof window !== 'undefined' ? window : Function('return this;')();
  224. var rawSet = function (dom, key, value) {
  225. if (isString(value) || isBoolean(value) || isNumber(value)) {
  226. dom.setAttribute(key, value + '');
  227. } else {
  228. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  229. throw new Error('Attribute value was not simple');
  230. }
  231. };
  232. var set = function (element, key, value) {
  233. rawSet(element.dom, key, value);
  234. };
  235. var remove = function (element, key) {
  236. element.dom.removeAttribute(key);
  237. };
  238. var fromHtml = function (html, scope) {
  239. var doc = scope || document;
  240. var div = doc.createElement('div');
  241. div.innerHTML = html;
  242. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  243. console.error('HTML does not have a single root node', html);
  244. throw new Error('HTML must have a single root node');
  245. }
  246. return fromDom(div.childNodes[0]);
  247. };
  248. var fromTag = function (tag, scope) {
  249. var doc = scope || document;
  250. var node = doc.createElement(tag);
  251. return fromDom(node);
  252. };
  253. var fromText = function (text, scope) {
  254. var doc = scope || document;
  255. var node = doc.createTextNode(text);
  256. return fromDom(node);
  257. };
  258. var fromDom = function (node) {
  259. if (node === null || node === undefined) {
  260. throw new Error('Node cannot be null or undefined');
  261. }
  262. return { dom: node };
  263. };
  264. var fromPoint = function (docElm, x, y) {
  265. return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  266. };
  267. var SugarElement = {
  268. fromHtml: fromHtml,
  269. fromTag: fromTag,
  270. fromText: fromText,
  271. fromDom: fromDom,
  272. fromPoint: fromPoint
  273. };
  274. var global$5 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  275. var global$4 = tinymce.util.Tools.resolve('tinymce.util.Promise');
  276. var global$3 = tinymce.util.Tools.resolve('tinymce.util.URI');
  277. var global$2 = tinymce.util.Tools.resolve('tinymce.util.XHR');
  278. var hasDimensions = function (editor) {
  279. return editor.getParam('image_dimensions', true, 'boolean');
  280. };
  281. var hasAdvTab = function (editor) {
  282. return editor.getParam('image_advtab', false, 'boolean');
  283. };
  284. var hasUploadTab = function (editor) {
  285. return editor.getParam('image_uploadtab', true, 'boolean');
  286. };
  287. var getPrependUrl = function (editor) {
  288. return editor.getParam('image_prepend_url', '', 'string');
  289. };
  290. var getClassList = function (editor) {
  291. return editor.getParam('image_class_list');
  292. };
  293. var hasDescription = function (editor) {
  294. return editor.getParam('image_description', true, 'boolean');
  295. };
  296. var hasImageTitle = function (editor) {
  297. return editor.getParam('image_title', false, 'boolean');
  298. };
  299. var hasImageCaption = function (editor) {
  300. return editor.getParam('image_caption', false, 'boolean');
  301. };
  302. var getImageList = function (editor) {
  303. return editor.getParam('image_list', false);
  304. };
  305. var hasUploadUrl = function (editor) {
  306. return isNonNullable(editor.getParam('images_upload_url'));
  307. };
  308. var hasUploadHandler = function (editor) {
  309. return isNonNullable(editor.getParam('images_upload_handler'));
  310. };
  311. var showAccessibilityOptions = function (editor) {
  312. return editor.getParam('a11y_advanced_options', false, 'boolean');
  313. };
  314. var isAutomaticUploadsEnabled = function (editor) {
  315. return editor.getParam('automatic_uploads', true, 'boolean');
  316. };
  317. var parseIntAndGetMax = function (val1, val2) {
  318. return Math.max(parseInt(val1, 10), parseInt(val2, 10));
  319. };
  320. var getImageSize = function (url) {
  321. return new global$4(function (callback) {
  322. var img = document.createElement('img');
  323. var done = function (dimensions) {
  324. img.onload = img.onerror = null;
  325. if (img.parentNode) {
  326. img.parentNode.removeChild(img);
  327. }
  328. callback(dimensions);
  329. };
  330. img.onload = function () {
  331. var width = parseIntAndGetMax(img.width, img.clientWidth);
  332. var height = parseIntAndGetMax(img.height, img.clientHeight);
  333. var dimensions = {
  334. width: width,
  335. height: height
  336. };
  337. done(global$4.resolve(dimensions));
  338. };
  339. img.onerror = function () {
  340. done(global$4.reject('Failed to get image dimensions for: ' + url));
  341. };
  342. var style = img.style;
  343. style.visibility = 'hidden';
  344. style.position = 'fixed';
  345. style.bottom = style.left = '0px';
  346. style.width = style.height = 'auto';
  347. document.body.appendChild(img);
  348. img.src = url;
  349. });
  350. };
  351. var removePixelSuffix = function (value) {
  352. if (value) {
  353. value = value.replace(/px$/, '');
  354. }
  355. return value;
  356. };
  357. var addPixelSuffix = function (value) {
  358. if (value.length > 0 && /^[0-9]+$/.test(value)) {
  359. value += 'px';
  360. }
  361. return value;
  362. };
  363. var mergeMargins = function (css) {
  364. if (css.margin) {
  365. var splitMargin = String(css.margin).split(' ');
  366. switch (splitMargin.length) {
  367. case 1:
  368. css['margin-top'] = css['margin-top'] || splitMargin[0];
  369. css['margin-right'] = css['margin-right'] || splitMargin[0];
  370. css['margin-bottom'] = css['margin-bottom'] || splitMargin[0];
  371. css['margin-left'] = css['margin-left'] || splitMargin[0];
  372. break;
  373. case 2:
  374. css['margin-top'] = css['margin-top'] || splitMargin[0];
  375. css['margin-right'] = css['margin-right'] || splitMargin[1];
  376. css['margin-bottom'] = css['margin-bottom'] || splitMargin[0];
  377. css['margin-left'] = css['margin-left'] || splitMargin[1];
  378. break;
  379. case 3:
  380. css['margin-top'] = css['margin-top'] || splitMargin[0];
  381. css['margin-right'] = css['margin-right'] || splitMargin[1];
  382. css['margin-bottom'] = css['margin-bottom'] || splitMargin[2];
  383. css['margin-left'] = css['margin-left'] || splitMargin[1];
  384. break;
  385. case 4:
  386. css['margin-top'] = css['margin-top'] || splitMargin[0];
  387. css['margin-right'] = css['margin-right'] || splitMargin[1];
  388. css['margin-bottom'] = css['margin-bottom'] || splitMargin[2];
  389. css['margin-left'] = css['margin-left'] || splitMargin[3];
  390. }
  391. delete css.margin;
  392. }
  393. return css;
  394. };
  395. var createImageList = function (editor, callback) {
  396. var imageList = getImageList(editor);
  397. if (isString(imageList)) {
  398. global$2.send({
  399. url: imageList,
  400. success: function (text) {
  401. callback(JSON.parse(text));
  402. }
  403. });
  404. } else if (isFunction(imageList)) {
  405. imageList(callback);
  406. } else {
  407. callback(imageList);
  408. }
  409. };
  410. var waitLoadImage = function (editor, data, imgElm) {
  411. var selectImage = function () {
  412. imgElm.onload = imgElm.onerror = null;
  413. if (editor.selection) {
  414. editor.selection.select(imgElm);
  415. editor.nodeChanged();
  416. }
  417. };
  418. imgElm.onload = function () {
  419. if (!data.width && !data.height && hasDimensions(editor)) {
  420. editor.dom.setAttribs(imgElm, {
  421. width: String(imgElm.clientWidth),
  422. height: String(imgElm.clientHeight)
  423. });
  424. }
  425. selectImage();
  426. };
  427. imgElm.onerror = selectImage;
  428. };
  429. var blobToDataUri = function (blob) {
  430. return new global$4(function (resolve, reject) {
  431. var reader = new FileReader();
  432. reader.onload = function () {
  433. resolve(reader.result);
  434. };
  435. reader.onerror = function () {
  436. reject(reader.error.message);
  437. };
  438. reader.readAsDataURL(blob);
  439. });
  440. };
  441. var isPlaceholderImage = function (imgElm) {
  442. return imgElm.nodeName === 'IMG' && (imgElm.hasAttribute('data-mce-object') || imgElm.hasAttribute('data-mce-placeholder'));
  443. };
  444. var isSafeImageUrl = function (editor, src) {
  445. return global$3.isDomSafe(src, 'img', editor.settings);
  446. };
  447. var DOM = global$5.DOM;
  448. var getHspace = function (image) {
  449. if (image.style.marginLeft && image.style.marginRight && image.style.marginLeft === image.style.marginRight) {
  450. return removePixelSuffix(image.style.marginLeft);
  451. } else {
  452. return '';
  453. }
  454. };
  455. var getVspace = function (image) {
  456. if (image.style.marginTop && image.style.marginBottom && image.style.marginTop === image.style.marginBottom) {
  457. return removePixelSuffix(image.style.marginTop);
  458. } else {
  459. return '';
  460. }
  461. };
  462. var getBorder = function (image) {
  463. if (image.style.borderWidth) {
  464. return removePixelSuffix(image.style.borderWidth);
  465. } else {
  466. return '';
  467. }
  468. };
  469. var getAttrib = function (image, name) {
  470. if (image.hasAttribute(name)) {
  471. return image.getAttribute(name);
  472. } else {
  473. return '';
  474. }
  475. };
  476. var getStyle = function (image, name) {
  477. return image.style[name] ? image.style[name] : '';
  478. };
  479. var hasCaption = function (image) {
  480. return image.parentNode !== null && image.parentNode.nodeName === 'FIGURE';
  481. };
  482. var updateAttrib = function (image, name, value) {
  483. if (value === '') {
  484. image.removeAttribute(name);
  485. } else {
  486. image.setAttribute(name, value);
  487. }
  488. };
  489. var wrapInFigure = function (image) {
  490. var figureElm = DOM.create('figure', { class: 'image' });
  491. DOM.insertAfter(figureElm, image);
  492. figureElm.appendChild(image);
  493. figureElm.appendChild(DOM.create('figcaption', { contentEditable: 'true' }, 'Caption'));
  494. figureElm.contentEditable = 'false';
  495. };
  496. var removeFigure = function (image) {
  497. var figureElm = image.parentNode;
  498. DOM.insertAfter(image, figureElm);
  499. DOM.remove(figureElm);
  500. };
  501. var toggleCaption = function (image) {
  502. if (hasCaption(image)) {
  503. removeFigure(image);
  504. } else {
  505. wrapInFigure(image);
  506. }
  507. };
  508. var normalizeStyle = function (image, normalizeCss) {
  509. var attrValue = image.getAttribute('style');
  510. var value = normalizeCss(attrValue !== null ? attrValue : '');
  511. if (value.length > 0) {
  512. image.setAttribute('style', value);
  513. image.setAttribute('data-mce-style', value);
  514. } else {
  515. image.removeAttribute('style');
  516. }
  517. };
  518. var setSize = function (name, normalizeCss) {
  519. return function (image, name, value) {
  520. if (image.style[name]) {
  521. image.style[name] = addPixelSuffix(value);
  522. normalizeStyle(image, normalizeCss);
  523. } else {
  524. updateAttrib(image, name, value);
  525. }
  526. };
  527. };
  528. var getSize = function (image, name) {
  529. if (image.style[name]) {
  530. return removePixelSuffix(image.style[name]);
  531. } else {
  532. return getAttrib(image, name);
  533. }
  534. };
  535. var setHspace = function (image, value) {
  536. var pxValue = addPixelSuffix(value);
  537. image.style.marginLeft = pxValue;
  538. image.style.marginRight = pxValue;
  539. };
  540. var setVspace = function (image, value) {
  541. var pxValue = addPixelSuffix(value);
  542. image.style.marginTop = pxValue;
  543. image.style.marginBottom = pxValue;
  544. };
  545. var setBorder = function (image, value) {
  546. var pxValue = addPixelSuffix(value);
  547. image.style.borderWidth = pxValue;
  548. };
  549. var setBorderStyle = function (image, value) {
  550. image.style.borderStyle = value;
  551. };
  552. var getBorderStyle = function (image) {
  553. return getStyle(image, 'borderStyle');
  554. };
  555. var isFigure = function (elm) {
  556. return elm.nodeName === 'FIGURE';
  557. };
  558. var isImage = function (elm) {
  559. return elm.nodeName === 'IMG';
  560. };
  561. var getIsDecorative = function (image) {
  562. return DOM.getAttrib(image, 'alt').length === 0 && DOM.getAttrib(image, 'role') === 'presentation';
  563. };
  564. var getAlt = function (image) {
  565. if (getIsDecorative(image)) {
  566. return '';
  567. } else {
  568. return getAttrib(image, 'alt');
  569. }
  570. };
  571. var defaultData = function () {
  572. return {
  573. src: '',
  574. alt: '',
  575. title: '',
  576. width: '',
  577. height: '',
  578. class: '',
  579. style: '',
  580. caption: false,
  581. hspace: '',
  582. vspace: '',
  583. border: '',
  584. borderStyle: '',
  585. isDecorative: false
  586. };
  587. };
  588. var getStyleValue = function (normalizeCss, data) {
  589. var image = document.createElement('img');
  590. updateAttrib(image, 'style', data.style);
  591. if (getHspace(image) || data.hspace !== '') {
  592. setHspace(image, data.hspace);
  593. }
  594. if (getVspace(image) || data.vspace !== '') {
  595. setVspace(image, data.vspace);
  596. }
  597. if (getBorder(image) || data.border !== '') {
  598. setBorder(image, data.border);
  599. }
  600. if (getBorderStyle(image) || data.borderStyle !== '') {
  601. setBorderStyle(image, data.borderStyle);
  602. }
  603. return normalizeCss(image.getAttribute('style'));
  604. };
  605. var create = function (normalizeCss, data) {
  606. var image = document.createElement('img');
  607. write(normalizeCss, __assign(__assign({}, data), { caption: false }), image);
  608. setAlt(image, data.alt, data.isDecorative);
  609. if (data.caption) {
  610. var figure = DOM.create('figure', { class: 'image' });
  611. figure.appendChild(image);
  612. figure.appendChild(DOM.create('figcaption', { contentEditable: 'true' }, 'Caption'));
  613. figure.contentEditable = 'false';
  614. return figure;
  615. } else {
  616. return image;
  617. }
  618. };
  619. var read = function (normalizeCss, image) {
  620. return {
  621. src: getAttrib(image, 'src'),
  622. alt: getAlt(image),
  623. title: getAttrib(image, 'title'),
  624. width: getSize(image, 'width'),
  625. height: getSize(image, 'height'),
  626. class: getAttrib(image, 'class'),
  627. style: normalizeCss(getAttrib(image, 'style')),
  628. caption: hasCaption(image),
  629. hspace: getHspace(image),
  630. vspace: getVspace(image),
  631. border: getBorder(image),
  632. borderStyle: getStyle(image, 'borderStyle'),
  633. isDecorative: getIsDecorative(image)
  634. };
  635. };
  636. var updateProp = function (image, oldData, newData, name, set) {
  637. if (newData[name] !== oldData[name]) {
  638. set(image, name, newData[name]);
  639. }
  640. };
  641. var setAlt = function (image, alt, isDecorative) {
  642. if (isDecorative) {
  643. DOM.setAttrib(image, 'role', 'presentation');
  644. var sugarImage = SugarElement.fromDom(image);
  645. set(sugarImage, 'alt', '');
  646. } else {
  647. if (isNull(alt)) {
  648. var sugarImage = SugarElement.fromDom(image);
  649. remove(sugarImage, 'alt');
  650. } else {
  651. var sugarImage = SugarElement.fromDom(image);
  652. set(sugarImage, 'alt', alt);
  653. }
  654. if (DOM.getAttrib(image, 'role') === 'presentation') {
  655. DOM.setAttrib(image, 'role', '');
  656. }
  657. }
  658. };
  659. var updateAlt = function (image, oldData, newData) {
  660. if (newData.alt !== oldData.alt || newData.isDecorative !== oldData.isDecorative) {
  661. setAlt(image, newData.alt, newData.isDecorative);
  662. }
  663. };
  664. var normalized = function (set, normalizeCss) {
  665. return function (image, name, value) {
  666. set(image, value);
  667. normalizeStyle(image, normalizeCss);
  668. };
  669. };
  670. var write = function (normalizeCss, newData, image) {
  671. var oldData = read(normalizeCss, image);
  672. updateProp(image, oldData, newData, 'caption', function (image, _name, _value) {
  673. return toggleCaption(image);
  674. });
  675. updateProp(image, oldData, newData, 'src', updateAttrib);
  676. updateProp(image, oldData, newData, 'title', updateAttrib);
  677. updateProp(image, oldData, newData, 'width', setSize('width', normalizeCss));
  678. updateProp(image, oldData, newData, 'height', setSize('height', normalizeCss));
  679. updateProp(image, oldData, newData, 'class', updateAttrib);
  680. updateProp(image, oldData, newData, 'style', normalized(function (image, value) {
  681. return updateAttrib(image, 'style', value);
  682. }, normalizeCss));
  683. updateProp(image, oldData, newData, 'hspace', normalized(setHspace, normalizeCss));
  684. updateProp(image, oldData, newData, 'vspace', normalized(setVspace, normalizeCss));
  685. updateProp(image, oldData, newData, 'border', normalized(setBorder, normalizeCss));
  686. updateProp(image, oldData, newData, 'borderStyle', normalized(setBorderStyle, normalizeCss));
  687. updateAlt(image, oldData, newData);
  688. };
  689. var normalizeCss$1 = function (editor, cssText) {
  690. var css = editor.dom.styles.parse(cssText);
  691. var mergedCss = mergeMargins(css);
  692. var compressed = editor.dom.styles.parse(editor.dom.styles.serialize(mergedCss));
  693. return editor.dom.styles.serialize(compressed);
  694. };
  695. var getSelectedImage = function (editor) {
  696. var imgElm = editor.selection.getNode();
  697. var figureElm = editor.dom.getParent(imgElm, 'figure.image');
  698. if (figureElm) {
  699. return editor.dom.select('img', figureElm)[0];
  700. }
  701. if (imgElm && (imgElm.nodeName !== 'IMG' || isPlaceholderImage(imgElm))) {
  702. return null;
  703. }
  704. return imgElm;
  705. };
  706. var splitTextBlock = function (editor, figure) {
  707. var dom = editor.dom;
  708. var textBlockElements = filter(editor.schema.getTextBlockElements(), function (_, parentElm) {
  709. return !editor.schema.isValidChild(parentElm, 'figure');
  710. });
  711. var textBlock = dom.getParent(figure.parentNode, function (node) {
  712. return hasNonNullableKey(textBlockElements, node.nodeName);
  713. }, editor.getBody());
  714. if (textBlock) {
  715. return dom.split(textBlock, figure);
  716. } else {
  717. return figure;
  718. }
  719. };
  720. var readImageDataFromSelection = function (editor) {
  721. var image = getSelectedImage(editor);
  722. return image ? read(function (css) {
  723. return normalizeCss$1(editor, css);
  724. }, image) : defaultData();
  725. };
  726. var insertImageAtCaret = function (editor, data) {
  727. var elm = create(function (css) {
  728. return normalizeCss$1(editor, css);
  729. }, data);
  730. editor.dom.setAttrib(elm, 'data-mce-id', '__mcenew');
  731. editor.focus();
  732. editor.selection.setContent(elm.outerHTML);
  733. var insertedElm = editor.dom.select('*[data-mce-id="__mcenew"]')[0];
  734. editor.dom.setAttrib(insertedElm, 'data-mce-id', null);
  735. if (isFigure(insertedElm)) {
  736. var figure = splitTextBlock(editor, insertedElm);
  737. editor.selection.select(figure);
  738. } else {
  739. editor.selection.select(insertedElm);
  740. }
  741. };
  742. var syncSrcAttr = function (editor, image) {
  743. editor.dom.setAttrib(image, 'src', image.getAttribute('src'));
  744. };
  745. var deleteImage = function (editor, image) {
  746. if (image) {
  747. var elm = editor.dom.is(image.parentNode, 'figure.image') ? image.parentNode : image;
  748. editor.dom.remove(elm);
  749. editor.focus();
  750. editor.nodeChanged();
  751. if (editor.dom.isEmpty(editor.getBody())) {
  752. editor.setContent('');
  753. editor.selection.setCursorLocation();
  754. }
  755. }
  756. };
  757. var writeImageDataToSelection = function (editor, data) {
  758. var image = getSelectedImage(editor);
  759. write(function (css) {
  760. return normalizeCss$1(editor, css);
  761. }, data, image);
  762. syncSrcAttr(editor, image);
  763. if (isFigure(image.parentNode)) {
  764. var figure = image.parentNode;
  765. splitTextBlock(editor, figure);
  766. editor.selection.select(image.parentNode);
  767. } else {
  768. editor.selection.select(image);
  769. waitLoadImage(editor, data, image);
  770. }
  771. };
  772. var sanitizeImageData = function (editor, data) {
  773. var src = data.src;
  774. return __assign(__assign({}, data), { src: isSafeImageUrl(editor, src) ? src : '' });
  775. };
  776. var insertOrUpdateImage = function (editor, partialData) {
  777. var image = getSelectedImage(editor);
  778. if (image) {
  779. var selectedImageData = read(function (css) {
  780. return normalizeCss$1(editor, css);
  781. }, image);
  782. var data = __assign(__assign({}, selectedImageData), partialData);
  783. var sanitizedData = sanitizeImageData(editor, data);
  784. if (data.src) {
  785. writeImageDataToSelection(editor, sanitizedData);
  786. } else {
  787. deleteImage(editor, image);
  788. }
  789. } else if (partialData.src) {
  790. insertImageAtCaret(editor, __assign(__assign({}, defaultData()), partialData));
  791. }
  792. };
  793. var deep = function (old, nu) {
  794. var bothObjects = isObject(old) && isObject(nu);
  795. return bothObjects ? deepMerge(old, nu) : nu;
  796. };
  797. var baseMerge = function (merger) {
  798. return function () {
  799. var objects = [];
  800. for (var _i = 0; _i < arguments.length; _i++) {
  801. objects[_i] = arguments[_i];
  802. }
  803. if (objects.length === 0) {
  804. throw new Error('Can\'t merge zero objects');
  805. }
  806. var ret = {};
  807. for (var j = 0; j < objects.length; j++) {
  808. var curObject = objects[j];
  809. for (var key in curObject) {
  810. if (has(curObject, key)) {
  811. ret[key] = merger(ret[key], curObject[key]);
  812. }
  813. }
  814. }
  815. return ret;
  816. };
  817. };
  818. var deepMerge = baseMerge(deep);
  819. var isNotEmpty = function (s) {
  820. return s.length > 0;
  821. };
  822. var global$1 = tinymce.util.Tools.resolve('tinymce.util.ImageUploader');
  823. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  824. var getValue = function (item) {
  825. return isString(item.value) ? item.value : '';
  826. };
  827. var getText = function (item) {
  828. if (isString(item.text)) {
  829. return item.text;
  830. } else if (isString(item.title)) {
  831. return item.title;
  832. } else {
  833. return '';
  834. }
  835. };
  836. var sanitizeList = function (list, extractValue) {
  837. var out = [];
  838. global.each(list, function (item) {
  839. var text = getText(item);
  840. if (item.menu !== undefined) {
  841. var items = sanitizeList(item.menu, extractValue);
  842. out.push({
  843. text: text,
  844. items: items
  845. });
  846. } else {
  847. var value = extractValue(item);
  848. out.push({
  849. text: text,
  850. value: value
  851. });
  852. }
  853. });
  854. return out;
  855. };
  856. var sanitizer = function (extractor) {
  857. if (extractor === void 0) {
  858. extractor = getValue;
  859. }
  860. return function (list) {
  861. if (list) {
  862. return Optional.from(list).map(function (list) {
  863. return sanitizeList(list, extractor);
  864. });
  865. } else {
  866. return Optional.none();
  867. }
  868. };
  869. };
  870. var sanitize = function (list) {
  871. return sanitizer(getValue)(list);
  872. };
  873. var isGroup = function (item) {
  874. return has(item, 'items');
  875. };
  876. var findEntryDelegate = function (list, value) {
  877. return findMap(list, function (item) {
  878. if (isGroup(item)) {
  879. return findEntryDelegate(item.items, value);
  880. } else if (item.value === value) {
  881. return Optional.some(item);
  882. } else {
  883. return Optional.none();
  884. }
  885. });
  886. };
  887. var findEntry = function (optList, value) {
  888. return optList.bind(function (list) {
  889. return findEntryDelegate(list, value);
  890. });
  891. };
  892. var ListUtils = {
  893. sanitizer: sanitizer,
  894. sanitize: sanitize,
  895. findEntry: findEntry
  896. };
  897. var makeTab$2 = function (_info) {
  898. return {
  899. title: 'Advanced',
  900. name: 'advanced',
  901. items: [
  902. {
  903. type: 'input',
  904. label: 'Style',
  905. name: 'style'
  906. },
  907. {
  908. type: 'grid',
  909. columns: 2,
  910. items: [
  911. {
  912. type: 'input',
  913. label: 'Vertical space',
  914. name: 'vspace',
  915. inputMode: 'numeric'
  916. },
  917. {
  918. type: 'input',
  919. label: 'Horizontal space',
  920. name: 'hspace',
  921. inputMode: 'numeric'
  922. },
  923. {
  924. type: 'input',
  925. label: 'Border width',
  926. name: 'border',
  927. inputMode: 'numeric'
  928. },
  929. {
  930. type: 'listbox',
  931. name: 'borderstyle',
  932. label: 'Border style',
  933. items: [
  934. {
  935. text: 'Select...',
  936. value: ''
  937. },
  938. {
  939. text: 'Solid',
  940. value: 'solid'
  941. },
  942. {
  943. text: 'Dotted',
  944. value: 'dotted'
  945. },
  946. {
  947. text: 'Dashed',
  948. value: 'dashed'
  949. },
  950. {
  951. text: 'Double',
  952. value: 'double'
  953. },
  954. {
  955. text: 'Groove',
  956. value: 'groove'
  957. },
  958. {
  959. text: 'Ridge',
  960. value: 'ridge'
  961. },
  962. {
  963. text: 'Inset',
  964. value: 'inset'
  965. },
  966. {
  967. text: 'Outset',
  968. value: 'outset'
  969. },
  970. {
  971. text: 'None',
  972. value: 'none'
  973. },
  974. {
  975. text: 'Hidden',
  976. value: 'hidden'
  977. }
  978. ]
  979. }
  980. ]
  981. }
  982. ]
  983. };
  984. };
  985. var AdvTab = { makeTab: makeTab$2 };
  986. var collect = function (editor) {
  987. var urlListSanitizer = ListUtils.sanitizer(function (item) {
  988. return editor.convertURL(item.value || item.url, 'src');
  989. });
  990. var futureImageList = new global$4(function (completer) {
  991. createImageList(editor, function (imageList) {
  992. completer(urlListSanitizer(imageList).map(function (items) {
  993. return flatten([
  994. [{
  995. text: 'None',
  996. value: ''
  997. }],
  998. items
  999. ]);
  1000. }));
  1001. });
  1002. });
  1003. var classList = ListUtils.sanitize(getClassList(editor));
  1004. var hasAdvTab$1 = hasAdvTab(editor);
  1005. var hasUploadTab$1 = hasUploadTab(editor);
  1006. var hasUploadUrl$1 = hasUploadUrl(editor);
  1007. var hasUploadHandler$1 = hasUploadHandler(editor);
  1008. var image = readImageDataFromSelection(editor);
  1009. var hasDescription$1 = hasDescription(editor);
  1010. var hasImageTitle$1 = hasImageTitle(editor);
  1011. var hasDimensions$1 = hasDimensions(editor);
  1012. var hasImageCaption$1 = hasImageCaption(editor);
  1013. var hasAccessibilityOptions = showAccessibilityOptions(editor);
  1014. var automaticUploads = isAutomaticUploadsEnabled(editor);
  1015. var prependURL = Optional.some(getPrependUrl(editor)).filter(function (preUrl) {
  1016. return isString(preUrl) && preUrl.length > 0;
  1017. });
  1018. return futureImageList.then(function (imageList) {
  1019. return {
  1020. image: image,
  1021. imageList: imageList,
  1022. classList: classList,
  1023. hasAdvTab: hasAdvTab$1,
  1024. hasUploadTab: hasUploadTab$1,
  1025. hasUploadUrl: hasUploadUrl$1,
  1026. hasUploadHandler: hasUploadHandler$1,
  1027. hasDescription: hasDescription$1,
  1028. hasImageTitle: hasImageTitle$1,
  1029. hasDimensions: hasDimensions$1,
  1030. hasImageCaption: hasImageCaption$1,
  1031. prependURL: prependURL,
  1032. hasAccessibilityOptions: hasAccessibilityOptions,
  1033. automaticUploads: automaticUploads
  1034. };
  1035. });
  1036. };
  1037. var makeItems = function (info) {
  1038. var imageUrl = {
  1039. name: 'src',
  1040. type: 'urlinput',
  1041. filetype: 'image',
  1042. label: 'Source'
  1043. };
  1044. var imageList = info.imageList.map(function (items) {
  1045. return {
  1046. name: 'images',
  1047. type: 'listbox',
  1048. label: 'Image list',
  1049. items: items
  1050. };
  1051. });
  1052. var imageDescription = {
  1053. name: 'alt',
  1054. type: 'input',
  1055. label: 'Alternative description',
  1056. disabled: info.hasAccessibilityOptions && info.image.isDecorative
  1057. };
  1058. var imageTitle = {
  1059. name: 'title',
  1060. type: 'input',
  1061. label: 'Image title'
  1062. };
  1063. var imageDimensions = {
  1064. name: 'dimensions',
  1065. type: 'sizeinput'
  1066. };
  1067. var isDecorative = {
  1068. type: 'label',
  1069. label: 'Accessibility',
  1070. items: [{
  1071. name: 'isDecorative',
  1072. type: 'checkbox',
  1073. label: 'Image is decorative'
  1074. }]
  1075. };
  1076. var classList = info.classList.map(function (items) {
  1077. return {
  1078. name: 'classes',
  1079. type: 'listbox',
  1080. label: 'Class',
  1081. items: items
  1082. };
  1083. });
  1084. var caption = {
  1085. type: 'label',
  1086. label: 'Caption',
  1087. items: [{
  1088. type: 'checkbox',
  1089. name: 'caption',
  1090. label: 'Show caption'
  1091. }]
  1092. };
  1093. var getDialogContainerType = function (useColumns) {
  1094. return useColumns ? {
  1095. type: 'grid',
  1096. columns: 2
  1097. } : { type: 'panel' };
  1098. };
  1099. return flatten([
  1100. [imageUrl],
  1101. imageList.toArray(),
  1102. info.hasAccessibilityOptions && info.hasDescription ? [isDecorative] : [],
  1103. info.hasDescription ? [imageDescription] : [],
  1104. info.hasImageTitle ? [imageTitle] : [],
  1105. info.hasDimensions ? [imageDimensions] : [],
  1106. [__assign(__assign({}, getDialogContainerType(info.classList.isSome() && info.hasImageCaption)), {
  1107. items: flatten([
  1108. classList.toArray(),
  1109. info.hasImageCaption ? [caption] : []
  1110. ])
  1111. })]
  1112. ]);
  1113. };
  1114. var makeTab$1 = function (info) {
  1115. return {
  1116. title: 'General',
  1117. name: 'general',
  1118. items: makeItems(info)
  1119. };
  1120. };
  1121. var MainTab = {
  1122. makeTab: makeTab$1,
  1123. makeItems: makeItems
  1124. };
  1125. var makeTab = function (_info) {
  1126. var items = [{
  1127. type: 'dropzone',
  1128. name: 'fileinput'
  1129. }];
  1130. return {
  1131. title: 'Upload',
  1132. name: 'upload',
  1133. items: items
  1134. };
  1135. };
  1136. var UploadTab = { makeTab: makeTab };
  1137. var createState = function (info) {
  1138. return {
  1139. prevImage: ListUtils.findEntry(info.imageList, info.image.src),
  1140. prevAlt: info.image.alt,
  1141. open: true
  1142. };
  1143. };
  1144. var fromImageData = function (image) {
  1145. return {
  1146. src: {
  1147. value: image.src,
  1148. meta: {}
  1149. },
  1150. images: image.src,
  1151. alt: image.alt,
  1152. title: image.title,
  1153. dimensions: {
  1154. width: image.width,
  1155. height: image.height
  1156. },
  1157. classes: image.class,
  1158. caption: image.caption,
  1159. style: image.style,
  1160. vspace: image.vspace,
  1161. border: image.border,
  1162. hspace: image.hspace,
  1163. borderstyle: image.borderStyle,
  1164. fileinput: [],
  1165. isDecorative: image.isDecorative
  1166. };
  1167. };
  1168. var toImageData = function (data, removeEmptyAlt) {
  1169. return {
  1170. src: data.src.value,
  1171. alt: data.alt.length === 0 && removeEmptyAlt ? null : data.alt,
  1172. title: data.title,
  1173. width: data.dimensions.width,
  1174. height: data.dimensions.height,
  1175. class: data.classes,
  1176. style: data.style,
  1177. caption: data.caption,
  1178. hspace: data.hspace,
  1179. vspace: data.vspace,
  1180. border: data.border,
  1181. borderStyle: data.borderstyle,
  1182. isDecorative: data.isDecorative
  1183. };
  1184. };
  1185. var addPrependUrl2 = function (info, srcURL) {
  1186. if (!/^(?:[a-zA-Z]+:)?\/\//.test(srcURL)) {
  1187. return info.prependURL.bind(function (prependUrl) {
  1188. if (srcURL.substring(0, prependUrl.length) !== prependUrl) {
  1189. return Optional.some(prependUrl + srcURL);
  1190. }
  1191. return Optional.none();
  1192. });
  1193. }
  1194. return Optional.none();
  1195. };
  1196. var addPrependUrl = function (info, api) {
  1197. var data = api.getData();
  1198. addPrependUrl2(info, data.src.value).each(function (srcURL) {
  1199. api.setData({
  1200. src: {
  1201. value: srcURL,
  1202. meta: data.src.meta
  1203. }
  1204. });
  1205. });
  1206. };
  1207. var formFillFromMeta2 = function (info, data, meta) {
  1208. if (info.hasDescription && isString(meta.alt)) {
  1209. data.alt = meta.alt;
  1210. }
  1211. if (info.hasAccessibilityOptions) {
  1212. data.isDecorative = meta.isDecorative || data.isDecorative || false;
  1213. }
  1214. if (info.hasImageTitle && isString(meta.title)) {
  1215. data.title = meta.title;
  1216. }
  1217. if (info.hasDimensions) {
  1218. if (isString(meta.width)) {
  1219. data.dimensions.width = meta.width;
  1220. }
  1221. if (isString(meta.height)) {
  1222. data.dimensions.height = meta.height;
  1223. }
  1224. }
  1225. if (isString(meta.class)) {
  1226. ListUtils.findEntry(info.classList, meta.class).each(function (entry) {
  1227. data.classes = entry.value;
  1228. });
  1229. }
  1230. if (info.hasImageCaption) {
  1231. if (isBoolean(meta.caption)) {
  1232. data.caption = meta.caption;
  1233. }
  1234. }
  1235. if (info.hasAdvTab) {
  1236. if (isString(meta.style)) {
  1237. data.style = meta.style;
  1238. }
  1239. if (isString(meta.vspace)) {
  1240. data.vspace = meta.vspace;
  1241. }
  1242. if (isString(meta.border)) {
  1243. data.border = meta.border;
  1244. }
  1245. if (isString(meta.hspace)) {
  1246. data.hspace = meta.hspace;
  1247. }
  1248. if (isString(meta.borderstyle)) {
  1249. data.borderstyle = meta.borderstyle;
  1250. }
  1251. }
  1252. };
  1253. var formFillFromMeta = function (info, api) {
  1254. var data = api.getData();
  1255. var meta = data.src.meta;
  1256. if (meta !== undefined) {
  1257. var newData = deepMerge({}, data);
  1258. formFillFromMeta2(info, newData, meta);
  1259. api.setData(newData);
  1260. }
  1261. };
  1262. var calculateImageSize = function (helpers, info, state, api) {
  1263. var data = api.getData();
  1264. var url = data.src.value;
  1265. var meta = data.src.meta || {};
  1266. if (!meta.width && !meta.height && info.hasDimensions) {
  1267. if (isNotEmpty(url)) {
  1268. helpers.imageSize(url).then(function (size) {
  1269. if (state.open) {
  1270. api.setData({ dimensions: size });
  1271. }
  1272. }).catch(function (e) {
  1273. return console.error(e);
  1274. });
  1275. } else {
  1276. api.setData({
  1277. dimensions: {
  1278. width: '',
  1279. height: ''
  1280. }
  1281. });
  1282. }
  1283. }
  1284. };
  1285. var updateImagesDropdown = function (info, state, api) {
  1286. var data = api.getData();
  1287. var image = ListUtils.findEntry(info.imageList, data.src.value);
  1288. state.prevImage = image;
  1289. api.setData({
  1290. images: image.map(function (entry) {
  1291. return entry.value;
  1292. }).getOr('')
  1293. });
  1294. };
  1295. var changeSrc = function (helpers, info, state, api) {
  1296. addPrependUrl(info, api);
  1297. formFillFromMeta(info, api);
  1298. calculateImageSize(helpers, info, state, api);
  1299. updateImagesDropdown(info, state, api);
  1300. };
  1301. var changeImages = function (helpers, info, state, api) {
  1302. var data = api.getData();
  1303. var image = ListUtils.findEntry(info.imageList, data.images);
  1304. image.each(function (img) {
  1305. var updateAlt = data.alt === '' || state.prevImage.map(function (image) {
  1306. return image.text === data.alt;
  1307. }).getOr(false);
  1308. if (updateAlt) {
  1309. if (img.value === '') {
  1310. api.setData({
  1311. src: img,
  1312. alt: state.prevAlt
  1313. });
  1314. } else {
  1315. api.setData({
  1316. src: img,
  1317. alt: img.text
  1318. });
  1319. }
  1320. } else {
  1321. api.setData({ src: img });
  1322. }
  1323. });
  1324. state.prevImage = image;
  1325. changeSrc(helpers, info, state, api);
  1326. };
  1327. var calcVSpace = function (css) {
  1328. var matchingTopBottom = css['margin-top'] && css['margin-bottom'] && css['margin-top'] === css['margin-bottom'];
  1329. return matchingTopBottom ? removePixelSuffix(String(css['margin-top'])) : '';
  1330. };
  1331. var calcHSpace = function (css) {
  1332. var matchingLeftRight = css['margin-right'] && css['margin-left'] && css['margin-right'] === css['margin-left'];
  1333. return matchingLeftRight ? removePixelSuffix(String(css['margin-right'])) : '';
  1334. };
  1335. var calcBorderWidth = function (css) {
  1336. return css['border-width'] ? removePixelSuffix(String(css['border-width'])) : '';
  1337. };
  1338. var calcBorderStyle = function (css) {
  1339. return css['border-style'] ? String(css['border-style']) : '';
  1340. };
  1341. var calcStyle = function (parseStyle, serializeStyle, css) {
  1342. return serializeStyle(parseStyle(serializeStyle(css)));
  1343. };
  1344. var changeStyle2 = function (parseStyle, serializeStyle, data) {
  1345. var css = mergeMargins(parseStyle(data.style));
  1346. var dataCopy = deepMerge({}, data);
  1347. dataCopy.vspace = calcVSpace(css);
  1348. dataCopy.hspace = calcHSpace(css);
  1349. dataCopy.border = calcBorderWidth(css);
  1350. dataCopy.borderstyle = calcBorderStyle(css);
  1351. dataCopy.style = calcStyle(parseStyle, serializeStyle, css);
  1352. return dataCopy;
  1353. };
  1354. var changeStyle = function (helpers, api) {
  1355. var data = api.getData();
  1356. var newData = changeStyle2(helpers.parseStyle, helpers.serializeStyle, data);
  1357. api.setData(newData);
  1358. };
  1359. var changeAStyle = function (helpers, info, api) {
  1360. var data = deepMerge(fromImageData(info.image), api.getData());
  1361. var style = getStyleValue(helpers.normalizeCss, toImageData(data, false));
  1362. api.setData({ style: style });
  1363. };
  1364. var changeFileInput = function (helpers, info, state, api) {
  1365. var data = api.getData();
  1366. api.block('Uploading image');
  1367. head(data.fileinput).fold(function () {
  1368. api.unblock();
  1369. }, function (file) {
  1370. var blobUri = URL.createObjectURL(file);
  1371. var finalize = function () {
  1372. api.unblock();
  1373. URL.revokeObjectURL(blobUri);
  1374. };
  1375. var updateSrcAndSwitchTab = function (url) {
  1376. api.setData({
  1377. src: {
  1378. value: url,
  1379. meta: {}
  1380. }
  1381. });
  1382. api.showTab('general');
  1383. changeSrc(helpers, info, state, api);
  1384. };
  1385. blobToDataUri(file).then(function (dataUrl) {
  1386. var blobInfo = helpers.createBlobCache(file, blobUri, dataUrl);
  1387. if (info.automaticUploads) {
  1388. helpers.uploadImage(blobInfo).then(function (result) {
  1389. updateSrcAndSwitchTab(result.url);
  1390. finalize();
  1391. }).catch(function (err) {
  1392. finalize();
  1393. helpers.alertErr(err);
  1394. });
  1395. } else {
  1396. helpers.addToBlobCache(blobInfo);
  1397. updateSrcAndSwitchTab(blobInfo.blobUri());
  1398. api.unblock();
  1399. }
  1400. });
  1401. });
  1402. };
  1403. var changeHandler = function (helpers, info, state) {
  1404. return function (api, evt) {
  1405. if (evt.name === 'src') {
  1406. changeSrc(helpers, info, state, api);
  1407. } else if (evt.name === 'images') {
  1408. changeImages(helpers, info, state, api);
  1409. } else if (evt.name === 'alt') {
  1410. state.prevAlt = api.getData().alt;
  1411. } else if (evt.name === 'style') {
  1412. changeStyle(helpers, api);
  1413. } else if (evt.name === 'vspace' || evt.name === 'hspace' || evt.name === 'border' || evt.name === 'borderstyle') {
  1414. changeAStyle(helpers, info, api);
  1415. } else if (evt.name === 'fileinput') {
  1416. changeFileInput(helpers, info, state, api);
  1417. } else if (evt.name === 'isDecorative') {
  1418. if (api.getData().isDecorative) {
  1419. api.disable('alt');
  1420. } else {
  1421. api.enable('alt');
  1422. }
  1423. }
  1424. };
  1425. };
  1426. var closeHandler = function (state) {
  1427. return function () {
  1428. state.open = false;
  1429. };
  1430. };
  1431. var makeDialogBody = function (info) {
  1432. if (info.hasAdvTab || info.hasUploadUrl || info.hasUploadHandler) {
  1433. var tabPanel = {
  1434. type: 'tabpanel',
  1435. tabs: flatten([
  1436. [MainTab.makeTab(info)],
  1437. info.hasAdvTab ? [AdvTab.makeTab(info)] : [],
  1438. info.hasUploadTab && (info.hasUploadUrl || info.hasUploadHandler) ? [UploadTab.makeTab(info)] : []
  1439. ])
  1440. };
  1441. return tabPanel;
  1442. } else {
  1443. var panel = {
  1444. type: 'panel',
  1445. items: MainTab.makeItems(info)
  1446. };
  1447. return panel;
  1448. }
  1449. };
  1450. var makeDialog = function (helpers) {
  1451. return function (info) {
  1452. var state = createState(info);
  1453. return {
  1454. title: 'Insert/Edit Image',
  1455. size: 'normal',
  1456. body: makeDialogBody(info),
  1457. buttons: [
  1458. {
  1459. type: 'cancel',
  1460. name: 'cancel',
  1461. text: 'Cancel'
  1462. },
  1463. {
  1464. type: 'submit',
  1465. name: 'save',
  1466. text: 'Save',
  1467. primary: true
  1468. }
  1469. ],
  1470. initialData: fromImageData(info.image),
  1471. onSubmit: helpers.onSubmit(info),
  1472. onChange: changeHandler(helpers, info, state),
  1473. onClose: closeHandler(state)
  1474. };
  1475. };
  1476. };
  1477. var submitHandler = function (editor) {
  1478. return function (info) {
  1479. return function (api) {
  1480. var data = deepMerge(fromImageData(info.image), api.getData());
  1481. editor.execCommand('mceUpdateImage', false, toImageData(data, info.hasAccessibilityOptions));
  1482. editor.editorUpload.uploadImagesAuto();
  1483. api.close();
  1484. };
  1485. };
  1486. };
  1487. var imageSize = function (editor) {
  1488. return function (url) {
  1489. if (!isSafeImageUrl(editor, url)) {
  1490. return global$4.resolve({
  1491. width: '',
  1492. height: ''
  1493. });
  1494. } else {
  1495. return getImageSize(editor.documentBaseURI.toAbsolute(url)).then(function (dimensions) {
  1496. return {
  1497. width: String(dimensions.width),
  1498. height: String(dimensions.height)
  1499. };
  1500. });
  1501. }
  1502. };
  1503. };
  1504. var createBlobCache = function (editor) {
  1505. return function (file, blobUri, dataUrl) {
  1506. return editor.editorUpload.blobCache.create({
  1507. blob: file,
  1508. blobUri: blobUri,
  1509. name: file.name ? file.name.replace(/\.[^\.]+$/, '') : null,
  1510. filename: file.name,
  1511. base64: dataUrl.split(',')[1]
  1512. });
  1513. };
  1514. };
  1515. var addToBlobCache = function (editor) {
  1516. return function (blobInfo) {
  1517. editor.editorUpload.blobCache.add(blobInfo);
  1518. };
  1519. };
  1520. var alertErr = function (editor) {
  1521. return function (message) {
  1522. editor.windowManager.alert(message);
  1523. };
  1524. };
  1525. var normalizeCss = function (editor) {
  1526. return function (cssText) {
  1527. return normalizeCss$1(editor, cssText);
  1528. };
  1529. };
  1530. var parseStyle = function (editor) {
  1531. return function (cssText) {
  1532. return editor.dom.parseStyle(cssText);
  1533. };
  1534. };
  1535. var serializeStyle = function (editor) {
  1536. return function (stylesArg, name) {
  1537. return editor.dom.serializeStyle(stylesArg, name);
  1538. };
  1539. };
  1540. var uploadImage = function (editor) {
  1541. return function (blobInfo) {
  1542. return global$1(editor).upload([blobInfo], false).then(function (results) {
  1543. if (results.length === 0) {
  1544. return global$4.reject('Failed to upload image');
  1545. } else if (results[0].status === false) {
  1546. return global$4.reject(results[0].error.message);
  1547. } else {
  1548. return results[0];
  1549. }
  1550. });
  1551. };
  1552. };
  1553. var Dialog = function (editor) {
  1554. var helpers = {
  1555. onSubmit: submitHandler(editor),
  1556. imageSize: imageSize(editor),
  1557. addToBlobCache: addToBlobCache(editor),
  1558. createBlobCache: createBlobCache(editor),
  1559. alertErr: alertErr(editor),
  1560. normalizeCss: normalizeCss(editor),
  1561. parseStyle: parseStyle(editor),
  1562. serializeStyle: serializeStyle(editor),
  1563. uploadImage: uploadImage(editor)
  1564. };
  1565. var open = function () {
  1566. collect(editor).then(makeDialog(helpers)).then(editor.windowManager.open);
  1567. };
  1568. return { open: open };
  1569. };
  1570. var register$1 = function (editor) {
  1571. editor.addCommand('mceImage', Dialog(editor).open);
  1572. editor.addCommand('mceUpdateImage', function (_ui, data) {
  1573. editor.undoManager.transact(function () {
  1574. return insertOrUpdateImage(editor, data);
  1575. });
  1576. });
  1577. };
  1578. var hasImageClass = function (node) {
  1579. var className = node.attr('class');
  1580. return className && /\bimage\b/.test(className);
  1581. };
  1582. var toggleContentEditableState = function (state) {
  1583. return function (nodes) {
  1584. var i = nodes.length;
  1585. var toggleContentEditable = function (node) {
  1586. node.attr('contenteditable', state ? 'true' : null);
  1587. };
  1588. while (i--) {
  1589. var node = nodes[i];
  1590. if (hasImageClass(node)) {
  1591. node.attr('contenteditable', state ? 'false' : null);
  1592. global.each(node.getAll('figcaption'), toggleContentEditable);
  1593. }
  1594. }
  1595. };
  1596. };
  1597. var setup = function (editor) {
  1598. editor.on('PreInit', function () {
  1599. editor.parser.addNodeFilter('figure', toggleContentEditableState(true));
  1600. editor.serializer.addNodeFilter('figure', toggleContentEditableState(false));
  1601. });
  1602. };
  1603. var register = function (editor) {
  1604. editor.ui.registry.addToggleButton('image', {
  1605. icon: 'image',
  1606. tooltip: 'Insert/edit image',
  1607. onAction: Dialog(editor).open,
  1608. onSetup: function (buttonApi) {
  1609. buttonApi.setActive(isNonNullable(getSelectedImage(editor)));
  1610. return editor.selection.selectorChangedWithUnbind('img:not([data-mce-object],[data-mce-placeholder]),figure.image', buttonApi.setActive).unbind;
  1611. }
  1612. });
  1613. editor.ui.registry.addMenuItem('image', {
  1614. icon: 'image',
  1615. text: 'Image...',
  1616. onAction: Dialog(editor).open
  1617. });
  1618. editor.ui.registry.addContextMenu('image', {
  1619. update: function (element) {
  1620. return isFigure(element) || isImage(element) && !isPlaceholderImage(element) ? ['image'] : [];
  1621. }
  1622. });
  1623. };
  1624. function Plugin () {
  1625. global$6.add('image', function (editor) {
  1626. setup(editor);
  1627. register(editor);
  1628. register$1(editor);
  1629. });
  1630. }
  1631. Plugin();
  1632. }());