plugin.js 52 KB

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