@leafer-in/export 1.8.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, Export, FileHelper, Platform, Matrix, MathHelper, Creator, TaskProcessor, Resource, LeaferCanvasBase, Debug, Plugin, 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,7 +47,7 @@ function getTrimBounds(canvas) {
45
47
  }
46
48
  index++;
47
49
  }
48
- const bounds = new Bounds();
50
+ const bounds = new Bounds;
49
51
  if (pointBounds) {
50
52
  toBounds(pointBounds, bounds);
51
53
  bounds.scale(1 / canvas.pixelRatio).ceil();
@@ -59,64 +61,80 @@ const ExportModule = {
59
61
  let result;
60
62
  try {
61
63
  const fileType = FileHelper.fileType(filename);
62
- const isDownload = filename.includes('.');
64
+ const isDownload = filename.includes(".");
63
65
  options = FileHelper.getExportOptions(options);
64
- const { toURL } = Platform;
65
- const { download } = Platform.origin;
66
- if (fileType === 'json') {
67
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
68
- result = { data: isDownload ? true : leaf.toJSON(options.json) };
69
- }
70
- else if (fileType === 'svg') {
71
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
72
- result = { data: isDownload ? true : leaf.toSVG() };
73
- }
74
- else {
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 {
75
79
  let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
76
- const { worldTransform, isLeafer, leafer, isFrame } = leaf;
77
- const { slice, clip, trim, screenshot, padding, onCanvas } = options;
78
- const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
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;
79
83
  const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
80
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
81
- const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix();
84
+ const fill = isLeafer && screenshot ? isUndefined(options.fill) ? leaf.fill : options.fill : options.fill;
85
+ const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix;
82
86
  if (screenshot) {
83
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
84
- }
85
- else {
86
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
87
+ renderBounds = screenshot === true ? isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds : screenshot;
88
+ } else {
89
+ let relative = options.relative || (isLeafer ? "inner" : "local");
87
90
  scaleX = worldTransform.scaleX;
88
91
  scaleY = worldTransform.scaleY;
89
92
  switch (relative) {
90
- case 'inner':
91
- matrix.set(worldTransform);
92
- break;
93
- case 'local':
94
- matrix.set(worldTransform).divide(leaf.localTransform);
95
- scaleX /= leaf.scaleX;
96
- scaleY /= leaf.scaleY;
97
- break;
98
- case 'world':
99
- scaleX = 1;
100
- scaleY = 1;
101
- break;
102
- case 'page':
103
- relative = leafer || leaf;
104
- default:
105
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
106
- const l = relative.worldTransform;
107
- scaleX /= scaleX / l.scaleX;
108
- scaleY /= scaleY / l.scaleY;
93
+ case "inner":
94
+ matrix.set(worldTransform);
95
+ break;
96
+
97
+ case "local":
98
+ matrix.set(worldTransform).divide(leaf.localTransform);
99
+ scaleX /= leaf.scaleX;
100
+ scaleY /= leaf.scaleY;
101
+ break;
102
+
103
+ case "world":
104
+ scaleX = 1;
105
+ scaleY = 1;
106
+ break;
107
+
108
+ case "page":
109
+ relative = leafer || leaf;
110
+
111
+ default:
112
+ matrix.set(worldTransform).divide(leaf.getTransform(relative));
113
+ const l = relative.worldTransform;
114
+ scaleX /= scaleX / l.scaleX;
115
+ scaleY /= scaleY / l.scaleY;
109
116
  }
110
- renderBounds = leaf.getBounds('render', relative);
117
+ renderBounds = leaf.getBounds("render", relative);
111
118
  }
112
- const scaleData = { scaleX: 1, scaleY: 1 };
119
+ const scaleData = {
120
+ scaleX: 1,
121
+ scaleY: 1
122
+ };
113
123
  MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
114
124
  let pixelRatio = options.pixelRatio || 1;
115
- let { x, y, width, height } = new Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
116
- if (clip)
117
- x += clip.x, y += clip.y, width = clip.width, height = clip.height;
118
- 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) };
119
- let canvas = Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
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
+ });
120
138
  let sliceLeaf;
121
139
  if (slice) {
122
140
  sliceLeaf = leaf;
@@ -125,122 +143,145 @@ const ExportModule = {
125
143
  renderOptions.bounds = canvas.bounds;
126
144
  }
127
145
  canvas.save();
128
- if (isFrame && fill !== undefined) {
129
- const oldFill = leaf.get('fill');
130
- leaf.fill = '';
146
+ if (isFrame && !isUndefined(fill)) {
147
+ const oldFill = leaf.get("fill");
148
+ leaf.fill = "";
131
149
  leaf.__render(canvas, renderOptions);
132
150
  leaf.fill = oldFill;
133
- }
134
- else {
151
+ } else {
135
152
  leaf.__render(canvas, renderOptions);
136
153
  }
137
154
  canvas.restore();
138
- if (sliceLeaf)
139
- sliceLeaf.__updateWorldOpacity();
155
+ if (sliceLeaf) sliceLeaf.__updateWorldOpacity();
140
156
  if (trim) {
141
157
  trimBounds = getTrimBounds(canvas);
142
- const old = canvas, { width, height } = trimBounds;
143
- const config = { x: 0, y: 0, width, height, pixelRatio };
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
+ };
144
166
  canvas = Creator.canvas(config);
145
167
  canvas.copyWorld(old, trimBounds, config);
168
+ old.destroy();
146
169
  }
147
170
  if (padding) {
148
171
  const [top, right, bottom, left] = MathHelper.fourNumber(padding);
149
- const old = canvas, { width, height } = old;
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 });
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();
152
185
  }
