(function(){"use strict";try{if(typeof document<"u"){var o=document.createElement("style");o.appendChild(document.createTextNode('.image-tool{--bg-color: #cdd1e0;--front-color: #388ae5;--border-color: #e8e8eb}.image-tool__image{border-radius:3px;overflow:hidden;margin-bottom:10px}.image-tool__image-picture{max-width:100%;vertical-align:bottom;display:block}.image-tool__image-preloader{width:50px;height:50px;border-radius:50%;background-size:cover;margin:auto;position:relative;background-color:var(--bg-color);background-position:center center}.image-tool__image-preloader:after{content:"";position:absolute;z-index:3;width:60px;height:60px;border-radius:50%;border:2px solid var(--bg-color);border-top-color:var(--front-color);left:50%;top:50%;margin-top:-30px;margin-left:-30px;animation:image-preloader-spin 2s infinite linear;box-sizing:border-box}.image-tool__caption[contentEditable=true][data-placeholder]:before{position:absolute!important;content:attr(data-placeholder);color:#707684;font-weight:400;display:none}.image-tool__caption[contentEditable=true][data-placeholder]:empty:before{display:block}.image-tool__caption[contentEditable=true][data-placeholder]:empty:focus:before{display:none}.image-tool--empty .image-tool__image,.image-tool--empty .image-tool__caption,.image-tool--loading .image-tool__caption{display:none}.image-tool .cdx-button{display:flex;align-items:center;justify-content:center}.image-tool .cdx-button svg{height:auto;margin:0 6px 0 0}.image-tool--filled .cdx-button,.image-tool--filled .image-tool__image-preloader{display:none}.image-tool--loading .image-tool__image{min-height:200px;display:flex;border:1px solid var(--border-color);background-color:#fff}.image-tool--loading .image-tool__image-picture,.image-tool--loading .cdx-button{display:none}.image-tool--withBorder .image-tool__image{border:1px solid var(--border-color)}.image-tool--withBackground .image-tool__image{padding:15px;background:var(--bg-color)}.image-tool--withBackground .image-tool__image-picture{max-width:60%;margin:0 auto}.image-tool--stretched .image-tool__image-picture{width:100%}@keyframes image-preloader-spin{0%{transform:rotate(0)}to{transform:rotate(360deg)}}')),document.head.appendChild(o)}}catch(e){console.error("vite-plugin-css-injected-by-js",e)}})();
(function(_,O){typeof exports=="object"&&typeof module<"u"?module.exports=O():typeof define=="function"&&define.amd?define(O):(_=typeof globalThis<"u"?globalThis:_||self,_.ImageTool=O())})(this,function(){"use strict";const _="",O='',D='',R='',x='';function M(E,a=null,i={}){const c=document.createElement(E);Array.isArray(a)?c.classList.add(...a):a&&c.classList.add(a);for(const r in i)c[r]=i[r];return c}class T{constructor({api:a,config:i,onSelectFile:c,readOnly:r}){this.api=a,this.config=i,this.onSelectFile=c,this.readOnly=r,this.nodes={wrapper:M("div",[this.CSS.baseClass,this.CSS.wrapper]),imageContainer:M("div",[this.CSS.imageContainer]),fileButton:this.createFileButton(),imageEl:void 0,imagePreloader:M("div",this.CSS.imagePreloader),caption:M("div",[this.CSS.input,this.CSS.caption],{contentEditable:!this.readOnly})},this.nodes.caption.dataset.placeholder=this.config.captionPlaceholder,this.nodes.imageContainer.appendChild(this.nodes.imagePreloader),this.nodes.wrapper.appendChild(this.nodes.imageContainer),this.nodes.wrapper.appendChild(this.nodes.caption),this.nodes.wrapper.appendChild(this.nodes.fileButton)}get CSS(){return{baseClass:this.api.styles.block,loading:this.api.styles.loader,input:this.api.styles.input,button:this.api.styles.button,wrapper:"image-tool",imageContainer:"image-tool__image",imagePreloader:"image-tool__image-preloader",imageEl:"image-tool__image-picture",caption:"image-tool__caption"}}static get status(){return{EMPTY:"empty",UPLOADING:"loading",FILLED:"filled"}}render(a){return!a.file||Object.keys(a.file).length===0?this.toggleStatus(T.status.EMPTY):this.toggleStatus(T.status.UPLOADING),this.nodes.wrapper}createFileButton(){const a=M("div",[this.CSS.button]);return a.innerHTML=this.config.buttonContent||`${R} ${this.api.i18n.t("Select an Image")}`,a.addEventListener("click",()=>{this.onSelectFile()}),a}showPreloader(a){this.nodes.imagePreloader.style.backgroundImage=`url(${a})`,this.toggleStatus(T.status.UPLOADING)}hidePreloader(){this.nodes.imagePreloader.style.backgroundImage="",this.toggleStatus(T.status.EMPTY)}fillImage(a){const i=/\.mp4$/.test(a)?"VIDEO":"IMG",c={src:a};let r="load";i==="VIDEO"&&(c.autoplay=!0,c.loop=!0,c.muted=!0,c.playsinline=!0,r="loadeddata"),this.nodes.imageEl=M(i,this.CSS.imageEl,c),this.nodes.imageEl.addEventListener(r,()=>{this.toggleStatus(T.status.FILLED),this.nodes.imagePreloader&&(this.nodes.imagePreloader.style.backgroundImage="")}),this.nodes.imageContainer.appendChild(this.nodes.imageEl)}fillCaption(a){this.nodes.caption&&(this.nodes.caption.innerHTML=a)}toggleStatus(a){for(const i in T.status)Object.prototype.hasOwnProperty.call(T.status,i)&&this.nodes.wrapper.classList.toggle(`${this.CSS.wrapper}--${T.status[i]}`,a===T.status[i])}applyTune(a,i){this.nodes.wrapper.classList.toggle(`${this.CSS.wrapper}--${a}`,i)}}function U(E){return E&&E.__esModule&&Object.prototype.hasOwnProperty.call(E,"default")?E.default:E}var H={exports:{}};(function(E,a){(function(i,c){E.exports=c()})(window,function(){return function(i){var c={};function r(n){if(c[n])return c[n].exports;var o=c[n]={i:n,l:!1,exports:{}};return i[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=i,r.c=c,r.d=function(n,o,d){r.o(n,o)||Object.defineProperty(n,o,{enumerable:!0,get:d})},r.r=function(n){typeof Symbol<"u"&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},r.t=function(n,o){if(1&o&&(n=r(n)),8&o||4&o&&typeof n=="object"&&n&&n.__esModule)return n;var d=Object.create(null);if(r.r(d),Object.defineProperty(d,"default",{enumerable:!0,value:n}),2&o&&typeof n!="string")for(var b in n)r.d(d,b,(function(u){return n[u]}).bind(null,b));return d},r.n=function(n){var o=n&&n.__esModule?function(){return n.default}:function(){return n};return r.d(o,"a",o),o},r.o=function(n,o){return Object.prototype.hasOwnProperty.call(n,o)},r.p="",r(r.s=3)}([function(i,c){var r;r=function(){return this}();try{r=r||new Function("return this")()}catch{typeof window=="object"&&(r=window)}i.exports=r},function(i,c,r){(function(n){var o=r(2),d=setTimeout;function b(){}function u(e){if(!(this instanceof u))throw new TypeError("Promises must be constructed via new");if(typeof e!="function")throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],t(e,this)}function p(e,l){for(;e._state===3;)e=e._value;e._state!==0?(e._handled=!0,u._immediateFn(function(){var s=e._state===1?l.onFulfilled:l.onRejected;if(s!==null){var g;try{g=s(e._value)}catch(m){return void y(l.promise,m)}f(l.promise,g)}else(e._state===1?f:y)(l.promise,e._value)})):e._deferreds.push(l)}function f(e,l){try{if(l===e)throw new TypeError("A promise cannot be resolved with itself.");if(l&&(typeof l=="object"||typeof l=="function")){var s=l.then;if(l instanceof u)return e._state=3,e._value=l,void w(e);if(typeof s=="function")return void t((g=s,m=l,function(){g.apply(m,arguments)}),e)}e._state=1,e._value=l,w(e)}catch(h){y(e,h)}var g,m}function y(e,l){e._state=2,e._value=l,w(e)}function w(e){e._state===2&&e._deferreds.length===0&&u._immediateFn(function(){e._handled||u._unhandledRejectionFn(e._value)});for(var l=0,s=e._deferreds.length;l0&&arguments[0]!==void 0?arguments[0]:{};if(t.url&&typeof t.url!="string")throw new Error("Url must be a string");if(t.url=t.url||"",t.method&&typeof t.method!="string")throw new Error("`method` must be a string or null");if(t.method=t.method?t.method.toUpperCase():"GET",t.headers&&n(t.headers)!=="object")throw new Error("`headers` must be an object or null");if(t.headers=t.headers||{},t.type&&(typeof t.type!="string"||!Object.values(o).includes(t.type)))throw new Error("`type` must be taken from module's «contentType» library");if(t.progress&&typeof t.progress!="function")throw new Error("`progress` must be a function or null");if(t.progress=t.progress||function(e){},t.beforeSend=t.beforeSend||function(e){},t.ratio&&typeof t.ratio!="number")throw new Error("`ratio` must be a number");if(t.ratio<0||t.ratio>100)throw new Error("`ratio` must be in a 0-100 interval");if(t.ratio=t.ratio||90,t.accept&&typeof t.accept!="string")throw new Error("`accept` must be a string with a list of allowed mime-types");if(t.accept=t.accept||"*/*",t.multiple&&typeof t.multiple!="boolean")throw new Error("`multiple` must be a true or false");if(t.multiple=t.multiple||!1,t.fieldName&&typeof t.fieldName!="string")throw new Error("`fieldName` must be a string");return t.fieldName=t.fieldName||"files",t},p=function(t){switch(t.method){case"GET":var e=f(t.data,o.URLENCODED);delete t.data,t.url=/\?/.test(t.url)?t.url+"&"+e:t.url+"?"+e;break;case"POST":case"PUT":case"DELETE":case"UPDATE":var l=function(){return(arguments.length>0&&arguments[0]!==void 0?arguments[0]:{}).type||o.JSON}(t);(w.isFormData(t.data)||w.isFormElement(t.data))&&(l=o.FORM),t.data=f(t.data,l),l!==v.contentType.FORM&&(t.headers["content-type"]=l)}return t},f=function(){var t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:{};switch(arguments.length>1?arguments[1]:void 0){case o.URLENCODED:return w.urlEncode(t);case o.JSON:return w.jsonEncode(t);case o.FORM:return w.formEncode(t);default:return t}},y=function(t){return t>=200&&t<300},{contentType:o={URLENCODED:"application/x-www-form-urlencoded; charset=utf-8",FORM:"multipart/form-data",JSON:"application/json; charset=utf-8"},request:d,get:function(t){return t.method="GET",d(t)},post:b,transport:function(t){return t=u(t),w.selectFiles(t).then(function(e){for(var l=new FormData,s=0;s=0&&(u._idleTimeoutId=setTimeout(function(){u._onTimeout&&u._onTimeout()},p))},r(6),c.setImmediate=typeof self<"u"&&self.setImmediate||n!==void 0&&n.setImmediate||this&&this.setImmediate,c.clearImmediate=typeof self<"u"&&self.clearImmediate||n!==void 0&&n.clearImmediate||this&&this.clearImmediate}).call(this,r(0))},function(i,c,r){(function(n,o){(function(d,b){if(!d.setImmediate){var u,p,f,y,w,v=1,t={},e=!1,l=d.document,s=Object.getPrototypeOf&&Object.getPrototypeOf(d);s=s&&s.setTimeout?s:d,{}.toString.call(d.process)==="[object process]"?u=function(h){o.nextTick(function(){m(h)})}:function(){if(d.postMessage&&!d.importScripts){var h=!0,k=d.onmessage;return d.onmessage=function(){h=!1},d.postMessage("","*"),d.onmessage=k,h}}()?(y="setImmediate$"+Math.random()+"$",w=function(h){h.source===d&&typeof h.data=="string"&&h.data.indexOf(y)===0&&m(+h.data.slice(y.length))},d.addEventListener?d.addEventListener("message",w,!1):d.attachEvent("onmessage",w),u=function(h){d.postMessage(y+h,"*")}):d.MessageChannel?((f=new MessageChannel).port1.onmessage=function(h){m(h.data)},u=function(h){f.port2.postMessage(h)}):l&&"onreadystatechange"in l.createElement("script")?(p=l.documentElement,u=function(h){var k=l.createElement("script");k.onreadystatechange=function(){m(h),k.onreadystatechange=null,p.removeChild(k),k=null},p.appendChild(k)}):u=function(h){setTimeout(m,0,h)},s.setImmediate=function(h){typeof h!="function"&&(h=new Function(""+h));for(var k=new Array(arguments.length-1),S=0;S"u"?n===void 0?this:n:self)}).call(this,r(0),r(7))},function(i,c){var r,n,o=i.exports={};function d(){throw new Error("setTimeout has not been defined")}function b(){throw new Error("clearTimeout has not been defined")}function u(s){if(r===setTimeout)return setTimeout(s,0);if((r===d||!r)&&setTimeout)return r=setTimeout,setTimeout(s,0);try{return r(s,0)}catch{try{return r.call(null,s,0)}catch{return r.call(this,s,0)}}}(function(){try{r=typeof setTimeout=="function"?setTimeout:d}catch{r=d}try{n=typeof clearTimeout=="function"?clearTimeout:b}catch{n=b}})();var p,f=[],y=!1,w=-1;function v(){y&&p&&(y=!1,p.length?f=p.concat(f):w=-1,f.length&&t())}function t(){if(!y){var s=u(v);y=!0;for(var g=f.length;g;){for(p=f,f=[];++w1)for(var m=1;m HTMLElement")}},{key:"isObject",value:function(f){return Object.prototype.toString.call(f)==="[object Object]"}},{key:"isFormData",value:function(f){return f instanceof FormData}},{key:"isFormElement",value:function(f){return f instanceof HTMLFormElement}},{key:"selectFiles",value:function(){var f=arguments.length>0&&arguments[0]!==void 0?arguments[0]:{};return new Promise(function(y,w){var v=document.createElement("INPUT");v.type="file",f.multiple&&v.setAttribute("multiple","multiple"),f.accept&&v.setAttribute("accept",f.accept),v.style.display="none",document.body.appendChild(v),v.addEventListener("change",function(t){var e=t.target.files;y(e),document.body.removeChild(v)},!1),v.click()})}},{key:"parseHeaders",value:function(f){var y=f.trim().split(/[\r\n]+/),w={};return y.forEach(function(v){var t=v.split(": "),e=t.shift(),l=t.join(": ");e&&(w[e]=l)}),w}}],(u=null)&&n(b.prototype,u),p&&n(b,p),d}()},function(i,c){var r=function(o){return encodeURIComponent(o).replace(/[!'()*]/g,escape).replace(/%20/g,"+")},n=function(o,d,b,u){return d=d||null,b=b||"&",u=u||null,o?function(p){for(var f=new Array,y=0;y{a(o.target.result)}};let c;this.config.uploader&&typeof this.config.uploader.uploadByFile=="function"?c=F.selectFiles({accept:this.config.types}).then(r=>{i(r[0]);const n=this.config.uploader.uploadByFile(r[0]);return L(n)||console.warn("Custom uploader method uploadByFile should return a Promise"),n}):c=F.transport({url:this.config.endpoints.byFile,data:this.config.additionalRequestData,accept:this.config.types,headers:this.config.additionalRequestHeaders,beforeSend:r=>{i(r[0])},fieldName:this.config.field}).then(r=>r.body),c.then(r=>{this.onUpload(r)}).catch(r=>{this.onError(r)})}uploadByUrl(a){let i;this.config.uploader&&typeof this.config.uploader.uploadByUrl=="function"?(i=this.config.uploader.uploadByUrl(a),L(i)||console.warn("Custom uploader method uploadByUrl should return a Promise")):i=F.post({url:this.config.endpoints.byUrl,data:Object.assign({url:a},this.config.additionalRequestData),type:F.contentType.JSON,headers:this.config.additionalRequestHeaders}).then(c=>c.body),i.then(c=>{this.onUpload(c)}).catch(c=>{this.onError(c)})}uploadByFile(a,{onPreview:i}){const c=new FileReader;c.readAsDataURL(a),c.onload=n=>{i(n.target.result)};let r;if(this.config.uploader&&typeof this.config.uploader.uploadByFile=="function")r=this.config.uploader.uploadByFile(a),L(r)||console.warn("Custom uploader method uploadByFile should return a Promise");else{const n=new FormData;n.append(this.config.field,a),this.config.additionalRequestData&&Object.keys(this.config.additionalRequestData).length&&Object.entries(this.config.additionalRequestData).forEach(([o,d])=>{n.append(o,d)}),r=F.post({url:this.config.endpoints.byFile,data:n,type:F.contentType.JSON,headers:this.config.additionalRequestHeaders}).then(o=>o.body)}r.then(n=>{this.onUpload(n)}).catch(n=>{this.onError(n)})}}/**
* Image Tool for the Editor.js
*
* @author CodeX
* @license MIT
* @see {@link https://github.com/editor-js/image}
*
* To developers.
* To simplify Tool structure, we split it to 4 parts:
* 1) index.js — main Tool's interface, public API and methods for working with data
* 2) uploader.js — module that has methods for sending files via AJAX: from device, by URL or File pasting
* 3) ui.js — module for UI manipulations: render, showing preloader, etc
* 4) tunes.js — working with Block Tunes: render buttons, handle clicks
*
* For debug purposes there is a testing server
* that can save uploaded files and return a Response {@link UploadResponseFormat}
*
* $ node dev/server.js
*
* It will expose 8008 port, so you can pass http://localhost:8008 with the Tools config:
*
* image: {
* class: ImageTool,
* config: {
* endpoints: {
* byFile: 'http://localhost:8008/uploadFile',
* byUrl: 'http://localhost:8008/fetchUrl',
* }
* },
* },
*/class P{static get isReadOnlySupported(){return!0}static get toolbox(){return{icon:R,title:"Image"}}static get tunes(){return[{name:"withBorder",icon:D,title:"With border",toggle:!0},{name:"stretched",icon:x,title:"Stretch image",toggle:!0},{name:"withBackground",icon:O,title:"With background",toggle:!0}]}constructor({data:a,config:i,api:c,readOnly:r,block:n}){this.api=c,this.readOnly=r,this.block=n,this.config={endpoints:i.endpoints||"",additionalRequestData:i.additionalRequestData||{},additionalRequestHeaders:i.additionalRequestHeaders||{},field:i.field||"image",types:i.types||"image/*",captionPlaceholder:this.api.i18n.t(i.captionPlaceholder||"Caption"),buttonContent:i.buttonContent||"",uploader:i.uploader||void 0,actions:i.actions||[]},this.uploader=new A({config:this.config,onUpload:o=>this.onUpload(o),onError:o=>this.uploadingFailed(o)}),this.ui=new T({api:c,config:this.config,onSelectFile:()=>{this.uploader.uploadSelectedFile({onPreview:o=>{this.ui.showPreloader(o)}})},readOnly:r}),this._data={},this.data=a}render(){return this.ui.render(this.data)}validate(a){return a.file&&a.file.url}save(){const a=this.ui.nodes.caption;return this._data.caption=a.innerHTML,this.data}renderSettings(){return P.tunes.concat(this.config.actions).map(i=>({icon:i.icon,label:this.api.i18n.t(i.title),name:i.name,toggle:i.toggle,isActive:this.data[i.name],onActivate:()=>{if(typeof i.action=="function"){i.action(i.name);return}this.tuneToggled(i.name)}}))}appendCallback(){this.ui.nodes.fileButton.click()}static get pasteConfig(){return{tags:[{img:{src:!0}}],patterns:{image:/https?:\/\/\S+\.(gif|jpe?g|tiff|png|svg|webp)(\?[a-z0-9=]*)?$/i},files:{mimeTypes:["image/*"]}}}async onPaste(a){switch(a.type){case"tag":{const i=a.detail.data;if(/^blob:/.test(i.src)){const r=await(await fetch(i.src)).blob();this.uploadFile(r);break}this.uploadUrl(i.src);break}case"pattern":{const i=a.detail.data;this.uploadUrl(i);break}case"file":{const i=a.detail.file;this.uploadFile(i);break}}}set data(a){this.image=a.file,this._data.caption=a.caption||"",this.ui.fillCaption(this._data.caption),P.tunes.forEach(({name:i})=>{const c=typeof a[i]<"u"?a[i]===!0||a[i]==="true":!1;this.setTune(i,c)})}get data(){return this._data}set image(a){this._data.file=a||{},a&&a.url&&this.ui.fillImage(a.url)}onUpload(a){a.success&&a.file?this.image=a.file:this.uploadingFailed("incorrect response: "+JSON.stringify(a))}uploadingFailed(a){console.log("Image Tool: uploading failed because of",a),this.api.notifier.show({message:this.api.i18n.t("Couldn’t upload image. Please try another."),style:"error"}),this.ui.hidePreloader()}tuneToggled(a){this.setTune(a,!this._data[a])}setTune(a,i){this._data[a]=i,this.ui.applyTune(a,i),a==="stretched"&&Promise.resolve().then(()=>{this.block.stretched=i}).catch(c=>{console.error(c)})}uploadFile(a){this.uploader.uploadByFile(a,{onPreview:i=>{this.ui.showPreloader(i)}})}uploadUrl(a){this.ui.showPreloader(a),this.uploader.uploadByUrl(a)}}return P});