plugin.js 47 KB

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