UNPKG

@leafer-in/export

Version:
3 lines (2 loc) 4.43 kB
"use strict";var e=require("@leafer-ui/draw");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:h,isLeafer:p,leafer:f,isFrame:g}=t,{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?t.fill:a.fill,B=e.FileHelper.isOpaqueImage(i)||_,S=new e.Matrix;if(m)l=!0===m?p?f.canvas.bounds:t.worldRenderBounds:m;else{let e=a.relative||(p?"inner":"local");switch(d=h.scaleX,u=h.scaleY,e){case"inner":S.set(h);break;case"local":S.set(h).divide(t.localTransform),d/=t.scaleX,u/=t.scaleY;break;case"world":d=1,u=1;break;case"page":e=f||t;default:S.set(h).divide(t.getTransform(e));const o=e.worldTransform;d/=d/o.scaleX,u/=u/o.scaleY}l=t.getBounds("render",e)}const R={scaleX:1,scaleY:1};e.MathHelper.getScaleData(a.scale,a.size,l,R);let F=a.pixelRatio||1,{x:O,y:T,width:C,height:H}=new e.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 X,Y=e.Creator.canvas({width:Math.floor(C),height:Math.floor(H),pixelRatio:F,smooth:b,contextSettings:E});if(x&&(X=t,X.__worldOpacity=0,t=f||t,L.bounds=Y.bounds),Y.save(),g&&void 0!==_){const e=t.get("fill");t.fill="",t.__render(Y,L),t.fill=e}else t.__render(Y,L);if(Y.restore(),X&&X.__updateWorldOpacity(),w){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 h=new e.Bounds;return d&&(r(d,h),h.scale(1/t.pixelRatio).ceil()),h}(Y);const t=Y,{width:i,height:a}=c,s={x:0,y:0,width:i,height:a,pixelRatio:F};Y=e.Creator.canvas(s),Y.copyWorld(t,c,s)}if(y){const[t,o,n,r]=e.MathHelper.fourNumber(y),i=Y,{width:a,height:s}=i;Y=e.Creator.canvas({width:a+r+o,height:s+t+n,pixelRatio:F}),Y.copyWorld(i,i.bounds,{x:r,y:t,width:a,height:s})}B&&Y.fillWorld(Y.bounds,_||"#FFFFFF","destination-over"),P&&P(Y);s={data:"canvas"===i?Y:Y.export(i,a),width:Y.pixelWidth,height:Y.pixelHeight,renderBounds:l,trimBounds:c}}}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)}; //# sourceMappingURL=export.min.cjs.map