-
Notifications
You must be signed in to change notification settings - Fork 1
/
photopea.min.js
1 lines (1 loc) · 2 KB
/
photopea.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Photopea=t()}(this,(function(){"use strict";class e{static async createEmbed(t,n){let o="";"object"==typeof n?o=JSON.stringify(n):"string"==typeof n&&(o=n);let a=document.createElement("iframe");a.style.border="0",a.style.width="100%",a.style.height="100%",a.src=n?"https://www.photopea.com#"+encodeURI(o):"https://www.photopea.com",t.appendChild(a);let s=new Promise((function(t,n){let o=n=>{if(n.source==a.contentWindow&&"done"==n.data){let n=new e(a.contentWindow);window.removeEventListener("message",o),t(n)}};window.addEventListener("message",o)}));return await s}contentWindow;constructor(e){this.contentWindow=e}async runScript(e){await this._pause();let t=new Promise(((t,n)=>{let o=[],a=e=>{e.source==this.contentWindow&&(o.push(e.data),"done"==e.data&&(window.removeEventListener("message",a),t(o)))};window.addEventListener("message",a),this.contentWindow.postMessage(e,"*")}));return await t}async loadAsset(e){await this._pause();let t=new Promise(((t,n)=>{let o=[],a=e=>{e.source==this.contentWindow&&(o.push(e.data),"done"==e.data&&(window.removeEventListener("message",a),t(o)))};window.addEventListener("message",a),this.contentWindow.postMessage(e,"*")}));return await t}async openFromURL(e,t=!0){await this._pause();let n="done";for(;"done"==n;)n=(await this.runScript(`app.echoToOE(${t?"app.activeDocument.layers.length":"app.documents.length"})`))[0];let o=n;for(await this.runScript(`app.open("${e}", null, ${t});`);o==n||"done"==o;)o=(await this.runScript(`app.echoToOE(${t?"app.activeDocument.layers.length":"app.documents.length"})`))[0];return["done"]}async exportImage(e="png"){await this._pause();let t="done";for(;"done"==t;){t=(await this.runScript(`app.activeDocument.saveToOE("${e}");`))[0]}return new Blob([t],{type:"image/"+e})}async _pause(e=10){return await new Promise(((t,n)=>{setTimeout((()=>{t()}),e)}))}}return e}));