153
- if (needFill)
154
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
155
- if (onCanvas)
156
- onCanvas(canvas);
157
- const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
158
- result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
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
+ };
159
196
  }
160
- }
161
- catch (error) {
162
- result = { data: '', error };
197
+ } catch (error) {
198
+ result = {
199
+ data: "",
200
+ error: error
201
+ };
163
202
  }
164
203
  Export.running = false;
165
204
  return result;
166
205
  },
167
206
  export(leaf, filename, options) {
168
207
  Export.running = true;
169
- return addTask((success) => new Promise((resolve) => {
170
- const getResult = () => __awaiter(this, void 0, void 0, function* () {
171
- if (!Resource.isComplete)
172
- return Platform.requestRender(getResult);
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);
173
211
  const result = Export.syncExport(leaf, filename, options);
174
- if (result.data instanceof Promise)
175
- result.data = yield result.data;
212
+ if (result.data instanceof Promise) result.data = yield result.data;
176
213
  success(result);
177
214
  resolve();
178
215
  });
179
216
  leaf.updateLayout();
180
217
  checkLazy(leaf);
181
- const { leafer } = leaf;
182
- if (leafer)
183
- leafer.waitViewCompleted(getResult);
184
- else
185
- getResult();
218
+ const {leafer: leafer} = leaf;
219
+ if (leafer) leafer.waitViewCompleted(getResult); else getResult();
186
220
  }));
187
221
  }
188
222
  };
223
+
189
224
  let tasker;
225
+
190
226
  function addTask(task) {
191
- if (!tasker)
192
- tasker = new TaskProcessor();
193
- return new Promise((resolve) => {
194
- 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
+ });
195
234
  });
196
235
  }
236
+
197
237
  function checkLazy(leaf) {
198
- if (leaf.__.__needComputePaint)
199
- leaf.__.__computePaint();
200
- if (leaf.isBranch)
201
- leaf.children.forEach(child => checkLazy(child));
238
+ if (leaf.__.__needComputePaint) leaf.__.__computePaint();
239
+ if (leaf.isBranch) leaf.children.forEach(child => checkLazy(child));
202
240
  }
203
241
 
204
242
  const canvas = LeaferCanvasBase.prototype;
