@leafer-in/export 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/export.cjs CHANGED
@@ -16,7 +16,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
16
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
17
  PERFORMANCE OF THIS SOFTWARE.
18
18
  ***************************************************************************** */
19
- /* global Reflect, Promise, SuppressedError, Symbol */
19
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
20
20
 
21
21
 
22
22
  function __awaiter(thisArg, _arguments, P, generator) {
@@ -77,10 +77,10 @@ const ExportModule = {
77
77
  const { leafer } = leaf;
78
78
  if (leafer) {
79
79
  checkLazy(leaf);
80
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
80
+ leafer.waitViewCompleted(() => __awaiter(this, undefined, undefined, function* () {
81
81
  let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
82
82
  const { worldTransform, isLeafer, isFrame } = leaf;
83
- const { slice, trim, onCanvas } = options;
83
+ const { slice, trim, padding, onCanvas } = options;
84
84
  const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
85
85
  const contextSettings = options.contextSettings || leafer.config.contextSettings;
86
86
  const screenshot = options.screenshot || leaf.isApp;
@@ -154,6 +154,12 @@ const ExportModule = {
154
154
  canvas = draw.Creator.canvas(config);
155
155
  canvas.copyWorld(old, trimBounds, config);
156
156
  }
157
+ if (padding) {
158
+ const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
159
+ const old = canvas, { width, height } = old;
160
+ canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
161
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
162
+ }
157
163
  if (needFill)
158
164
  canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
159
165
  if (onCanvas)
@@ -173,7 +179,7 @@ function addTask(task) {
173
179
  if (!tasker)
174
180
  tasker = new draw.TaskProcessor();
175
181
  return new Promise((resolve) => {
176
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
182
+ tasker.add(() => __awaiter(this, undefined, undefined, function* () { return yield task(resolve); }), { parallel: false });
177
183
  });
178
184
  }
179
185
  function checkLazy(leaf) {
@@ -14,7 +14,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
14
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
15
  PERFORMANCE OF THIS SOFTWARE.
16
16
  ***************************************************************************** */
17
- /* global Reflect, Promise, SuppressedError, Symbol */
17
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
18
18
 
19
19
 
20
20
  function __awaiter(thisArg, _arguments, P, generator) {
@@ -75,10 +75,10 @@ const ExportModule = {
75
75
  const { leafer } = leaf;
76
76
  if (leafer) {
77
77
  checkLazy(leaf);
78
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
78
+ leafer.waitViewCompleted(() => __awaiter(this, undefined, undefined, function* () {
79
79
  let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
80
80
  const { worldTransform, isLeafer, isFrame } = leaf;
81
- const { slice, trim, onCanvas } = options;
81
+ const { slice, trim, padding, onCanvas } = options;
82
82
  const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
83
83
  const contextSettings = options.contextSettings || leafer.config.contextSettings;
84
84
  const screenshot = options.screenshot || leaf.isApp;
@@ -152,6 +152,12 @@ const ExportModule = {
152
152
  canvas = Creator.canvas(config);
153
153
  canvas.copyWorld(old, trimBounds, config);
154
154
  }
155
+ if (padding) {
156
+ const [top, right, bottom, left] = MathHelper.fourNumber(padding);
157
+ const old = canvas, { width, height } = old;
158
+ canvas = Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
159
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
160
+ }
155
161
  if (needFill)
156
162
  canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
157
163
  if (onCanvas)
@@ -171,7 +177,7 @@ function addTask(task) {
171
177
  if (!tasker)
172
178
  tasker = new TaskProcessor();
173
179
  return new Promise((resolve) => {
174
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
180
+ tasker.add(() => __awaiter(this, undefined, undefined, function* () { return yield task(resolve); }), { parallel: false });
175
181
  });
176
182
  }
177
183
  function checkLazy(leaf) {
@@ -1 +1 @@
1
- import{Bounds as e,TwoPointBoundsHelper as t,FileHelper as n,TaskProcessor as o,Platform as i,Matrix as s,MathHelper as a,Creator as r,LeaferCanvasBase as c,Debug as l,Plugin as d,Export as u,UI as h}from"@leafer-ui/draw";function f(e,t,n,o){return new(n||(n=Promise))((function(i,s){function a(e){try{c(o.next(e))}catch(e){s(e)}}function r(e){try{c(o.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}c((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const{setPoint:p,addPoint:g,toBounds:v}=t;const w={export(t,c,l){this.running=!0;const d=n.fileType(c),u=c.includes(".");return l=n.getExportOptions(l),function(e){x||(x=new o);return new Promise((t=>{x.add((()=>f(this,void 0,void 0,(function*(){return yield e(t)}))),{parallel:!1})}))}((o=>new Promise((h=>{const w=e=>{o(e),h(),this.running=!1},{toURL:x}=i,{download:y}=i.origin;if("json"===d)return u&&y(x(JSON.stringify(t.toJSON(l.json)),"text"),c),w({data:!!u||t.toJSON(l.json)});if("svg"===d)return u&&y(x(t.toSVG(),"svg"),c),w({data:!!u||t.toSVG()});const{leafer:_}=t;_?(m(t),_.waitViewCompleted((()=>f(this,void 0,void 0,(function*(){let o,i,d=1,u=1;const{worldTransform:h,isLeafer:f,isFrame:x}=t,{slice:m,trim:y,onCanvas:b}=l,S=void 0===l.smooth?_.config.smooth:l.smooth,R=l.contextSettings||_.config.contextSettings,B=l.screenshot||t.isApp,O=f&&B&&void 0===l.fill?t.fill:l.fill,P=n.isOpaqueImage(c)||O,X=new s;if(B)o=!0===B?f?_.canvas.bounds:t.worldRenderBounds:B;else{let e=l.relative||(f?"inner":"local");switch(d=h.scaleX,u=h.scaleY,e){case"inner":X.set(h);break;case"local":X.set(h).divide(t.localTransform),d/=t.scaleX,u/=t.scaleY;break;case"world":d=1,u=1;break;case"page":e=t.leafer;default:X.set(h).divide(t.getTransform(e));const n=e.worldTransform;d/=d/n.scaleX,u/=u/n.scaleY}o=t.getBounds("render",e)}const Y={scaleX:1,scaleY:1};a.getScaleData(l.scale,l.size,o,Y);let F=l.pixelRatio||1;t.isApp&&(Y.scaleX*=F,Y.scaleY*=F,F=t.app.pixelRatio);const{x:T,y:A,width:D,height:E}=new e(o).scale(Y.scaleX,Y.scaleY),L={matrix:X.scale(1/Y.scaleX,1/Y.scaleY).invert().translate(-T,-A).withScale(1/d*Y.scaleX,1/u*Y.scaleY)};let j,U=r.canvas({width:Math.round(D),height:Math.round(E),pixelRatio:F,smooth:S,contextSettings:R});if(m&&(j=t,j.__worldOpacity=0,t=_,L.bounds=U.bounds),U.save(),x&&void 0!==O){const e=t.get("fill");t.fill="",t.__render(U,L),t.fill=e}else t.__render(U,L);if(U.restore(),j&&j.__updateWorldOpacity(),y){i=function(t){const{width:n,height:o}=t.view,{data:i}=t.context.getImageData(0,0,n,o);let s,a,r,c=0;for(let e=0;e<i.length;e+=4)0!==i[e+3]&&(s=c%n,a=(c-s)/n,r?g(r,s,a):p(r={},s,a)),c++;const l=new e;return v(r,l),l.scale(1/t.pixelRatio).ceil()}(U);const t=U,{width:n,height:o}=i,s={x:0,y:0,width:n,height:o,pixelRatio:F};U=r.canvas(s),U.copyWorld(t,i,s)}P&&U.fillWorld(U.bounds,O||"#FFFFFF","destination-over"),b&&b(U);const W="canvas"===c?U:yield U.export(c,l);w({data:W,width:U.pixelWidth,height:U.pixelHeight,renderBounds:o,trimBounds:i})}))))):w({data:!1})}))))}};let x;function m(e){e.__.__needComputePaint&&e.__.__computePaint(),e.isBranch&&e.children.forEach((e=>m(e)))}const y=c.prototype,_=l.get("@leafer-in/export");y.export=function(e,t){const{quality:o,blob:i}=n.getExportOptions(t);return e.includes(".")?this.saveAs(e,o):i?this.toBlob(e,o):this.toDataURL(e,o)},y.toBlob=function(e,t){return new Promise((n=>{i.origin.canvasToBolb(this.view,e,t).then((e=>{n(e)})).catch((e=>{_.error(e),n(null)}))}))},y.toDataURL=function(e,t){return i.origin.canvasToDataURL(this.view,e,t)},y.saveAs=function(e,t){return new Promise((n=>{i.origin.canvasSaveAs(this.view,e,t).then((()=>{n(!0)})).catch((e=>{_.error(e),n(!1)}))}))},d.add("export"),Object.assign(u,w),h.prototype.export=function(e,t){return u.export(this,e,t)};
1
+ import{Bounds as t,TwoPointBoundsHelper as e,FileHelper as n,TaskProcessor as o,Platform as i,Matrix as a,MathHelper as s,Creator as r,LeaferCanvasBase as c,Debug as l,Plugin as d,Export as h,UI as u}from"@leafer-ui/draw";function f(t,e,n,o){return new(n||(n=Promise))((function(i,a){function s(t){try{c(o.next(t))}catch(t){a(t)}}function r(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(s,r)}c((o=o.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const{setPoint:p,addPoint:g,toBounds:w}=e;const v={export(e,c,l){this.running=!0;const d=n.fileType(c),h=c.includes(".");return l=n.getExportOptions(l),function(t){x||(x=new o);return new Promise((e=>{x.add((()=>f(this,void 0,void 0,(function*(){return yield t(e)}))),{parallel:!1})}))}((o=>new Promise((u=>{const v=t=>{o(t),u(),this.running=!1},{toURL:x}=i,{download:y}=i.origin;if("json"===d)return h&&y(x(JSON.stringify(e.toJSON(l.json)),"text"),c),v({data:!!h||e.toJSON(l.json)});if("svg"===d)return h&&y(x(e.toSVG(),"svg"),c),v({data:!!h||e.toSVG()});const{leafer:_}=e;_?(m(e),_.waitViewCompleted((()=>f(this,void 0,void 0,(function*(){let o,i,d=1,h=1;const{worldTransform:u,isLeafer:f,isFrame:x}=e,{slice:m,trim:y,padding:b,onCanvas:S}=l,R=void 0===l.smooth?_.config.smooth:l.smooth,B=l.contextSettings||_.config.contextSettings,O=l.screenshot||e.isApp,P=f&&O&&void 0===l.fill?e.fill:l.fill,X=n.isOpaqueImage(c)||P,Y=new a;if(O)o=!0===O?f?_.canvas.bounds:e.worldRenderBounds:O;else{let t=l.relative||(f?"inner":"local");switch(d=u.scaleX,h=u.scaleY,t){case"inner":Y.set(u);break;case"local":Y.set(u).divide(e.localTransform),d/=e.scaleX,h/=e.scaleY;break;case"world":d=1,h=1;break;case"page":t=e.leafer;default:Y.set(u).divide(e.getTransform(t));const n=t.worldTransform;d/=d/n.scaleX,h/=h/n.scaleY}o=e.getBounds("render",t)}const F={scaleX:1,scaleY:1};s.getScaleData(l.scale,l.size,o,F);let T=l.pixelRatio||1;e.isApp&&(F.scaleX*=T,F.scaleY*=T,T=e.app.pixelRatio);const{x:A,y:D,width:E,height:L}=new t(o).scale(F.scaleX,F.scaleY),W={matrix:Y.scale(1/F.scaleX,1/F.scaleY).invert().translate(-A,-D).withScale(1/d*F.scaleX,1/h*F.scaleY)};let j,N=r.canvas({width:Math.round(E),height:Math.round(L),pixelRatio:T,smooth:R,contextSettings:B});if(m&&(j=e,j.__worldOpacity=0,e=_,W.bounds=N.bounds),N.save(),x&&void 0!==P){const t=e.get("fill");e.fill="",e.__render(N,W),e.fill=t}else e.__render(N,W);if(N.restore(),j&&j.__updateWorldOpacity(),y){i=function(e){const{width:n,height:o}=e.view,{data:i}=e.context.getImageData(0,0,n,o);let a,s,r,c=0;for(let t=0;t<i.length;t+=4)0!==i[t+3]&&(a=c%n,s=(c-a)/n,r?g(r,a,s):p(r={},a,s)),c++;const l=new t;return w(r,l),l.scale(1/e.pixelRatio).ceil()}(N);const e=N,{width:n,height:o}=i,a={x:0,y:0,width:n,height:o,pixelRatio:T};N=r.canvas(a),N.copyWorld(e,i,a)}if(b){const[t,e,n,o]=s.fourNumber(b),i=N,{width:a,height:c}=i;N=r.canvas({width:a+o+e,height:c+t+n,pixelRatio:T}),N.copyWorld(i,i.bounds,{x:o,y:t,width:a,height:c})}X&&N.fillWorld(N.bounds,P||"#FFFFFF","destination-over"),S&&S(N);const U="canvas"===c?N:yield N.export(c,l);v({data:U,width:N.pixelWidth,height:N.pixelHeight,renderBounds:o,trimBounds:i})}))))):v({data:!1})}))))}};let x;function m(t){t.__.__needComputePaint&&t.__.__computePaint(),t.isBranch&&t.children.forEach((t=>m(t)))}const y=c.prototype,_=l.get("@leafer-in/export");y.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)},y.toBlob=function(t,e){return new Promise((n=>{i.origin.canvasToBolb(this.view,t,e).then((t=>{n(t)})).catch((t=>{_.error(t),n(null)}))}))},y.toDataURL=function(t,e){return i.origin.canvasToDataURL(this.view,t,e)},y.saveAs=function(t,e){return new Promise((n=>{i.origin.canvasSaveAs(this.view,t,e).then((()=>{n(!0)})).catch((t=>{_.error(t),n(!1)}))}))},d.add("export"),Object.assign(h,v),u.prototype.export=function(t,e){return h.export(this,t,e)};
package/dist/export.js CHANGED
@@ -15,7 +15,7 @@
15
15
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
16
  PERFORMANCE OF THIS SOFTWARE.
17
17
  ***************************************************************************** */
18
- /* global Reflect, Promise, SuppressedError, Symbol */
18
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
19
19
 
20
20
 
21
21
  function __awaiter(thisArg, _arguments, P, generator) {
@@ -76,10 +76,10 @@
76
76
  const { leafer } = leaf;
77
77
  if (leafer) {
78
78
  checkLazy(leaf);
79
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
79
+ leafer.waitViewCompleted(() => __awaiter(this, undefined, undefined, function* () {
80
80
  let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
81
81
  const { worldTransform, isLeafer, isFrame } = leaf;
82
- const { slice, trim, onCanvas } = options;
82
+ const { slice, trim, padding, onCanvas } = options;
83
83
  const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
84
84
  const contextSettings = options.contextSettings || leafer.config.contextSettings;
85
85
  const screenshot = options.screenshot || leaf.isApp;
@@ -153,6 +153,12 @@
153
153
  canvas = draw.Creator.canvas(config);
154
154
  canvas.copyWorld(old, trimBounds, config);
155
155
  }
156
+ if (padding) {
157
+ const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
158
+ const old = canvas, { width, height } = old;
159
+ canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
160
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
161
+ }
156
162
  if (needFill)
157
163
  canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
158
164
  if (onCanvas)
@@ -172,7 +178,7 @@
172
178
  if (!tasker)
173
179
  tasker = new draw.TaskProcessor();
174
180
  return new Promise((resolve) => {
175
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
181
+ tasker.add(() => __awaiter(this, undefined, undefined, function* () { return yield task(resolve); }), { parallel: false });
176
182
  });
177
183
  }
178
184
  function checkLazy(leaf) {
@@ -1 +1 @@
1
- "use strict";var e=require("@leafer-ui/draw");function t(e,t,o,n){return new(o||(o=Promise))((function(i,r){function a(e){try{l(n.next(e))}catch(e){r(e)}}function s(e){try{l(n.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(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:i}=e.TwoPointBoundsHelper;const r={export(r,l,c){this.running=!0;const d=e.FileHelper.fileType(l),u=l.includes(".");return c=e.FileHelper.getExportOptions(c),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})}))}((a=>new Promise((p=>{const f=e=>{a(e),p(),this.running=!1},{toURL:h}=e.Platform,{download:g}=e.Platform.origin;if("json"===d)return u&&g(h(JSON.stringify(r.toJSON(c.json)),"text"),l),f({data:!!u||r.toJSON(c.json)});if("svg"===d)return u&&g(h(r.toSVG(),"svg"),l),f({data:!!u||r.toSVG()});const{leafer:v}=r;v?(s(r),v.waitViewCompleted((()=>t(this,void 0,void 0,(function*(){let t,a,s=1,d=1;const{worldTransform:u,isLeafer:p,isFrame:h}=r,{slice:g,trim:w,onCanvas:x}=c,m=void 0===c.smooth?v.config.smooth:c.smooth,P=c.contextSettings||v.config.contextSettings,y=c.screenshot||r.isApp,_=p&&y&&void 0===c.fill?r.fill:c.fill,b=e.FileHelper.isOpaqueImage(l)||_,B=new e.Matrix;if(y)t=!0===y?p?v.canvas.bounds:r.worldRenderBounds:y;else{let e=c.relative||(p?"inner":"local");switch(s=u.scaleX,d=u.scaleY,e){case"inner":B.set(u);break;case"local":B.set(u).divide(r.localTransform),s/=r.scaleX,d/=r.scaleY;break;case"world":s=1,d=1;break;case"page":e=r.leafer;default:B.set(u).divide(r.getTransform(e));const t=e.worldTransform;s/=s/t.scaleX,d/=d/t.scaleY}t=r.getBounds("render",e)}const S={scaleX:1,scaleY:1};e.MathHelper.getScaleData(c.scale,c.size,t,S);let F=c.pixelRatio||1;r.isApp&&(S.scaleX*=F,S.scaleY*=F,F=r.app.pixelRatio);const{x:R,y:O,width:T,height:X}=new e.Bounds(t).scale(S.scaleX,S.scaleY),Y={matrix:B.scale(1/S.scaleX,1/S.scaleY).invert().translate(-R,-O).withScale(1/s*S.scaleX,1/d*S.scaleY)};let E,H=e.Creator.canvas({width:Math.round(T),height:Math.round(X),pixelRatio:F,smooth:m,contextSettings:P});if(g&&(E=r,E.__worldOpacity=0,r=v,Y.bounds=H.bounds),H.save(),h&&void 0!==_){const e=r.get("fill");r.fill="",r.__render(H,Y),r.fill=e}else r.__render(H,Y);if(H.restore(),E&&E.__updateWorldOpacity(),w){a=function(t){const{width:r,height:a}=t.view,{data:s}=t.context.getImageData(0,0,r,a);let l,c,d,u=0;for(let e=0;e<s.length;e+=4)0!==s[e+3]&&(l=u%r,c=(u-l)/r,d?n(d,l,c):o(d={},l,c)),u++;const p=new e.Bounds;return i(d,p),p.scale(1/t.pixelRatio).ceil()}(H);const t=H,{width:r,height:s}=a,l={x:0,y:0,width:r,height:s,pixelRatio:F};H=e.Creator.canvas(l),H.copyWorld(t,a,l)}b&&H.fillWorld(H.bounds,_||"#FFFFFF","destination-over"),x&&x(H);const C="canvas"===l?H:yield H.export(l,c);f({data:C,width:H.pixelWidth,height:H.pixelHeight,renderBounds:t,trimBounds:a})}))))):f({data:!1})}))))}};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:i}=e.FileHelper.getExportOptions(o);return t.includes(".")?this.saveAs(t,n):i?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,r),e.UI.prototype.export=function(t,o){return e.Export.export(this,t,o)};
1
+ "use strict";var e=require("@leafer-ui/draw");function t(e,t,o,n){return new(o||(o=Promise))((function(i,r){function a(e){try{l(n.next(e))}catch(e){r(e)}}function s(e){try{l(n.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(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:i}=e.TwoPointBoundsHelper;const r={export(r,l,c){this.running=!0;const d=e.FileHelper.fileType(l),u=l.includes(".");return c=e.FileHelper.getExportOptions(c),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})}))}((a=>new Promise((h=>{const p=e=>{a(e),h(),this.running=!1},{toURL:f}=e.Platform,{download:g}=e.Platform.origin;if("json"===d)return u&&g(f(JSON.stringify(r.toJSON(c.json)),"text"),l),p({data:!!u||r.toJSON(c.json)});if("svg"===d)return u&&g(f(r.toSVG(),"svg"),l),p({data:!!u||r.toSVG()});const{leafer:v}=r;v?(s(r),v.waitViewCompleted((()=>t(this,void 0,void 0,(function*(){let t,a,s=1,d=1;const{worldTransform:u,isLeafer:h,isFrame:f}=r,{slice:g,trim:w,padding:x,onCanvas:m}=c,y=void 0===c.smooth?v.config.smooth:c.smooth,P=c.contextSettings||v.config.contextSettings,b=c.screenshot||r.isApp,_=h&&b&&void 0===c.fill?r.fill:c.fill,B=e.FileHelper.isOpaqueImage(l)||_,S=new e.Matrix;if(b)t=!0===b?h?v.canvas.bounds:r.worldRenderBounds:b;else{let e=c.relative||(h?"inner":"local");switch(s=u.scaleX,d=u.scaleY,e){case"inner":S.set(u);break;case"local":S.set(u).divide(r.localTransform),s/=r.scaleX,d/=r.scaleY;break;case"world":s=1,d=1;break;case"page":e=r.leafer;default:S.set(u).divide(r.getTransform(e));const t=e.worldTransform;s/=s/t.scaleX,d/=d/t.scaleY}t=r.getBounds("render",e)}const F={scaleX:1,scaleY:1};e.MathHelper.getScaleData(c.scale,c.size,t,F);let R=c.pixelRatio||1;r.isApp&&(F.scaleX*=R,F.scaleY*=R,R=r.app.pixelRatio);const{x:O,y:T,width:H,height:X}=new e.Bounds(t).scale(F.scaleX,F.scaleY),Y={matrix:S.scale(1/F.scaleX,1/F.scaleY).invert().translate(-O,-T).withScale(1/s*F.scaleX,1/d*F.scaleY)};let C,E=e.Creator.canvas({width:Math.round(H),height:Math.round(X),pixelRatio:R,smooth:y,contextSettings:P});if(g&&(C=r,C.__worldOpacity=0,r=v,Y.bounds=E.bounds),E.save(),f&&void 0!==_){const e=r.get("fill");r.fill="",r.__render(E,Y),r.fill=e}else r.__render(E,Y);if(E.restore(),C&&C.__updateWorldOpacity(),w){a=function(t){const{width:r,height:a}=t.view,{data:s}=t.context.getImageData(0,0,r,a);let l,c,d,u=0;for(let e=0;e<s.length;e+=4)0!==s[e+3]&&(l=u%r,c=(u-l)/r,d?n(d,l,c):o(d={},l,c)),u++;const h=new e.Bounds;return i(d,h),h.scale(1/t.pixelRatio).ceil()}(E);const t=E,{width:r,height:s}=a,l={x:0,y:0,width:r,height:s,pixelRatio:R};E=e.Creator.canvas(l),E.copyWorld(t,a,l)}if(x){const[t,o,n,i]=e.MathHelper.fourNumber(x),r=E,{width:a,height:s}=r;E=e.Creator.canvas({width:a+i+o,height:s+t+n,pixelRatio:R}),E.copyWorld(r,r.bounds,{x:i,y:t,width:a,height:s})}B&&E.fillWorld(E.bounds,_||"#FFFFFF","destination-over"),m&&m(E);const D="canvas"===l?E:yield E.export(l,c);p({data:D,width:E.pixelWidth,height:E.pixelHeight,renderBounds:t,trimBounds:a})}))))):p({data:!1})}))))}};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:i}=e.FileHelper.getExportOptions(o);return t.includes(".")?this.saveAs(t,n):i?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,r),e.UI.prototype.export=function(t,o){return e.Export.export(this,t,o)};
@@ -1 +1 @@
1
- !function(e){"use strict";function t(e,t,o,n){return new(o||(o=Promise))((function(i,r){function a(e){try{l(n.next(e))}catch(e){r(e)}}function s(e){try{l(n.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(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:i}=e.TwoPointBoundsHelper;const r={export(r,l,c){this.running=!0;const d=e.FileHelper.fileType(l),u=l.includes(".");return c=e.FileHelper.getExportOptions(c),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})}))}((a=>new Promise((f=>{const p=e=>{a(e),f(),this.running=!1},{toURL:h}=e.Platform,{download:g}=e.Platform.origin;if("json"===d)return u&&g(h(JSON.stringify(r.toJSON(c.json)),"text"),l),p({data:!!u||r.toJSON(c.json)});if("svg"===d)return u&&g(h(r.toSVG(),"svg"),l),p({data:!!u||r.toSVG()});const{leafer:v}=r;v?(s(r),v.waitViewCompleted((()=>t(this,void 0,void 0,(function*(){let t,a,s=1,d=1;const{worldTransform:u,isLeafer:f,isFrame:h}=r,{slice:g,trim:w,onCanvas:x}=c,m=void 0===c.smooth?v.config.smooth:c.smooth,P=c.contextSettings||v.config.contextSettings,y=c.screenshot||r.isApp,_=f&&y&&void 0===c.fill?r.fill:c.fill,b=e.FileHelper.isOpaqueImage(l)||_,B=new e.Matrix;if(y)t=!0===y?f?v.canvas.bounds:r.worldRenderBounds:y;else{let e=c.relative||(f?"inner":"local");switch(s=u.scaleX,d=u.scaleY,e){case"inner":B.set(u);break;case"local":B.set(u).divide(r.localTransform),s/=r.scaleX,d/=r.scaleY;break;case"world":s=1,d=1;break;case"page":e=r.leafer;default:B.set(u).divide(r.getTransform(e));const t=e.worldTransform;s/=s/t.scaleX,d/=d/t.scaleY}t=r.getBounds("render",e)}const S={scaleX:1,scaleY:1};e.MathHelper.getScaleData(c.scale,c.size,t,S);let F=c.pixelRatio||1;r.isApp&&(S.scaleX*=F,S.scaleY*=F,F=r.app.pixelRatio);const{x:R,y:O,width:T,height:X}=new e.Bounds(t).scale(S.scaleX,S.scaleY),Y={matrix:B.scale(1/S.scaleX,1/S.scaleY).invert().translate(-R,-O).withScale(1/s*S.scaleX,1/d*S.scaleY)};let E,H=e.Creator.canvas({width:Math.round(T),height:Math.round(X),pixelRatio:F,smooth:m,contextSettings:P});if(g&&(E=r,E.__worldOpacity=0,r=v,Y.bounds=H.bounds),H.save(),h&&void 0!==_){const e=r.get("fill");r.fill="",r.__render(H,Y),r.fill=e}else r.__render(H,Y);if(H.restore(),E&&E.__updateWorldOpacity(),w){a=function(t){const{width:r,height:a}=t.view,{data:s}=t.context.getImageData(0,0,r,a);let l,c,d,u=0;for(let e=0;e<s.length;e+=4)0!==s[e+3]&&(l=u%r,c=(u-l)/r,d?n(d,l,c):o(d={},l,c)),u++;const f=new e.Bounds;return i(d,f),f.scale(1/t.pixelRatio).ceil()}(H);const t=H,{width:r,height:s}=a,l={x:0,y:0,width:r,height:s,pixelRatio:F};H=e.Creator.canvas(l),H.copyWorld(t,a,l)}b&&H.fillWorld(H.bounds,_||"#FFFFFF","destination-over"),x&&x(H);const L="canvas"===l?H:yield H.export(l,c);p({data:L,width:H.pixelWidth,height:H.pixelHeight,renderBounds:t,trimBounds:a})}))))):p({data:!1})}))))}};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:i}=e.FileHelper.getExportOptions(o);return t.includes(".")?this.saveAs(t,n):i?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,r),e.UI.prototype.export=function(t,o){return e.Export.export(this,t,o)}}(LeaferUI);
1
+ !function(e){"use strict";function t(e,t,o,n){return new(o||(o=Promise))((function(i,r){function a(e){try{l(n.next(e))}catch(e){r(e)}}function s(e){try{l(n.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(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:i}=e.TwoPointBoundsHelper;const r={export(r,l,c){this.running=!0;const d=e.FileHelper.fileType(l),u=l.includes(".");return c=e.FileHelper.getExportOptions(c),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})}))}((a=>new Promise((h=>{const p=e=>{a(e),h(),this.running=!1},{toURL:f}=e.Platform,{download:g}=e.Platform.origin;if("json"===d)return u&&g(f(JSON.stringify(r.toJSON(c.json)),"text"),l),p({data:!!u||r.toJSON(c.json)});if("svg"===d)return u&&g(f(r.toSVG(),"svg"),l),p({data:!!u||r.toSVG()});const{leafer:v}=r;v?(s(r),v.waitViewCompleted((()=>t(this,void 0,void 0,(function*(){let t,a,s=1,d=1;const{worldTransform:u,isLeafer:h,isFrame:f}=r,{slice:g,trim:w,padding:x,onCanvas:m}=c,y=void 0===c.smooth?v.config.smooth:c.smooth,P=c.contextSettings||v.config.contextSettings,b=c.screenshot||r.isApp,_=h&&b&&void 0===c.fill?r.fill:c.fill,B=e.FileHelper.isOpaqueImage(l)||_,S=new e.Matrix;if(b)t=!0===b?h?v.canvas.bounds:r.worldRenderBounds:b;else{let e=c.relative||(h?"inner":"local");switch(s=u.scaleX,d=u.scaleY,e){case"inner":S.set(u);break;case"local":S.set(u).divide(r.localTransform),s/=r.scaleX,d/=r.scaleY;break;case"world":s=1,d=1;break;case"page":e=r.leafer;default:S.set(u).divide(r.getTransform(e));const t=e.worldTransform;s/=s/t.scaleX,d/=d/t.scaleY}t=r.getBounds("render",e)}const F={scaleX:1,scaleY:1};e.MathHelper.getScaleData(c.scale,c.size,t,F);let R=c.pixelRatio||1;r.isApp&&(F.scaleX*=R,F.scaleY*=R,R=r.app.pixelRatio);const{x:O,y:T,width:H,height:X}=new e.Bounds(t).scale(F.scaleX,F.scaleY),Y={matrix:S.scale(1/F.scaleX,1/F.scaleY).invert().translate(-O,-T).withScale(1/s*F.scaleX,1/d*F.scaleY)};let C,E=e.Creator.canvas({width:Math.round(H),height:Math.round(X),pixelRatio:R,smooth:y,contextSettings:P});if(g&&(C=r,C.__worldOpacity=0,r=v,Y.bounds=E.bounds),E.save(),f&&void 0!==_){const e=r.get("fill");r.fill="",r.__render(E,Y),r.fill=e}else r.__render(E,Y);if(E.restore(),C&&C.__updateWorldOpacity(),w){a=function(t){const{width:r,height:a}=t.view,{data:s}=t.context.getImageData(0,0,r,a);let l,c,d,u=0;for(let e=0;e<s.length;e+=4)0!==s[e+3]&&(l=u%r,c=(u-l)/r,d?n(d,l,c):o(d={},l,c)),u++;const h=new e.Bounds;return i(d,h),h.scale(1/t.pixelRatio).ceil()}(E);const t=E,{width:r,height:s}=a,l={x:0,y:0,width:r,height:s,pixelRatio:R};E=e.Creator.canvas(l),E.copyWorld(t,a,l)}if(x){const[t,o,n,i]=e.MathHelper.fourNumber(x),r=E,{width:a,height:s}=r;E=e.Creator.canvas({width:a+i+o,height:s+t+n,pixelRatio:R}),E.copyWorld(r,r.bounds,{x:i,y:t,width:a,height:s})}B&&E.fillWorld(E.bounds,_||"#FFFFFF","destination-over"),m&&m(E);const L="canvas"===l?E:yield E.export(l,c);p({data:L,width:E.pixelWidth,height:E.pixelHeight,renderBounds:t,trimBounds:a})}))))):p({data:!1})}))))}};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:i}=e.FileHelper.getExportOptions(o);return t.includes(".")?this.saveAs(t,n):i?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,r),e.UI.prototype.export=function(t,o){return e.Export.export(this,t,o)}}(LeaferUI);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-in/export",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "description": "@leafer-in/export",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -34,8 +34,8 @@
34
34
  "leaferjs"
35
35
  ],
36
36
  "peerDependencies": {
37
- "@leafer-ui/draw": "^1.3.2",
38
- "@leafer-ui/interface": "^1.3.2",
39
- "@leafer-in/interface": "^1.3.2"
37
+ "@leafer-ui/draw": "^1.3.3",
38
+ "@leafer-ui/interface": "^1.3.3",
39
+ "@leafer-in/interface": "^1.3.3"
40
40
  }
41
41
  }
package/src/export.ts CHANGED
@@ -48,7 +48,7 @@ export const ExportModule: IExportModule = {
48
48
 
49
49
  let renderBounds: IBoundsData, trimBounds: IBounds, scaleX = 1, scaleY = 1
50
50
  const { worldTransform, isLeafer, isFrame } = leaf
51
- const { slice, trim, onCanvas } = options
51
+ const { slice, trim, padding, onCanvas } = options
52
52
  const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth
53
53
  const contextSettings = options.contextSettings || leafer.config.contextSettings
54
54
 
@@ -143,6 +143,14 @@ export const ExportModule: IExportModule = {
143
143
  canvas.copyWorld(old, trimBounds, config)
144
144
  }
145
145
 
146
+ if (padding) {
147
+ const [top, right, bottom, left] = MathHelper.fourNumber(padding)
148
+ const old = canvas, { width, height } = old
149
+
150
+ canvas = Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio })
151
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height })
152
+ }
153
+
146
154
  if (needFill) canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over')
147
155
  if (onCanvas) onCanvas(canvas)
148
156