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