UNPKG

@leafer-in/export

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