205
- const debug = Debug.get('@leafer-in/export');
206
- canvas.export = function (filename, options) {
207
- const { quality, blob } = FileHelper.getExportOptions(options);
208
- if (filename.includes('.'))
209
- return this.saveAs(filename, quality);
210
- else if (blob)
211
- return this.toBlob(filename, quality);
212
- else
213
- 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);
214
249
  };
215
- canvas.toBlob = function (type, quality) {
216
- return new Promise((resolve) => {
217
- 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 => {
218
254
  resolve(blob);
219
- }).catch((e) => {
255
+ }).catch(e => {
220
256
  debug.error(e);
221
257
  resolve(null);
222
258
  });
223
259
  });
224
260
  };
225
- canvas.toDataURL = function (type, quality) {
261
+
262
+ canvas.toDataURL = function(type, quality) {
226
263
  return Platform.origin.canvasToDataURL(this.view, type, quality);
227
264
  };
228
- canvas.saveAs = function (filename, quality) {
229
- return new Promise((resolve) => {
265
+
266
+ canvas.saveAs = function(filename, quality) {
267
+ return new Promise(resolve => {
230
268
  Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
231
269
  resolve(true);
232
- }).catch((e) => {
270
+ }).catch(e => {
233
271
  debug.error(e);
234
272
  resolve(false);
235
273
  });
236
274
  });
237
275
  };
238
276
 
239
- Plugin.add('export');
277
+ Plugin.add("export");
278
+
240
279
  Object.assign(Export, ExportModule);
241
- UI.prototype.export = function (filename, options) {
280
+
281
+ UI.prototype.export = function(filename, options) {
242
282
  return Export.export(this, filename, options);
243
283
  };
244
- UI.prototype.syncExport = function (filename, options) {
284
+
285
+ UI.prototype.syncExport = function(filename, options) {
245
286
  return Export.syncExport(this, filename, options);
246
287
  };
@@ -1,2 +1,2 @@
1
- import{TwoPointBoundsHelper as t,Bounds as e,Export as o,FileHelper as n,Platform as i,Matrix as r,MathHelper as a,Creator as s,TaskProcessor as c,Resource as l,LeaferCanvasBase as d,Debug as h,Plugin as u,UI as f}from"@leafer-ui/draw";function p(t,e,o,n){return new(o||(o=Promise))((function(i,r){function a(t){try{c(n.next(t))}catch(t){r(t)}}function s(t){try{c(n.throw(t))}catch(t){r(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(a,s)}c((n=n.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const{setPoint:g,addPoint:w,toBounds:v}=t;const x={syncExport(t,c,l){let d;o.running=!0;try{const o=n.fileType(c),h=c.includes(".");l=n.getExportOptions(l);const{toURL:u}=i,{download:f}=i.origin;if("json"===o)h&&f(u(JSON.stringify(t.toJSON(l.json)),"text"),c),d={data:!!h||t.toJSON(l.json)};else if("svg"===o)h&&f(u(t.toSVG(),"svg"),c),d={data:!!h||t.toSVG()};else{let o,i,h=1,u=1;const{worldTransform:f,isLeafer:p,leafer:x,isFrame:m}=t,{slice:y,clip:_,trim:b,screenshot:S,padding:R,onCanvas:P}=l,B=void 0===l.smooth?!x||x.config.smooth:l.smooth,E=l.contextSettings||(x?x.config.contextSettings:void 0),O=p&&S&&void 0===l.fill?t.fill:l.fill,F=n.isOpaqueImage(c)||O,T=new r;if(S)o=!0===S?p?x.canvas.bounds:t.worldRenderBounds:S;else{let e=l.relative||(p?"inner":"local");switch(h=f.scaleX,u=f.scaleY,e){case"inner":T.set(f);break;case"local":T.set(f).divide(t.localTransform),h/=t.scaleX,u/=t.scaleY;break;case"world":h=1,u=1;break;case"page":e=x||t;default:T.set(f).divide(t.getTransform(e));const o=e.worldTransform;h/=h/o.scaleX,u/=u/o.scaleY}o=t.getBounds("render",e)}const X={scaleX:1,scaleY:1};a.getScaleData(l.scale,l.size,o,X);let Y=l.pixelRatio||1,{x:L,y:D,width:W,height:j}=new e(o).scale(X.scaleX,X.scaleY);_&&(L+=_.x,D+=_.y,W=_.width,j=_.height);const C={exporting:!0,matrix:T.scale(1/X.scaleX,1/X.scaleY).invert().translate(-L,-D).withScale(1/h*X.scaleX,1/u*X.scaleY)};let N,U=s.canvas({width:Math.floor(W),height:Math.floor(j),pixelRatio:Y,smooth:B,contextSettings:E});if(y&&(N=t,N.__worldOpacity=0,t=x||t,C.bounds=U.bounds),U.save(),m&&void 0!==O){const e=t.get("fill");t.fill="",t.__render(U,C),t.fill=e}else t.__render(U,C);if(U.restore(),N&&N.__updateWorldOpacity(),b){i=function(t){const{width:o,height:n}=t.view,{data:i}=t.context.getImageData(0,0,o,n);let r,a,s,c=0;for(let t=0;t<i.length;t+=4)0!==i[t+3]&&(r=c%o,a=(c-r)/o,s?w(s,r,a):g(s={},r,a)),c++;const l=new e;return s&&(v(s,l),l.scale(1/t.pixelRatio).ceil()),l}(U);const t=U,{width:o,height:n}=i,r={x:0,y:0,width:o,height:n,pixelRatio:Y};U=s.canvas(r),U.copyWorld(t,i,r)}if(R){const[t,e,o,n]=a.fourNumber(R),i=U,{width:r,height:c}=i;U=s.canvas({width:r+n+e,height:c+t+o,pixelRatio:Y}),U.copyWorld(i,i.bounds,{x:n,y:t,width:r,height:c})}F&&U.fillWorld(U.bounds,O||"#FFFFFF","destination-over"),P&&P(U);d={data:"canvas"===c?U:U.export(c,l),width:U.pixelWidth,height:U.pixelHeight,renderBounds:o,trimBounds:i}}}catch(t){d={data:"",error:t}}return o.running=!1,d},export(t,e,n){return o.running=!0,function(t){m||(m=new c);return new Promise((e=>{m.add((()=>p(this,void 0,void 0,(function*(){return yield t(e)}))),{parallel:!1})}))}((r=>new Promise((a=>{const s=()=>p(this,void 0,void 0,(function*(){if(!l.isComplete)return i.requestRender(s);const c=o.syncExport(t,e,n);c.data instanceof Promise&&(c.data=yield c.data),r(c),a()}));t.updateLayout(),y(t);const{leafer:c}=t;c?c.waitViewCompleted(s):s()}))))}};let m;function y(t){t.__.__needComputePaint&&t.__.__computePaint(),t.isBranch&&t.children.forEach((t=>y(t)))}const _=d.prototype,b=h.get("@leafer-in/export");_.export=function(t,e){const{quality:o,blob:i}=n.getExportOptions(e);return t.includes(".")?this.saveAs(t,o):i?this.toBlob(t,o):this.toDataURL(t,o)},_.toBlob=function(t,e){return new Promise((o=>{i.origin.canvasToBolb(this.view,t,e).then((t=>{o(t)})).catch((t=>{b.error(t),o(null)}))}))},_.toDataURL=function(t,e){return i.origin.canvasToDataURL(this.view,t,e)},_.saveAs=function(t,e){return new Promise((o=>{i.origin.canvasSaveAs(this.view,t,e).then((()=>{o(!0)})).catch((t=>{b.error(t),o(!1)}))}))},u.add("export"),Object.assign(o,x),f.prototype.export=function(t,e){return o.export(this,t,e)},f.prototype.syncExport=function(t,e){return o.syncExport(this,t,e)};
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