UNPKG

@leafer-in/export

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