@leafer-in/export
Version:
3 lines (2 loc) • 4.55 kB
JavaScript
!function(e){"use strict";function t(e,t,o,n){return new(o||(o=Promise))(function(r,i){function a(e){try{l(n.next(e))}catch(e){i(e)}}function s(e){try{l(n.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?r(e.value):(t=e.value,t instanceof o?t:new o(function(e){e(t)})).then(a,s)}l((n=n.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const{setPoint:o,addPoint:n,toBounds:r}=e.TwoPointBoundsHelper;const i={syncExport(t,i,a){let s;e.Export.running=!0;try{const l=e.FileHelper.fileType(i),c=i.includes(".");a=e.FileHelper.getExportOptions(a);const{toURL:d}=e.Platform,{download:u}=e.Platform.origin;if("json"===l)c&&u(d(JSON.stringify(t.toJSON(a.json)),"text"),i),s={data:!!c||t.toJSON(a.json)};else if("svg"===l)c&&u(d(t.toSVG(),"svg"),i),s={data:!!c||t.toSVG()};else{let l,c,d=1,u=1;const{worldTransform:p,isLeafer:f,leafer:h,isFrame:g}=t,{slice:x,clip:w,trim:v,screenshot:m,padding:y,onCanvas:P}=a,b=e.isUndefined(a.smooth)?!h||h.config.smooth:a.smooth,E=a.contextSettings||(h?h.config.contextSettings:void 0),B=f&&m&&e.isUndefined(a.fill)?t.fill:a.fill,R=e.FileHelper.isOpaqueImage(i)||B,S=new e.Matrix;if(m)l=!0===m?f?h.canvas.bounds:t.worldRenderBounds:m;else{let e=a.relative||(f?"inner":"local");switch(d=p.scaleX,u=p.scaleY,e){case"inner":S.set(p);break;case"local":S.set(p).divide(t.localTransform),d/=t.scaleX,u/=t.scaleY;break;case"world":d=1,u=1;break;case"page":e=h||t;default:S.set(p).divide(t.getTransform(e));const o=e.worldTransform;d/=d/o.scaleX,u/=u/o.scaleY}l=t.getBounds("render",e)}const _={scaleX:1,scaleY:1};e.MathHelper.getScaleData(a.scale,a.size,l,_);let F=a.pixelRatio||1,{x:O,y:U,width:T,height:C}=new e.Bounds(l).scale(_.scaleX,_.scaleY);w&&(O+=w.x,U+=w.y,T=w.width,C=w.height,w.rotation&&S.rotateOfInner({x:O,y:U},-w.rotation));const H={exporting:!0,matrix:S.scale(1/_.scaleX,1/_.scaleY).invert().translate(-O,-U).withScale(1/d*_.scaleX,1/u*_.scaleY)};let L,M=e.Creator.canvas({width:Math.floor(T),height:Math.floor(C),pixelRatio:F,smooth:b,contextSettings:E});x&&(L=t,L.__worldOpacity=0,t=h||t,H.bounds=M.bounds),M.save();const X=g&&!e.isUndefined(B),Y=t.get("fill");if(X&&(t.fill=""),e.Platform.render(t,M,H),X&&(t.fill=Y),M.restore(),L&&L.__updateWorldOpacity(),v){c=function(t){const{width:i,height:a}=t.view,{data:s}=t.context.getImageData(0,0,i,a);let l,c,d,u=0;for(let e=0;e<s.length;e+=4)0!==s[e+3]&&(l=u%i,c=(u-l)/i,d?n(d,l,c):o(d={},l,c)),u++;const p=new e.Bounds;return d&&(r(d,p),p.scale(1/t.pixelRatio).ceil()),p}(M);const t=M,{width:i,height:a}=c,s={x:0,y:0,width:i,height:a,pixelRatio:F};M=e.Creator.canvas(s),M.copyWorld(t,c,s),t.destroy()}if(y){const[t,o,n,r]=e.MathHelper.fourNumber(y),i=M,{width:a,height:s}=i;M=e.Creator.canvas({width:a+r+o,height:s+t+n,pixelRatio:F}),M.copyWorld(i,i.bounds,{x:r,y:t,width:a,height:s}),i.destroy()}R&&M.fillWorld(M.bounds,B||"#FFFFFF","destination-over"),P&&P(M);s={data:"canvas"===i?M:M.export(i,a),width:M.pixelWidth,height:M.pixelHeight,renderBounds:l,trimBounds:c};const D=h&&h.app;D&&D.canvasManager&&D.canvasManager.clearRecycled()}}catch(e){s={data:"",error:e}}return e.Export.running=!1,s},export(o,n,r){return e.Export.running=!0,function(o){a||(a=new e.TaskProcessor);return new Promise(e=>{a.add(()=>t(this,void 0,void 0,function*(){return yield o(e)}),{parallel:!1})})}(i=>new Promise(a=>{const l=()=>t(this,void 0,void 0,function*(){if(!e.Resource.isComplete)return e.Platform.requestRender(l);const t=e.Export.syncExport(o,n,r);t.data instanceof Promise&&(t.data=yield t.data),i(t),a()});o.updateLayout(),s(o);const{leafer:c}=o;c?c.waitViewCompleted(l):l()}))}};let a;function s(e){e.__.__needComputePaint&&e.__.__computePaint(),e.isBranch&&e.children.forEach(e=>s(e))}const l=e.LeaferCanvasBase.prototype,c=e.Debug.get("@leafer-in/export");l.export=function(t,o){const{quality:n,blob:r}=e.FileHelper.getExportOptions(o);return t.includes(".")?this.saveAs(t,n):r?this.toBlob(t,n):this.toDataURL(t,n)},l.toBlob=function(t,o){return new Promise(n=>{e.Platform.origin.canvasToBolb(this.view,t,o).then(e=>{n(e)}).catch(e=>{c.error(e),n(null)})})},l.toDataURL=function(t,o){return e.Platform.origin.canvasToDataURL(this.view,t,o)},l.saveAs=function(t,o){return new Promise(n=>{e.Platform.origin.canvasSaveAs(this.view,t,o).then(()=>{n(!0)}).catch(e=>{c.error(e),n(!1)})})},e.Plugin.add("export"),Object.assign(e.Export,i),e.UI.prototype.export=function(t,o){return e.Export.export(this,t,o)},e.UI.prototype.syncExport=function(t,o){return e.Export.syncExport(this,t,o)}}(LeaferUI);
//# sourceMappingURL=export.min.js.map