plugin.js 52 KB

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