@leafer-in/export 1.3.1 → 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 +10 -4
- package/dist/export.esm.js +10 -4
- package/dist/export.esm.min.js +1 -1
- package/dist/export.js +10 -4
- package/dist/export.min.cjs +1 -1
- package/dist/export.min.js +1 -1
- package/package.json +4 -4
- package/src/export.ts +9 -1
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,
|
|
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,
|
|
182
|
+
tasker.add(() => __awaiter(this, undefined, undefined, function* () { return yield task(resolve); }), { parallel: false });
|
|
177
183
|
});
|
|
178
184
|
}
|
|
179
185
|
function checkLazy(leaf) {
|
package/dist/export.esm.js
CHANGED
|
@@ -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,
|
|
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,
|
|
180
|
+
tasker.add(() => __awaiter(this, undefined, undefined, function* () { return yield task(resolve); }), { parallel: false });
|
|
175
181
|
});
|
|
176
182
|
}
|
|
177
183
|
function checkLazy(leaf) {
|
package/dist/export.esm.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Bounds as
|
|
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,
|
|
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,
|
|
181
|
+
tasker.add(() => __awaiter(this, undefined, undefined, function* () { return yield task(resolve); }), { parallel: false });
|
|
176
182
|
});
|
|
177
183
|
}
|
|
178
184
|
function checkLazy(leaf) {
|
package/dist/export.min.cjs
CHANGED
|
@@ -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((
|
|
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)};
|
package/dist/export.min.js
CHANGED
|
@@ -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((
|
|
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.
|
|
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.
|
|
38
|
-
"@leafer-ui/interface": "^1.3.
|
|
39
|
-
"@leafer-in/interface": "^1.3.
|
|
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
|
|