@leafer-in/export 1.7.0 → 1.9.0

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.js CHANGED
@@ -1,42 +1,40 @@
1
- (function (draw) {
2
- 'use strict';
3
-
4
- /******************************************************************************
5
- Copyright (c) Microsoft Corporation.
6
-
7
- Permission to use, copy, modify, and/or distribute this software for any
8
- purpose with or without fee is hereby granted.
9
-
10
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
- PERFORMANCE OF THIS SOFTWARE.
17
- ***************************************************************************** */
18
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
19
-
20
-
21
- function __awaiter(thisArg, _arguments, P, generator) {
22
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
- return new (P || (P = Promise))(function (resolve, reject) {
24
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
- step((generator = generator.apply(thisArg, _arguments || [])).next());
28
- });
29
- }
30
-
31
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
32
- var e = new Error(message);
33
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1
+ (function(draw) {
2
+ "use strict";
3
+ function __awaiter(thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P ? value : new P(function(resolve) {
6
+ resolve(value);
7
+ });
8
+ }
9
+ return new (P || (P = Promise))(function(resolve, reject) {
10
+ function fulfilled(value) {
11
+ try {
12
+ step(generator.next(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ }
17
+ function rejected(value) {
18
+ try {
19
+ step(generator["throw"](value));
20
+ } catch (e) {
21
+ reject(e);
22
+ }
23
+ }
24
+ function step(result) {
25
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
26
+ }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ }
30
+ typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
31
+ var e = new Error(message);
32
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
34
33
  };
35
-
36
- const { setPoint, addPoint, toBounds } = draw.TwoPointBoundsHelper;
34
+ const {setPoint: setPoint, addPoint: addPoint, toBounds: toBounds} = draw.TwoPointBoundsHelper;
37
35
  function getTrimBounds(canvas) {
38
- const { width, height } = canvas.view;
39
- const { data } = canvas.context.getImageData(0, 0, width, height);
36
+ const {width: width, height: height} = canvas.view;
37
+ const {data: data} = canvas.context.getImageData(0, 0, width, height);
40
38
  let x, y, pointBounds, index = 0;
41
39
  for (let i = 0; i < data.length; i += 4) {
42
40
  if (data[i + 3] !== 0) {
@@ -46,196 +44,228 @@
46
44
  }
47
45
  index++;
48
46
  }
49
- const bounds = new draw.Bounds();
50
- toBounds(pointBounds, bounds);
51
- return bounds.scale(1 / canvas.pixelRatio).ceil();
47
+ const bounds = new draw.Bounds;
48
+ if (pointBounds) {
49
+ toBounds(pointBounds, bounds);
50
+ bounds.scale(1 / canvas.pixelRatio).ceil();
51
+ }
52
+ return bounds;
52
53
  }
53
-
54
54
  const ExportModule = {
55
55
  syncExport(leaf, filename, options) {
56
- this.running = true;
56
+ draw.Export.running = true;
57
57
  let result;
58
- const fileType = draw.FileHelper.fileType(filename);
59
- const isDownload = filename.includes('.');
60
- options = draw.FileHelper.getExportOptions(options);
61
- const { toURL } = draw.Platform;
62
- const { download } = draw.Platform.origin;
63
- if (fileType === 'json') {
64
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
65
- result = { data: isDownload ? true : leaf.toJSON(options.json) };
66
- }
67
- else if (fileType === 'svg') {
68
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
69
- result = { data: isDownload ? true : leaf.toSVG() };
70
- }
71
- else {
72
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
73
- const { worldTransform, isLeafer, leafer, isFrame } = leaf;
74
- const { slice, clip, trim, screenshot, padding, onCanvas } = options;
75
- const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
76
- const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
77
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
78
- const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
79
- if (screenshot) {
80
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
81
- }
82
- else {
83
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
84
- scaleX = worldTransform.scaleX;
85
- scaleY = worldTransform.scaleY;
86
- switch (relative) {
87
- case 'inner':
58
+ try {
59
+ const fileType = draw.FileHelper.fileType(filename);
60
+ const isDownload = filename.includes(".");
61
+ options = draw.FileHelper.getExportOptions(options);
62
+ const {toURL: toURL} = draw.Platform;
63
+ const {download: download} = draw.Platform.origin;
64
+ if (fileType === "json") {
65
+ isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), "text"), filename);
66
+ result = {
67
+ data: isDownload ? true : leaf.toJSON(options.json)
68
+ };
69
+ } else if (fileType === "svg") {
70
+ isDownload && download(toURL(leaf.toSVG(), "svg"), filename);
71
+ result = {
72
+ data: isDownload ? true : leaf.toSVG()
73
+ };
74
+ } else {
75
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
76
+ const {worldTransform: worldTransform, isLeafer: isLeafer, leafer: leafer, isFrame: isFrame} = leaf;
77
+ const {slice: slice, clip: clip, trim: trim, screenshot: screenshot, padding: padding, onCanvas: onCanvas} = options;
78
+ const smooth = draw.isUndefined(options.smooth) ? leafer ? leafer.config.smooth : true : options.smooth;
79
+ const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
80
+ const fill = isLeafer && screenshot ? draw.isUndefined(options.fill) ? leaf.fill : options.fill : options.fill;
81
+ const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix;
82
+ if (screenshot) {
83
+ renderBounds = screenshot === true ? isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds : screenshot;
84
+ } else {
85
+ let relative = options.relative || (isLeafer ? "inner" : "local");
86
+ scaleX = worldTransform.scaleX;
87
+ scaleY = worldTransform.scaleY;
88
+ switch (relative) {
89
+ case "inner":
88
90
  matrix.set(worldTransform);
89
91
  break;
90
- case 'local':
92
+
93
+ case "local":
91
94
  matrix.set(worldTransform).divide(leaf.localTransform);
92
95
  scaleX /= leaf.scaleX;
93
96
  scaleY /= leaf.scaleY;
94
97
  break;
95
- case 'world':
98
+
99
+ case "world":
96
100
  scaleX = 1;
97
101
  scaleY = 1;
98
102
  break;
99
- case 'page':
103
+
104
+ case "page":
100
105
  relative = leafer || leaf;
101
- default:
106
+
107
+ default:
102
108
  matrix.set(worldTransform).divide(leaf.getTransform(relative));
103
109
  const l = relative.worldTransform;
104
110
  scaleX /= scaleX / l.scaleX;
105
111
  scaleY /= scaleY / l.scaleY;
112
+ }
113
+ renderBounds = leaf.getBounds("render", relative);
106
114
  }
107
- renderBounds = leaf.getBounds('render', relative);
108
- }
109
- const scaleData = { scaleX: 1, scaleY: 1 };
110
- draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
111
- let pixelRatio = options.pixelRatio || 1;
112
- let { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
113
- if (clip)
114
- x += clip.x, y += clip.y, width = clip.width, height = clip.height;
115
- const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
116
- let canvas = draw.Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
117
- let sliceLeaf;
118
- if (slice) {
119
- sliceLeaf = leaf;
120
- sliceLeaf.__worldOpacity = 0;
121
- leaf = leafer || leaf;
122
- renderOptions.bounds = canvas.bounds;
123
- }
124
- canvas.save();
125
- if (isFrame && fill !== undefined) {
126
- const oldFill = leaf.get('fill');
127
- leaf.fill = '';
128
- leaf.__render(canvas, renderOptions);
129
- leaf.fill = oldFill;
130
- }
131
- else {
132
- leaf.__render(canvas, renderOptions);
133
- }
134
- canvas.restore();
135
- if (sliceLeaf)
136
- sliceLeaf.__updateWorldOpacity();
137
- if (trim) {
138
- trimBounds = getTrimBounds(canvas);
139
- const old = canvas, { width, height } = trimBounds;
140
- const config = { x: 0, y: 0, width, height, pixelRatio };
141
- canvas = draw.Creator.canvas(config);
142
- canvas.copyWorld(old, trimBounds, config);
143
- }
144
- if (padding) {
145
- const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
146
- const old = canvas, { width, height } = old;
147
- canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
148
- canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
115
+ const scaleData = {
116
+ scaleX: 1,
117
+ scaleY: 1
118
+ };
119
+ draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
120
+ let pixelRatio = options.pixelRatio || 1;
121
+ let {x: x, y: y, width: width, height: height} = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
122
+ if (clip) x += clip.x, y += clip.y, width = clip.width, height = clip.height;
123
+ const renderOptions = {
124
+ exporting: true,
125
+ matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY)
126
+ };
127
+ let canvas = draw.Creator.canvas({
128
+ width: Math.floor(width),
129
+ height: Math.floor(height),
130
+ pixelRatio: pixelRatio,
131
+ smooth: smooth,
132
+ contextSettings: contextSettings
133
+ });
134
+ let sliceLeaf;
135
+ if (slice) {
136
+ sliceLeaf = leaf;
137
+ sliceLeaf.__worldOpacity = 0;
138
+ leaf = leafer || leaf;
139
+ renderOptions.bounds = canvas.bounds;
140
+ }
141
+ canvas.save();
142
+ if (isFrame && !draw.isUndefined(fill)) {
143
+ const oldFill = leaf.get("fill");
144
+ leaf.fill = "";
145
+ leaf.__render(canvas, renderOptions);
146
+ leaf.fill = oldFill;
147
+ } else {
148
+ leaf.__render(canvas, renderOptions);
149
+ }
150
+ canvas.restore();
151
+ if (sliceLeaf) sliceLeaf.__updateWorldOpacity();
152
+ if (trim) {
153
+ trimBounds = getTrimBounds(canvas);
154
+ const old = canvas, {width: width, height: height} = trimBounds;
155
+ const config = {
156
+ x: 0,
157
+ y: 0,
158
+ width: width,
159
+ height: height,
160
+ pixelRatio: pixelRatio
161
+ };
162
+ canvas = draw.Creator.canvas(config);
163
+ canvas.copyWorld(old, trimBounds, config);
164
+ old.destroy();
165
+ }
166
+ if (padding) {
167
+ const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
168
+ const old = canvas, {width: width, height: height} = old;
169
+ canvas = draw.Creator.canvas({
170
+ width: width + left + right,
171
+ height: height + top + bottom,
172
+ pixelRatio: pixelRatio
173
+ });
174
+ canvas.copyWorld(old, old.bounds, {
175
+ x: left,
176
+ y: top,
177
+ width: width,
178
+ height: height
179
+ });
180
+ old.destroy();
181
+ }
182
+ if (needFill) canvas.fillWorld(canvas.bounds, fill || "#FFFFFF", "destination-over");
183
+ if (onCanvas) onCanvas(canvas);
184
+ const data = filename === "canvas" ? canvas : canvas.export(filename, options);
185
+ result = {
186
+ data: data,
187
+ width: canvas.pixelWidth,
188
+ height: canvas.pixelHeight,
189
+ renderBounds: renderBounds,
190
+ trimBounds: trimBounds
191
+ };
149
192
  }
150
- if (needFill)
151
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
152
- if (onCanvas)
153
- onCanvas(canvas);
154
- const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
155
- result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
193
+ } catch (error) {
194
+ result = {
195
+ data: "",
196
+ error: error
197
+ };
156
198
  }
157
- this.running = false;
199
+ draw.Export.running = false;
158
200
  return result;
159
201
  },
160
202
  export(leaf, filename, options) {
161
- this.running = true;
162
- return addTask((success) => new Promise((resolve) => {
163
- const getResult = () => __awaiter(this, void 0, void 0, function* () {
164
- if (!draw.Resource.isComplete)
165
- return draw.Platform.requestRender(getResult);
166
- const result = ExportModule.syncExport(leaf, filename, options);
167
- if (result.data instanceof Promise)
168
- result.data = yield result.data;
203
+ draw.Export.running = true;
204
+ return addTask(success => new Promise(resolve => {
205
+ const getResult = () => __awaiter(this, void 0, void 0, function*() {
206
+ if (!draw.Resource.isComplete) return draw.Platform.requestRender(getResult);
207
+ const result = draw.Export.syncExport(leaf, filename, options);
208
+ if (result.data instanceof Promise) result.data = yield result.data;
169
209
  success(result);
170
210
  resolve();
171
211
  });
172
212
  leaf.updateLayout();
173
213
  checkLazy(leaf);
174
- const { leafer } = leaf;
175
- if (leafer)
176
- leafer.waitViewCompleted(getResult);
177
- else
178
- getResult();
214
+ const {leafer: leafer} = leaf;
215
+ if (leafer) leafer.waitViewCompleted(getResult); else getResult();
179
216
  }));
180
217
  }
181
218
  };
182
219
  let tasker;
183
220
  function addTask(task) {
184
- if (!tasker)
185
- tasker = new draw.TaskProcessor();
186
- return new Promise((resolve) => {
187
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
221
+ if (!tasker) tasker = new draw.TaskProcessor;
222
+ return new Promise(resolve => {
223
+ tasker.add(() => __awaiter(this, void 0, void 0, function*() {
224
+ return yield task(resolve);
225
+ }), {
226
+ parallel: false
227
+ });
188
228
  });
189
229
  }
190
230
  function checkLazy(leaf) {
191
- if (leaf.__.__needComputePaint)
192
- leaf.__.__computePaint();
193
- if (leaf.isBranch)
194
- leaf.children.forEach(child => checkLazy(child));
231
+ if (leaf.__.__needComputePaint) leaf.__.__computePaint();
232
+ if (leaf.isBranch) leaf.children.forEach(child => checkLazy(child));
195
233
  }
196
-
197
234
  const canvas = draw.LeaferCanvasBase.prototype;
198
- const debug = draw.Debug.get('@leafer-in/export');
199
- canvas.export = function (filename, options) {
200
- const { quality, blob } = draw.FileHelper.getExportOptions(options);
201
- if (filename.includes('.'))
202
- return this.saveAs(filename, quality);
203
- else if (blob)
204
- return this.toBlob(filename, quality);
205
- else
206
- return this.toDataURL(filename, quality);
235
+ const debug = draw.Debug.get("@leafer-in/export");
236
+ canvas.export = function(filename, options) {
237
+ const {quality: quality, blob: blob} = draw.FileHelper.getExportOptions(options);
238
+ if (filename.includes(".")) return this.saveAs(filename, quality); else if (blob) return this.toBlob(filename, quality); else return this.toDataURL(filename, quality);
207
239
  };
208
- canvas.toBlob = function (type, quality) {
209
- return new Promise((resolve) => {
210
- draw.Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
240
+ canvas.toBlob = function(type, quality) {
241
+ return new Promise(resolve => {
242
+ draw.Platform.origin.canvasToBolb(this.view, type, quality).then(blob => {
211
243
  resolve(blob);
212
- }).catch((e) => {
244
+ }).catch(e => {
213
245
  debug.error(e);
214
246
  resolve(null);
215
247
  });
216
248
  });
217
249
  };
218
- canvas.toDataURL = function (type, quality) {
250
+ canvas.toDataURL = function(type, quality) {
219
251
  return draw.Platform.origin.canvasToDataURL(this.view, type, quality);
220
252
  };
221
- canvas.saveAs = function (filename, quality) {
222
- return new Promise((resolve) => {
253
+ canvas.saveAs = function(filename, quality) {
254
+ return new Promise(resolve => {
223
255
  draw.Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
224
256
  resolve(true);
225
- }).catch((e) => {
257
+ }).catch(e => {
226
258
  debug.error(e);
227
259
  resolve(false);
228
260
  });
229
261
  });
230
262
  };
231
-
232
- draw.Plugin.add('export');
263
+ draw.Plugin.add("export");
233
264
  Object.assign(draw.Export, ExportModule);
234
- draw.UI.prototype.export = function (filename, options) {
265
+ draw.UI.prototype.export = function(filename, options) {
235
266
  return draw.Export.export(this, filename, options);
236
267
  };
237
- draw.UI.prototype.syncExport = function (filename, options) {
268
+ draw.UI.prototype.syncExport = function(filename, options) {
238
269
  return draw.Export.syncExport(this, filename, options);
239
270
  };
240
-
241
271
  })(LeaferUI);
@@ -1,2 +1,2 @@
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={syncExport(t,r,a){let s;this.running=!0;const l=e.FileHelper.fileType(r),c=r.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"),r),s={data:!!c||t.toJSON(a.json)};else if("svg"===l)c&&u(d(t.toSVG(),"svg"),r),s={data:!!c||t.toSVG()};else{let l,c,d=1,u=1;const{worldTransform:h,isLeafer:p,leafer:f,isFrame:g}=t,{slice:v,clip:w,trim:x,screenshot:m,padding:y,onCanvas:P}=a,b=void 0===a.smooth?!f||f.config.smooth:a.smooth,_=a.contextSettings||(f?f.config.contextSettings:void 0),B=p&&m&&void 0===a.fill?t.fill:a.fill,S=e.FileHelper.isOpaqueImage(r)||B,E=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":E.set(h);break;case"local":E.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:E.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);w&&(O+=w.x,T+=w.y,C=w.width,H=w.height);const L={exporting:!0,matrix:E.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:_});if(v&&(X=t,X.__worldOpacity=0,t=f||t,L.bounds=Y.bounds),Y.save(),g&&void 0!==B){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(),x){c=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()}(Y);const t=Y,{width:r,height:a}=c,s={x:0,y:0,width:r,height:a,pixelRatio:F};Y=e.Creator.canvas(s),Y.copyWorld(t,c,s)}if(y){const[t,o,n,i]=e.MathHelper.fourNumber(y),r=Y,{width:a,height:s}=r;Y=e.Creator.canvas({width:a+i+o,height:s+t+n,pixelRatio:F}),Y.copyWorld(r,r.bounds,{x:i,y:t,width:a,height:s})}S&&Y.fillWorld(Y.bounds,B||"#FFFFFF","destination-over"),P&&P(Y);s={data:"canvas"===r?Y:Y.export(r,a),width:Y.pixelWidth,height:Y.pixelHeight,renderBounds:l,trimBounds:c}}return this.running=!1,s},export(o,n,i){return this.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})}))}((a=>new Promise((l=>{const c=()=>t(this,void 0,void 0,(function*(){if(!e.Resource.isComplete)return e.Platform.requestRender(c);const t=r.syncExport(o,n,i);t.data instanceof Promise&&(t.data=yield t.data),a(t),l()}));o.updateLayout(),s(o);const{leafer:d}=o;d?d.waitViewCompleted(c):c()}))))}};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)},e.UI.prototype.syncExport=function(t,o){return e.Export.syncExport(this,t,o)};
1
+ "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:w,trim:v,screenshot:m,padding:y,onCanvas:P}=a,b=e.isUndefined(a.smooth)?!f||f.config.smooth:a.smooth,E=a.contextSettings||(f?f.config.contextSettings:void 0),_=p&&m&&e.isUndefined(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:U,height:C}=new e.Bounds(l).scale(R.scaleX,R.scaleY);w&&(O+=w.x,T+=w.y,U=w.width,C=w.height);const H={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 L,X=e.Creator.canvas({width:Math.floor(U),height:Math.floor(C),pixelRatio:F,smooth:b,contextSettings:E});if(x&&(L=t,L.__worldOpacity=0,t=f||t,H.bounds=X.bounds),X.save(),g&&!e.isUndefined(_)){const e=t.get("fill");t.fill="",t.__render(X,H),t.fill=e}else t.__render(X,H);if(X.restore(),L&&L.__updateWorldOpacity(),v){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}(X);const t=X,{width:i,height:a}=c,s={x:0,y:0,width:i,height:a,pixelRatio:F};X=e.Creator.canvas(s),X.copyWorld(t,c,s),t.destroy()}if(y){const[t,o,n,r]=e.MathHelper.fourNumber(y),i=X,{width:a,height:s}=i;X=e.Creator.canvas({width:a+r+o,height:s+t+n,pixelRatio:F}),X.copyWorld(i,i.bounds,{x:r,y:t,width:a,height:s}),i.destroy()}B&&X.fillWorld(X.bounds,_||"#FFFFFF","destination-over"),P&&P(X);s={data:"canvas"===i?X:X.export(i,a),width:X.pixelWidth,height:X.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)};
2
2
  //# sourceMappingURL=export.min.cjs.map