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