UNPKG

@leafer-in/export

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