You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

9 lines
18 KiB
JavaScript

/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*
* Version: 5.9.2 (2021-09-08)
*/
!function(){"use strict";function c(t){var e=t;return{get:function(){return e},set:function(t){e=t}}}function h(t){return null!=t}function r(){}function i(t){return function(){return t}}function t(t){return t}function e(){return d}function s(t){return n=typeof(e=t),"array"==(null===e?"null":"object"==n&&(Array.prototype.isPrototypeOf(e)||e.constructor&&"Array"===e.constructor.name)?"array":"object"==n&&(String.prototype.isPrototypeOf(e)||e.constructor&&"String"===e.constructor.name)?"string":n);var e,n}var n,o=tinymce.util.Tools.resolve("tinymce.PluginManager"),l=tinymce.util.Tools.resolve("tinymce.util.Tools"),a=function(t){return typeof t===n},u=i(!(n="function")),f=i(!0),d={fold:function(t,e){return t()},isSome:u,isNone:f,getOr:t,getOrThunk:m,getOrDie:function(t){throw new Error(t||"error: getOrDie called on none.")},getOrNull:i(null),getOrUndefined:i(void 0),or:t,orThunk:m,map:e,each:r,bind:e,exists:u,forall:f,filter:function(){return d},toArray:function(){return[]},toString:i("none()")};function m(t){return t()}function g(t,e){return _(document.createElement("canvas"),t,e)}function p(t){var e=g(t.width,t.height);return A(e).drawImage(t,0,0),e}var v,y,w,b,E,I=function(n){function t(){return o}function e(t){return t(n)}var r=i(n),o={fold:function(t,e){return e(n)},isSome:f,isNone:u,getOr:r,getOrThunk:r,getOrDie:r,getOrNull:r,getOrUndefined:r,or:t,orThunk:t,map:function(t){return I(t(n))},each:function(t){t(n)},bind:e,exists:e,forall:e,filter:function(t){return t(n)?o:d},toArray:function(){return[n]},toString:function(){return"some("+n+")"}};return o},T={some:I,none:e,from:function(t){return null==t?d:I(t)}},A=function(t){return t.getContext("2d")},_=function(t,e,n){return t.width=e,t.height=n,t},R=window.Promise||(v=window,y=x.immediateFn||"function"==typeof v.setImmediate&&v.setImmediate||function(t){return setTimeout(t,1)},w=function(n,r){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return n.apply(r,t)}},b=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)},E=function(t,e,n){var r=!1;try{t(function(t){r||(r=!0,e(t))},function(t){r||(r=!0,n(t))})}catch(t){if(r)return;r=!0,n(t)}},x.prototype.catch=function(t){return this.then(null,t)},x.prototype.then=function(n,r){var o=this;return new x(function(t,e){U.call(o,new O(n,r,t,e))})},x.all=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var u=Array.prototype.slice.call(1===t.length&&b(t[0])?t[0]:t);return new x(function(r,o){if(0===u.length)return r([]);for(var i=u.length,a=function(e,t){try{if(t&&("object"==typeof t||"function"==typeof t)){var n=t.then;if("function"==typeof n)return void n.call(t,function(t){a(e,t)},o)}u[e]=t,0==--i&&r(u)}catch(t){o(t)}},t=0;t<u.length;t++)a(t,u[t])})},x.resolve=function(e){return e&&"object"==typeof e&&e.constructor===x?e:new x(function(t){t(e)})},x.reject=function(n){return new x(function(t,e){e(n)})},x.race=function(o){return new x(function(t,e){for(var n=0,r=o;n<r.length;n++)r[n].then(t,e)})},x);function x(t){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof t)throw new TypeError("not a function");this._state=null,this._value=null,this._deferreds=[],E(t,w(j,this),w(S,this))}function U(n){var r=this;null!==this._state?y(function(){var t,e=r._state?n.onFulfilled:n.onRejected;if(null!==e){try{t=e(r._value)}catch(t){return void n.reject(t)}n.resolve(t)}else(r._state?n.resolve:n.reject)(r._value)}):this._deferreds.push(n)}function j(t){try{if(t===this)throw new TypeError("A promise cannot be resolved with itself.");if(t&&("object"==typeof t||"function"==typeof t)){var e=t.then;if("function"==typeof e)return void E(w(e,t),w(j,this),w(S,this))}this._state=!0,this._value=t,M.call(this)}catch(t){S.call(this,t)}}function S(t){this._state=!1,this._value=t,M.call(this)}function M(){for(var t=0,e=this._deferreds;t<e.length;t++){var n=e[t];U.call(this,n)}this._deferreds=[]}function O(t,e,n,r){this.onFulfilled="function"==typeof t?t:null,this.onRejected="function"==typeof e?e:null,this.resolve=n,this.reject=r}function L(u){return new R(function(t,e){function n(){o.removeEventListener("load",i),o.removeEventListener("error",a)}var r=URL.createObjectURL(u),o=new Image,i=function(){n(),t(o)},a=function(){n(),e("Unable to load data of type "+u.type+": "+r)};o.addEventListener("load",i),o.addEventListener("error",a),o.src=r,o.complete&&setTimeout(i,0)})}function k(t,r,o){return r=r||"image/png",a(HTMLCanvasElement.prototype.toBlob)?new R(function(e,n){t.toBlob(function(t){t?e(t):n()},r,o)}):N(t.toDataURL(r,o))}function P(t,e){for(var n=0,r=t.length;n<r;n++)e(t[n],n)}function C(t,e){return function(t,e,n){for(var r=0,o=t.length;r<o;r++){var i=t[r];if(e(i,r))return T.some(i);if(n(i,r))break}return T.none()}(t,e,u)}var B=function(r){return new R(function(t,e){var n=new XMLHttpRequest;n.open("GET",r,!0),n.responseType="blob",n.onload=function(){200===this.status&&t(this.response)},n.onerror=function(){var t;e(0===this.status?((t=new Error("No access to download image")).code=18,t.name="SecurityError",t):new Error("Error "+this.status+" downloading image"))},n.send()})},N=function(h){return new R(function(t,e){!function(){var t=h.split(","),e=/data:([^;]+)/.exec(t[0]);if(!e)return T.none();for(var n=e[1],r=t[1],o=atob(r),i=o.length,a=Math.ceil(i/1024),u=new Array(a),c=0;c<a;++c){for(var s=1024*c,l=Math.min(1024+s,i),f=new Array(l-s),d=s,m=0;d<l;++m,++d)f[m]=o[d].charCodeAt(0);u[c]=new Uint8Array(f)}return T.some(new Blob(u,{type:n}))}().fold(function(){e("uri is not base64: "+h)},t)})},D=function(t){URL.revokeObjectURL(t.src)},F=L,H=Array.prototype.indexOf,z=Object.keys;function q(t,e,n){function r(e,n){return t.then(function(t){return t.toDataURL(e||"image/png",n)})}return{getType:i(e.type),toBlob:function(){return R.resolve(e)},toDataURL:i(n),toBase64:function(){return n.split(",")[1]},toAdjustedBlob:function(e,n){return t.then(function(t){return k(t,e,n)})},toAdjustedDataURL:r,toAdjustedBase64:function(t,e){return r(t,e).then(function(t){return t.split(",")[1]})},toCanvas:function(){return t.then(p)}}}function $(e,t){return k(e,t).then(function(t){return q(R.resolve(e),t,e.toDataURL())})}function W(t,e){void 0===e&&(e=2);var n=Math.pow(10,e),r=Math.round(t*n);return Math.ceil(r/n)}function V(e,n,r){return void 0===r&&(r=!1),new R(function(t){var a=new XMLHttpRequest;a.onreadystatechange=function(){4===a.readyState&&t({status:a.status,blob:a.response})},a.open("GET",e,!0),a.withCredentials=r,function(t){for(var e,n=z(t),r=0,o=n.length;r<o;r++){var i=n[r];e=t[i],a.setRequestHeader(i,e)}}(n),a.responseType="blob",a.send()})}function G(t){var e,n=(e=t,"ImageProxy HTTP error: "+C(rt,function(t){return e===t.code}).fold(i("Unknown ImageProxy error"),function(t){return t.message}));return R.reject(n)}function J(e){return C(ot,function(t){return t.type===e}).fold(i("Unknown service error"),function(t){return t.message})}function K(t){return"ImageProxy Service error: "+function(t){try{return T.some(JSON.parse(t))}catch(t){return T.none()}}(t).bind(function(t){return n=function(t,e){return h(t)?t[e]:void 0},r=t,P(["error","type"],function(t,e){r=n(r,t)}),e=r,T.from(e).map(J);var n,r,e}).getOr("Invalid JSON in service error message")}function X(t){return t<200||300<=t}function Q(t,e){var n,r,o,i={"Content-Type":"application/json;charset=UTF-8","tiny-api-key":e};return V((r=e,o=-1===(n=t).indexOf("?")?"?":"&",/[?&]apiKey=/.test(n)?n:n+o+"apiKey="+encodeURIComponent(r)),i).then(function(t){return X(t.status)?(e=t.status,"application/json"!==(null==(n=t.blob)?void 0:n.type)||400!==e&&403!==e&&404!==e&&500!==e?G(e):(r=n,new R(function(t,e){var n=new FileReader;n.onload=function(){t(n.result)},n.onerror=function(t){e(t)},n.readAsText(r)}).then(function(t){var e=K(t);return R.reject(e)}))):R.resolve(t.blob);var r,e,n})}function Y(t,e,n){return void 0===n&&(n=!1),e?Q(t,e):V(t,{},n).then(function(t){return X(t.status)?G(t.status):R.resolve(t.blob)})}function Z(e){return n=e,new R(function(t){var e=new FileReader;e.onloadend=function(){t(e.result)},e.readAsDataURL(n)}).then(function(t){return q(L(e).then(function(t){D(t);var e=g(t.naturalWidth||t.width,t.naturalHeight||t.height);return A(e).drawImage(t,0,0),e}),e,t)});var n}!function(a){if(!s(a))throw new Error("cases must be an array");if(0===a.length)throw new Error("there must be at least one case");var u=[],n={};P(a,function(t,r){var e=z(t);if(1!==e.length)throw new Error("one and only one name per case");var i=e[0],o=t[i];if(void 0!==n[i])throw new Error("duplicate key detected:"+i);if("cata"===i)throw new Error("cannot have a case named cata (sorry)");if(!s(o))throw new Error("case arguments must be an array");u.push(i),n[i]=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=n.length;if(e!==o.length)throw new Error("Wrong number of arguments to case "+i+". Expected "+o.length+" ("+o+"), got "+e);return{fold:function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(t.length!==a.length)throw new Error("Wrong number of arguments to fold. Expected "+a.length+", got "+t.length);return t[r].apply(null,n)},match:function(t){var o=z(t);if(u.length!==o.length)throw new Error("Wrong number of arguments to match. Expected: "+u.join(",")+"\nActual: "+o.join(","));if(!function(t){for(var e,n=0,r=t.length;n<r;++n)if(!0!==(e=t[n],function(t,e){return-1<H.call(t,e)}(o,e)))return;return 1}(u))throw new Error("Not all branches were specified when using match. Specified: "+o.join(", ")+"\nRequired: "+u.join(", "));return t[i].apply(null,n)},log:function(t){console.log(t,{constructors:u,constructor:i,params:n})}}}})}([{bothErrors:["error1","error2"]},{firstError:["error1","value2"]},{secondError:["value1","error2"]},{bothValues:["value1","value2"]}]);function tt(t){if(null==t)throw new Error("Node cannot be null or undefined");return{dom:t}}var et=function(t,e,n){var r=(n<0?360+n:n)*Math.PI/180,o=t.width,i=t.height,a=Math.sin(r),u=Math.cos(r),c=W(Math.abs(o*u)+Math.abs(i*a)),s=W(Math.abs(o*a)+Math.abs(i*u)),l=g(c,s),f=A(l);return f.translate(c/2,s/2),f.rotate(r),f.drawImage(t,-o/2,-i/2),$(l,e)},nt=function(t,e,n){var r=g(t.width,t.height),o=A(r);return"v"===n?(o.scale(1,-1),o.drawImage(t,0,-r.height)):(o.scale(-1,1),o.drawImage(t,-r.width,0)),$(r,e)},rt=[{code:404,message:"Could not find Image Proxy"},{code:403,message:"Rejected request"},{code:0,message:"Incorrect Image Proxy URL"}],ot=[{type:"not_found",message:"Failed to load image."},{type:"key_missing",message:"The request did not include an api key."},{type:"key_not_found",message:"The provided api key could not be found."},{type:"domain_not_trusted",message:"The api key is not valid for the request origins."}],it={fromHtml:function(t,e){var n=(e||document).createElement("div");if(n.innerHTML=t,!n.hasChildNodes()||1<n.childNodes.length)throw console.error("HTML does not have a single root node",t),new Error("HTML must have a single root node");return tt(n.childNodes[0])},fromTag:function(t,e){var n=(e||document).createElement(t);return tt(n)},fromText:function(t,e){var n=(e||document).createTextNode(t);return tt(n)},fromDom:tt,fromPoint:function(t,e,n){return T.from(t.dom.elementFromPoint(e,n)).map(tt)}};function at(t){return t.getParam("imagetools_proxy")}function ut(t){function e(t){return/^[0-9\.]+px$/.test(t)}var n=t.style.width,r=t.style.height;return n||r?e(n)&&e(r)?{w:parseInt(n,10),h:parseInt(r,10)}:null:(n=t.width,r=t.height,n&&r?{w:parseInt(n,10),h:parseInt(r,10)}:null)}function ct(t){return{w:t.naturalWidth,h:t.naturalHeight}}function st(t){return e=it.fromDom(t),n="img",r=function(r){return function(t){var e=r.dom;if(1!==e.nodeType)return!1;var n=e;if(void 0!==n.matches)return n.matches(t);if(void 0!==n.msMatchesSelector)return n.msMatchesSelector(t);if(void 0!==n.webkitMatchesSelector)return n.webkitMatchesSelector(t);if(void 0!==n.mozMatchesSelector)return n.mozMatchesSelector(t);throw new Error("Browser lacks native selectors")}(n)},C(e.dom.childNodes,function(t){return r(it.fromDom(t))}).map(it.fromDom);var e,n,r}function lt(t,e){return t.dom.is(e,"figure")}function ft(t,e){return t.dom.is(e,"img:not([data-mce-object],[data-mce-placeholder])")}function dt(e,t){function n(t){return ft(e,t)&&(jt(e,t)||St(e,t)||h(at(e)))}return lt(e,t)?st(t).bind(function(t){return n(t.dom)?T.some(t.dom):T.none()}):n(t)?T.some(t):T.none()}function mt(t,e){t.notificationManager.open({text:e,type:"error"})}function ht(t){var e=t.selection.getNode(),n=t.dom.getParent(e,"figure.image");return null!==n&&lt(t,n)?st(n):ft(t,e)?T.some(it.fromDom(e)):T.none()}function gt(t,e,n){var r=e.match(/(?:\/|^)(([^\/\?]+)\.(?:[a-z0-9.]+))(?:\?|$)/i);return h(r)?t.dom.encode(r[n]):null}function pt(t,e){if(St(t,e))return Y(e.src,null,(n=e,-1!==l.inArray(t.getParam("imagetools_credentials_hosts",[],"string[]"),new xt(n.src).host)));var n,r;if(jt(t,e))return(0===(r=e.src).indexOf("data:")?N:B)(r);var o=at(t);return Y(o+(-1===o.indexOf("?")?"?":"&")+"url="+encodeURIComponent(e.src),t.getParam("api_key",t.getParam("imagetools_api_key","","string"),"string"),!1)}function vt(t,e){var n,r,o=t.editorUpload.blobCache.getByUri(e.src);return o?Rt.resolve(o.blob()):(n=t,r=e,T.from(n.getParam("imagetools_fetch_image",null,"function")).fold(function(){return pt(n,r)},function(t){return t(r)}))}function yt(t){_t.clearTimeout(t.get())}function wt(u,c,s,l,f,d,m){return s.toBlob().then(function(t){var e,n,o,r=u.editorUpload.blobCache,i=d.src,a=c.type===t.type;return u.getParam("images_reuse_filename",!1,"boolean")&&(n=h(o=r.getByUri(i))?(i=o.uri(),e=o.name(),o.filename()):(e=gt(u,i,2),gt(u,i,1))),o=r.create({id:"imagetools"+Ut++,blob:t,base64:s.toBase64(),uri:i,name:e,filename:a?n:void 0}),r.add(o),u.undoManager.transact(function(){var r=function(){var t,e,n;u.$(d).off("load",r),u.nodeChanged(),l?u.editorUpload.uploadImagesAuto():(yt(f),t=u,e=f,n=_t.setEditorTimeout(t,function(){t.editorUpload.uploadImagesAuto()},t.getParam("images_upload_timeout",3e4,"number")),e.set(n))};u.$(d).on("load",r),m&&u.$(d).attr({width:m.w,height:m.h}),u.$(d).attr({src:o.blobUri()}).removeAttr("data-mce-src")}),o})}function bt(r,o,t,i){return function(){return ht(r).fold(function(){mt(r,"Could not find selected image")},function(n){return r._scanForImages().then(function(){return vt(r,n.dom)}).then(function(e){return Z(e).then(t).then(function(t){return wt(r,e,t,!1,o,n.dom,i)})}).catch(function(t){mt(r,t)})})}}function Et(e,n,r){return function(){var t=ht(e).map(function(t){var e=ut(t.dom);return e?{w:e.h,h:e.w}:null}).getOrNull();return bt(e,n,function(t){return n=r,(e=t).toCanvas().then(function(t){return et(t,e.getType(),n)});var e,n},t)()}}function It(t,e,r){return function(){return bt(t,e,function(t){return n=r,(e=t).toCanvas().then(function(t){return nt(t,e.getType(),n)});var e,n})()}}function Tt(s,l){return function(){var n=ht(s),i=n.map(function(t){return ct(t.dom)});n.each(function(e){dt(s,e.dom).each(function(t){vt(s,e.dom).then(function(t){var e={blob:t,url:URL.createObjectURL(t)};s.windowManager.open({title:"Edit Image",size:"large",body:{type:"panel",items:[{type:"imagetools",name:"imagetools",label:"Edit Image",currentState:e}]},buttons:[{type:"cancel",name:"cancel",text:"Cancel"},{type:"submit",name:"save",text:"Save",primary:!0,disabled:!0}],onSubmit:function(t){var o=t.getData().imagetools.blob;n.each(function(r){i.each(function(t){var e,n,a,u,c;e=s,n=l,a=r.dom,u=t,F(c=o).then(function(t){var e,n,r,o,i=ct(t);return u.w===i.w&&u.h===i.h||ut(a)&&(e=a,(n=i)&&(r=e.style.width,o=e.style.height,(r||o)&&(e.style.width=n.w+"px",e.style.height=n.h+"px",e.removeAttribute("data-mce-style")),r=e.width,o=e.height,(r||o)&&(e.setAttribute("width",String(n.w)),e.setAttribute("height",String(n.h))))),URL.revokeObjectURL(t.src),c}).then(Z).then(function(t){return wt(e,c,t,!0,n,a)})})}),t.close()},onCancel:r,onAction:function(t,e){switch(e.name){case"save-state":e.value?t.enable("save"):t.disable("save");break;case"disable":t.disable("save"),t.disable("cancel");break;case"enable":t.enable("cancel")}}})})})})}}function At(e){function n(t){return function(){return e.execCommand(t)}}function t(){return ht(e).exists(function(t){return dt(e,t.dom).isSome()})}function r(e){function i(t){return e.setDisabled(!t)}return i(t()),o=o.concat([i]),function(){o=function(t){for(var e=[],n=0,r=t.length;n<r;n++){var o=t[n];o!==i&&e.push(o)}return e}(o)}}var o=[];e.on("NodeChange",function(){var e=t();P(o,function(t){return t(e)})}),e.ui.registry.addButton("rotateleft",{tooltip:"Rotate counterclockwise",icon:"rotate-left",onAction:n("mceImageRotateLeft"),onSetup:r}),e.ui.registry.addButton("rotateright",{tooltip:"Rotate clockwise",icon:"rotate-right",onAction:n("mceImageRotateRight"),onSetup:r}),e.ui.registry.addButton("flipv",{tooltip:"Flip vertically",icon:"flip-vertically",onAction:n("mceImageFlipVertical"),onSetup:r}),e.ui.registry.addButton("fliph",{tooltip:"Flip horizontally",icon:"flip-horizontally",onAction:n("mceImageFlipHorizontal"),onSetup:r}),e.ui.registry.addButton("editimage",{tooltip:"Edit image",icon:"edit-image",onAction:n("mceEditImage"),onSetup:r}),e.ui.registry.addButton("imageoptions",{tooltip:"Image options",icon:"image",onAction:n("mceImage")}),e.ui.registry.addContextMenu("imagetools",{update:function(t){return dt(e,t).map(function(t){return{text:"Edit image",icon:"edit-image",onAction:n("mceEditImage")}}).toArray()}})}"undefined"!=typeof window||Function("return this;")();var _t=tinymce.util.Tools.resolve("tinymce.util.Delay"),Rt=tinymce.util.Tools.resolve("tinymce.util.Promise"),xt=tinymce.util.Tools.resolve("tinymce.util.URI"),Ut=0,jt=function(t,e){var n=e.src;return 0===n.indexOf("data:")||0===n.indexOf("blob:")||new xt(n).host===t.documentBaseURI.host},St=function(t,e){return-1!==l.inArray(t.getParam("imagetools_cors_hosts",[],"string[]"),new xt(e.src).host)};o.add("imagetools",function(t){var e,r,o,i,n=c(0),a=c(null),u=t;l.each({mceImageRotateLeft:Et(u,n,-90),mceImageRotateRight:Et(u,n,90),mceImageFlipVertical:It(u,n,"v"),mceImageFlipHorizontal:It(u,n,"h"),mceEditImage:Tt(u,n)},function(t,e){u.addCommand(e,t)}),At(t),(e=t).ui.registry.addContextToolbar("imagetools",{items:e.getParam("imagetools_toolbar","rotateleft rotateright flipv fliph editimage imageoptions"),predicate:function(t){return dt(e,t).isSome()},position:"node",scope:"node"}),o=n,i=a,(r=t).on("NodeChange",function(t){var e=i.get(),n=dt(r,t.element);e&&!n.exists(function(t){return e.src===t.src})&&(yt(o),r.editorUpload.uploadImagesAuto(),i.set(null)),n.each(i.set)})})}();