tinymce-plugin
Version:
tinymce-plugin
44 lines • 17.5 kB
JavaScript
/**
* imagetools (Enhancement 1.1v)
* The tinymce-plugins is used to import imagetools (Enhancement)
*
* https://github.com/Five-great/tinymce-plugins
*
* Copyright 2020, Five(Li Hailong) The Chengdu, China https://www.fivecc.cn/
*
* Licensed under MIT
*/
(function(){var P=function(a){var b=a;return{get:function(){return b},set:function(c){b=c}}},ra=tinymce.util.Tools.resolve("tinymce.PluginManager"),H=tinymce.util.Tools.resolve("tinymce.util.Tools"),Q=function(){},v=function(a){return function(){return a}},w=v(!1),I=v(!0),B=function(){return J},J=function(){var a=function(d){return d.isNone()},b=function(d){return d()},c=function(d){return d};return{fold:function(d,e){return d()},is:w,isSome:w,isNone:I,getOr:c,getOrThunk:b,getOrDie:function(d){throw Error(d||
"error: getOrDie called on none.");},getOrNull:v(null),getOrUndefined:v(void 0),or:c,orThunk:b,map:B,each:Q,bind:B,exists:w,forall:I,filter:B,equals:a,equals_:a,toArray:function(){return[]},toString:v("none()")}}(),K=function(a){var b=v(a),c=function(){return e},d=function(f){return f(a)},e={fold:function(f,h){return h(a)},is:function(f){return a===f},isSome:I,isNone:w,getOr:b,getOrThunk:b,getOrDie:b,getOrNull:b,getOrUndefined:b,or:c,orThunk:c,map:function(f){return K(f(a))},each:function(f){f(a)},
bind:d,exists:d,forall:d,filter:function(f){return f(a)?e:J},toArray:function(){return[a]},toString:function(){return"some("+a+")"},equals:function(f){return f.is(a)},equals_:function(f,h){return f.fold(w,function(k){return h(a,k)})}};return e},q={some:K,none:B,from:function(a){return null===a||void 0===a?J:K(a)}},C=function(a){return null===a||void 0===a},sa=function(a){return function(b){return typeof b===a}}("function"),D=function(a,b){var c=document.createElement("canvas");c.width=a;c.height=
b;return c},ta=function(a){var b=D(a.width,a.height);b.getContext("2d").drawImage(a,0,0);return b},va=function(){function a(g){var l=this;null===this._state?this._deferreds.push(g):k(function(){var m=l._state?g.onFulfilled:g.onRejected;if(null===m)(l._state?g.resolve:g.reject)(l._value);else{try{var n=m(l._value)}catch(u){g.reject(u);return}g.resolve(n)}})}function b(g){try{if(g===this)throw new TypeError("A promise cannot be resolved with itself.");if(g&&("object"===typeof g||"function"===typeof g)){var l=
g.then;if("function"===typeof l){E(p(l,g),p(b,this),p(c,this));return}}this._state=!0;this._value=g;d.call(this)}catch(m){c.call(this,m)}}function c(g){this._state=!1;this._value=g;d.call(this)}function d(){for(var g=0,l=this._deferreds;g<l.length;g++)a.call(this,l[g]);this._deferreds=[]}function e(g,l,m,n){this.onFulfilled="function"===typeof g?g:null;this.onRejected="function"===typeof l?l:null;this.resolve=m;this.reject=n}var f=function(g){if("object"!==typeof this)throw new TypeError("Promises must be constructed via new");
if("function"!==typeof g)throw new TypeError("not a function");this._value=this._state=null;this._deferreds=[];E(g,p(b,this),p(c,this))},h=window,k=f.immediateFn||"function"===typeof h.setImmediate&&h.setImmediate||function(g){return setTimeout(g,1)},p=function(g,l){return function(){for(var m=[],n=0;n<arguments.length;n++)m[n]=arguments[n];return g.apply(l,m)}},r=Array.isArray||function(g){return"[object Array]"===Object.prototype.toString.call(g)},E=function(g,l,m){var n=!1;try{g(function(u){n||
(n=!0,l(u))},function(u){n||(n=!0,m(u))})}catch(u){n||(n=!0,m(u))}};f.prototype.catch=function(g){return this.then(null,g)};f.prototype.then=function(g,l){var m=this;return new f(function(n,u){a.call(m,new e(g,l,n,u))})};f.all=function(){for(var g=[],l=0;l<arguments.length;l++)g[l]=arguments[l];var m=Array.prototype.slice.call(1===g.length&&r(g[0])?g[0]:g);return new f(function(n,u){if(0===m.length)return n([]);for(var ua=m.length,T=function(R,x){try{if(x&&("object"===typeof x||"function"===typeof x)){var S=
x.then;if("function"===typeof S){S.call(x,function(L){T(R,L)},u);return}}m[R]=x;0===--ua&&n(m)}catch(L){u(L)}},F=0;F<m.length;F++)T(F,m[F])})};f.resolve=function(g){return g&&"object"===typeof g&&g.constructor===f?g:new f(function(l){l(g)})};f.reject=function(g){return new f(function(l,m){m(g)})};f.race=function(g){return new f(function(l,m){for(var n=0;n<g.length;n++)g[n].then(l,m)})};return f},t=window.Promise?window.Promise:va(),U=function(a){return new t(function(b,c){var d=URL.createObjectURL(a),
e=new Image,f=function(){e.removeEventListener("load",h);e.removeEventListener("error",k)},h=function(){f();b(e)},k=function(){f();c("Unable to load data of type "+a.type+": "+d)};e.addEventListener("load",h);e.addEventListener("error",k);e.src=d;e.complete&&setTimeout(h,0)})},wa=function(a){return new t(function(b,c){var d=new XMLHttpRequest;d.open("GET",a,!0);d.responseType="blob";d.onload=function(){200===this.status&&b(this.response)};d.onerror=function(){if(0===this.status){var e=Error("No access to download image");
e.code=18;e.name="SecurityError"}else e=Error("Error "+this.status+" downloading image");c(e)};d.send()})},xa=function(a){var b=a.split(",");a=/data:([^;]+)/.exec(b[0]);if(!a)return q.none();a=a[1];b=atob(b[1]);for(var c=b.length,d=Math.ceil(c/1024),e=Array(d),f=0;f<d;++f){for(var h=1024*f,k=Math.min(h+1024,c),p=Array(k-h),r=0;h<k;++r,++h)p[r]=b[h].charCodeAt(0);e[f]=new Uint8Array(p)}return q.some(new Blob(e,{type:a}))},V=function(a){return new t(function(b,c){xa(a).fold(function(){c("uri is not base64: "+
a)},b)})},W=function(a,b,c){b=b||"image/png";return sa(HTMLCanvasElement.prototype.toBlob)?new t(function(d,e){a.toBlob(function(f){f?d(f):e()},b,c)}):V(a.toDataURL(b,c))},ya=function(a){return U(a).then(function(b){URL.revokeObjectURL(b.src);var c=D(b.naturalWidth||b.width,b.naturalHeight||b.height);c.getContext("2d").drawImage(b,0,0);return c})},za=function(a){return new t(function(b){var c=new FileReader;c.onloadend=function(){b(c.result)};c.readAsDataURL(a)})},Aa=function(a,b){for(var c=0,d=a.length;c<
d;c++)b(a[c],c)},Ba=function(a,b,c){Aa(a,function(d){c=b(c,d)});return c},M=function(a,b){a:{for(var c=0,d=a.length;c<d;c++){var e=a[c];if(b(e,c)){a=q.some(e);break a}else if(w(e,c))break}a=q.none()}return a},X=function(a,b,c){var d=v(b.type),e=v(c),f=function(h,k){return a.then(function(p){return p.toDataURL(h||"image/png",k)})};return{getType:d,toBlob:function(){return t.resolve(b)},toDataURL:e,toBase64:function(){return c.split(",")[1]},toAdjustedBlob:function(h,k){return a.then(function(p){return W(p,
h,k)})},toAdjustedDataURL:f,toAdjustedBase64:function(h,k){return f(h,k).then(function(p){return p.split(",")[1]})},toCanvas:function(){return a.then(ta)}}},Y=function(a){return za(a).then(function(b){return X(ya(a),a,b)})},Z=function(a,b){return W(a,b).then(function(c){return X(t.resolve(a),c,a.toDataURL())})},Ca=function(a,b){return a.toCanvas().then(function(c){var d=a.getType(),e=b,f=D(c.width,c.height),h=f.getContext("2d"),k=0,p=0;e=0>e?360+e:e;if(90===e||270===e){var r=f.width;f.width=f.height;
f.height=r}if(90===e||180===e)k=f.width;if(270===e||180===e)p=f.height;h.translate(k,p);h.rotate(e*Math.PI/180);h.drawImage(c,0,0);return Z(f,d)})},Da=function(a,b){return a.toCanvas().then(function(c){var d=a.getType(),e=D(c.width,c.height),f=e.getContext("2d");"v"===b?(f.scale(1,-1),f.drawImage(c,0,-e.height)):(f.scale(-1,1),f.drawImage(c,-e.width,0));return Z(e,d)})},Ea=Object.keys,Fa=function(a,b){for(var c=Ea(a),d=0,e=c.length;d<e;d++){var f=c[d];b(a[f],f)}},aa=function(a,b,c){void 0===c&&(c=
!1);return new t(function(d){var e=new XMLHttpRequest;e.onreadystatechange=function(){4===e.readyState&&d({status:e.status,blob:e.response})};e.open("GET",a,!0);e.withCredentials=c;Fa(b,function(f,h){e.setRequestHeader(h,f)});e.responseType="blob";e.send()})},Ga=function(a){return new t(function(b,c){var d=new FileReader;d.onload=function(){b(d.result)};d.onerror=function(e){c(e)};d.readAsText(a)})},Ha=[{code:404,message:"Could not find Image Proxy"},{code:403,message:"Rejected request"},{code:0,
message:"Incorrect Image Proxy URL"}],Ia=[{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."}],Ja=function(a,b){a=Ba(b,function(c,d){return C(c)?void 0:c[d]},a);return q.from(a)},Ka=function(a){return"ImageProxy HTTP error: "+M(Ha,function(b){return a===b.code}).fold(v("Unknown ImageProxy error"),
function(b){return b.message})},ba=function(a){a=Ka(a);return t.reject(a)},La=function(a){return M(Ia,function(b){return b.type===a}).fold(v("Unknown service error"),function(b){return b.message})},Ma=function(a){try{var b=q.some(JSON.parse(a))}catch(c){b=q.none()}return"ImageProxy Service error: "+b.bind(function(c){return Ja(c,["error","type"]).map(La)}).getOr("Invalid JSON in service error message")},Na=function(a){return Ga(a).then(function(b){b=Ma(b);return t.reject(b)})},Oa=function(a,b){var c=
-1===a.indexOf("?")?"?":"&";return/[?&]apiKey=/.test(a)?a:a+c+"apiKey="+encodeURIComponent(b)},ca=function(a){return 200>a||300<=a},Pa=function(a,b){var c={"Content-Type":"application/json;charset=UTF-8","tiny-api-key":b};return aa(Oa(a,b),c).then(function(d){if(ca(d.status)){var e=d.status;d=d.blob;e="application/json"!==(null===d||void 0===d?void 0:d.type)||400!==e&&403!==e&&404!==e&&500!==e?ba(e):Na(d)}else e=t.resolve(d.blob);return e})},Qa=function(a,b){return aa(a,{},b).then(function(c){return ca(c.status)?
ba(c.status):t.resolve(c.blob)})},da=function(a,b,c){void 0===c&&(c=!1);return b?Pa(a,b):Qa(a,c)},Ra=function(a){return Y(a)},y=function(a){if(null===a||void 0===a)throw Error("Node cannot be null or undefined");return{dom:a}},z={fromHtml:function(a,b){b=(b||document).createElement("div");b.innerHTML=a;if(!b.hasChildNodes()||1<b.childNodes.length)throw console.error("HTML does not have a single root node",a),Error("HTML must have a single root node");return y(b.childNodes[0])},fromTag:function(a,
b){a=(b||document).createElement(a);return y(a)},fromText:function(a,b){a=(b||document).createTextNode(a);return y(a)},fromDom:y,fromPoint:function(a,b,c){return q.from(a.dom.elementFromPoint(b,c)).map(y)}},Sa=function(a,b){a=a.dom;if(1!==a.nodeType)return!1;if(void 0!==a.matches)return a.matches(b);if(void 0!==a.msMatchesSelector)return a.msMatchesSelector(b);if(void 0!==a.webkitMatchesSelector)return a.webkitMatchesSelector(b);if(void 0!==a.mozMatchesSelector)return a.mozMatchesSelector(b);throw Error("Browser lacks native selectors");
};"undefined"!==typeof window||Function("return this;")();var Ta=function(a,b){return M(a.dom.childNodes,function(c){return b(z.fromDom(c))}).map(z.fromDom)},ea=function(a,b){return Ta(a,function(c){return Sa(c,b)})},N=tinymce.util.Tools.resolve("tinymce.util.Delay"),Ua=tinymce.util.Tools.resolve("tinymce.util.Promise"),O=tinymce.util.Tools.resolve("tinymce.util.URI"),fa=function(a){return a.getParam("imagetools_isEditorAll",!0,"boolean")},ha=function(a){var b=a.style.width;var c=a.style.height;if(b||
c)return/^[0-9\.]+px$/.test(b)&&/^[0-9\.]+px$/.test(c)?{w:parseInt(b,10),h:parseInt(c,10)}:null;b=a.width;c=a.height;return b&&c?{w:parseInt(b,10),h:parseInt(c,10)}:null},Va=0,ia=function(a,b){return a.dom.is(b,"img:not([data-mce-object],[data-mce-placeholder])")},A=function(a,b){var c=function(d){return ia(a,d)&&(ja(a,d)||ka(a,d)||!C(a.getParam("imagetools_proxy")||fa(a)))};return a.dom.is(b,"figure")?ea(z.fromDom(b),"img").bind(function(d){return c(d.dom)?q.some(d.dom):q.none()}):c(b)?q.some(b):
q.none()},G=function(a){var b=a.selection.getNode(),c=a.dom.getParent(b,"figure.image");return null!==c&&a.dom.is(c,"figure")?ea(z.fromDom(c),"img"):ia(a,b)?q.some(z.fromDom(b)):q.none()},la=function(a,b,c){b=b.match(/(?:\/|^)(([^\/\?]+)\.(?:[a-z0-9.]+))(?:\?|$)/i);return C(b)?null:a.dom.encode(b[c])},ja=function(a,b){b=b.src;return 0===b.indexOf("data:")||0===b.indexOf("blob:")||(new O(b)).host===a.documentBaseURI.host},ka=function(a,b){return-1!==H.inArray(a.getParam("imagetools_cors_hosts",[],
"string[]"),(new O(b.src)).host)},Wa=function(a,b){return q.from(a.getParam("imagetools_fetch_image",null,"function")).fold(function(){if(ka(a,b)){var c=b.src;var d=-1!==H.inArray(a.getParam("imagetools_credentials_hosts",[],"string[]"),(new O(b.src)).host);c=da(c,null,d)}else ja(a,b)||fa(a)?(c=b.src,c=0===c.indexOf("data:")?V(c):wa(c)):(c=a.getParam("imagetools_proxy"),c=c+(-1===c.indexOf("?")?"?":"&")+"url="+encodeURIComponent(b.src),d=a.getParam("api_key",a.getParam("imagetools_api_key","","string"),
"string"),c=da(c,d,!1));return c},function(c){return c(b)})},ma=function(a,b){var c=a.editorUpload.blobCache.getByUri(b.src);return c?Ua.resolve(c.blob()):Wa(a,b)},Xa=function(a,b){var c=N.setEditorTimeout(a,function(){a.editorUpload.uploadImagesAuto()},a.getParam("images_upload_timeout",3E4,"number"));b.set(c)},na=function(a,b,c,d,e,f,h){return c.toBlob().then(function(k){var p=a.editorUpload.blobCache;var r=f.src;var E=b.type===k.type;if(a.getParam("images_reuse_filename",!1,"boolean")){var g=p.getByUri(r);
if(C(g)){var l=la(a,r,2);var m=la(a,r,1)}else r=g.uri(),l=g.name(),m=g.filename()}g=p.create({id:"imagetools"+Va++,blob:k,base64:c.toBase64(),uri:r,name:l,filename:E?m:void 0});p.add(g);a.undoManager.transact(function(){var n=function(){a.$(f).off("load",n);a.nodeChanged();d?a.editorUpload.uploadImagesAuto():(N.clearTimeout(e.get()),Xa(a,e))};a.$(f).on("load",n);h&&a.$(f).attr({width:h.w,height:h.h});a.$(f).attr({src:g.blobUri()}).removeAttr("data-mce-src")});return g})},oa=function(a,b,c,d){return function(){return G(a).fold(function(){a.notificationManager.open({text:"Could not find selected image",
type:"error"})},function(e){return a._scanForImages().then(function(){return ma(a,e.dom)}).then(function(f){return Y(f).then(c).then(function(h){return na(a,f,h,!1,b,e.dom,d)})}).catch(function(f){a.notificationManager.open({text:f,type:"error"})})})}},pa=function(a,b,c){return function(){var d=G(a).fold(function(){return null},function(e){return(e=ha(e.dom))?{w:e.h,h:e.w}:null});return oa(a,b,function(e){return Ca(e,c)},d)()}},qa=function(a,b,c){return function(){return oa(a,b,function(d){return Da(d,
c)})()}},Ya=function(a,b,c,d,e){return U(e).then(function(f){var h={w:f.naturalWidth,h:f.naturalHeight};if((d.w!==h.w||d.h!==h.h)&&ha(c)&&h){var k=c.style.width;var p=c.style.height;if(k||p)c.style.width=h.w+"px",c.style.height=h.h+"px",c.removeAttribute("data-mce-style");k=c.width;p=c.height;if(k||p)c.setAttribute("width",String(h.w)),c.setAttribute("height",String(h.h))}URL.revokeObjectURL(f.src);return e}).then(Ra).then(function(f){return na(a,e,f,!0,b,c)})},Za=function(a,b){return function(){var c=
function(f){return{title:"Edit Image",size:"large",body:{type:"panel",items:[{type:"imagetools",name:"imagetools",label:"Edit Image",currentState:f}]},buttons:[{type:"cancel",name:"cancel",text:"Cancel"},{type:"submit",name:"save",text:"Save",primary:!0,disabled:!0}],onSubmit:function(h){var k=h.getData().imagetools.blob;d.each(function(p){e.each(function(r){Ya(a,b,p.dom,r,k)})});h.close()},onCancel:Q,onAction:function(h,k){switch(k.name){case "save-state":k.value?h.enable("save"):h.disable("save");
break;case "disable":h.disable("save");h.disable("cancel");break;case "enable":h.enable("cancel")}}}},d=G(a),e=d.map(function(f){f=f.dom;return{w:f.naturalWidth,h:f.naturalHeight}});d.each(function(f){A(a,f.dom).each(function(h){ma(a,f.dom).then(function(k){k={blob:k,url:URL.createObjectURL(k)};a.windowManager.open(c(k))})})})}},$a=function(a,b){H.each({mceImageRotateLeft:pa(a,b,-90),mceImageRotateRight:pa(a,b,90),mceImageFlipVertical:qa(a,b,"v"),mceImageFlipHorizontal:qa(a,b,"h"),mceEditImage:Za(a,
b)},function(c,d){a.addCommand(d,c)})},ab=function(a,b,c){a.on("NodeChange",function(d){var e=c.get();d=A(a,d.element);e&&!d.exists(function(f){return e.src===f.src})&&(N.clearTimeout(b.get()),a.editorUpload.uploadImagesAuto(),c.set(null));d.each(c.set)})},bb=function(a){var b=function(c){return function(){return a.execCommand(c)}};a.ui.registry.addButton("rotateleft",{tooltip:"Rotate counterclockwise",icon:"rotate-left",onAction:b("mceImageRotateLeft")});a.ui.registry.addButton("rotateright",{tooltip:"Rotate clockwise",
icon:"rotate-right",onAction:b("mceImageRotateRight")});a.ui.registry.addButton("flipv",{tooltip:"Flip vertically",icon:"flip-vertically",onAction:b("mceImageFlipVertical")});a.ui.registry.addButton("fliph",{tooltip:"Flip horizontally",icon:"flip-horizontally",onAction:b("mceImageFlipHorizontal")});a.ui.registry.addButton("editimage",{tooltip:"Edit image",icon:"edit-image",onAction:b("mceEditImage"),onSetup:function(c){var d=function(){var e=G(a).forall(function(f){return A(a,f.dom).isNone()});c.setDisabled(e)};
a.on("NodeChange",d);return function(){a.off("NodeChange",d)}}});a.ui.registry.addButton("imageoptions",{tooltip:"Image options",icon:"image",onAction:b("mceImage")});a.ui.registry.addContextMenu("imagetools",{update:function(c){return A(a,c).fold(function(){return[]},function(d){return[{text:"Edit image",icon:"edit-image",onAction:b("mceEditImage")}]})}})},cb=function(a){a.ui.registry.addContextToolbar("imagetools",{items:a.getParam("imagetools_toolbar","rotateleft rotateright flipv fliph editimage imageoptions"),
predicate:function(b){return A(a,b).isSome()},position:"node",scope:"node"})};(function(){ra.add("imagetools",function(a){var b=P(0),c=P(null);$a(a,b);bb(a);cb(a);ab(a,b,c)})})()})();